diff options
author | Caleb Bassi <calebjbassi@gmail.com> | 2019-02-15 04:43:09 -0800 |
---|---|---|
committer | Caleb Bassi <calebjbassi@gmail.com> | 2019-02-15 05:56:09 -0800 |
commit | 29b3922f9efb9f277641a63f8f1719a15cbb8d16 (patch) | |
tree | d919ba11fa5c754565c64c43cb1614f7ae4d2546 /src/commands/delete_files.rs | |
parent | 3853eef2d052460982903038daac7abd2b71d12e (diff) |
refactor: project layout
Diffstat (limited to 'src/commands/delete_files.rs')
-rw-r--r-- | src/commands/delete_files.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs new file mode 100644 index 0000000..f1a9d48 --- /dev/null +++ b/src/commands/delete_files.rs @@ -0,0 +1,79 @@ +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>) { + for path in &paths { + if let Ok(metadata) = fs::symlink_metadata(path) { + if metadata.is_dir() { + fs::remove_dir_all(&path).unwrap(); + } else { + fs::remove_file(&path).unwrap(); + } + } + } + } +} + +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) { + Self::remove_files(paths); + } + } + ui::wprint_msg(&context.views.bot_win, "Deleted files"); + + 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(); + } +} |