233 lines
7.5 KiB
Rust
233 lines
7.5 KiB
Rust
use ratatui::layout::{Constraint, Layout, Rect};
|
|
use ratatui::style::{Modifier, Style};
|
|
use ratatui::text::{Line as RLine, Span};
|
|
use ratatui::widgets::{Block, Borders, List, ListItem, Paragraph};
|
|
use ratatui::Frame;
|
|
|
|
use crate::app::App;
|
|
use crate::model::{Word, WORDS};
|
|
use crate::state::DictFocus;
|
|
use crate::theme::{dict, search};
|
|
|
|
const CATEGORIES: &[&str] = &[
|
|
// Forth core
|
|
"Stack",
|
|
"Arithmetic",
|
|
"Comparison",
|
|
"Logic",
|
|
"Variables",
|
|
"Randomness",
|
|
"Probability",
|
|
"Lists",
|
|
"Definitions",
|
|
// Live coding
|
|
"Sound",
|
|
"Time",
|
|
"Context",
|
|
"Music",
|
|
"LFO",
|
|
// Synthesis
|
|
"Oscillator",
|
|
"Envelope",
|
|
"Pitch Env",
|
|
"Gain",
|
|
"Sample",
|
|
// Effects
|
|
"Filter",
|
|
"Modulation",
|
|
"Mod FX",
|
|
"Lo-fi",
|
|
"Delay",
|
|
"Reverb",
|
|
];
|
|
|
|
pub fn render(frame: &mut Frame, app: &App, area: Rect) {
|
|
let [header_area, body_area] =
|
|
Layout::vertical([Constraint::Length(5), Constraint::Fill(1)]).areas(area);
|
|
|
|
render_header(frame, header_area);
|
|
|
|
let [cat_area, words_area] =
|
|
Layout::horizontal([Constraint::Length(16), Constraint::Fill(1)]).areas(body_area);
|
|
|
|
let is_searching = !app.ui.dict_search_query.is_empty();
|
|
render_categories(frame, app, cat_area, is_searching);
|
|
render_words(frame, app, words_area, is_searching);
|
|
}
|
|
|
|
fn render_header(frame: &mut Frame, area: Rect) {
|
|
use ratatui::widgets::Wrap;
|
|
let desc = "Forth uses a stack: values are pushed, functions (called words) consume and \
|
|
produce values. Read left to right: 3 4 + -> push 3, push 4, + pops both, \
|
|
pushes 7. This page lists all words with their signature ( inputs -- outputs ).";
|
|
let block = Block::default()
|
|
.borders(Borders::ALL)
|
|
.border_style(Style::new().fg(dict::BORDER_NORMAL))
|
|
.title("Dictionary");
|
|
let para = Paragraph::new(desc)
|
|
.style(Style::new().fg(dict::HEADER_DESC))
|
|
.wrap(Wrap { trim: false })
|
|
.block(block);
|
|
frame.render_widget(para, area);
|
|
}
|
|
|
|
fn render_categories(frame: &mut Frame, app: &App, area: Rect, dimmed: bool) {
|
|
let focused = app.ui.dict_focus == DictFocus::Categories && !dimmed;
|
|
|
|
let items: Vec<ListItem> = CATEGORIES
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(i, name)| {
|
|
let is_selected = i == app.ui.dict_category;
|
|
let style = if dimmed {
|
|
Style::new().fg(dict::CATEGORY_DIMMED)
|
|
} else if is_selected && focused {
|
|
Style::new().fg(dict::CATEGORY_FOCUSED).add_modifier(Modifier::BOLD)
|
|
} else if is_selected {
|
|
Style::new().fg(dict::CATEGORY_SELECTED)
|
|
} else {
|
|
Style::new().fg(dict::CATEGORY_NORMAL)
|
|
};
|
|
let prefix = if is_selected && !dimmed { "> " } else { " " };
|
|
ListItem::new(format!("{prefix}{name}")).style(style)
|
|
})
|
|
.collect();
|
|
|
|
let border_color = if focused { dict::BORDER_FOCUSED } else { dict::BORDER_NORMAL };
|
|
let block = Block::default()
|
|
.borders(Borders::ALL)
|
|
.border_style(Style::new().fg(border_color))
|
|
.title("Categories");
|
|
let list = List::new(items).block(block);
|
|
frame.render_widget(list, area);
|
|
}
|
|
|
|
fn render_words(frame: &mut Frame, app: &App, area: Rect, is_searching: bool) {
|
|
let focused = app.ui.dict_focus == DictFocus::Words;
|
|
|
|
// Filter words by search query or category
|
|
let words: Vec<&Word> = if is_searching {
|
|
let query = app.ui.dict_search_query.to_lowercase();
|
|
WORDS
|
|
.iter()
|
|
.filter(|w| {
|
|
w.name.to_lowercase().contains(&query)
|
|
|| w.aliases.iter().any(|a| a.to_lowercase().contains(&query))
|
|
})
|
|
.collect()
|
|
} else {
|
|
let category = CATEGORIES[app.ui.dict_category];
|
|
WORDS
|
|
.iter()
|
|
.filter(|w| w.category == category)
|
|
.collect()
|
|
};
|
|
|
|
// Split area for search bar when search is active or has query
|
|
let show_search = app.ui.dict_search_active || is_searching;
|
|
let (search_area, content_area) = if show_search {
|
|
let [s, c] =
|
|
Layout::vertical([Constraint::Length(1), Constraint::Fill(1)]).areas(area);
|
|
(Some(s), c)
|
|
} else {
|
|
(None, area)
|
|
};
|
|
|
|
// Render search bar
|
|
if let Some(sa) = search_area {
|
|
render_search_bar(frame, app, sa);
|
|
}
|
|
|
|
let content_width = content_area.width.saturating_sub(2) as usize;
|
|
|
|
let mut lines: Vec<RLine> = Vec::new();
|
|
|
|
for word in &words {
|
|
let name_bg = dict::WORD_BG;
|
|
let name_style = Style::new()
|
|
.fg(dict::WORD_NAME)
|
|
.bg(name_bg)
|
|
.add_modifier(Modifier::BOLD);
|
|
let alias_style = Style::new().fg(dict::ALIAS).bg(name_bg);
|
|
let name_text = if word.aliases.is_empty() {
|
|
format!(" {}", word.name)
|
|
} else {
|
|
format!(" {} ({})", word.name, word.aliases.join(", "))
|
|
};
|
|
let padding = " ".repeat(content_width.saturating_sub(name_text.chars().count()));
|
|
if word.aliases.is_empty() {
|
|
lines.push(RLine::from(Span::styled(
|
|
format!("{name_text}{padding}"),
|
|
name_style,
|
|
)));
|
|
} else {
|
|
lines.push(RLine::from(vec![
|
|
Span::styled(format!(" {}", word.name), name_style),
|
|
Span::styled(format!(" ({})", word.aliases.join(", ")), alias_style),
|
|
Span::styled(padding, name_style),
|
|
]));
|
|
}
|
|
|
|
let stack_style = Style::new().fg(dict::STACK_SIG);
|
|
lines.push(RLine::from(vec![
|
|
Span::raw(" "),
|
|
Span::styled(word.stack.to_string(), stack_style),
|
|
]));
|
|
|
|
let desc_style = Style::new().fg(dict::DESCRIPTION);
|
|
lines.push(RLine::from(vec![
|
|
Span::raw(" "),
|
|
Span::styled(word.desc.to_string(), desc_style),
|
|
]));
|
|
|
|
let example_style = Style::new().fg(dict::EXAMPLE);
|
|
lines.push(RLine::from(vec![
|
|
Span::raw(" "),
|
|
Span::styled(format!("e.g. {}", word.example), example_style),
|
|
]));
|
|
|
|
lines.push(RLine::from(""));
|
|
}
|
|
|
|
let visible_height = content_area.height.saturating_sub(2) as usize;
|
|
let total_lines = lines.len();
|
|
let max_scroll = total_lines.saturating_sub(visible_height);
|
|
let scroll = app.ui.dict_scroll.min(max_scroll);
|
|
|
|
let visible: Vec<RLine> = lines
|
|
.into_iter()
|
|
.skip(scroll)
|
|
.take(visible_height)
|
|
.collect();
|
|
|
|
let title = if is_searching {
|
|
format!("Search: {} matches", words.len())
|
|
} else {
|
|
let category = CATEGORIES[app.ui.dict_category];
|
|
format!("{category} ({} words)", words.len())
|
|
};
|
|
let border_color = if focused { dict::BORDER_FOCUSED } else { dict::BORDER_NORMAL };
|
|
let block = Block::default()
|
|
.borders(Borders::ALL)
|
|
.border_style(Style::new().fg(border_color))
|
|
.title(title);
|
|
let para = Paragraph::new(visible).block(block);
|
|
frame.render_widget(para, content_area);
|
|
}
|
|
|
|
fn render_search_bar(frame: &mut Frame, app: &App, area: Rect) {
|
|
let style = if app.ui.dict_search_active {
|
|
Style::new().fg(search::ACTIVE)
|
|
} else {
|
|
Style::new().fg(search::INACTIVE)
|
|
};
|
|
let cursor = if app.ui.dict_search_active { "_" } else { "" };
|
|
let text = format!(" /{}{}", app.ui.dict_search_query, cursor);
|
|
let line = RLine::from(Span::styled(text, style));
|
|
frame.render_widget(Paragraph::new(vec![line]), area);
|
|
}
|
|
|
|
pub fn category_count() -> usize {
|
|
CATEGORIES.len()
|
|
}
|