From 1793d635ebae3b276c0ff1da68f8ba14bd2d42a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Thu, 21 Sep 2023 12:39:38 +0800 Subject: [PATCH] feat: new `force` option added for the `remove` command, which does not show the confirmation dialog on trashing/deleting (#173) --- app/src/executor.rs | 4 +++- config/docs/keymap.md | 1 + core/src/tasks/tasks.rs | 35 +++++++++++++++++++++++------------ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/app/src/executor.rs b/app/src/executor.rs index bbc1ffec..3203a0da 100644 --- a/app/src/executor.rs +++ b/app/src/executor.rs @@ -110,7 +110,9 @@ impl Executor { } "remove" => { let targets = cx.manager.selected().into_iter().map(|f| f.url_owned()).collect(); - cx.tasks.file_remove(targets, exec.named.contains_key("permanently")) + let force = exec.named.contains_key("force"); + let permanently = exec.named.contains_key("permanently"); + cx.tasks.file_remove(targets, force, permanently) } "create" => cx.manager.create(), "rename" => cx.manager.rename(), diff --git a/config/docs/keymap.md b/config/docs/keymap.md index fff92991..c6ebb0f7 100644 --- a/config/docs/keymap.md +++ b/config/docs/keymap.md @@ -60,6 +60,7 @@ - remove: Move the files to the trash/recycle bin. + - `--force`: Don't show the confirmation dialog, and trash/delete the files directly. - `--permanently`: Permanently delete the files. - create: Create a file or directory (ends with `/` for directories). diff --git a/core/src/tasks/tasks.rs b/core/src/tasks/tasks.rs index f11de926..8c94a547 100644 --- a/core/src/tasks/tasks.rs +++ b/core/src/tasks/tasks.rs @@ -156,30 +156,41 @@ impl Tasks { } pub fn file_cut(&self, src: &HashSet, dest: Url, force: bool) -> bool { - for p in src { - let to = dest.join(p.file_name().unwrap()); - if force && p == &to { + for u in src { + let to = dest.join(u.file_name().unwrap()); + if force && u == &to { trace!("file_cut: same file, skipping {:?}", to); } else { - self.scheduler.file_cut(p.clone(), to, force); + self.scheduler.file_cut(u.clone(), to, force); } } false } pub fn file_copy(&self, src: &HashSet, dest: Url, force: bool, follow: bool) -> bool { - for p in src { - let to = dest.join(p.file_name().unwrap()); - if force && p == &to { + for u in src { + let to = dest.join(u.file_name().unwrap()); + if force && u == &to { trace!("file_copy: same file, skipping {:?}", to); } else { - self.scheduler.file_copy(p.clone(), to, force, follow); + self.scheduler.file_copy(u.clone(), to, force, follow); } } false } - pub fn file_remove(&self, targets: Vec, permanently: bool) -> bool { + pub fn file_remove(&self, targets: Vec, force: bool, permanently: bool) -> bool { + if force { + for u in targets { + if permanently { + self.scheduler.file_delete(u); + } else { + self.scheduler.file_trash(u); + } + } + return false; + } + let scheduler = self.scheduler.clone(); tokio::spawn(async move { let s = if targets.len() > 1 { "s" } else { "" }; @@ -193,11 +204,11 @@ impl Tasks { if choice != "y" && choice != "Y" { return; } - for p in targets { + for u in targets { if permanently { - scheduler.file_delete(p); + scheduler.file_delete(u); } else { - scheduler.file_trash(p); + scheduler.file_trash(u); } } }