diff --git a/src/tui/widgets/content.rs b/src/tui/widgets/content.rs index 106e6d7..682bac4 100644 --- a/src/tui/widgets/content.rs +++ b/src/tui/widgets/content.rs @@ -113,12 +113,25 @@ pub fn render( }) .collect(); - let block = Block::default() + let search_line = match tab { + ContentTab::Mods => mods_state.search.title_line(), + ContentTab::ResourcePacks => resource_packs_state.search.title_line(), + ContentTab::Shaders => shaders_state.search.title_line(), + ContentTab::Worlds => worlds_state.search.title_line(), + ContentTab::Screenshots => screenshots_state.search.title_line(), + ContentTab::Logs => logs_state.search.title_line(), + }; + + let mut block = Block::default() .title_top(Line::from(tab_titles)) .borders(Borders::ALL) .border_type(BorderType::Rounded) .border_style(Style::default().fg(border_color)); + if let Some(sl) = search_line { + block = block.title_top(sl); + } + let content_area = block.inner(area); frame.render_widget(block, area); diff --git a/src/tui/widgets/content_list.rs b/src/tui/widgets/content_list.rs index dcf05a1..c9164ec 100644 --- a/src/tui/widgets/content_list.rs +++ b/src/tui/widgets/content_list.rs @@ -28,6 +28,7 @@ pub struct ContentListState { pub scrollbar_state: ScrollbarState, pub loaded_for: Option, pub loading: bool, + pub search: super::search::SearchState, cache: HashMap, pending: Arc)>>>, } @@ -40,12 +41,24 @@ impl Default for ContentListState { scrollbar_state: ScrollbarState::default(), loaded_for: None, loading: false, + search: super::search::SearchState::default(), cache: HashMap::new(), pending: Arc::new(Mutex::new(None)), } } } +impl ContentListState { + pub fn filtered_indices(&self) -> Vec { + self.entries + .iter() + .enumerate() + .filter(|(_, e)| self.search.matches(&e.name)) + .map(|(i, _)| i) + .collect() + } +} + impl ContentListState { pub fn start_load(&mut self, instances_dir: &Path, instance_name: &str, scan_fn: F) where @@ -172,10 +185,45 @@ impl ContentListState { } } +fn handle_search_keys(key_event: &KeyEvent, state: &mut ContentListState) -> bool { + if state.search.active { + match key_event.code { + KeyCode::Esc => { + state.search.deactivate(); + state.list_state.selected = Some(0); + state.update_scrollbar(); + } + KeyCode::Backspace => { + state.search.pop(); + state.list_state.selected = Some(0); + state.update_scrollbar(); + } + KeyCode::Char(c) => { + state.search.push(c); + state.list_state.selected = Some(0); + state.update_scrollbar(); + } + _ => {} + } + return true; + } + if key_event.code == KeyCode::Char('/') { + state.search.activate(); + state.list_state.selected = Some(0); + state.update_scrollbar(); + return true; + } + false +} + pub fn handle_key_no_toggle(key_event: &KeyEvent, state: &mut ContentListState) -> bool { + if handle_search_keys(key_event, state) { + return true; + } + let filtered = state.filtered_indices(); + let count = filtered.len(); match key_event.code { KeyCode::Char('j') | KeyCode::Down => { - let count = state.entries.len(); if count == 0 { return true; } @@ -191,12 +239,12 @@ pub fn handle_key_no_toggle(key_event: &KeyEvent, state: &mut ContentListState) true } KeyCode::Enter if key_event.modifiers.contains(KeyModifiers::SHIFT) => { - if let Some(entry) = state + if let Some(&real_idx) = state .list_state .selected - .and_then(|i| state.entries.get(i)) + .and_then(|i| filtered.get(i)) { - if let Some(dir) = entry.path.parent() { + if let Some(dir) = state.entries[real_idx].path.parent() { if let Err(e) = std::process::Command::new("xdg-open") .arg(dir) .stdout(std::process::Stdio::null()) @@ -215,9 +263,13 @@ pub fn handle_key_no_toggle(key_event: &KeyEvent, state: &mut ContentListState) /// Returns `true` if the key was consumed. pub fn handle_key(key_event: &KeyEvent, state: &mut ContentListState) -> bool { + if handle_search_keys(key_event, state) { + return true; + } + let filtered = state.filtered_indices(); + let count = filtered.len(); match key_event.code { KeyCode::Char('j') | KeyCode::Down => { - let count = state.entries.len(); if count == 0 { return true; } @@ -233,12 +285,12 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut ContentListState) -> bool { true } KeyCode::Enter if key_event.modifiers.contains(KeyModifiers::SHIFT) => { - if let Some(entry) = state + if let Some(&real_idx) = state .list_state .selected - .and_then(|i| state.entries.get(i)) + .and_then(|i| filtered.get(i)) { - if let Some(dir) = entry.path.parent() { + if let Some(dir) = state.entries[real_idx].path.parent() { if let Err(e) = std::process::Command::new("xdg-open") .arg(dir) .stdout(std::process::Stdio::null()) @@ -252,7 +304,20 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut ContentListState) -> bool { true } KeyCode::Enter => { - state.toggle_selected(); + if let Some(&real_idx) = state + .list_state + .selected + .and_then(|i| filtered.get(i)) + { + state.list_state.selected = Some(real_idx); + state.toggle_selected(); + state.list_state.selected = Some( + filtered + .iter() + .position(|&i| i == real_idx) + .unwrap_or(0), + ); + } true } _ => false, @@ -275,7 +340,9 @@ pub fn render( return; } - if state.entries.is_empty() { + let filtered = state.filtered_indices(); + + if filtered.is_empty() { frame.render_widget( Paragraph::new(empty_text).style(Style::default().fg(THEME.colors.text_idle)), area, @@ -283,12 +350,12 @@ pub fn render( return; } - let count = state.entries.len(); + let count = filtered.len(); - let snapshot: Vec<(String, String, bool, Option>>)> = state - .entries + let snapshot: Vec<(String, String, bool, Option>>)> = filtered .iter() - .map(|entry| { + .map(|&i| { + let entry = &state.entries[i]; ( entry.name.clone(), entry.description.clone(), diff --git a/src/tui/widgets/logs_viewer.rs b/src/tui/widgets/logs_viewer.rs index eb69431..0b7f4fd 100644 --- a/src/tui/widgets/logs_viewer.rs +++ b/src/tui/widgets/logs_viewer.rs @@ -27,6 +27,7 @@ pub struct LogsState { pub viewer_max_scroll: usize, pub scrollbar_state: ScrollbarState, pub viewer_scrollbar_state: ScrollbarState, + pub search: super::search::SearchState, selected_path: Option, pending: Arc)>>>, rescan_counter: u8, @@ -46,6 +47,7 @@ impl Default for LogsState { viewer_max_scroll: 0, scrollbar_state: ScrollbarState::default(), viewer_scrollbar_state: ScrollbarState::default(), + search: super::search::SearchState::default(), selected_path: None, pending: Arc::new(Mutex::new(None)), rescan_counter: 0, @@ -251,6 +253,35 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut LogsState) -> bool { _ => false, } } else { + if state.search.active { + match key_event.code { + KeyCode::Esc => { + state.search.deactivate(); + state.list_state.selected = Some(0); + state.update_scrollbar(); + } + KeyCode::Backspace => { + state.search.pop(); + state.list_state.selected = Some(0); + state.update_scrollbar(); + } + KeyCode::Char(c) => { + state.search.push(c); + state.list_state.selected = Some(0); + state.update_scrollbar(); + } + _ => {} + } + return true; + } + + if key_event.code == KeyCode::Char('/') { + state.search.activate(); + state.list_state.selected = Some(0); + state.update_scrollbar(); + return true; + } + let display_count = state.display_count(); match key_event.code { KeyCode::Char('J') | KeyCode::Down if shift => { diff --git a/src/tui/widgets/screenshots_grid.rs b/src/tui/widgets/screenshots_grid.rs index c8aae6d..0e18420 100644 --- a/src/tui/widgets/screenshots_grid.rs +++ b/src/tui/widgets/screenshots_grid.rs @@ -32,6 +32,7 @@ pub struct ScreenshotsState { cols: usize, visible_rows: usize, pub scrollbar_state: ScrollbarState, + pub search: super::search::SearchState, pub font_size: (u16, u16), pending_entries: Arc)>>>, pending_images: Arc>>, @@ -50,6 +51,7 @@ impl Default for ScreenshotsState { cols: 3, visible_rows: 2, scrollbar_state: ScrollbarState::default(), + search: super::search::SearchState::default(), font_size: (8, 16), pending_entries: Arc::new(Mutex::new(None)), pending_images: Arc::new(Mutex::new(Vec::new())), @@ -170,13 +172,48 @@ impl ScreenshotsState { } pub fn handle_key(key_event: &KeyEvent, state: &mut ScreenshotsState) -> bool { - let count = state.entries.len(); + if state.search.active { + match key_event.code { + KeyCode::Esc => { + state.search.deactivate(); + state.selected = 0; + } + KeyCode::Backspace => { + state.search.pop(); + state.selected = 0; + } + KeyCode::Char(c) => { + state.search.push(c); + state.selected = 0; + } + _ => {} + } + return true; + } + + let filtered: Vec = state + .entries + .iter() + .enumerate() + .filter(|(_, e)| state.search.matches(&e.name)) + .map(|(i, _)| i) + .collect(); + let count = filtered.len(); if count == 0 { + if key_event.code == KeyCode::Char('/') { + state.search.activate(); + return true; + } return false; } let cols = state.cols.max(1); match key_event.code { + KeyCode::Char('/') => { + state.search.activate(); + state.selected = 0; + true + } KeyCode::Enter if key_event.modifiers.contains(KeyModifiers::SHIFT) => { if let Some(entry) = state.entries.get(state.selected) { if let Some(dir) = entry.path.parent() {