mirror of
https://github.com/ChrisTitusTech/linutil.git
synced 2024-11-22 05:12:27 +00:00
Removed try_scroll down and up, there is a builtin for that
This commit is contained in:
parent
5d68f49e59
commit
c2c4c8796d
85
src/list.rs
85
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<Command> {
|
||||
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() {
|
||||
let mut actual_index = selected_index;
|
||||
if !self.at_root() {
|
||||
idx += 1;
|
||||
actual_index -= 1; // Adjust for the ".." item if not at root
|
||||
}
|
||||
if idx == selected {
|
||||
|
||||
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<Command> {
|
||||
// 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
|
||||
let mut actual_index = selected_index;
|
||||
if !self.at_root() {
|
||||
idx += 1;
|
||||
actual_index -= 1; // Adjust for the ".." item if not at root
|
||||
}
|
||||
if idx == selected {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user