From 84b6f8ce829e7a57604b4e983c91bc52a7299ac4 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 3 Jun 2019 16:06:02 +0530 Subject: add 'u' key to go up one level --- src/interactive/app.rs | 15 +++++++++++++++ tests/interactive.rs | 33 +++++++++++++++++++++++++++------ 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(()) -- cgit v1.2.3