feat(tui): add search to all content tabs

This commit is contained in:
objz committed 2025-12-13 20:38:42 +01:00
1 parent 3a637a1dbd
commit cba8fa7661
4 files changed
+164 -16

No files matched your search

+14 -1
View File
@@ -113,12 +113,25 @@ pub fn render(
}) })
.collect(); .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)) .title_top(Line::from(tab_titles))
.borders(Borders::ALL) .borders(Borders::ALL)
.border_type(BorderType::Rounded) .border_type(BorderType::Rounded)
.border_style(Style::default().fg(border_color)); .border_style(Style::default().fg(border_color));
if let Some(sl) = search_line {
block = block.title_top(sl);
}
let content_area = block.inner(area); let content_area = block.inner(area);
frame.render_widget(block, area); frame.render_widget(block, area);
+81 -14
View File
@@ -28,6 +28,7 @@ pub struct ContentListState {
pub scrollbar_state: ScrollbarState, pub scrollbar_state: ScrollbarState,
pub loaded_for: Option<String>, pub loaded_for: Option<String>,
pub loading: bool, pub loading: bool,
pub search: super::search::SearchState,
cache: HashMap<String, CachedList>, cache: HashMap<String, CachedList>,
pending: Arc<Mutex<Option<(String, Vec<ModEntry>)>>>, pending: Arc<Mutex<Option<(String, Vec<ModEntry>)>>>,
} }
@@ -40,12 +41,24 @@ impl Default for ContentListState {
scrollbar_state: ScrollbarState::default(), scrollbar_state: ScrollbarState::default(),
loaded_for: None, loaded_for: None,
loading: false, loading: false,
search: super::search::SearchState::default(),
cache: HashMap::new(), cache: HashMap::new(),
pending: Arc::new(Mutex::new(None)), pending: Arc::new(Mutex::new(None)),
} }
} }
} }
impl ContentListState {
pub fn filtered_indices(&self) -> Vec<usize> {
self.entries
.iter()
.enumerate()
.filter(|(_, e)| self.search.matches(&e.name))
.map(|(i, _)| i)
.collect()
}
}
impl ContentListState { impl ContentListState {
pub fn start_load<F>(&mut self, instances_dir: &Path, instance_name: &str, scan_fn: F) pub fn start_load<F>(&mut self, instances_dir: &Path, instance_name: &str, scan_fn: F)
where 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 { 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 { match key_event.code {
KeyCode::Char('j') | KeyCode::Down => { KeyCode::Char('j') | KeyCode::Down => {
let count = state.entries.len();
if count == 0 { if count == 0 {
return true; return true;
} }
@@ -191,12 +239,12 @@ pub fn handle_key_no_toggle(key_event: &KeyEvent, state: &mut ContentListState)
true true
} }
KeyCode::Enter if key_event.modifiers.contains(KeyModifiers::SHIFT) => { KeyCode::Enter if key_event.modifiers.contains(KeyModifiers::SHIFT) => {
if let Some(entry) = state if let Some(&real_idx) = state
.list_state .list_state
.selected .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") if let Err(e) = std::process::Command::new("xdg-open")
.arg(dir) .arg(dir)
.stdout(std::process::Stdio::null()) .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. /// Returns `true` if the key was consumed.
pub fn handle_key(key_event: &KeyEvent, state: &mut ContentListState) -> bool { 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 { match key_event.code {
KeyCode::Char('j') | KeyCode::Down => { KeyCode::Char('j') | KeyCode::Down => {
let count = state.entries.len();
if count == 0 { if count == 0 {
return true; return true;
} }
@@ -233,12 +285,12 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut ContentListState) -> bool {
true true
} }
KeyCode::Enter if key_event.modifiers.contains(KeyModifiers::SHIFT) => { KeyCode::Enter if key_event.modifiers.contains(KeyModifiers::SHIFT) => {
if let Some(entry) = state if let Some(&real_idx) = state
.list_state .list_state
.selected .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") if let Err(e) = std::process::Command::new("xdg-open")
.arg(dir) .arg(dir)
.stdout(std::process::Stdio::null()) .stdout(std::process::Stdio::null())
@@ -252,7 +304,20 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut ContentListState) -> bool {
true true
} }
KeyCode::Enter => { 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 true
} }
_ => false, _ => false,
@@ -275,7 +340,9 @@ pub fn render(
return; return;
} }
if state.entries.is_empty() { let filtered = state.filtered_indices();
if filtered.is_empty() {
frame.render_widget( frame.render_widget(
Paragraph::new(empty_text).style(Style::default().fg(THEME.colors.text_idle)), Paragraph::new(empty_text).style(Style::default().fg(THEME.colors.text_idle)),
area, area,
@@ -283,12 +350,12 @@ pub fn render(
return; return;
} }
let count = state.entries.len(); let count = filtered.len();
let snapshot: Vec<(String, String, bool, Option<Vec<Vec<IconCell>>>)> = state let snapshot: Vec<(String, String, bool, Option<Vec<Vec<IconCell>>>)> = filtered
.entries
.iter() .iter()
.map(|entry| { .map(|&i| {
let entry = &state.entries[i];
( (
entry.name.clone(), entry.name.clone(),
entry.description.clone(), entry.description.clone(),
+31
View File
@@ -27,6 +27,7 @@ pub struct LogsState {
pub viewer_max_scroll: usize, pub viewer_max_scroll: usize,
pub scrollbar_state: ScrollbarState, pub scrollbar_state: ScrollbarState,
pub viewer_scrollbar_state: ScrollbarState, pub viewer_scrollbar_state: ScrollbarState,
pub search: super::search::SearchState,
selected_path: Option<std::path::PathBuf>, selected_path: Option<std::path::PathBuf>,
pending: Arc<Mutex<Option<(String, Vec<LogFileEntry>)>>>, pending: Arc<Mutex<Option<(String, Vec<LogFileEntry>)>>>,
rescan_counter: u8, rescan_counter: u8,
@@ -46,6 +47,7 @@ impl Default for LogsState {
viewer_max_scroll: 0, viewer_max_scroll: 0,
scrollbar_state: ScrollbarState::default(), scrollbar_state: ScrollbarState::default(),
viewer_scrollbar_state: ScrollbarState::default(), viewer_scrollbar_state: ScrollbarState::default(),
search: super::search::SearchState::default(),
selected_path: None, selected_path: None,
pending: Arc::new(Mutex::new(None)), pending: Arc::new(Mutex::new(None)),
rescan_counter: 0, rescan_counter: 0,
@@ -251,6 +253,35 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut LogsState) -> bool {
_ => false, _ => false,
} }
} else { } 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(); let display_count = state.display_count();
match key_event.code { match key_event.code {
KeyCode::Char('J') | KeyCode::Down if shift => { KeyCode::Char('J') | KeyCode::Down if shift => {
+38 -1
View File
@@ -32,6 +32,7 @@ pub struct ScreenshotsState {
cols: usize, cols: usize,
visible_rows: usize, visible_rows: usize,
pub scrollbar_state: ScrollbarState, pub scrollbar_state: ScrollbarState,
pub search: super::search::SearchState,
pub font_size: (u16, u16), pub font_size: (u16, u16),
pending_entries: Arc<Mutex<Option<(String, Vec<ScreenshotEntry>)>>>, pending_entries: Arc<Mutex<Option<(String, Vec<ScreenshotEntry>)>>>,
pending_images: Arc<Mutex<Vec<(usize, image::DynamicImage)>>>, pending_images: Arc<Mutex<Vec<(usize, image::DynamicImage)>>>,
@@ -50,6 +51,7 @@ impl Default for ScreenshotsState {
cols: 3, cols: 3,
visible_rows: 2, visible_rows: 2,
scrollbar_state: ScrollbarState::default(), scrollbar_state: ScrollbarState::default(),
search: super::search::SearchState::default(),
font_size: (8, 16), font_size: (8, 16),
pending_entries: Arc::new(Mutex::new(None)), pending_entries: Arc::new(Mutex::new(None)),
pending_images: Arc::new(Mutex::new(Vec::new())), 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 { 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<usize> = state
.entries
.iter()
.enumerate()
.filter(|(_, e)| state.search.matches(&e.name))
.map(|(i, _)| i)
.collect();
let count = filtered.len();
if count == 0 { if count == 0 {
if key_event.code == KeyCode::Char('/') {
state.search.activate();
return true;
}
return false; return false;
} }
let cols = state.cols.max(1); let cols = state.cols.max(1);
match key_event.code { match key_event.code {
KeyCode::Char('/') => {
state.search.activate();
state.selected = 0;
true
}
KeyCode::Enter if key_event.modifiers.contains(KeyModifiers::SHIFT) => { KeyCode::Enter if key_event.modifiers.contains(KeyModifiers::SHIFT) => {
if let Some(entry) = state.entries.get(state.selected) { if let Some(entry) = state.entries.get(state.selected) {
if let Some(dir) = entry.path.parent() { if let Some(dir) = entry.path.parent() {