From 25ceae2779e3e20b4ff4ac3d6149410e5f851775 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 3 Jun 2019 15:02:18 +0530 Subject: add 'o' navigation add 'o' --- src/interactive/app.rs | 11 +++++++++++ tests/interactive.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/interactive/app.rs b/src/interactive/app.rs index 4fcf8aa..5e350a5 100644 --- a/src/interactive/app.rs +++ b/src/interactive/app.rs @@ -68,6 +68,7 @@ impl TerminalApp { self.draw(terminal)?; for key in keys.filter_map(Result::ok) { match key { + Char('o') => self.enter_node(), Char('k') => self.change_vertical_index(CursorDirection::Up), Char('j') => self.change_vertical_index(CursorDirection::Down), Char('s') => self.state.sorting.toggle_size(), @@ -81,6 +82,16 @@ impl TerminalApp { }) } + fn enter_node(&mut self) -> () { + if let Some(idx) = self.state.selected { + let entries = sorted_entries(&self.traversal.tree, idx, self.state.sorting); + if let Some((next_selection, _)) = entries.get(0) { + self.state.root = idx; + self.state.selected = Some(*next_selection); + } + } + } + fn change_vertical_index(&mut self, direction: CursorDirection) -> () { let entries = sorted_entries(&self.traversal.tree, self.state.root, self.state.sorting); let next_selected_pos = match self.state.selected { diff --git a/tests/interactive.rs b/tests/interactive.rs index 50d80c8..8d5eb18 100644 --- a/tests/interactive.rs +++ b/tests/interactive.rs @@ -54,14 +54,29 @@ mod app { node_by_index(app, index_by_name(&app, name)) } - fn index_by_name(app: &TerminalApp, name: impl AsRef) -> TreeIndex { + fn index_by_name_and_size( + app: &TerminalApp, + name: impl AsRef, + size: Option, + ) -> TreeIndex { let name = name.as_ref(); let t: Vec<_> = app .traversal .tree .node_indices() .map(|idx| (idx, node_by_index(app, idx))) - .filter_map(|(idx, e)| if e.name == name { Some(idx) } else { None }) + .filter_map(|(idx, e)| { + if e.name == name + && match size { + Some(s) => s == e.size, + None => true, + } + { + Some(idx) + } else { + None + } + }) .collect(); match t.len() { 1 => t[0], @@ -69,11 +84,14 @@ mod app { n => panic!("Node named '{}' found {} times", name.to_string_lossy(), n), } } + fn index_by_name(app: &TerminalApp, name: impl AsRef) -> TreeIndex { + index_by_name_and_size(app, name, None) + } #[test] fn simple_user_journey() -> Result<(), Error> { let long_root = "sample-02/dir"; - let short_root = "sample-02"; + let short_root = "sample-01"; let (mut terminal, mut app) = initialized_app_and_terminal(&[short_root, long_root])?; // POST-INIT @@ -147,6 +165,34 @@ mod app { node_by_index(&app, *app.state.selected.as_ref().unwrap()), "it stays at the current cursor position as there is nowhere to go" ); + // when hitting the o key with a directory selected + app.process_events(&mut terminal, b"o".keys())?; + { + let new_root_idx = index_by_name(&app, fixture_str(short_root)); + assert_eq!( + new_root_idx, app.state.root, + "it enters the entry if it is a directory, changing the root" + ); + assert_eq!( + index_by_name(&app, "dir"), + *app.state.selected.as_ref().unwrap(), + "it selects the first entry in the directory" + ); + + // when trying to enter a file (a node with no children) + app.process_events(&mut terminal, b"jo".keys())?; + { + assert_eq!( + new_root_idx, app.state.root, + "it does not enter it, keeping the previous root" + ); + assert_eq!( + node_by_index(&app, index_by_name(&app, ".hidden.666")), + node_by_index(&app, *app.state.selected.as_ref().unwrap()), + "it does not change the selection" + ); + } + } } Ok(()) -- cgit v1.2.3