diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-11-07 09:42:15 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-11-07 09:42:15 +0100 |
commit | e1628f5e2e6b540c8680a76931d34ea0aff189ff (patch) | |
tree | e06c34631a56e463fbf0358af81d16d5daf220b5 | |
parent | 7047cce927791ca386a87edf289d2fd1a5cbd91b (diff) |
use Go trait instead of selections
-rw-r--r-- | src/event_exec.rs | 2 | ||||
-rw-r--r-- | src/tab.rs | 17 | ||||
-rw-r--r-- | src/tree.rs | 10 |
3 files changed, 15 insertions, 14 deletions
diff --git a/src/event_exec.rs b/src/event_exec.rs index 1dc4cc5..dd467dd 100644 --- a/src/event_exec.rs +++ b/src/event_exec.rs @@ -622,7 +622,7 @@ impl EventAction { } Mode::Preview => tab.page_up(), Mode::Tree => { - tab.tree_page_up()?; + tab.tree_page_up(); status.update_second_pane_for_preview()?; } _ => (), @@ -342,7 +342,7 @@ impl Tab { self.set_pathcontent(parent.to_owned().as_ref())?; self.make_tree(Some(self.path_content.sort_kind.clone())) } else { - self.tree.select_parent(); + self.tree.go(To::Parent); Ok(()) } } @@ -350,7 +350,7 @@ impl Tab { /// Move down 10 times in the tree pub fn tree_page_down(&mut self) -> Result<()> { for _ in 1..10 { - self.tree.select_next()?; + self.tree.go(To::Next); if self.tree.is_on_last() { break; } @@ -359,14 +359,13 @@ impl Tab { } /// Move up 10 times in the tree - pub fn tree_page_up(&mut self) -> Result<()> { + pub fn tree_page_up(&mut self) { for _ in 1..10 { - self.tree.select_prev(); + self.tree.go(To::Prev); if self.tree.is_on_root() { break; } } - Ok(()) } /// Select the next sibling. @@ -377,13 +376,13 @@ impl Tab { /// Select the previous siblging pub fn tree_select_prev(&mut self) -> Result<()> { - self.tree.select_prev(); + self.tree.go(To::Prev); Ok(()) } /// Go to the last leaf. pub fn tree_go_to_bottom_leaf(&mut self) -> Result<()> { - self.tree.select_last(); + self.tree.go(To::Last); Ok(()) } @@ -525,7 +524,7 @@ impl Tab { /// Fold every child node in the tree. /// Recursively explore the tree and fold every node. Reset the display. pub fn tree_go_to_root(&mut self) -> Result<()> { - self.tree.select_root(); + self.tree.go(To::Root); Ok(()) } @@ -652,7 +651,7 @@ impl Tab { let (top, _) = calculate_tree_window(selected_index, term_height - 2); let index = screen_index + top; let (_, _, colored_path) = content.get(index).context("no selected file")?; - self.tree.select_path(&colored_path.path); + self.tree.go(To::Path(&colored_path.path)); Ok(()) } diff --git a/src/tree.rs b/src/tree.rs index 68229e7..aee8312 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -86,6 +86,8 @@ impl Node { self.children = children } } + +/// Describe a movement in a navigable structure pub trait Go { fn go(&mut self, direction: To); } @@ -345,7 +347,7 @@ impl Tree { } } - pub fn select_root(&mut self) { + fn select_root(&mut self) { let Some(selected_node) = self.nodes.get_mut(&self.selected) else { unreachable!("selected path should be in node") }; @@ -358,7 +360,7 @@ impl Tree { self.reset_required_height() } - pub fn select_last(&mut self) { + fn select_last(&mut self) { let Some(selected_node) = self.nodes.get_mut(&self.selected) else { unreachable!("selected path should be in node") }; @@ -371,7 +373,7 @@ impl Tree { self.set_required_height_to_max() } - pub fn select_parent(&mut self) { + fn select_parent(&mut self) { if let Some(parent_path) = self.selected.parent() { let Some(parent_node) = self.nodes.get_mut(parent_path) else { return; @@ -386,7 +388,7 @@ impl Tree { } } - pub fn select_path(&mut self, clicked_path: &Path) { + fn select_path(&mut self, clicked_path: &Path) { let Some(new_node) = self.nodes.get_mut(clicked_path) else { return; }; |