From c2c4c8796d01c78daf2ee21a8e2a4c3bf5bcb2bc Mon Sep 17 00:00:00 2001 From: afonsofrancof Date: Wed, 31 Jul 2024 00:13:57 +0100 Subject: [PATCH] Removed try_scroll down and up, there is a builtin for that --- src/list.rs | 93 +++++++++++++++++++++-------------------------------- 1 file changed, 37 insertions(+), 56 deletions(-) diff --git a/src/list.rs b/src/list.rs index 567d2d70..d6e9336c 100644 --- a/src/list.rs +++ b/src/list.rs @@ -50,6 +50,19 @@ impl PreviewWindowState { } } +use std::fs::OpenOptions; +use std::io::Write; + +fn log_debug_info(info: &str) { + let mut file = OpenOptions::new() + .append(true) + .create(true) + .open("debug.log") + .unwrap(); + + writeln!(file, "{}", info).unwrap(); +} + impl CustomList { pub fn new() -> Self { // When a function call ends with an exclamation mark, it means it's a macro, like in this @@ -264,7 +277,7 @@ impl CustomList { return None; } - self.try_scroll_down(); + self.list_state.select_next(); None } KeyCode::Char('k') | KeyCode::Up => { @@ -275,7 +288,7 @@ impl CustomList { return None; } - self.try_scroll_up(); + self.list_state.select_previous(); None } // The 'p' key toggles the preview on and off @@ -320,38 +333,6 @@ impl CustomList { } } - fn try_scroll_up(&mut self) { - 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) { - let count = if self.filter_query.is_empty() { - let curr = self - .inner_tree - .get(*self.visit_stack.last().unwrap()) - .unwrap(); - curr.children().count() - } else { - 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))); - } - } - } - /// Scroll the preview window down fn scroll_preview_window_down(&mut self) { if let Some(pw_state) = &mut self.preview_window_state { @@ -376,7 +357,7 @@ impl CustomList { /// 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. fn get_selected_command(&self) -> Option { - let selected = self.list_state.selected().unwrap(); + let selected_index = self.list_state.selected().unwrap_or(0); if self.filter_query.is_empty() { // No filter query, use the regular tree navigation @@ -385,25 +366,27 @@ impl CustomList { .get(*self.visit_stack.last().unwrap()) .unwrap(); - // 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_index == 0 { return None; } - for (mut idx, node) in curr.children().enumerate() { - if !self.at_root() { - idx += 1; - } - if idx == selected { + let mut actual_index = selected_index; + if !self.at_root() { + actual_index -= 1; // Adjust for the ".." item if not at root + } + + for (idx, node) in curr.children().enumerate() { + if idx == actual_index { 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) { + if let Some(filtered_node) = self.filtered_items.get(selected_index) { return Some(filtered_node.command.clone()); } } + None } @@ -414,8 +397,7 @@ impl CustomList { /// /// Returns `Some(command)` when command is selected, othervise we returns `None` fn handle_enter(&mut self) -> Option { - // Get the selected index - let selected = self.list_state.selected().unwrap(); + let selected_index = self.list_state.selected().unwrap_or(0); if self.filter_query.is_empty() { // No filter query, use the regular tree navigation @@ -424,21 +406,19 @@ impl CustomList { .get(*self.visit_stack.last().unwrap()) .unwrap(); - // if we are not at the root, and the first element is selected, - // we can be sure it's '..', so we go up the directory - if !self.at_root() && selected == 0 { + if !self.at_root() && selected_index == 0 { self.visit_stack.pop(); self.list_state.select(Some(0)); return None; } - for (mut idx, node) in curr.children().enumerate() { - // at this point, we know that we are not on the .. item, and our indexes of the items never had .. - // item. so to balance it out, in case the selection index contains .., se add 1 to our node index - if !self.at_root() { - idx += 1; - } - if idx == selected { + let mut actual_index = selected_index; + if !self.at_root() { + actual_index -= 1; // Adjust for the ".." item if not at root + } + + for (idx, node) in curr.children().enumerate() { + if idx == actual_index { if node.has_children() { self.visit_stack.push(node.id()); self.list_state.select(Some(0)); @@ -450,10 +430,11 @@ impl CustomList { } } else { // Filter query is active, use the filtered items - if let Some(filtered_node) = self.filtered_items.get(selected) { + if let Some(filtered_node) = self.filtered_items.get(selected_index) { return Some(filtered_node.command.clone()); } } + None }