Change filter logic and fix wrong command on select bug

This commit is contained in:
afonsofrancof 2024-07-31 01:14:33 +01:00
parent 2891589652
commit bddc943258
No known key found for this signature in database
2 changed files with 13 additions and 7 deletions

View File

@ -129,8 +129,7 @@ impl CustomList {
} }
/// Draw our custom widget to the frame /// Draw our custom widget to the frame
pub fn draw(&mut self, frame: &mut Frame, area: Rect, filter: String, state: &AppState) { pub fn draw(&mut self, frame: &mut Frame, area: Rect, state: &AppState) {
self.filter(filter);
let item_list: Vec<Line> = if self.filter_query.is_empty() { let item_list: Vec<Line> = if self.filter_query.is_empty() {
let mut items: Vec<Line> = vec![]; let mut items: Vec<Line> = vec![];
@ -168,9 +167,7 @@ impl CustomList {
} }
items items
} else { } else {
let mut sorted_items = self.filtered_items.clone(); self.filtered_items
sorted_items.sort_by(|a, b| a.name.cmp(b.name));
sorted_items
.iter() .iter()
.map(|node| { .map(|node| {
Line::from(format!("{} {}", state.theme.cmd_icon, node.name)) Line::from(format!("{} {}", state.theme.cmd_icon, node.name))
@ -225,6 +222,7 @@ impl CustomList {
self.filtered_items.clear(); self.filtered_items.clear();
let query_lower = query.to_lowercase(); let query_lower = query.to_lowercase();
let mut stack = vec![self.inner_tree.root().id()]; let mut stack = vec![self.inner_tree.root().id()];
while let Some(node_id) = stack.pop() { while let Some(node_id) = stack.pop() {
@ -238,6 +236,7 @@ impl CustomList {
stack.push(child.id()); stack.push(child.id());
} }
} }
self.filtered_items.sort_by(|a, b| a.name.cmp(b.name));
} }
/// Resets the selection to the first item /// 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. /// duplication, but I don't want to make too major changes to the codebase.
fn get_selected_command(&self) -> Option<Command> { fn get_selected_command(&self) -> Option<Command> {
let selected_index = self.list_state.selected().unwrap_or(0); let selected_index = self.list_state.selected().unwrap_or(0);
println!("Selected Index: {}", selected_index);
if self.filter_query.is_empty() { if self.filter_query.is_empty() {
// No filter query, use the regular tree navigation // No filter query, use the regular tree navigation
@ -370,6 +370,7 @@ impl CustomList {
} else { } else {
// Filter query is active, use the filtered items // Filter query is active, use the filtered items
if let Some(filtered_node) = self.filtered_items.get(selected_index) { if let Some(filtered_node) = self.filtered_items.get(selected_index) {
println!("Filtered Node Name: {}", filtered_node.name);
return Some(filtered_node.command.clone()); return Some(filtered_node.command.clone());
} }
} }

View File

@ -117,7 +117,7 @@ fn run<B: Backend>(terminal: &mut Terminal<B>, state: &AppState) -> io::Result<(
//Render the search bar (First chunk of the screen) //Render the search bar (First chunk of the screen)
frame.render_widget(search_bar, chunks[0]); frame.render_widget(search_bar, chunks[0]);
//Render the command list (Second chunk of the screen) //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 { if let Some(ref mut command) = &mut command_opt {
command.draw(frame, state); command.draw(frame, state);
@ -154,12 +154,17 @@ fn run<B: Backend>(terminal: &mut Terminal<B>, state: &AppState) -> io::Result<(
//Insert user input into the search bar //Insert user input into the search bar
if in_search_mode { if in_search_mode {
match key.code { 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 => { KeyCode::Backspace => {
search_input.pop(); search_input.pop();
custom_list.filter(search_input.clone());
} }
KeyCode::Esc => { KeyCode::Esc => {
search_input = String::new(); search_input = String::new();
custom_list.filter(search_input.clone());
in_search_mode = false in_search_mode = false
} }
KeyCode::Enter => { KeyCode::Enter => {