diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-02-15 18:57:14 -0500 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-02-15 18:57:14 -0500 |
commit | 055ac602929af367da2af77999e73de6bf8fd344 (patch) | |
tree | cf7b8581611606086033c3290da38877fb8ea802 /src/commands/delete_files.rs | |
parent | 6ea8282b2bff390b760dcb3835031721581f32e2 (diff) | |
parent | 937bb346485245a918a29f368cf92491970641bb (diff) |
merge 'master' with 'dev'
Diffstat (limited to 'src/commands/delete_files.rs')
-rw-r--r-- | src/commands/delete_files.rs | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs new file mode 100644 index 0000000..e6c8467 --- /dev/null +++ b/src/commands/delete_files.rs @@ -0,0 +1,82 @@ +extern crate ncurses; + +use std::fs; +use std::path; + +use commands::{self, JoshutoCommand, JoshutoRunnable}; +use config::keymap; +use context::JoshutoContext; +use preview; +use ui; + +#[derive(Clone, Debug)] +pub struct DeleteFiles; + +impl DeleteFiles { + pub fn new() -> Self { + DeleteFiles + } + pub const fn command() -> &'static str { + "delete_files" + } + + pub fn remove_files(paths: Vec<path::PathBuf>) -> Result<(), std::io::Error> { + 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(()) + } +} + +impl JoshutoCommand for DeleteFiles {} + +impl std::fmt::Display for DeleteFiles { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str(Self::command()) + } +} + +impl JoshutoRunnable for DeleteFiles { + fn execute(&self, context: &mut JoshutoContext) { + ui::wprint_msg(&context.views.bot_win, "Delete selected files? (Y/n)"); + ncurses::timeout(-1); + ncurses::doupdate(); + + let ch: i32 = ncurses::getch(); + if ch == 'y' as i32 || ch == keymap::ENTER as i32 { + if let Some(s) = context.tabs[context.curr_tab_index].curr_list.as_ref() { + if let Some(paths) = commands::collect_selected_paths(s) { + match Self::remove_files(paths) { + Ok(_) => ui::wprint_msg(&context.views.bot_win, "Deleted files"), + Err(e) => ui::wprint_err(&context.views.bot_win, e.to_string().as_str()), + } + } + } + + let curr_tab = &mut context.tabs[context.curr_tab_index]; + curr_tab.reload_contents(&context.config_t.sort_type); + curr_tab.refresh( + &context.views, + &context.config_t, + &context.username, + &context.hostname, + ); + } else { + let curr_tab = &context.tabs[context.curr_tab_index]; + curr_tab.refresh_file_status(&context.views.bot_win); + curr_tab.refresh_path_status( + &context.views.top_win, + &context.username, + &context.hostname, + context.config_t.tilde_in_titlebar, + ); + } + ncurses::doupdate(); + } +} |