summaryrefslogtreecommitdiffstats
path: root/src/commands/delete_files.rs
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2020-09-19 21:21:33 -0400
committerJiayi Zhao <jeff.no.zhao@gmail.com>2020-09-19 21:21:33 -0400
commit7b4c611ed4d804bd52aeda0a619a54bea9b1e13d (patch)
tree7ba31fff7d5de171aadfc32c67f81dd64428b3ba /src/commands/delete_files.rs
parent7741d4d2c8798ad0bb609e97fb3bda86c5318a36 (diff)
Change command to use an enum instead of polymorphism
Diffstat (limited to 'src/commands/delete_files.rs')
-rw-r--r--src/commands/delete_files.rs135
1 files changed, 58 insertions, 77 deletions
diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs
index 25a13f8..bc070f1 100644
--- a/src/commands/delete_files.rs
+++ b/src/commands/delete_files.rs
@@ -3,104 +3,85 @@ use std::path;
use termion::event::Key;
-use crate::commands::{JoshutoCommand, JoshutoRunnable, ReloadDirList};
use crate::context::JoshutoContext;
-use crate::error::JoshutoResult;
use crate::history::DirectoryHistory;
use crate::ui::widgets::TuiPrompt;
use crate::ui::TuiBackend;
use crate::util::load_child::LoadChild;
-#[derive(Clone, Debug)]
-pub struct DeleteFiles;
+use super::reload;
-impl DeleteFiles {
- pub fn new() -> Self {
- DeleteFiles
- }
- pub const fn command() -> &'static str {
- "delete_files"
- }
-
- pub fn remove_files<'a, I>(paths: I) -> std::io::Result<()>
- where
- I: Iterator<Item = &'a 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)?;
- }
+pub fn remove_files<'a, I>(paths: I) -> std::io::Result<()>
+where
+ I: Iterator<Item = &'a 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(())
}
+ 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() {
- Some(s) => s.get_selected_paths(),
- None => vec![],
- };
- let paths_len = paths.len();
+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() {
+ Some(s) => s.get_selected_paths(),
+ None => vec![],
+ };
+ let paths_len = paths.len();
- if paths_len == 0 {
- return Err(std::io::Error::new(
- std::io::ErrorKind::Other,
- "no files selected",
- ));
- }
+ if paths_len == 0 {
+ return Err(std::io::Error::new(
+ std::io::ErrorKind::Other,
+ "no files selected",
+ ));
+ }
- let ch = {
- let prompt_str = format!("Delete {} files? (Y/n)", paths_len);
- let mut prompt = TuiPrompt::new(&prompt_str);
- prompt.get_key(backend, context)
- };
+ let ch = {
+ let prompt_str = format!("Delete {} files? (Y/n)", paths_len);
+ let mut prompt = TuiPrompt::new(&prompt_str);
+ prompt.get_key(backend, context)
+ };
- if ch == Key::Char('y') || ch == Key::Char('\n') {
- if paths_len > 1 {
- let ch = {
- let prompt_str = "Are you sure? (y/N)";
- let mut prompt = TuiPrompt::new(prompt_str);
- prompt.get_key(backend, context)
- };
- if ch == Key::Char('y') {
- Self::remove_files(paths.iter().map(|p| p.as_path()))?;
- ReloadDirList::reload(tab_index, context)?;
- let msg = format!("Deleted {} files", paths_len);
- context.push_msg(msg);
- }
- } else {
- Self::remove_files(paths.iter().map(|p| p.as_path()))?;
- ReloadDirList::reload(tab_index, context)?;
+ if ch == Key::Char('y') || ch == Key::Char('\n') {
+ if paths_len > 1 {
+ let ch = {
+ let prompt_str = "Are you sure? (y/N)";
+ let mut prompt = TuiPrompt::new(prompt_str);
+ prompt.get_key(backend, context)
+ };
+ if ch == Key::Char('y') {
+ remove_files(paths.iter().map(|p| p.as_path()))?;
+ 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()))?;
+ reload::reload(context, tab_index)?;
+ let msg = format!("Deleted {} files", paths_len);
+ context.push_msg(msg);
}
- 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())
}
+ Ok(())
}
-impl JoshutoRunnable for DeleteFiles {
- fn execute(&self, context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> {
- Self::delete_files(context, backend)?;
+pub fn delete_selected_files(
+ context: &mut JoshutoContext,
+ backend: &mut TuiBackend,
+) -> std::io::Result<()> {
+ delete_files(context, backend)?;
- let options = context.config_t.sort_option.clone();
- let curr_path = context.tab_context_ref().curr_tab_ref().pwd().to_path_buf();
- for tab in context.tab_context_mut().iter_mut() {
- tab.history_mut().reload(&curr_path, &options)?;
- }
- LoadChild::load_child(context)?;
- Ok(())
+ let options = context.config_t.sort_option.clone();
+ let curr_path = context.tab_context_ref().curr_tab_ref().pwd().to_path_buf();
+ for tab in context.tab_context_mut().iter_mut() {
+ tab.history_mut().reload(&curr_path, &options)?;
}
+ LoadChild::load_child(context)?;
+ Ok(())
}