diff --git a/filetree/src/filetree.rs b/filetree/src/filetree.rs index b7d11a34..5a92a67f 100644 --- a/filetree/src/filetree.rs +++ b/filetree/src/filetree.rs @@ -46,6 +46,11 @@ impl FileTree { Ok(new_self) } + /// + pub const fn is_empty(&self) -> bool { + self.items.file_count() == 0 + } + /// pub fn collapse_but_root(&mut self) { self.items.collapse(0, true); @@ -67,46 +72,23 @@ impl FileTree { ) } - fn visual_index_to_absolute( - &self, - visual_index: usize, - ) -> Option { - self.items - .iterate(0, self.items.len()) - .enumerate() - .find_map(|(i, (abs, _))| { - if i == visual_index { - Some(abs) - } else { - None - } - }) - } - /// pub const fn visual_selection(&self) -> Option<&VisualSelection> { self.visual_selection.as_ref() } - fn calc_visual_selection(&self) -> Option { - self.selection.map(|selection_absolute| { - let mut count = 0; - let mut visual_index = 0; - for (index, _item) in - self.items.iterate(0, self.items.len()) - { - if selection_absolute == index { - visual_index = count; - } + /// + pub fn collapse_recursive(&mut self) { + if let Some(selection) = self.selection { + self.items.collapse(selection, true); + } + } - count += 1; - } - - VisualSelection { - index: visual_index, - count, - } - }) + /// + pub fn expand_recursive(&mut self) { + if let Some(selection) = self.selection { + self.items.expand(selection, true); + } } /// @@ -141,16 +123,41 @@ impl FileTree { }) } - pub fn collapse_recursive(&mut self) { - if let Some(selection) = self.selection { - self.items.collapse(selection, true); - } + fn visual_index_to_absolute( + &self, + visual_index: usize, + ) -> Option { + self.items + .iterate(0, self.items.len()) + .enumerate() + .find_map(|(i, (abs, _))| { + if i == visual_index { + Some(abs) + } else { + None + } + }) } - pub fn expand_recursive(&mut self) { - if let Some(selection) = self.selection { - self.items.expand(selection, true); - } + fn calc_visual_selection(&self) -> Option { + self.selection.map(|selection_absolute| { + let mut count = 0; + let mut visual_index = 0; + for (index, _item) in + self.items.iterate(0, self.items.len()) + { + if selection_absolute == index { + visual_index = count; + } + + count += 1; + } + + VisualSelection { + index: visual_index, + count, + } + }) } const fn selection_start(current_index: usize) -> Option { diff --git a/src/components/revision_files.rs b/src/components/revision_files.rs index 182864a0..32608d50 100644 --- a/src/components/revision_files.rs +++ b/src/components/revision_files.rs @@ -7,7 +7,7 @@ use super::{ use crate::{ keys::SharedKeyConfig, queue::Queue, - strings, + strings::{self, order}, ui::{self, style::SharedTheme}, }; use anyhow::Result; @@ -109,10 +109,6 @@ impl RevisionFilesComponent { let path = format!("{}{}{}", indent_str, path_arrow, path); Span::styled(path, theme.file_tree_item(is_path, selected)) } - - fn move_selection(&mut self, dir: MoveSelection) -> bool { - self.tree.move_selection(dir) - } } impl DrawableComponent for RevisionFilesComponent { @@ -154,13 +150,6 @@ impl DrawableComponent for RevisionFilesComponent { f, area, &self.title, - // &format!( - // "{}/{} (height: {}) (top: {})", - // selection.index, - // selection.count, - // tree_height, - // self.scroll_top.get() - // ), items, true, &self.theme, @@ -188,6 +177,8 @@ impl Component for RevisionFilesComponent { ) .order(1), ); + + tree_nav_cmds(&self.tree, &self.key_config, out); } visibility_blocking(self) @@ -202,33 +193,8 @@ impl Component for RevisionFilesComponent { let consumed = if key == self.key_config.exit_popup { self.hide(); true - } else if key == self.key_config.move_down { - self.move_selection(MoveSelection::Down) - } else if key == self.key_config.move_up { - self.move_selection(MoveSelection::Up) - } else if key == self.key_config.move_right { - self.move_selection(MoveSelection::Right) - } else if key == self.key_config.move_left { - self.move_selection(MoveSelection::Left) - } else if key == self.key_config.home - || key == self.key_config.shift_up - { - self.move_selection(MoveSelection::Top) - } else if key == self.key_config.end - || key == self.key_config.shift_down - { - self.move_selection(MoveSelection::End) - } else if key - == self.key_config.tree_collapse_recursive - { - self.tree.collapse_recursive(); - true - } else if key == self.key_config.tree_expand_recursive - { - self.tree.expand_recursive(); - true } else { - false + tree_nav(&mut self.tree, &self.key_config, key) }; return Ok(consumed.into()); @@ -252,3 +218,48 @@ impl Component for RevisionFilesComponent { Ok(()) } } + +//TODO: reuse for other tree usages +fn tree_nav_cmds( + tree: &FileTree, + key_config: &SharedKeyConfig, + out: &mut Vec, +) { + out.push( + CommandInfo::new( + strings::commands::navigate_tree(key_config), + !tree.is_empty(), + true, + ) + .order(order::NAV), + ); +} + +//TODO: reuse for other tree usages +fn tree_nav( + tree: &mut FileTree, + key_config: &SharedKeyConfig, + key: crossterm::event::KeyEvent, +) -> bool { + if key == key_config.move_down { + tree.move_selection(MoveSelection::Down) + } else if key == key_config.move_up { + tree.move_selection(MoveSelection::Up) + } else if key == key_config.move_right { + tree.move_selection(MoveSelection::Right) + } else if key == key_config.move_left { + tree.move_selection(MoveSelection::Left) + } else if key == key_config.home || key == key_config.shift_up { + tree.move_selection(MoveSelection::Top) + } else if key == key_config.end || key == key_config.shift_down { + tree.move_selection(MoveSelection::End) + } else if key == key_config.tree_collapse_recursive { + tree.collapse_recursive(); + true + } else if key == key_config.tree_expand_recursive { + tree.expand_recursive(); + true + } else { + false + } +} diff --git a/src/strings.rs b/src/strings.rs index 6ddf3e5e..7359bb63 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -351,7 +351,7 @@ pub mod commands { key_config.get_hint(key_config.move_right), key_config.get_hint(key_config.move_left) ), - "navigate tree view", + "navigate tree view, collapse, expand", CMD_GROUP_GENERAL, ) }