diff options
author | Sebastian Thiel <sebastian.thiel@icloud.com> | 2020-03-29 13:19:48 +0800 |
---|---|---|
committer | Sebastian Thiel <sebastian.thiel@icloud.com> | 2020-03-29 13:19:54 +0800 |
commit | 00e70066ea495af9464b9d12cfd8ef15a40c6584 (patch) | |
tree | 5539864fb838d0a03b61543d1a07bc60cc1f0c3b | |
parent | ede622480acb4066ea864bae200ea89de46dbcdd (diff) |
Also exit quickly when ctrl+c is pressed
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/interactive/app/eventloop.rs | 27 | ||||
-rw-r--r-- | src/interactive/app/handlers.rs | 2 | ||||
-rw-r--r-- | src/interactive/app_test/utils.rs | 7 | ||||
-rw-r--r-- | src/main.rs | 9 |
6 files changed, 33 insertions, 16 deletions
@@ -182,7 +182,7 @@ checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" [[package]] name = "dua-cli" -version = "2.3.9" +version = "3.0.0" dependencies = [ "atty", "byte-unit", @@ -1,6 +1,6 @@ [package] name = "dua-cli" -version = "2.3.9" +version = "3.0.0" authors = ["Sebastian Thiel <byronimo@gmail.com>"] edition = "2018" include = ["src/**/*", "Cargo.*", "*.md", "LICENSE"] diff --git a/src/interactive/app/eventloop.rs b/src/interactive/app/eventloop.rs index 88f2c11..c81960c 100644 --- a/src/interactive/app/eventloop.rs +++ b/src/interactive/app/eventloop.rs @@ -37,6 +37,8 @@ pub struct AppState { pub bookmarks: BTreeMap<TreeIndex, TreeIndex>, } +impl AppState {} + /// State and methods representing the interactive disk usage analyser for the terminal pub struct TerminalApp { pub traversal: Traversal, @@ -80,24 +82,25 @@ impl TerminalApp { { use termion::event::Key::*; use FocussedPane::*; + fn exit_now<B: Backend>(terminal: Terminal<B>) -> ! { + drop(terminal); + io::stdout().flush().ok(); + // Exit 'quickly' to avoid having to wait for all memory to be freed by us. + // Let the OS do it - we have nothing to lose, literally. + std::process::exit(0); + } self.draw(&mut terminal)?; for key in keys.filter_map(Result::ok) { - self.update_message(); + self.reset_message(); match key { Char('?') => self.toggle_help_pane(), Char('\t') => { self.cycle_focus(); } - Ctrl('c') => break, + Ctrl('c') => exit_now(terminal), Char('q') | Esc => match self.state.focussed { - Main => { - drop(terminal); - io::stdout().flush().ok(); - // Exit 'quickly' to avoid having to wait for all memory to be freed by us. - // Let the OS do it - we have nothing to lose, literally. - std::process::exit(0); - } + Main => exit_now(terminal), Mark => self.state.focussed = Main, Help => { self.state.focussed = Main; @@ -138,6 +141,7 @@ impl TerminalApp { terminal: &mut Terminal<B>, options: WalkOptions, input: Vec<PathBuf>, + mode: Interaction, ) -> Result<TerminalApp, Error> where B: Backend, @@ -183,3 +187,8 @@ impl TerminalApp { }) } } + +pub enum Interaction { + Limited, + None, +} diff --git a/src/interactive/app/handlers.rs b/src/interactive/app/handlers.rs index 40eb5c2..103db4f 100644 --- a/src/interactive/app/handlers.rs +++ b/src/interactive/app/handlers.rs @@ -170,7 +170,7 @@ impl TerminalApp { } } - pub fn update_message(&mut self) { + pub fn reset_message(&mut self) { self.state.message = None; } diff --git a/src/interactive/app_test/utils.rs b/src/interactive/app_test/utils.rs index 1aa1a8c..4aabf18 100644 --- a/src/interactive/app_test/utils.rs +++ b/src/interactive/app_test/utils.rs @@ -1,5 +1,7 @@ -use crate::interactive::app_test::FIXTURE_PATH; -use crate::interactive::TerminalApp; +use crate::{ + interactive::app_test::FIXTURE_PATH, + interactive::{Interaction, TerminalApp}, +}; use dua::{ traverse::{EntryData, Tree, TreeIndex}, ByteFormat, Color, TraversalSorting, WalkOptions, @@ -175,6 +177,7 @@ pub fn initialized_app_and_terminal_with_closure<P: AsRef<Path>>( sorting: TraversalSorting::AlphabeticalByFileName, }, input, + Interaction::None, )?; Ok((terminal, app)) } diff --git a/src/main.rs b/src/main.rs index 91cb79b..f5c6197 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ extern crate failure; extern crate failure_tools; extern crate structopt; -use crate::interactive::TerminalApp; +use crate::interactive::{Interaction, TerminalApp}; use dua::{ByteFormat, Color, TraversalSorting}; use failure::{Error, ResultExt}; use failure_tools::ok_or_exit; @@ -43,7 +43,12 @@ fn run() -> Result<(), Error> { let backend = TermionBackend::new(stdout); Terminal::new(backend)? }; - let mut app = TerminalApp::initialize(&mut terminal, walk_options, paths_from(input)?)?; + let mut app = TerminalApp::initialize( + &mut terminal, + walk_options, + paths_from(input)?, + Interaction::Limited, + )?; let res = app.process_events(terminal, io::stdin().keys())?; io::stdout().flush().ok(); res |