Fix some bugs

This commit is contained in:
afonsofrancof 2024-07-30 22:12:03 +01:00
parent 71c81b2302
commit 847e09b637
No known key found for this signature in database

View File

@ -325,7 +325,7 @@ impl CustomList {
self.filtered_items.len() self.filtered_items.len()
}; };
let curr_selection = self.list_state.selected().unwrap(); if let Some(curr_selection) = self.list_state.selected() {
if self.at_root() { if self.at_root() {
self.list_state self.list_state
.select(Some((curr_selection + 1).min(count - 1))); .select(Some((curr_selection + 1).min(count - 1)));
@ -335,6 +335,9 @@ impl CustomList {
self.list_state self.list_state
.select(Some((curr_selection + 1).min(count))); .select(Some((curr_selection + 1).min(count)));
} }
} else if count > 0 {
self.list_state.select(Some(0));
}
} }
/// Scroll the preview window down /// Scroll the preview window down
@ -355,17 +358,20 @@ impl CustomList {
} }
} }
/// This method return the currently selected command, or None if no command is selected. /// This method returns the currently selected command, or None if no command is selected.
/// It was extracted from the 'handle_enter()' /// It was extracted from the 'handle_enter()'
/// ///
/// This could probably be integrated into the 'handle_enter()' method as to avoid code /// This could probably be integrated into the 'handle_enter()' method to avoid code
/// 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 = self.list_state.selected().unwrap();
if self.filter_query.is_empty() {
// No filter query, use the regular tree navigation
let curr = self let curr = self
.inner_tree .inner_tree
.get(*self.visit_stack.last().unwrap()) .get(*self.visit_stack.last().unwrap())
.unwrap(); .unwrap();
let selected = self.list_state.selected().unwrap();
// If we are not at the root and the first item is selected, it's the `..` item // If we are not at the root and the first item is selected, it's the `..` item
if !self.at_root() && selected == 0 { if !self.at_root() && selected == 0 {
@ -380,6 +386,12 @@ impl CustomList {
return Some(node.value().command.clone()); return Some(node.value().command.clone());
} }
} }
} else {
// Filter query is active, use the filtered items
if let Some(filtered_node) = self.filtered_items.get(selected) {
return Some(filtered_node.command.clone());
}
}
None None
} }
@ -390,6 +402,11 @@ impl CustomList {
/// ///
/// Returns `Some(command)` when command is selected, othervise we returns `None` /// Returns `Some(command)` when command is selected, othervise we returns `None`
fn handle_enter(&mut self) -> Option<Command> { fn handle_enter(&mut self) -> Option<Command> {
// Ensure an item is selected if none is selected
if self.list_state.selected().is_none() {
self.list_state.select(Some(0));
}
// Get the selected index // Get the selected index
let selected = self.list_state.selected().unwrap(); let selected = self.list_state.selected().unwrap();