summaryrefslogtreecommitdiffstats
path: root/src/commands
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2020-12-26 21:45:46 -0500
committerJiayi Zhao <jeff.no.zhao@gmail.com>2020-12-26 21:45:46 -0500
commit2b2672123d31bdcfd579916fa3fdc6cd961008a3 (patch)
tree3537f86ff7e4eb7b0851db7974138edc3e105556 /src/commands
parent883a20d31a05ca0b3eac09ee1952904ec0b2131a (diff)
add support for trashing files instead of permanently deleting
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/delete_files.rs44
1 files changed, 39 insertions, 5 deletions
diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs
index ed73e18..443875e 100644
--- a/src/commands/delete_files.rs
+++ b/src/commands/delete_files.rs
@@ -11,9 +11,26 @@ use crate::util::load_child::LoadChild;
use super::reload;
-pub fn remove_files<'a, I>(paths: I) -> std::io::Result<()>
+fn trash_error_to_io_error(err: trash::Error) -> std::io::Error {
+ match err {
+ trash::Error::Unknown => std::io::Error::new(std::io::ErrorKind::Other, "Unknown Error"),
+ trash::Error::TargetedRoot => {
+ std::io::Error::new(std::io::ErrorKind::Other, "Targeted Root")
+ }
+ trash::Error::CanonicalizePath { code: _ } => {
+ std::io::Error::new(std::io::ErrorKind::NotFound, "Not found")
+ }
+ trash::Error::Remove { code: Some(1) } => std::io::Error::new(
+ std::io::ErrorKind::InvalidData,
+ "Cannot move files to trash from mounted system",
+ ),
+ _ => std::io::Error::new(std::io::ErrorKind::Other, "Unknown Error"),
+ }
+}
+
+pub fn remove_files<P>(paths: &[P]) -> std::io::Result<()>
where
- I: Iterator<Item = &'a path::Path>,
+ P: AsRef<path::Path>,
{
for path in paths {
if let Ok(metadata) = fs::symlink_metadata(path) {
@@ -27,6 +44,18 @@ where
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 JoshutoContext, backend: &mut TuiBackend) -> std::io::Result<()> {
let tab_index = context.tab_context_ref().get_index();
let paths = match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
@@ -34,7 +63,6 @@ fn delete_files(context: &mut JoshutoContext, backend: &mut TuiBackend) -> std::
None => vec![],
};
let paths_len = paths.len();
-
if paths_len == 0 {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
@@ -42,6 +70,12 @@ fn delete_files(context: &mut JoshutoContext, backend: &mut TuiBackend) -> std::
));
}
+ let delete_func = if context.config_ref().use_trash {
+ trash_files
+ } else {
+ remove_files
+ };
+
let ch = {
let prompt_str = format!("Delete {} files? (Y/n)", paths_len);
let mut prompt = TuiPrompt::new(&prompt_str);
@@ -56,13 +90,13 @@ fn delete_files(context: &mut JoshutoContext, backend: &mut TuiBackend) -> std::
prompt.get_key(backend, context)
};
if ch == Key::Char('y') {
- remove_files(paths.iter().map(|p| p.as_path()))?;
+ delete_func(&paths)?;
reload::reload(context, tab_index)?;
let msg = format!("Deleted {} files", paths_len);
context.push_msg(msg);
}
} else {
- remove_files(paths.iter().map(|p| p.as_path()))?;
+ delete_func(&paths)?;
reload::reload(context, tab_index)?;
let msg = format!("Deleted {} files", paths_len);
context.push_msg(msg);