From e85cc943b321515391b04178d72819951362dd6a Mon Sep 17 00:00:00 2001 From: afonsofrancof Date: Tue, 30 Jul 2024 22:46:26 +0100 Subject: [PATCH 1/3] Fix selection bug on search --- src/list.rs | 28 ++++++++++++++++------------ src/main.rs | 5 ++++- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/list.rs b/src/list.rs index 0f7b9bdd..e3e14b69 100644 --- a/src/list.rs +++ b/src/list.rs @@ -238,6 +238,13 @@ impl CustomList { } } + /// Resets the selection to the first item + pub fn reset_selection(&mut self) { + if !self.filtered_items.is_empty() { + self.list_state.select(Some(0)); + } + } + /// Handle key events, we are only interested in `Press` and `Repeat` events pub fn handle_key(&mut self, event: KeyEvent, state: &AppState) -> Option { if event.kind == KeyEventKind::Release { @@ -325,18 +332,15 @@ impl CustomList { self.filtered_items.len() }; - if let Some(curr_selection) = self.list_state.selected() { - if self.at_root() { - self.list_state - .select(Some((curr_selection + 1).min(count - 1))); - } else { - // When we are not at the root, we have to account for 1 more "virtual" node, `..`. So - // the count is 1 bigger (select is 0 based, because it's an index) - self.list_state - .select(Some((curr_selection + 1).min(count))); - } - } else if count > 0 { - self.list_state.select(Some(0)); + let curr_selection = self.list_state.selected().unwrap(); + if self.at_root() { + self.list_state + .select(Some((curr_selection + 1).min(count - 1))); + } else { + // When we are not at the root, we have to account for 1 more "virtual" node, `..`. So + // the count is 1 bigger (select is 0 based, because it's an index) + self.list_state + .select(Some((curr_selection + 1).min(count))); } } diff --git a/src/main.rs b/src/main.rs index c1122acb..b6a175b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -162,7 +162,10 @@ fn run(terminal: &mut Terminal, state: &AppState) -> io::Result<( search_input = String::new(); in_search_mode = false } - KeyCode::Enter => in_search_mode = false, + KeyCode::Enter => { + in_search_mode = false; + custom_list.reset_selection(); + } _ => {} } } else if let Some(cmd) = custom_list.handle_key(key, state) { From b7c2ecedd9b00dc92e48609f0c53a4df482228ca Mon Sep 17 00:00:00 2001 From: afonsofrancof Date: Tue, 30 Jul 2024 22:49:51 +0100 Subject: [PATCH 2/3] Fix crash when trying to scroll on empty results --- src/list.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/list.rs b/src/list.rs index e3e14b69..9abb156d 100644 --- a/src/list.rs +++ b/src/list.rs @@ -242,6 +242,8 @@ impl CustomList { pub fn reset_selection(&mut self) { if !self.filtered_items.is_empty() { self.list_state.select(Some(0)); + } else { + self.list_state.select(None); } } @@ -317,8 +319,11 @@ impl CustomList { } fn try_scroll_up(&mut self) { - self.list_state - .select(Some(self.list_state.selected().unwrap().saturating_sub(1))); + if let Some(selected) = self.list_state.selected() { + if selected > 0 { + self.list_state.select(Some(selected.saturating_sub(1))); + } + } } fn try_scroll_down(&mut self) { @@ -332,15 +337,16 @@ impl CustomList { self.filtered_items.len() }; - let curr_selection = self.list_state.selected().unwrap(); - if self.at_root() { - self.list_state - .select(Some((curr_selection + 1).min(count - 1))); - } else { - // When we are not at the root, we have to account for 1 more "virtual" node, `..`. So - // the count is 1 bigger (select is 0 based, because it's an index) - self.list_state - .select(Some((curr_selection + 1).min(count))); + if let Some(curr_selection) = self.list_state.selected() { + if self.at_root() { + self.list_state + .select(Some((curr_selection + 1).min(count - 1))); + } else { + // When we are not at the root, we have to account for 1 more "virtual" node, `..`. So + // the count is 1 bigger (select is 0 based, because it's an index) + self.list_state + .select(Some((curr_selection + 1).min(count))); + } } } From c39972295941445ba3021ab02599b372426372d2 Mon Sep 17 00:00:00 2001 From: afonsofrancof Date: Tue, 30 Jul 2024 22:56:38 +0100 Subject: [PATCH 3/3] Sorting alphabetically --- src/list.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/list.rs b/src/list.rs index 9abb156d..753b9e02 100644 --- a/src/list.rs +++ b/src/list.rs @@ -168,7 +168,9 @@ impl CustomList { } items } else { - self.filtered_items + let mut sorted_items = self.filtered_items.clone(); + sorted_items.sort_by(|a, b| a.name.cmp(b.name)); + sorted_items .iter() .map(|node| { Line::from(format!("{} {}", state.theme.cmd_icon, node.name))