Skip to content

Commit

Permalink
add 'u' key to go up one level
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 3, 2019
1 parent 74e5116 commit 84b6f8c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/interactive/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
33 changes: 27 additions & 6 deletions tests/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(())
Expand Down

0 comments on commit 84b6f8c

Please sign in to comment.