summaryrefslogtreecommitdiffstats
path: root/src/commands
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2022-07-18 12:06:49 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2022-07-18 12:06:49 -0400
commit743354da056c65c6ce0910394230270ef793d8cc (patch)
tree22795339118dfdf2e819a03fa3b8081d87226339 /src/commands
parenteb8b155329b2c2689cc5b854f0b87f4448485c66 (diff)
asynchronize delete operation
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/delete_files.rs70
-rw-r--r--src/commands/file_ops.rs10
2 files changed, 14 insertions, 66 deletions
diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs
index b702771..c56118d 100644
--- a/src/commands/delete_files.rs
+++ b/src/commands/delete_files.rs
@@ -1,63 +1,14 @@
-use std::fs;
use std::path;
use termion::event::Key;
use crate::context::AppContext;
use crate::history::DirectoryHistory;
+use crate::io::{FileOperation, FileOperationOptions, IoWorkerThread};
use crate::ui::widgets::TuiPrompt;
use crate::ui::AppBackend;
-use super::reload;
-
-fn trash_error_to_io_error(err: trash::Error) -> std::io::Error {
- match err {
- trash::Error::Unknown { description } => {
- std::io::Error::new(std::io::ErrorKind::Other, description)
- }
- trash::Error::TargetedRoot => {
- std::io::Error::new(std::io::ErrorKind::Other, "Targeted Root")
- }
- _ => std::io::Error::new(std::io::ErrorKind::Other, "Unknown Error"),
- }
-}
-
-pub fn remove_files<P>(paths: &[P]) -> std::io::Result<()>
-where
- P: AsRef<path::Path>,
-{
- for path in paths {
- if let Ok(metadata) = fs::symlink_metadata(path) {
- if metadata.is_dir() {
- fs::remove_dir_all(&path)?;
- } else {
- fs::remove_file(&path)?;
- }
- }
- }
- Ok(())
-}
-
-pub fn trash_files<P>(paths: &[P]) -> std::io::Result<()>
-where
- P: AsRef<path::Path>,
-{
- for path in paths {
- if let Err(e) = trash::delete(path) {
- return Err(trash_error_to_io_error(e));
- }
- }
- Ok(())
-}
-
fn delete_files(context: &mut AppContext, backend: &mut AppBackend) -> std::io::Result<()> {
- let delete_func = if context.config_ref().use_trash {
- trash_files
- } else {
- remove_files
- };
-
- let tab_index = context.tab_context_ref().index;
let paths = context
.tab_context_ref()
.curr_tab_ref()
@@ -92,17 +43,16 @@ fn delete_files(context: &mut AppContext, backend: &mut AppBackend) -> std::io::
true
};
if confirm_delete {
- delete_func(&paths)?;
+ let file_op = FileOperation::Delete;
+ let options = FileOperationOptions {
+ overwrite: false,
+ skip_exist: false,
+ permanently: !context.config_ref().use_trash,
+ };
- // remove directory previews
- for tab in context.tab_context_mut().iter_mut() {
- for p in &paths {
- tab.history_mut().remove(p.as_path());
- }
- }
- reload::reload(context, tab_index)?;
- let msg = format!("Deleted {} files", paths_len);
- context.message_queue_mut().push_success(msg);
+ let dest = path::PathBuf::new();
+ let worker_thread = IoWorkerThread::new(file_op, paths, dest, options);
+ context.worker_context_mut().push_worker(worker_thread);
}
Ok(())
}
diff --git a/src/commands/file_ops.rs b/src/commands/file_ops.rs
index 9e9498d..ce9b787 100644
--- a/src/commands/file_ops.rs
+++ b/src/commands/file_ops.rs
@@ -3,9 +3,7 @@ use std::process::Command;
use crate::context::{AppContext, LocalStateContext};
use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult};
-use crate::io::FileOp;
-
-use crate::io::{IoWorkerOptions, IoWorkerThread};
+use crate::io::{FileOperation, FileOperationOptions, IoWorkerThread};
pub fn cut(context: &mut AppContext) -> JoshutoResult {
if let Some(list) = context.tab_context_ref().curr_tab_ref().curr_list_ref() {
@@ -13,7 +11,7 @@ pub fn cut(context: &mut AppContext) -> JoshutoResult {
let mut local_state = LocalStateContext::new();
local_state.set_paths(selected.into_iter());
- local_state.set_file_op(FileOp::Cut);
+ local_state.set_file_op(FileOperation::Cut);
context.set_local_state(local_state);
}
@@ -26,14 +24,14 @@ pub fn copy(context: &mut AppContext) -> JoshutoResult {
let mut local_state = LocalStateContext::new();
local_state.set_paths(selected.into_iter());
- local_state.set_file_op(FileOp::Copy);
+ local_state.set_file_op(FileOperation::Copy);
context.set_local_state(local_state);
}
Ok(())
}
-pub fn paste(context: &mut AppContext, options: IoWorkerOptions) -> JoshutoResult {
+pub fn paste(context: &mut AppContext, options: FileOperationOptions) -> JoshutoResult {
match context.take_local_state() {
Some(state) if !state.paths.is_empty() => {
let dest = context.tab_context_ref().curr_tab_ref().cwd().to_path_buf();