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) {