From bddc943258df6b5e3ce349e58090bcde62b5857f Mon Sep 17 00:00:00 2001 From: afonsofrancof Date: Wed, 31 Jul 2024 01:14:33 +0100 Subject: [PATCH] Change filter logic and fix wrong command on select bug --- src/list.rs | 11 ++++++----- src/main.rs | 9 +++++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/list.rs b/src/list.rs index c794aafa..193092a5 100644 --- a/src/list.rs +++ b/src/list.rs @@ -129,8 +129,7 @@ impl CustomList { } /// Draw our custom widget to the frame - pub fn draw(&mut self, frame: &mut Frame, area: Rect, filter: String, state: &AppState) { - self.filter(filter); + pub fn draw(&mut self, frame: &mut Frame, area: Rect, state: &AppState) { let item_list: Vec = if self.filter_query.is_empty() { let mut items: Vec = vec![]; @@ -168,9 +167,7 @@ impl CustomList { } items } else { - let mut sorted_items = self.filtered_items.clone(); - sorted_items.sort_by(|a, b| a.name.cmp(b.name)); - sorted_items + self.filtered_items .iter() .map(|node| { Line::from(format!("{} {}", state.theme.cmd_icon, node.name)) @@ -225,6 +222,7 @@ impl CustomList { self.filtered_items.clear(); let query_lower = query.to_lowercase(); + let mut stack = vec![self.inner_tree.root().id()]; while let Some(node_id) = stack.pop() { @@ -238,6 +236,7 @@ impl CustomList { stack.push(child.id()); } } + self.filtered_items.sort_by(|a, b| a.name.cmp(b.name)); } /// Resets the selection to the first item @@ -345,6 +344,7 @@ impl CustomList { /// duplication, but I don't want to make too major changes to the codebase. fn get_selected_command(&self) -> Option { let selected_index = self.list_state.selected().unwrap_or(0); + println!("Selected Index: {}", selected_index); if self.filter_query.is_empty() { // No filter query, use the regular tree navigation @@ -370,6 +370,7 @@ impl CustomList { } else { // Filter query is active, use the filtered items if let Some(filtered_node) = self.filtered_items.get(selected_index) { + println!("Filtered Node Name: {}", filtered_node.name); return Some(filtered_node.command.clone()); } } diff --git a/src/main.rs b/src/main.rs index b6a175b7..e222c3ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,7 +117,7 @@ fn run(terminal: &mut Terminal, state: &AppState) -> io::Result<( //Render the search bar (First chunk of the screen) frame.render_widget(search_bar, chunks[0]); //Render the command list (Second chunk of the screen) - custom_list.draw(frame, chunks[1], search_input.clone(), state); + custom_list.draw(frame, chunks[1], state); if let Some(ref mut command) = &mut command_opt { command.draw(frame, state); @@ -154,12 +154,17 @@ fn run(terminal: &mut Terminal, state: &AppState) -> io::Result<( //Insert user input into the search bar if in_search_mode { match key.code { - KeyCode::Char(c) => search_input.push(c), + KeyCode::Char(c) => { + search_input.push(c); + custom_list.filter(search_input.clone()); + }, KeyCode::Backspace => { search_input.pop(); + custom_list.filter(search_input.clone()); } KeyCode::Esc => { search_input = String::new(); + custom_list.filter(search_input.clone()); in_search_mode = false } KeyCode::Enter => {