summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Thiel <sthiel@thoughtworks.com>2019-06-03 16:06:02 +0530
committerSebastian Thiel <sthiel@thoughtworks.com>2019-06-03 16:06:02 +0530
commit84b6f8ce829e7a57604b4e983c91bc52a7299ac4 (patch)
tree144987beb596c72643bf136d84cde9ab2a355266
parent74e511631a7f05143e487584a4325fe65c774ba5 (diff)
add 'u' key to go up one level
-rw-r--r--src/interactive/app.rs15
-rw-r--r--tests/interactive.rs33
2 files changed, 42 insertions, 6 deletions
diff --git a/src/interactive/app.rs b/src/interactive/app.rs
index 5e350a5..139283c 100644
--- a/src/interactive/app.rs
+++ b/src/interactive/app.rs
@@ -2,6 +2,7 @@ use super::widgets::{DisplayState, MainWindow};
use crate::{interactive::Traversal, sorted_entries, ByteFormat, WalkOptions, WalkResult};
use failure::Error;
use itertools::Itertools;
+use petgraph::Direction;
use std::{io, path::PathBuf};
use termion::input::{Keys, TermReadEventsAndRaw};
use tui::widgets::Widget;
@@ -68,6 +69,20 @@ impl TerminalApp {
self.draw(terminal)?;
for key in keys.filter_map(Result::ok) {
match key {
+ Char('u') => {
+ if let Some(parent_idx) = self
+ .traversal
+ .tree
+ .neighbors_directed(self.state.root, Direction::Incoming)
+ .next()
+ {
+ self.state.root = parent_idx;
+ self.state.selected =
+ sorted_entries(&self.traversal.tree, parent_idx, self.state.sorting)
+ .get(0)
+ .map(|(idx, _)| *idx);
+ }
+ }
Char('o') => self.enter_node(),
Char('k') => self.change_vertical_index(CursorDirection::Up),
Char('j') => self.change_vertical_index(CursorDirection::Down),
diff --git a/tests/interactive.rs b/tests/interactive.rs
index 8d5eb18..e21a092 100644
--- a/tests/interactive.rs
+++ b/tests/interactive.rs
@@ -115,6 +115,11 @@ mod app {
node_by_index(&app, *app.state.selected.as_ref().unwrap()),
"it selects the first node in the list",
);
+
+ assert_eq!(
+ app.traversal.root_index, app.state.root,
+ "the root is the 'virtual' root",
+ );
}
// SORTING
@@ -179,20 +184,36 @@ mod app {
"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())?;
+ // when hitting the u key while inside a sub-directory
+ app.process_events(&mut terminal, b"u".keys())?;
{
assert_eq!(
- new_root_idx, app.state.root,
- "it does not enter it, keeping the previous root"
+ app.traversal.root_index,
+ app.state.root,
+ "it sets the root to be the (roots) parent directory, being the virtual root"
);
assert_eq!(
- node_by_index(&app, index_by_name(&app, ".hidden.666")),
+ node_by_name(&app, fixture_str(short_root)),
node_by_index(&app, *app.state.selected.as_ref().unwrap()),
- "it does not change the selection"
+ "changes the selection to the first item in the list of entries"
);
}
}
+ // when hitting the u key while inside of the root directory
+ // We are moving the cursor down just to have a non-default selection
+ app.process_events(&mut terminal, b"ju".keys())?;
+ {
+ assert_eq!(
+ app.traversal.root_index,
+ app.state.root,
+ "it keeps the root - it can't go further up"
+ );
+ assert_eq!(
+ node_by_name(&app, fixture_str(long_root)),
+ node_by_index(&app, *app.state.selected.as_ref().unwrap()),
+ "keeps the previous selection"
+ );
+ }
}
Ok(())