diff options
author | Canop <cano.petrole@gmail.com> | 2021-06-13 20:15:08 +0200 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2021-06-13 20:15:08 +0200 |
commit | 569b7ce9405d3118980e7f61fd9b3d81f79f5fda (patch) | |
tree | a77211062b6b86bc640d8dc5ad44b99f0c20201e /src | |
parent | 7611a5166ce63b735e9d2dad5bee8fdd570336ba (diff) |
:root_up and :root_down internals
They make it easier to adjust the tree root to the intended scope
without changing the selection.
They're mapped by default to ctrl-up and ctrl-down
Fix #399
Diffstat (limited to 'src')
-rw-r--r-- | src/browser/browser_state.rs | 57 | ||||
-rw-r--r-- | src/verb/builtin.rs | 10 | ||||
-rw-r--r-- | src/verb/internal.rs | 2 |
3 files changed, 59 insertions, 10 deletions
diff --git a/src/browser/browser_state.rs b/src/browser/browser_state.rs index a136514..08645cd 100644 --- a/src/browser/browser_state.rs +++ b/src/browser/browser_state.rs @@ -58,6 +58,30 @@ impl BrowserState { })) } + /// build a cmdResult asking for the addition of a new state + /// being a browser state similar to the current one but with + /// different options or a different root, or both + fn modified( + &self, + screen: Screen, + root: PathBuf, + options: TreeOptions, + in_new_panel: bool, + con: &AppContext, + ) -> CmdResult { + let tree = self.displayed_tree(); + let mut new_state = BrowserState::new(root, options, screen, con, &Dam::unlimited()); + if let Ok(Some(bs)) = &mut new_state { + if tree.selection != 0 { + bs.displayed_tree_mut().try_select_path(&tree.selected_line().path); + } + } + CmdResult::from_optional_state( + new_state, + in_new_panel, + ) + } + pub fn root(&self) -> &Path { self.tree.root() } @@ -205,16 +229,7 @@ impl PanelState for BrowserState { let tree = self.displayed_tree(); let mut options = tree.options.clone(); change_options(&mut options); - let mut new_state = BrowserState::new(tree.root().clone(), options, screen, con, &Dam::unlimited()); - if let Ok(Some(bs)) = &mut new_state { - if tree.selection != 0 { - bs.displayed_tree_mut().try_select_path(&tree.selected_line().path); - } - } - CmdResult::from_optional_state( - new_state, - in_new_panel, - ) + self.modified(screen, tree.root().clone(), options, in_new_panel, con) } fn clear_pending(&mut self) { @@ -409,6 +424,28 @@ impl PanelState for BrowserState { Internal::print_tree => { print::print_tree(&self.displayed_tree(), cc.app.screen, &cc.app.panel_skin, con)? } + Internal::root_up => { + let tree = self.displayed_tree(); + let root = tree.root(); + if let Some(new_root) = root.parent() { + self.modified(screen, new_root.to_path_buf(), tree.options.clone(), bang, con) + } else { + CmdResult::error(format!("{:?} has no parent", root)) + } + } + Internal::root_down => { + let tree = self.displayed_tree(); + if tree.selection > 0 { + let root_len = tree.root().components().count(); + let new_root = tree.selected_line().path + .components() + .take(root_len + 1) + .collect(); + self.modified(screen, new_root, tree.options.clone(), bang, con) + } else { + CmdResult::error("No selected line") + } + } Internal::select_first => { self.displayed_tree_mut().try_select_first(); CmdResult::Keep diff --git a/src/verb/builtin.rs b/src/verb/builtin.rs index d6edfc2..f149461 100644 --- a/src/verb/builtin.rs +++ b/src/verb/builtin.rs @@ -200,6 +200,16 @@ pub fn builtin_verbs() -> Vec<Verb> { .with_control_key('q') .with_shortcut("q"), internal(refresh).with_key(F5), + internal(root_up) + .with_key(KeyEvent { + code: KeyCode::Up, + modifiers: KeyModifiers::CONTROL, + }), + internal(root_down) + .with_key(KeyEvent { + code: KeyCode::Down, + modifiers: KeyModifiers::CONTROL, + }), internal(select_first).with_key(HOME), internal(select_last).with_key(END), internal(clear_stage).with_shortcut("cls"), diff --git a/src/verb/internal.rs b/src/verb/internal.rs index e08e046..da62ebc 100644 --- a/src/verb/internal.rs +++ b/src/verb/internal.rs @@ -104,6 +104,8 @@ Internals! { start_end_panel: "either open or close an additional panel" true, quit: "quit Broot" false, refresh: "refresh tree and clear size cache" false, + root_up: "move tree root up" true, + root_down: "move tree root down" true, //restore_pattern: "restore a pattern which was just removed" false, select_first: "select the first item" false, select_last: "select the last item" false, |