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
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<Line> = if self.filter_query.is_empty() {
let mut items: Vec<Line> = 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<Command> {
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());
}
}

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)
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<B: Backend>(terminal: &mut Terminal<B>, 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 => {