summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Thiel <sthiel@thoughtworks.com>2019-06-03 15:02:18 +0530
committerSebastian Thiel <sthiel@thoughtworks.com>2019-06-03 15:20:14 +0530
commit25ceae2779e3e20b4ff4ac3d6149410e5f851775 (patch)
treea2935200a9bfd0eb03e7922545bfb821c6ab0611
parent748dfc353a7d8c7bbb6bbfb097bacec18b80e32a (diff)
add 'o' navigation
add 'o'
-rw-r--r--src/interactive/app.rs11
-rw-r--r--tests/interactive.rs52
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<OsStr>) -> TreeIndex {
+ fn index_by_name_and_size(
+ app: &TerminalApp,
+ name: impl AsRef<OsStr>,
+ size: Option<u64>,
+ ) -> 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<OsStr>) -> 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(())