diff --git a/src/tui/widgets/mod.rs b/src/tui/widgets/mod.rs index 7b155d3..f3c34bd 100644 --- a/src/tui/widgets/mod.rs +++ b/src/tui/widgets/mod.rs @@ -9,6 +9,7 @@ pub mod content; pub mod details; pub mod popups; pub mod profiles; +pub mod search; pub mod status; pub fn styled_title(title: &str, highlight: bool) -> Line<'_> { diff --git a/src/tui/widgets/popups/base.rs b/src/tui/widgets/popups/base.rs index c37d7b3..8fb8509 100644 --- a/src/tui/widgets/popups/base.rs +++ b/src/tui/widgets/popups/base.rs @@ -11,6 +11,7 @@ pub struct PopupFrame<'a> { pub border_color: Color, pub bg: Option, pub keybinds: Option>, + pub search_line: Option>, pub content: Box, } @@ -28,6 +29,10 @@ impl<'a> Widget for PopupFrame<'a> { .border_type(BorderType::Rounded) .border_style(Style::default().fg(self.border_color)); + if let Some(sl) = self.search_line { + block = block.title_top(sl.alignment(Alignment::Right)); + } + if let Some(kb) = self.keybinds { block = block.title_bottom(kb.alignment(Alignment::Right)); } diff --git a/src/tui/widgets/popups/confirm.rs b/src/tui/widgets/popups/confirm.rs index 2b9880c..f7afeb5 100644 --- a/src/tui/widgets/popups/confirm.rs +++ b/src/tui/widgets/popups/confirm.rs @@ -76,6 +76,7 @@ impl Widget for ConfirmPopup { border_color: THEME.colors.border_focused, bg: Some(THEME.colors.row_alternate_bg), keybinds: Some(kb), + search_line: None, content: Box::new(|inner, buf| { Paragraph::new("This will permanently remove the instance") .style(Style::default().fg(THEME.colors.foreground)) diff --git a/src/tui/widgets/popups/error.rs b/src/tui/widgets/popups/error.rs index 52cf452..03e82fb 100644 --- a/src/tui/widgets/popups/error.rs +++ b/src/tui/widgets/popups/error.rs @@ -43,6 +43,7 @@ impl Widget for ErrorPopup { border_color, bg: None, keybinds: None, + search_line: None, content: Box::new(move |inner, buf| { Paragraph::new(message.as_str()) .wrap(Wrap { trim: true }) diff --git a/src/tui/widgets/popups/new_instance.rs b/src/tui/widgets/popups/new_instance.rs index aee98f1..6df41d2 100644 --- a/src/tui/widgets/popups/new_instance.rs +++ b/src/tui/widgets/popups/new_instance.rs @@ -59,8 +59,7 @@ pub struct WizardState { pub loader_idx: usize, pub loader_versions: LoadState>, pub loader_version_idx: usize, - pub version_search: String, - pub version_search_active: bool, + pub version_search: crate::tui::widgets::search::SearchState, } impl WizardState { @@ -131,11 +130,14 @@ pub fn render(frame: &mut Frame, area: Rect, _focused: FocusedArea) { let keybinds = step_keybinds(&snapshot); + let search_line = snapshot.version_search.title_line(); + let popup = PopupFrame { title: wizard_title(&snapshot), border_color: THEME.colors.border_focused, bg: Some(THEME.colors.row_alternate_bg), keybinds: Some(keybinds), + search_line, content: Box::new(move |popup_area, buf| { let chunks = Layout::default() .direction(Direction::Vertical) @@ -245,11 +247,11 @@ fn handle_version_key( profiles_state: &mut profiles::State, ) { // Search mode: route char input to search query - if state.version_search_active { + if state.version_search.active { match key_event.code { KeyCode::Esc => { - state.version_search_active = false; - state.version_search.clear(); + state.version_search.active = false; + state.version_search.deactivate(); clamp_version_index(state); return; } @@ -277,15 +279,15 @@ fn handle_version_key( match key_event.code { KeyCode::Esc => { - if state.version_search_active { - state.version_search_active = false; - state.version_search.clear(); + if state.version_search.active { + state.version_search.active = false; + state.version_search.deactivate(); clamp_version_index(state); } else { close_popup(state, profiles_state); } } - KeyCode::Left | KeyCode::Char('h') if !state.version_search_active => { + KeyCode::Left | KeyCode::Char('h') if !state.version_search.active => { state.step = WizardStep::Loader; } KeyCode::Char('j') | KeyCode::Down => { @@ -296,15 +298,15 @@ fn handle_version_key( KeyCode::Char('k') | KeyCode::Up => { state.version_idx = state.version_idx.saturating_sub(1); } - KeyCode::Char('s') if !state.version_search_active => { + KeyCode::Char('s') if !state.version_search.active => { state.show_snapshots = !state.show_snapshots; clamp_version_index(state); } - KeyCode::Char('/') if !state.version_search_active => { - state.version_search_active = true; + KeyCode::Char('/') if !state.version_search.active => { + state.version_search.activate(); state.version_idx = 0; } - KeyCode::Enter | KeyCode::Right | KeyCode::Char('l') if !state.version_search_active => { + KeyCode::Enter | KeyCode::Right | KeyCode::Char('l') if !state.version_search.active => { if state.selected_version().is_none() { return; } @@ -321,8 +323,8 @@ fn handle_version_key( } } } - KeyCode::Enter if state.version_search_active => { - state.version_search_active = false; + KeyCode::Enter if state.version_search.active => { + state.version_search.active = false; } _ => {} } @@ -345,8 +347,8 @@ fn handle_loader_key( KeyCode::Enter | KeyCode::Right | KeyCode::Char('l') => { state.versions = LoadState::Idle; state.version_idx = 0; - state.version_search.clear(); - state.version_search_active = false; + state.version_search.deactivate(); + state.version_search.active = false; state.step = WizardStep::Version; ensure_versions_loaded(state); } @@ -484,24 +486,6 @@ fn render_version_step(state: &WizardState, area: Rect, buf: &mut ratatui::buffe .render(area, buf); } LoadState::Loaded(_) => { - // Split area for search bar (when active) + list - let (search_area, list_area) = if state.version_search_active || !state.version_search.is_empty() { - let chunks = Layout::default() - .direction(Direction::Vertical) - .constraints([Constraint::Length(1), Constraint::Min(1)]) - .split(area); - (Some(chunks[0]), chunks[1]) - } else { - (None, area) - }; - - if let Some(sa) = search_area { - let search_display = format!("/ {}█", state.version_search); - Paragraph::new(search_display.as_str()) - .style(Style::default().fg(THEME.colors.border_focused)) - .render(sa, buf); - } - let items: Vec = visible_versions(state) .into_iter() .map(|version| { @@ -523,7 +507,7 @@ fn render_version_step(state: &WizardState, area: Rect, buf: &mut ratatui::buffe .highlight_symbol("▶ "); let mut list_state = ListState::default().with_selected(Some(state.version_idx)); - StatefulWidget::render(list, list_area, buf, &mut list_state); + StatefulWidget::render(list, area, buf, &mut list_state); } } } @@ -638,7 +622,7 @@ fn render_confirm_step(state: &WizardState, area: Rect, buf: &mut ratatui::buffe } fn visible_versions(state: &WizardState) -> Vec { - let q = state.version_search.to_lowercase(); + let q = state.version_search.query.to_lowercase(); match &state.versions { LoadState::Loaded(versions) => versions .iter() diff --git a/src/tui/widgets/profiles.rs b/src/tui/widgets/profiles.rs index d2ccccb..2185697 100644 --- a/src/tui/widgets/profiles.rs +++ b/src/tui/widgets/profiles.rs @@ -1,11 +1,11 @@ use crossterm::event::KeyCode; use ratatui::{ - layout::{Constraint, Direction, Layout, Rect}, + layout::Rect, style::{Modifier, Style}, text::{Line, Span, Text}, widgets::{ - Block, BorderType, Borders, List, ListItem, ListState, Paragraph, Scrollbar, - ScrollbarOrientation, ScrollbarState, Widget, + Block, BorderType, Borders, List, ListItem, ListState, Scrollbar, ScrollbarOrientation, + ScrollbarState, }, Frame, }; @@ -13,7 +13,7 @@ use ratatui::{ use crate::instance::models::InstanceConfig; use crate::tui::{layout::FocusedArea, theme::THEME}; -use super::{popups, styled_title, WidgetKey}; +use super::{popups, search::SearchState, styled_title, WidgetKey}; #[derive(Debug, Default)] pub struct State { @@ -21,8 +21,7 @@ pub struct State { pub list_state: ListState, pub scrollbar_state: ScrollbarState, pub show_popup: bool, - pub search_mode: bool, - pub search_query: String, + pub search: SearchState, } impl State { @@ -33,8 +32,7 @@ impl State { list_state: ListState::default(), scrollbar_state: ScrollbarState::default(), show_popup: false, - search_mode: false, - search_query: String::new(), + search: SearchState::default(), }; if count > 0 { s.list_state.select(Some(0)); @@ -52,17 +50,12 @@ impl State { } fn filtered_indices(&self) -> Vec { - if self.search_query.is_empty() { - (0..self.instances.len()).collect() - } else { - let q = self.search_query.to_lowercase(); - self.instances - .iter() - .enumerate() - .filter(|(_, inst)| inst.name.to_lowercase().contains(&q)) - .map(|(i, _)| i) - .collect() - } + self.instances + .iter() + .enumerate() + .filter(|(_, inst)| self.search.matches(&inst.name)) + .map(|(i, _)| i) + .collect() } fn next(&mut self) { @@ -142,24 +135,23 @@ impl State { impl WidgetKey for State { fn handle_key(&mut self, key_event: &crossterm::event::KeyEvent) { match key_event.code { - KeyCode::Char('/') if !self.search_mode => { - self.search_mode = true; + KeyCode::Char('/') if !self.search.active => { + self.search.activate(); self.list_state.select(Some(0)); self.update_scrollbar(); } - KeyCode::Esc if self.search_mode => { - self.search_mode = false; - self.search_query.clear(); + KeyCode::Esc if self.search.active => { + self.search.deactivate(); self.list_state.select(Some(0)); self.update_scrollbar(); } - KeyCode::Backspace if self.search_mode => { - self.search_query.pop(); + KeyCode::Backspace if self.search.active => { + self.search.pop(); self.list_state.select(Some(0)); self.update_scrollbar(); } - KeyCode::Char(c) if self.search_mode && c != 'j' && c != 'k' => { - self.search_query.push(c); + KeyCode::Char(c) if self.search.active && c != 'j' && c != 'k' => { + self.search.push(c); self.list_state.select(Some(0)); self.update_scrollbar(); } @@ -167,9 +159,7 @@ impl WidgetKey for State { self.show_popup = true; self.update_scrollbar(); } - KeyCode::Char('d') => { - // Deletion handled by layout.rs which calls remove_instance - } + KeyCode::Char('d') => {} KeyCode::Char('j') | KeyCode::Down => self.next(), KeyCode::Char('k') | KeyCode::Up => self.previous(), _ => {} @@ -184,12 +174,16 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut S THEME.colors.border_unfocused }; - let block = Block::default() + let mut block = Block::default() .title(styled_title("Profiles", true)) .borders(Borders::ALL) .border_type(BorderType::Rounded) .border_style(Style::default().fg(color)); + if let Some(search_line) = state.search.title_line() { + block = block.title_top(search_line); + } + frame.render_widget(&block, area); let scrollbar_area = Rect { @@ -200,16 +194,7 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut S }; let inner_area = block.inner(area); - - let (list_area, search_area) = if state.search_mode { - let chunks = Layout::default() - .direction(Direction::Vertical) - .constraints([Constraint::Min(1), Constraint::Length(1)]) - .split(inner_area); - (chunks[0], Some(chunks[1])) - } else { - (inner_area, None) - }; + let list_area = inner_area; let filtered = state.filtered_indices(); let items: Vec = filtered @@ -245,13 +230,6 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut S frame.render_stateful_widget(list, list_area, &mut state.list_state); - if let Some(sa) = search_area { - let search_text = format!("/ {}\u{2588}", state.search_query); - let paragraph = - Paragraph::new(search_text).style(Style::default().fg(THEME.colors.border_focused)); - paragraph.render(sa, frame.buffer_mut()); - } - frame.render_stateful_widget( Scrollbar::default() .orientation(ScrollbarOrientation::VerticalRight) diff --git a/src/tui/widgets/search.rs b/src/tui/widgets/search.rs new file mode 100644 index 0000000..92a3f48 --- /dev/null +++ b/src/tui/widgets/search.rs @@ -0,0 +1,66 @@ +use ratatui::{ + style::{Modifier, Style}, + text::{Line, Span}, +}; + +use crate::tui::theme::THEME; + +#[derive(Debug, Default, Clone)] +pub struct SearchState { + pub query: String, + pub active: bool, +} + +impl SearchState { + pub fn activate(&mut self) { + self.active = true; + } + + pub fn deactivate(&mut self) { + self.active = false; + self.query.clear(); + } + + pub fn push(&mut self, c: char) { + self.query.push(c); + } + + pub fn pop(&mut self) { + self.query.pop(); + } + + pub fn is_empty(&self) -> bool { + self.query.is_empty() + } + + pub fn matches(&self, text: &str) -> bool { + if self.query.is_empty() { + return true; + } + text.to_lowercase().contains(&self.query.to_lowercase()) + } + + pub fn title_line(&self) -> Option> { + if !self.active && self.query.is_empty() { + return None; + } + + let dim = Style::default().fg(THEME.colors.border_unfocused); + let accent = Style::default() + .fg(THEME.colors.border_focused) + .add_modifier(Modifier::BOLD); + + let mut spans = vec![ + Span::styled(" / ", dim), + Span::styled(self.query.clone(), accent), + ]; + + if self.active { + spans.push(Span::styled("\u{2588}", accent)); + } + + spans.push(Span::raw(" ")); + + Some(Line::from(spans).right_aligned()) + } +}