diff options
author | qkzk <qu3nt1n@gmail.com> | 2022-12-17 21:25:58 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2022-12-17 21:25:58 +0100 |
commit | ca2ff278511585ec5e2b7713773f80078ca48e7c (patch) | |
tree | e55295025799ebc2b676f44ed220064b9cfeafc4 /src | |
parent | 445a0fd923bb806e787892fc195a0674f8e0d2c4 (diff) |
create an enum for confirmed action
Diffstat (limited to 'src')
-rw-r--r-- | src/event_dispatch.rs | 4 | ||||
-rw-r--r-- | src/event_exec.rs | 34 | ||||
-rw-r--r-- | src/mode.rs | 34 | ||||
-rw-r--r-- | src/term_manager.rs | 34 |
4 files changed, 78 insertions, 28 deletions
diff --git a/src/event_dispatch.rs b/src/event_dispatch.rs index 2c2d9d8..8b90ebd 100644 --- a/src/event_dispatch.rs +++ b/src/event_dispatch.rs @@ -74,9 +74,9 @@ impl EventDispatcher { } Mode::Jump => Ok(()), Mode::History => Ok(()), - Mode::NeedConfirmation => { + Mode::NeedConfirmation(confirmed_action) => { if c == 'y' { - let _ = EventExec::exec_last_edition(status); + let _ = EventExec::exec_confirmed_action(status, confirmed_action); } EventExec::event_leave_need_confirmation(status.selected()); Ok(()) diff --git a/src/event_exec.rs b/src/event_exec.rs index 700344a..0bbfb8e 100644 --- a/src/event_exec.rs +++ b/src/event_exec.rs @@ -12,6 +12,7 @@ use crate::fileinfo::{FileKind, PathContent, SortBy}; use crate::filter::FilterKind; use crate::fm_error::{ErrorVariant, FmError, FmResult}; use crate::last_edition::LastEdition; +use crate::mode::ConfirmedAction; use crate::mode::{MarkAction, Mode}; use crate::opener::execute_in_child; use crate::preview::Preview; @@ -241,12 +242,14 @@ impl EventExec { status.reset_tabs_view() } - fn _exec_last_edition(status: &mut Status) -> FmResult<()> { - match status.selected().last_edition { - LastEdition::Delete => Self::exec_delete_files(status), - LastEdition::CutPaste => Self::exec_cut_paste(status), - LastEdition::CopyPaste => Self::exec_copy_paste(status), - LastEdition::Nothing => Ok(()), + fn _exec_confirmed_action( + status: &mut Status, + confirmed_action: ConfirmedAction, + ) -> FmResult<()> { + match confirmed_action { + ConfirmedAction::Delete => Self::exec_delete_files(status), + ConfirmedAction::Move => Self::exec_cut_paste(status), + ConfirmedAction::Copy => Self::exec_copy_paste(status), } } @@ -279,8 +282,11 @@ impl EventExec { } /// Execute a command requiring a confirmation (Delete, Move or Copy). - pub fn exec_last_edition(status: &mut Status) -> FmResult<()> { - Self::_exec_last_edition(status)?; + pub fn exec_confirmed_action( + status: &mut Status, + confirmed_action: ConfirmedAction, + ) -> FmResult<()> { + Self::_exec_confirmed_action(status, confirmed_action)?; status.selected().mode = Mode::Normal; status.selected().last_edition = LastEdition::Nothing; Ok(()) @@ -497,14 +503,14 @@ impl EventExec { /// Enter a copy paste mode. pub fn event_copy_paste(tab: &mut Tab) -> FmResult<()> { - tab.mode = Mode::NeedConfirmation; + tab.mode = Mode::NeedConfirmation(crate::mode::ConfirmedAction::Copy); tab.last_edition = LastEdition::CopyPaste; Ok(()) } /// Enter the 'move' mode. pub fn event_cut_paste(tab: &mut Tab) -> FmResult<()> { - tab.mode = Mode::NeedConfirmation; + tab.mode = Mode::NeedConfirmation(crate::mode::ConfirmedAction::Move); tab.last_edition = LastEdition::CutPaste; Ok(()) } @@ -551,7 +557,7 @@ impl EventExec { /// Enter the delete mode. /// A confirmation is then asked. pub fn event_delete_file(tab: &mut Tab) -> FmResult<()> { - tab.mode = Mode::NeedConfirmation; + tab.mode = Mode::NeedConfirmation(crate::mode::ConfirmedAction::Delete); tab.last_edition = LastEdition::Delete; Ok(()) } @@ -1251,7 +1257,11 @@ impl EventExec { Mode::Shortcut => EventExec::exec_shortcut(status.selected())?, Mode::Filter => EventExec::exec_filter(status.selected())?, Mode::Normal => EventExec::exec_file(status)?, - Mode::NeedConfirmation | Mode::Help | Mode::Sort | Mode::Preview | Mode::Marks(_) => (), + Mode::NeedConfirmation(_) + | Mode::Help + | Mode::Sort + | Mode::Preview + | Mode::Marks(_) => (), }; status.selected().input.reset(); diff --git a/src/mode.rs b/src/mode.rs index f652a57..cf12918 100644 --- a/src/mode.rs +++ b/src/mode.rs @@ -6,6 +6,36 @@ pub enum MarkAction { New, } +#[derive(Clone, Copy)] +pub enum ConfirmedAction { + Copy, + Delete, + Move, +} + +impl ConfirmedAction { + /// Offset before the cursor. + /// Since we ask the user confirmation, we need to know how much space + /// is needed. + pub fn cursor_offset(&self) -> usize { + match *self { + Self::Copy => 37, + Self::Delete => 31, + Self::Move => 29, + } + } +} + +impl std::fmt::Display for ConfirmedAction { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Self::Delete => write!(f, "Delete files :"), + Self::Move => write!(f, "Move files :"), + Self::Copy => write!(f, "Copy & Paste files :"), + } + } +} + /// Different mode in which the application can be. /// It dictates the reaction to event and what to display. #[derive(Clone)] @@ -34,7 +64,7 @@ pub enum Mode { Jump, /// Confirmation is required before modification is made to files : /// delete, move, copy - NeedConfirmation, + NeedConfirmation(ConfirmedAction), /// Change the type of sort Sort, /// Preview a file content @@ -64,7 +94,7 @@ impl fmt::Debug for Mode { Mode::RegexMatch => write!(f, "Regex : "), Mode::Jump => write!(f, "Jump : "), Mode::History => write!(f, "History :"), - Mode::NeedConfirmation => write!(f, "Y/N :"), + Mode::NeedConfirmation(_) => write!(f, "Y/N :"), Mode::Sort => write!(f, "Sort: Kind Name Modif Size Ext Rev :"), Mode::Preview => write!(f, "Preview : "), Mode::Shortcut => write!(f, "Shortcut :"), diff --git a/src/term_manager.rs b/src/term_manager.rs index 97c8be2..f7be764 100644 --- a/src/term_manager.rs +++ b/src/term_manager.rs @@ -12,8 +12,7 @@ use crate::config::Colors; use crate::content_window::ContentWindow; use crate::fileinfo::fileinfo_attr; use crate::fm_error::{ErrorVariant, FmError, FmResult}; -use crate::last_edition::LastEdition; -use crate::mode::{MarkAction, Mode}; +use crate::mode::{ConfirmedAction, MarkAction, Mode}; use crate::preview::{Preview, Window}; use crate::status::Status; use crate::tab::Tab; @@ -61,7 +60,9 @@ impl<'a> Draw for WinTab<'a> { Mode::Jump => self.jump_list(self.status, canvas), Mode::History => self.history(self.tab, canvas), Mode::Exec | Mode::Goto | Mode::Search => self.completion(self.tab, canvas), - Mode::NeedConfirmation => self.confirmation(self.status, self.tab, canvas), + Mode::NeedConfirmation(confirmed_mode) => { + self.confirmation(self.status, self.tab, confirmed_mode, canvas) + } Mode::Preview | Mode::Help => self.preview(self.tab, canvas), Mode::Shortcut => self.shortcuts(self.tab, canvas), Mode::Marks(_) => self.marks(self.status, self.tab, canvas), @@ -127,11 +128,14 @@ impl<'a> WinTab<'a> { format!("{} ", &tab.path_content.git_string()?), ] } - Mode::NeedConfirmation => { - vec![ - format!("Confirm {}", tab.last_edition), - "(y/n) : ".to_owned(), - ] + Mode::NeedConfirmation(ConfirmedAction::Copy) => { + vec!["Copy (y, n)".to_owned()] + } + Mode::NeedConfirmation(ConfirmedAction::Move) => { + vec!["Move (y, n)".to_owned()] + } + Mode::NeedConfirmation(ConfirmedAction::Delete) => { + vec!["Delete (y, n)".to_owned()] } Mode::Preview => match tab.path_content.selected_file() { Some(fileinfo) => { @@ -227,8 +231,8 @@ impl<'a> WinTab<'a> { | Mode::History => { canvas.show_cursor(false)?; } - Mode::NeedConfirmation => { - canvas.set_cursor(0, tab.last_edition.offset())?; + Mode::NeedConfirmation(confirmed_action) => { + canvas.set_cursor(0, confirmed_action.cursor_offset())?; } Mode::Sort => { canvas.set_cursor(0, Self::SORT_CURSOR_OFFSET)?; @@ -323,7 +327,13 @@ impl<'a> WinTab<'a> { } /// Display a list of edited (deleted, copied, moved) files for confirmation - fn confirmation(&self, status: &Status, tab: &Tab, canvas: &mut dyn Canvas) -> FmResult<()> { + fn confirmation( + &self, + status: &Status, + tab: &Tab, + confirmed_mode: ConfirmedAction, + canvas: &mut dyn Canvas, + ) -> FmResult<()> { for (row, path) in status.flagged.iter().enumerate() { canvas.print_with_attr( row + ContentWindow::WINDOW_MARGIN_TOP + 2, @@ -338,7 +348,7 @@ impl<'a> WinTab<'a> { )?; } info!("last_edition: {}", tab.last_edition); - if let LastEdition::CopyPaste = tab.last_edition { + if let ConfirmedAction::Copy = confirmed_mode { let content = format!( "Files will be copied to {}", tab.path_content.path_to_str()? |