diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-08-01 12:00:36 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-08-01 12:00:36 -0400 |
commit | ee0dec521b73a4328660969c3be8454dc0bf73e8 (patch) | |
tree | 7c13b7f82a0faf6c9116ddfdc8458b43289d63a0 | |
parent | 7cb3e8be67621191a43d530a5a0a4e49baec26e0 (diff) |
add delete_files with foreground option
-rw-r--r-- | src/commands/delete_files.rs | 47 | ||||
-rw-r--r-- | src/io/file_operation.rs | 1 | ||||
-rw-r--r-- | src/key_command/command.rs | 5 | ||||
-rw-r--r-- | src/key_command/impl_appcommand.rs | 5 | ||||
-rw-r--r-- | src/key_command/impl_appexecute.rs | 14 | ||||
-rw-r--r-- | src/key_command/impl_comment.rs | 11 | ||||
-rw-r--r-- | src/key_command/impl_display.rs | 3 | ||||
-rw-r--r-- | src/key_command/impl_from_str.rs | 12 |
8 files changed, 79 insertions, 19 deletions
diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs index c56118d..272fd1a 100644 --- a/src/commands/delete_files.rs +++ b/src/commands/delete_files.rs @@ -1,14 +1,20 @@ use std::path; +use std::sync::mpsc; use termion::event::Key; use crate::context::AppContext; +use crate::error::JoshutoResult; use crate::history::DirectoryHistory; use crate::io::{FileOperation, FileOperationOptions, IoWorkerThread}; use crate::ui::widgets::TuiPrompt; use crate::ui::AppBackend; -fn delete_files(context: &mut AppContext, backend: &mut AppBackend) -> std::io::Result<()> { +fn delete_files( + context: &mut AppContext, + backend: &mut AppBackend, + background: bool, +) -> std::io::Result<()> { let paths = context .tab_context_ref() .curr_tab_ref() @@ -47,12 +53,18 @@ fn delete_files(context: &mut AppContext, backend: &mut AppBackend) -> std::io:: let options = FileOperationOptions { overwrite: false, skip_exist: false, + symlink: false, permanently: !context.config_ref().use_trash, }; let dest = path::PathBuf::new(); let worker_thread = IoWorkerThread::new(file_op, paths, dest, options); - context.worker_context_mut().push_worker(worker_thread); + if background { + context.worker_context_mut().push_worker(worker_thread); + } else { + let (wtx, _) = mpsc::channel(); + worker_thread.start(wtx)?; + } } Ok(()) } @@ -60,11 +72,11 @@ fn delete_files(context: &mut AppContext, backend: &mut AppBackend) -> std::io:: } } -pub fn delete_selected_files( +fn _delete_selected_files( context: &mut AppContext, backend: &mut AppBackend, ) -> std::io::Result<()> { - let _ = delete_files(context, backend)?; + let _ = delete_files(context, backend, false)?; let options = context.config_ref().display_options_ref().clone(); let curr_path = context.tab_context_ref().curr_tab_ref().cwd().to_path_buf(); @@ -73,3 +85,30 @@ pub fn delete_selected_files( } Ok(()) } + +pub fn delete_selected_files(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult { + _delete_selected_files(context, backend)?; + Ok(()) +} + +fn _delete_selected_files_background( + context: &mut AppContext, + backend: &mut AppBackend, +) -> std::io::Result<()> { + let _ = delete_files(context, backend, true)?; + + let options = context.config_ref().display_options_ref().clone(); + let curr_path = context.tab_context_ref().curr_tab_ref().cwd().to_path_buf(); + for tab in context.tab_context_mut().iter_mut() { + tab.history_mut().reload(&curr_path, &options)?; + } + Ok(()) +} + +pub fn delete_selected_files_background( + context: &mut AppContext, + backend: &mut AppBackend, +) -> JoshutoResult { + _delete_selected_files(context, backend)?; + Ok(()) +} diff --git a/src/io/file_operation.rs b/src/io/file_operation.rs index a0000f7..14693ec 100644 --- a/src/io/file_operation.rs +++ b/src/io/file_operation.rs @@ -10,6 +10,7 @@ pub struct FileOperationOptions { // cut, copy pub overwrite: bool, pub skip_exist: bool, + pub symlink: bool, // delete pub permanently: bool, diff --git a/src/key_command/command.rs b/src/key_command/command.rs index 4d4e0ba..8d1110c 100644 --- a/src/key_command/command.rs +++ b/src/key_command/command.rs @@ -17,11 +17,13 @@ pub enum Command { CutFiles, CopyFiles, - PasteFiles(FileOperationOptions), CopyFileName, CopyFileNameWithoutExtension, CopyFilePath, CopyDirPath, + PasteFiles(FileOperationOptions), + + DeleteFiles { background: bool }, CursorMoveUp(usize), CursorMoveDown(usize), @@ -41,7 +43,6 @@ pub enum Command { // ChildCursorMoveUp(usize), // ChildCursorMoveDown(usize), - DeleteFiles, NewDirectory(path::PathBuf), OpenFile, OpenFileWith(Option<usize>), diff --git a/src/key_command/impl_appcommand.rs b/src/key_command/impl_appcommand.rs index c04ade7..7ea8f93 100644 --- a/src/key_command/impl_appcommand.rs +++ b/src/key_command/impl_appcommand.rs @@ -20,11 +20,13 @@ impl AppCommand for Command { Self::CutFiles => CMD_CUT_FILES, Self::CopyFiles => CMD_COPY_FILES, - Self::PasteFiles(_) => CMD_PASTE_FILES, Self::CopyFileName => CMD_COPY_FILENAME, Self::CopyFileNameWithoutExtension => CMD_COPY_FILENAME_WITHOUT_EXTENSION, Self::CopyFilePath => CMD_COPY_FILEPATH, Self::CopyDirPath => CMD_COPY_DIRECTORY_PATH, + Self::PasteFiles(_) => CMD_PASTE_FILES, + + Self::DeleteFiles { .. } => CMD_DELETE_FILES, Self::CursorMoveUp(_) => CMD_CURSOR_MOVE_UP, Self::CursorMoveDown(_) => CMD_CURSOR_MOVE_DOWN, @@ -42,7 +44,6 @@ impl AppCommand for Command { Self::PreviewCursorMoveUp(_) => CMD_PREVIEW_CURSOR_MOVE_UP, Self::PreviewCursorMoveDown(_) => CMD_PREVIEW_CURSOR_MOVE_DOWN, - Self::DeleteFiles => CMD_DELETE_FILES, Self::NewDirectory(_) => CMD_NEW_DIRECTORY, Self::OpenFile => CMD_OPEN_FILE, Self::OpenFileWith(_) => CMD_OPEN_FILE_WITH, diff --git a/src/key_command/impl_appexecute.rs b/src/key_command/impl_appexecute.rs index 4febf1e..37f0c35 100644 --- a/src/key_command/impl_appexecute.rs +++ b/src/key_command/impl_appexecute.rs @@ -30,7 +30,6 @@ impl AppExecute for Command { } Self::CutFiles => file_ops::cut(context), Self::CopyFiles => file_ops::copy(context), - Self::PasteFiles(options) => file_ops::paste(context, *options), Self::CopyFileName => file_ops::copy_filename(context), Self::CopyFileNameWithoutExtension => { file_ops::copy_filename_without_extension(context) @@ -38,6 +37,15 @@ impl AppExecute for Command { Self::CopyFilePath => file_ops::copy_filepath(context), Self::CopyDirPath => file_ops::copy_dirpath(context), + Self::PasteFiles(options) => file_ops::paste(context, *options), + + Self::DeleteFiles { background: false } => { + delete_files::delete_selected_files(context, backend) + } + Self::DeleteFiles { background: true } => { + delete_files::delete_selected_files_background(context, backend) + } + Self::CursorMoveUp(u) => cursor_move::up(context, *u), Self::CursorMoveDown(u) => cursor_move::down(context, *u), Self::CursorMoveHome => cursor_move::home(context), @@ -55,10 +63,6 @@ impl AppExecute for Command { Self::PreviewCursorMoveUp(u) => preview_cursor_move::preview_up(context, *u), Self::PreviewCursorMoveDown(u) => preview_cursor_move::preview_down(context, *u), - Self::DeleteFiles => { - delete_files::delete_selected_files(context, backend)?; - Ok(()) - } Self::NewDirectory(p) => new_directory::new_directory(context, p.as_path()), Self::OpenFile => open_file::open(context, backend), Self::OpenFileWith(None) => open_file::open_with_interactive(context, backend), diff --git a/src/key_command/impl_comment.rs b/src/key_command/impl_comment.rs index c441a6b..a351fed 100644 --- a/src/key_command/impl_comment.rs +++ b/src/key_command/impl_comment.rs @@ -27,6 +27,11 @@ impl CommandComment for Command { Self::CutFiles => "Cut selected files", Self::CopyFiles => "Copy selected files", + Self::CopyFileName => "Copy filename", + Self::CopyFileNameWithoutExtension => "Copy filename without extension", + Self::CopyFilePath => "Copy path to file", + Self::CopyDirPath => "Copy directory name", + Self::PasteFiles(FileOperationOptions { overwrite, skip_exist, @@ -36,10 +41,7 @@ impl CommandComment for Command { (false, true) => "Paste, skip existing files", _ => "Paste", }, - Self::CopyFileName => "Copy filename", - Self::CopyFileNameWithoutExtension => "Copy filename without extension", - Self::CopyFilePath => "Copy path to file", - Self::CopyDirPath => "Copy directory name", + Self::DeleteFiles { .. } => "Delete selected files", Self::CursorMoveUp(_) => "Move cursor up", Self::CursorMoveDown(_) => "Move cursor down", @@ -58,7 +60,6 @@ impl CommandComment for Command { Self::PreviewCursorMoveUp(_) => "Cursor up in file preview", Self::PreviewCursorMoveDown(_) => "Cursor down in file preview", - Self::DeleteFiles => "Delete selected files", Self::NewDirectory(_) => "Make a new directory", Self::OpenFile => "Open a file", Self::OpenFileWith(_) => "Open using selected program", diff --git a/src/key_command/impl_display.rs b/src/key_command/impl_display.rs index 151b464..9c9a7b4 100644 --- a/src/key_command/impl_display.rs +++ b/src/key_command/impl_display.rs @@ -11,6 +11,9 @@ impl std::fmt::Display for Command { Self::NewDirectory(d) => write!(f, "{} {:?}", self.command(), d), Self::PasteFiles(options) => write!(f, "{} {}", self.command(), options), + Self::DeleteFiles { background: false } => { + write!(f, "{} --foreground=true", self.command(),) + } Self::RenameFile(name) => write!(f, "{} {:?}", self.command(), name), diff --git a/src/key_command/impl_from_str.rs b/src/key_command/impl_from_str.rs index 74c92e4..224b8d6 100644 --- a/src/key_command/impl_from_str.rs +++ b/src/key_command/impl_from_str.rs @@ -55,7 +55,6 @@ impl std::str::FromStr for Command { simple_command_conversion_case!(command, CMD_CURSOR_MOVE_PAGEEND, Self::CursorMovePageEnd); simple_command_conversion_case!(command, CMD_CUT_FILES, Self::CutFiles); - simple_command_conversion_case!(command, CMD_DELETE_FILES, Self::DeleteFiles); simple_command_conversion_case!(command, CMD_COPY_FILES, Self::CopyFiles); simple_command_conversion_case!(command, CMD_COPY_FILENAME, Self::CopyFileName); @@ -216,6 +215,17 @@ impl std::str::FromStr for Command { } } Ok(Self::PasteFiles(options)) + } else if command == CMD_DELETE_FILES { + match arg { + "--foreground=true" => return Ok(Self::DeleteFiles { background: true }), + "--foreground=false" => return Ok(Self::DeleteFiles { background: false }), + _ => { + return Err(JoshutoError::new( + JoshutoErrorKind::UnrecognizedArgument, + format!("{}: unknown option '{}'", command, arg), + )); + } + } } else if command == CMD_RENAME_FILE { match arg { "" => Err(JoshutoError::new( |