From c478f3ae30a9d317fca2c540bc35626106e1f581 Mon Sep 17 00:00:00 2001 From: Jiayi Zhao Date: Tue, 19 Feb 2019 11:26:12 -0500 Subject: add force_quit command for when there are background operations running --- src/commands/mod.rs | 2 ++ src/commands/quit.rs | 43 ++++++++++++++++++++++++++++++++++++++++++ src/commands/tab_operations.rs | 4 ++-- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index e7b4df3..c055d64 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -26,6 +26,7 @@ pub use self::file_operations::{CopyFiles, CutFiles, FileOperationThread, PasteF pub use self::new_directory::NewDirectory; pub use self::open_file::{OpenFile, OpenFileWith}; pub use self::parent_directory::ParentDirectory; +pub use self::quit::ForceQuit; pub use self::quit::Quit; pub use self::reload_dir::ReloadDirList; pub use self::rename_file::{RenameFile, RenameFileMethod}; @@ -111,6 +112,7 @@ pub fn from_args(command: &str, args: Option<&Vec>) -> Option Some(Box::new(self::CursorMovePageDown::new())), "cut_files" => Some(Box::new(self::CutFiles::new())), "delete_files" => Some(Box::new(self::DeleteFiles::new())), + "force_quit" => Some(Box::new(self::ForceQuit::new())), "mkdir" => Some(Box::new(self::NewDirectory::new())), "new_tab" => Some(Box::new(self::NewTab::new())), "open_file" => Some(Box::new(self::OpenFile::new())), diff --git a/src/commands/quit.rs b/src/commands/quit.rs index 3173ca1..249db4b 100644 --- a/src/commands/quit.rs +++ b/src/commands/quit.rs @@ -1,5 +1,6 @@ use commands::{JoshutoCommand, JoshutoRunnable}; use context::JoshutoContext; +use ui; #[derive(Clone, Debug)] pub struct Quit; @@ -11,6 +12,18 @@ impl Quit { pub const fn command() -> &'static str { "quit" } + + pub fn quit(context: &mut JoshutoContext) { + if (!context.threads.is_empty()) { + ui::wprint_err( + &context.views.bot_win, + "Error: operations running in background, use force_quit to quit", + ); + ncurses::doupdate(); + return; + } + context.exit = true; + } } impl JoshutoCommand for Quit {} @@ -23,6 +36,36 @@ impl std::fmt::Display for Quit { impl JoshutoRunnable for Quit { fn execute(&self, context: &mut JoshutoContext) { + Self::quit(context); + } +} + +#[derive(Clone, Debug)] +pub struct ForceQuit; + +impl ForceQuit { + pub fn new() -> Self { + ForceQuit + } + pub const fn command() -> &'static str { + "force_quit" + } + + pub fn force_quit(context: &mut JoshutoContext) { context.exit = true; } } + +impl JoshutoCommand for ForceQuit {} + +impl std::fmt::Display for ForceQuit { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str(Self::command()) + } +} + +impl JoshutoRunnable for ForceQuit { + fn execute(&self, context: &mut JoshutoContext) { + Self::force_quit(context); + } +} diff --git a/src/commands/tab_operations.rs b/src/commands/tab_operations.rs index 1e6676e..05948a7 100644 --- a/src/commands/tab_operations.rs +++ b/src/commands/tab_operations.rs @@ -1,7 +1,7 @@ use std::env; use std::path; -use commands::{JoshutoCommand, JoshutoRunnable, TabSwitch}; +use commands::{JoshutoCommand, JoshutoRunnable, Quit, TabSwitch}; use context::JoshutoContext; use tab::JoshutoTab; use ui; @@ -67,7 +67,7 @@ impl CloseTab { pub fn close_tab(context: &mut JoshutoContext) { if context.tabs.len() <= 1 { - context.exit = true; + Quit::quit(context); return; } -- cgit v1.2.3