diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-04-16 00:27:14 +0200 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-04-16 00:27:14 +0200 |
commit | f4b12cfd7a6288b07649fca49b7ac75414bbec5c (patch) | |
tree | 1641c5b378414be101c87121fedce4ea8322578a /src/event_exec.rs | |
parent | 9a51c76e85ce35580967baaa9f07a7fdb77b9102 (diff) |
refactor event exec, event dispatch
Diffstat (limited to 'src/event_exec.rs')
-rw-r--r-- | src/event_exec.rs | 961 |
1 files changed, 481 insertions, 480 deletions
diff --git a/src/event_exec.rs b/src/event_exec.rs index aabc35d..2875ac5 100644 --- a/src/event_exec.rs +++ b/src/event_exec.rs @@ -40,11 +40,11 @@ use crate::status::Status; use crate::tab::Tab; use crate::utils::{current_username, disk_used_by_path, filename_from_path}; -/// Every kind of mutation of the application is defined here. +/// Links events from tuikit to custom actions. /// It mutates `Status` or its children `Tab`. -pub struct EventExec {} +pub struct EventAction {} -impl EventExec { +impl EventAction { /// Remove every flag on files in this directory and others. pub fn event_clear_flags(status: &mut Status) -> Result<()> { status.flagged.clear(); @@ -83,7 +83,7 @@ impl EventExec { let Some(file) = tab.path_content.selected() else { return Ok(()) }; let path = file.path.clone(); status.toggle_flag_on_path(&path); - helper_down_one_row(status.selected()); + Helper::down_one_row(status.selected()); } Mode::Tree => { let path = tab.directory.tree.current_node.filepath(); @@ -273,7 +273,7 @@ impl EventExec { tab.set_mode(Mode::Preview); tab.preview = Preview::log(log); tab.window.reset(tab.preview.len()); - helper_go_bottom(tab); + Helper::go_bottom(tab); Ok(()) } @@ -334,7 +334,7 @@ impl EventExec { .clone(); let opener = status.opener.open_info(filepath); if let Some(InternalVariant::NotSupported) = opener.internal_variant.as_ref() { - helper_mount_iso_drive(status)?; + Helper::mount_iso_drive(status)?; } else { match status.opener.open(filepath) { Ok(_) => (), @@ -520,7 +520,7 @@ impl EventExec { /// Does nothing if the selected item is already the first in list. pub fn event_move_up(status: &mut Status, colors: &Colors) -> Result<()> { match status.selected().mode { - Mode::Normal | Mode::Preview => helper_up_one_row(status.selected()), + Mode::Normal | Mode::Preview => Helper::up_one_row(status.selected()), Mode::Navigate(Navigate::Jump) => status.flagged.prev(), Mode::Navigate(Navigate::History) => status.selected().history.prev(), Mode::Navigate(Navigate::Trash) => status.trash.prev(), @@ -532,7 +532,7 @@ impl EventExec { Mode::Navigate(Navigate::CliInfo) => status.cli_info.prev(), Mode::Navigate(Navigate::EncryptedDrive) => status.encrypted_devices.prev(), Mode::InputCompleted(_) => status.selected().completion.prev(), - Mode::Tree => helper_select_prev(status.selected(), colors)?, + Mode::Tree => Helper::select_prev(status.selected(), colors)?, _ => (), }; Ok(()) @@ -542,7 +542,7 @@ impl EventExec { /// Does nothing if the user is already at the bottom. pub fn event_move_down(status: &mut Status, colors: &Colors) -> Result<()> { match status.selected().mode { - Mode::Normal | Mode::Preview => helper_down_one_row(status.selected()), + Mode::Normal | Mode::Preview => Helper::down_one_row(status.selected()), Mode::Navigate(Navigate::Jump) => status.flagged.next(), Mode::Navigate(Navigate::History) => status.selected().history.next(), Mode::Navigate(Navigate::Trash) => status.trash.next(), @@ -554,7 +554,7 @@ impl EventExec { Mode::Navigate(Navigate::CliInfo) => status.cli_info.next(), Mode::Navigate(Navigate::EncryptedDrive) => status.encrypted_devices.next(), Mode::InputCompleted(_) => status.selected().completion.next(), - Mode::Tree => helper_select_next(status.selected(), colors)?, + Mode::Tree => Helper::select_next(status.selected(), colors)?, _ => (), }; Ok(()) @@ -565,10 +565,10 @@ impl EventExec { pub fn event_move_left(status: &mut Status, colors: &Colors) -> Result<()> { let tab = status.selected(); match tab.mode { - Mode::Normal => helper_move_to_parent(tab), - Mode::Tree => helper_select_parent(tab, colors), + Mode::Normal => Helper::move_to_parent(tab), + Mode::Tree => Helper::select_parent(tab, colors), Mode::InputSimple(_) | Mode::InputCompleted(_) => { - helper_move_cursor_left(tab); + Helper::move_cursor_left(tab); Ok(()) } @@ -580,10 +580,10 @@ impl EventExec { /// Move the cursor one char to right in mode requiring text input. pub fn event_move_right(status: &mut Status, colors: &Colors) -> Result<()> { match status.selected().mode { - Mode::Normal => LeaveMode::leave_file(status), - Mode::Tree => helper_select_first_child(status.selected(), colors), + Mode::Normal => LeaveMode::file(status), + Mode::Tree => Helper::select_first_child(status.selected(), colors), Mode::InputSimple(_) | Mode::InputCompleted(_) => { - helper_move_cursor_right(status.selected()); + Helper::move_cursor_right(status.selected()); Ok(()) } _ => Ok(()), @@ -594,7 +594,7 @@ impl EventExec { pub fn event_backspace(status: &mut Status) -> Result<()> { match status.selected().mode { Mode::InputSimple(_) | Mode::InputCompleted(_) => { - helper_delete_char_left(status.selected()); + Helper::delete_char_left(status.selected()); Ok(()) } Mode::Normal => Ok(()), @@ -606,7 +606,7 @@ impl EventExec { pub fn event_delete(status: &mut Status) -> Result<()> { match status.selected().mode { Mode::InputSimple(_) | Mode::InputCompleted(_) => { - helper_delete_chars_right(status.selected()); + Helper::delete_chars_right(status.selected()); Ok(()) } _ => Ok(()), @@ -617,9 +617,9 @@ impl EventExec { pub fn event_key_home(status: &mut Status, colors: &Colors) -> Result<()> { let tab = status.selected(); match tab.mode { - Mode::Normal | Mode::Preview => helper_go_top(tab), - Mode::Tree => helper_tree_go_to_root(tab, colors)?, - _ => helper_cursor_home(tab), + Mode::Normal | Mode::Preview => Helper::go_top(tab), + Mode::Tree => Helper::tree_go_to_root(tab, colors)?, + _ => Helper::cursor_home(tab), }; Ok(()) } @@ -628,9 +628,9 @@ impl EventExec { pub fn event_end(status: &mut Status, colors: &Colors) -> Result<()> { let tab = status.selected(); match tab.mode { - Mode::Normal | Mode::Preview => helper_go_bottom(tab), - Mode::Tree => helper_tree_go_to_bottom_leaf(tab, colors)?, - _ => helper_cursor_end(tab), + Mode::Normal | Mode::Preview => Helper::go_bottom(tab), + Mode::Tree => Helper::tree_go_to_bottom_leaf(tab, colors)?, + _ => Helper::cursor_end(tab), }; Ok(()) } @@ -639,7 +639,7 @@ impl EventExec { pub fn event_page_up(status: &mut Status, colors: &Colors) -> Result<()> { match status.selected().mode { Mode::Normal | Mode::Preview => page_up(status.selected()), - Mode::Tree => helper_tree_page_up(status.selected(), colors)?, + Mode::Tree => Helper::tree_page_up(status.selected(), colors)?, _ => (), }; Ok(()) @@ -649,7 +649,7 @@ impl EventExec { pub fn event_page_down(status: &mut Status, colors: &Colors) -> Result<()> { match status.selected().mode { Mode::Normal | Mode::Preview => page_down(status.selected()), - Mode::Tree => helper_tree_page_down(status.selected(), colors)?, + Mode::Tree => Helper::tree_page_down(status.selected(), colors)?, _ => (), }; Ok(()) @@ -664,56 +664,50 @@ impl EventExec { let mut must_refresh = true; let mut must_reset_mode = true; match status.selected_non_mut().mode { - Mode::InputSimple(InputSimple::Rename) => LeaveMode::leave_rename(status.selected())?, - Mode::InputSimple(InputSimple::Newfile) => LeaveMode::leave_newfile(status.selected())?, - Mode::InputSimple(InputSimple::Newdir) => LeaveMode::leave_newdir(status.selected())?, - Mode::InputSimple(InputSimple::Chmod) => LeaveMode::leave_chmod(status)?, - Mode::InputSimple(InputSimple::RegexMatch) => LeaveMode::leave_regex(status)?, - Mode::InputSimple(InputSimple::SetNvimAddr) => LeaveMode::leave_set_nvim_addr(status)?, + Mode::InputSimple(InputSimple::Rename) => LeaveMode::rename(status.selected())?, + Mode::InputSimple(InputSimple::Newfile) => LeaveMode::newfile(status.selected())?, + Mode::InputSimple(InputSimple::Newdir) => LeaveMode::newdir(status.selected())?, + Mode::InputSimple(InputSimple::Chmod) => LeaveMode::chmod(status)?, + Mode::InputSimple(InputSimple::RegexMatch) => LeaveMode::regex(status)?, + Mode::InputSimple(InputSimple::SetNvimAddr) => LeaveMode::set_nvim_addr(status)?, Mode::InputSimple(InputSimple::Shell) => { must_reset_mode = false; - must_refresh = LeaveMode::leave_shell(status)?; + must_refresh = LeaveMode::shell(status)?; } Mode::InputSimple(InputSimple::Filter) => { must_refresh = false; - LeaveMode::leave_filter(status, colors)? + LeaveMode::filter(status, colors)? } Mode::InputSimple(InputSimple::Password(kind, action, dest)) => { must_refresh = false; must_reset_mode = false; - LeaveMode::leave_password(status, kind)?; + LeaveMode::password(status, kind)?; dispatch_password(status, dest, action, colors)?; } - Mode::Navigate(Navigate::Jump) => LeaveMode::leave_jump(status)?, - Mode::Navigate(Navigate::History) => LeaveMode::leave_history(status.selected())?, - Mode::Navigate(Navigate::Shortcut) => LeaveMode::leave_shortcut(status.selected())?, - Mode::Navigate(Navigate::Trash) => LeaveMode::leave_trash(status)?, - Mode::Navigate(Navigate::Bulk) => LeaveMode::leave_bulk(status)?, - Mode::Navigate(Navigate::ShellMenu) => LeaveMode::leave_shellmenu(status)?, + Mode::Navigate(Navigate::Jump) => LeaveMode::jump(status)?, + Mode::Navigate(Navigate::History) => LeaveMode::history(status.selected())?, + Mode::Navigate(Navigate::Shortcut) => LeaveMode::shortcut(status.selected())?, + Mode::Navigate(Navigate::Trash) => LeaveMode::trash(status)?, + Mode::Navigate(Navigate::Bulk) => LeaveMode::bulk(status)?, + Mode::Navigate(Navigate::ShellMenu) => LeaveMode::shellmenu(status)?, Mode::Navigate(Navigate::CliInfo) => { must_refresh = false; must_reset_mode = false; - LeaveMode::leave_cli_info(status)?; + LeaveMode::cli_info(status)?; } Mode::Navigate(Navigate::EncryptedDrive) => (), - Mode::Navigate(Navigate::Marks(MarkAction::New)) => { - LeaveMode::leave_marks_update(status)? - } - Mode::Navigate(Navigate::Marks(MarkAction::Jump)) => { - LeaveMode::leave_marks_jump(status)? - } - Mode::Navigate(Navigate::Compress) => LeaveMode::leave_compress(status)?, - Mode::InputCompleted(InputCompleted::Exec) => LeaveMode::leave_exec(status.selected())?, + Mode::Navigate(Navigate::Marks(MarkAction::New)) => LeaveMode::marks_update(status)?, + Mode::Navigate(Navigate::Marks(MarkAction::Jump)) => LeaveMode::marks_jump(status)?, + Mode::Navigate(Navigate::Compress) => LeaveMode::compress(status)?, + Mode::InputCompleted(InputCompleted::Exec) => LeaveMode::exec(status.selected())?, Mode::InputCompleted(InputCompleted::Search) => { must_refresh = false; - LeaveMode::leave_search(status, colors)? + LeaveMode::search(status, colors)? } - Mode::InputCompleted(InputCompleted::Goto) => LeaveMode::leave_goto(status.selected())?, - Mode::InputCompleted(InputCompleted::Command) => { - LeaveMode::leave_command(status, colors)? - } - Mode::Normal => LeaveMode::leave_file(status)?, - Mode::Tree => LeaveMode::leave_tree(status, colors)?, + Mode::InputCompleted(InputCompleted::Goto) => LeaveMode::goto(status.selected())?, + Mode::InputCompleted(InputCompleted::Command) => LeaveMode::command(status, colors)?, + Mode::Normal => LeaveMode::file(status)?, + Mode::Tree => LeaveMode::tree(status, colors)?, Mode::NeedConfirmation(_) | Mode::Preview | Mode::InputCompleted(InputCompleted::Nothing) @@ -725,7 +719,7 @@ impl EventExec { status.selected().reset_mode(); } if must_refresh { - refresh_status(status, colors)?; + Helper::refresh_status(status, colors)?; } Ok(()) } @@ -734,7 +728,7 @@ impl EventExec { /// insert a completion in modes allowing completion. pub fn event_tab(status: &mut Status) -> Result<()> { match status.selected().mode { - Mode::InputCompleted(_) => helper_replace_input_with_completion(status.selected()), + Mode::InputCompleted(_) => Helper::replace_input_with_completion(status.selected()), Mode::Normal | Mode::Tree => status.next(), _ => (), }; @@ -768,7 +762,7 @@ impl EventExec { /// Copy the filename of the selected file in normal mode. pub fn event_copy_filename(status: &mut Status) -> Result<()> { if let Mode::Normal | Mode::Tree = status.selected_non_mut().mode { - return helper_filename_to_clipboard(status.selected()); + return Helper::filename_to_clipboard(status.selected()); } Ok(()) } @@ -776,7 +770,7 @@ impl EventExec { /// Copy the filepath of the selected file in normal mode. pub fn event_copy_filepath(status: &mut Status) -> Result<()> { if let Mode::Normal | Mode::Tree = status.selected_non_mut().mode { - return helper_filepath_to_clipboard(status.selected()); + return Helper::filepath_to_clipboard(status.selected()); } Ok(()) } @@ -784,7 +778,7 @@ impl EventExec { /// Refresh the current view, reloading the files. Move the selection to top. pub fn event_refreshview(status: &mut Status, colors: &Colors) -> Result<()> { status.encrypted_devices.update()?; - refresh_status(status, colors) + Helper::refresh_status(status, colors) } /// Display mediainfo details of an image @@ -895,7 +889,7 @@ impl EventExec { let (tree, _, _) = tab.directory.tree.explore_position(false); tree.node.toggle_fold(); tab.directory.make_preview(colors); - helper_select_next(tab, colors) + Helper::select_next(tab, colors) } /// Unfold every child node in the tree. @@ -999,7 +993,7 @@ impl EventExec { /// Execute a custom event on the selected file pub fn event_custom(status: &mut Status, string: String) -> Result<()> { - info!("helper_custom {string}"); + info!("custom {string}"); let parser = ShellCommandParser::new(&string); let mut args = parser.compute(status)?; let command = args.remove(0); @@ -1010,19 +1004,20 @@ impl EventExec { } } +/// Methods called when executing something with Enter key. pub struct LeaveMode {} impl LeaveMode { /// Restore a file from the trash if possible. /// Parent folders are created if needed. - pub fn leave_trash(status: &mut Status) -> Result<()> { + pub fn trash(status: &mut Status) -> Result<()> { status.trash.restore()?; status.selected().reset_mode(); status.selected().refresh_view()?; Ok(()) } /// Open the file with configured opener or enter the directory. - pub fn leave_file(status: &mut Status) -> Result<()> { + pub fn file(status: &mut Status) -> Result<()> { let tab = status.selected(); if tab.path_content.is_empty() { return Ok(()); @@ -1030,12 +1025,12 @@ impl LeaveMode { if tab.path_content.is_selected_dir()? { tab.go_to_child() } else { - EventExec::event_open_file(status) + EventAction::event_open_file(status) } } /// Jump to the current mark. - pub fn leave_marks_jump(status: &mut Status) -> Result<()> { + pub fn marks_jump(status: &mut Status) -> Result<()> { let marks = status.marks.clone(); let tab = status.selected(); if let Some((_, path)) = marks.selected() { @@ -1050,7 +1045,7 @@ impl LeaveMode { /// Update the selected mark with the current path. /// Doesn't change its char. /// If it doesn't fail, a new pair will be set with (oldchar, new path). - pub fn leave_marks_update(status: &mut Status) -> Result<()> { + pub fn marks_update(status: &mut Status) -> Result<()> { let marks = status.marks.clone(); let len = status.selected_non_mut().path_content.content.len(); if let Some((ch, _)) = marks.selected() { @@ -1063,15 +1058,15 @@ impl LeaveMode { Ok(()) } - pub fn leave_bulk(status: &mut Status) -> Result<()> { + pub fn bulk(status: &mut Status) -> Result<()> { status.bulk.execute_bulk(status) } - pub fn leave_shellmenu(status: &mut Status) -> Result<()> { + pub fn shellmenu(status: &mut Status) -> Result<()> { status.shell_menu.execute(status) } - pub fn leave_cli_info(status: &mut Status) -> Result<()> { + pub fn cli_info(status: &mut Status) -> Result<()> { let output = status.cli_info.execute()?; info!("output\n{output}"); status.selected().set_mode(Mode::Preview); @@ -1085,7 +1080,7 @@ impl LeaveMode { /// the file. /// Nothing is done if the user typed nothing or an invalid permission like /// 955. - pub fn leave_chmod(status: &mut Status) -> Result<()> { + pub fn chmod(status: &mut Status) -> Result<()> { if status.selected().input.is_empty() { return Ok(()); } @@ -1101,7 +1096,7 @@ impl LeaveMode { status.reset_tabs_view() } - pub fn leave_set_nvim_addr(status: &mut Status) -> Result<()> { + pub fn set_nvim_addr(status: &mut Status) -> Result<()> { status.nvim_server = status.selected_non_mut().input.string(); status.selected().reset_mode(); Ok(()) @@ -1110,7 +1105,7 @@ impl LeaveMode { /// Execute a jump to the selected flagged file. /// If the user selected a directory, we jump inside it. /// Otherwise, we jump to the parent and select the file. - pub fn leave_jump(status: &mut Status) -> Result<()> { + pub fn jump(status: &mut Status) -> Result<()> { let Some(jump_target) = status.flagged.selected() else { return Ok(()) }; let jump_target = jump_target.to_owned(); let target_dir = match jump_target.parent() { @@ -1125,7 +1120,7 @@ impl LeaveMode { } /// Select the first file matching the typed regex in current dir. - pub fn leave_regex(status: &mut Status) -> Result<(), regex::Error> { + pub fn regex(status: &mut Status) -> Result<(), regex::Error> { status.select_from_regex()?; status.selected().input.reset(); Ok(()) @@ -1134,7 +1129,7 @@ impl LeaveMode { /// Execute a shell command typed by the user. /// pipes and redirections aren't NotSupported /// expansions are supported - pub fn leave_shell(status: &mut Status) -> Result<bool> { + pub fn shell(status: &mut Status) -> Result<bool> { let shell_command = status.selected_non_mut().input.string(); let mut args = ShellCommandParser::new(&shell_command).compute(status)?; info!("command {shell_command} args: {args:?}"); @@ -1145,7 +1140,7 @@ impl LeaveMode { let executable = args.remove(0); if is_sudo_command(&executable) { status.sudo_command = Some(shell_command); - helper_ask_password(status, PasswordKind::SUDO, None, PasswordUsage::SUDOCOMMAND)?; + Helper::ask_password(status, PasswordKind::SUDO, None, PasswordUsage::SUDOCOMMAND)?; Ok(false) } else { let Ok(executable) = which(executable) else { return Ok(true); }; @@ -1168,7 +1163,7 @@ impl LeaveMode { /// It uses the `fs::rename` function and has the same limitations. /// We only try to rename in the same directory, so it shouldn't be a problem. /// Filename is sanitized before processing. - pub fn leave_rename(tab: &mut Tab) -> Result<()> { + pub fn rename(tab: &mut Tab) -> Result<()> { let fileinfo = match tab.previous_mode { Mode::Tree => &tab.directory.tree.current_node.fileinfo, _ => tab @@ -1199,7 +1194,7 @@ impl LeaveMode { /// Creates a new file with input string as name. /// Nothing is done if the file already exists. /// Filename is sanitized before processing. - pub fn leave_newfile(tab: &mut Tab) -> Result<()> { + pub fn newfile(tab: &mut Tab) -> Result<()> { let path = tab .path_content .path @@ -1216,7 +1211,7 @@ impl LeaveMode { /// We use `fs::create_dir` internally so it will fail if the input string /// ie. the user can create `newdir` or `newdir/newfolder`. /// Directory name is sanitized before processing. - pub fn leave_newdir(tab: &mut Tab) -> Result<()> { + pub fn newdir(tab: &mut Tab) -> Result<()> { let path = tab .path_content .path @@ -1232,7 +1227,7 @@ impl LeaveMode { /// from the input string. It will fail silently if the executable can't /// be found. /// Optional parameters can be passed normally. ie. `"ls -lah"` - pub fn leave_exec(tab: &mut Tab) -> Result<()> { + pub fn exec(tab: &mut Tab) -> Result<()> { if tab.path_content.content.is_empty() { return Err(anyhow!("exec exec: empty directory")); } @@ -1251,7 +1246,7 @@ impl LeaveMode { /// ie. If you typed `"jpg"` before, it will move to the first file /// whose filename contains `"jpg"`. /// The current order of files is used. - pub fn leave_search(status: &mut Status, colors: &Colors) -> Result<()> { + pub fn search(status: &mut Status, colors: &Colors) -> Result<()> { let tab = status.selected(); let searched = tab.input.string(); tab.input.reset(); @@ -1285,7 +1280,7 @@ impl LeaveMode { /// The first completion proposition is used, `~` expansion is done. /// If no result were found, no cd is done and we go back to normal mode /// silently. - pub fn leave_goto(tab: &mut Tab) -> Result<()> { + pub fn goto(tab: &mut Tab) -> Result<()> { if tab.completion.is_empty() { return Ok(()); } @@ -1300,7 +1295,7 @@ impl LeaveMode { /// Move to the selected shortcut. /// It may fail if the user has no permission to visit the path. - pub fn leave_shortcut(tab: &mut Tab) -> Result<()> { + pub fn shortcut(tab: &mut Tab) -> Result<()> { tab.input.reset(); let path = tab .shortcut @@ -1314,7 +1309,7 @@ impl LeaveMode { /// Move back to a previously visited path. /// It may fail if the user has no permission to visit the path - pub fn leave_history(tab: &mut Tab) -> Result<()> { + pub fn history(tab: &mut Tab) -> Result<()> { tab.input.reset(); let path = tab .history @@ -1327,11 +1322,11 @@ impl LeaveMode { } /// Execute the selected node if it's a file else enter the directory. - pub fn leave_tree(status: &mut Status, colors: &Colors) -> Result<()> { + pub fn tree(status: &mut Status, colors: &Colors) -> Result<()> { let tab = status.selected(); let node = tab.directory.tree.current_node.clone(); if !node.is_dir { - EventExec::event_open_file(status) + EventAction::event_open_file(status) } else { tab.set_pathcontent(&node.filepath())?; tab.make_tree(colors)?; @@ -1340,7 +1335,7 @@ impl LeaveMode { } /// Store a password of some kind (sudo or device passphrase). - fn leave_password(status: &mut Status, password_kind: PasswordKind) -> Result<()> { + fn password(status: &mut Status, password_kind: PasswordKind) -> Result<()> { let password = status.selected_non_mut().input.string(); match password_kind { PasswordKind::SUDO => status.password_holder.set_sudo(password), @@ -1353,7 +1348,7 @@ impl LeaveMode { /// Compress the flagged files into an archive. /// Compression method is chosen by the user. /// The archive is created in the current directory and is named "archive.tar.??" or "archive.zip". - fn leave_compress(status: &mut Status) -> Result<()> { + fn compress(status: &mut Status) -> Result<()> { let cwd = std::env::current_dir()?; let files_with_relative_paths = status .flagged @@ -1367,7 +1362,7 @@ impl LeaveMode { /// Execute the selected command. /// Some commands does nothing as they require to be executed from a specific /// context. - pub fn leave_command(status: &mut Status, colors: &Colors) -> Result<()> { + pub fn command(status: &mut Status, colors: &Colors) -> Result<()> { let command_str = status.selected_non_mut().completion.current_proposition(); let Ok(command) = ActionMap::from_str(command_str) else { return Ok(()) }; command.matcher(status, colors) @@ -1375,7 +1370,7 @@ impl LeaveMode { /// Apply a filter to the displayed files. /// See `crate::filter` for more details. - pub fn leave_filter(status: &mut Status, colors: &Colors) -> Result<()> { + pub fn filter(status: &mut Status, colors: &Colors) -> Result<()> { let tab = status.selected(); let filter = FilterKind::from_input(&tab.input.string()); tab.set_filter(filter); @@ -1388,435 +1383,463 @@ impl LeaveMode { Ok(()) } } -/// Move down one row if possible. -pub fn helper_down_one_row(tab: &mut Tab) { - match tab.mode { - Mode::Normal => { - tab.path_content.unselect_current(); - tab.path_content.next(); - tab.path_content.select_current(); - tab.window.scroll_down_one(tab.path_content.index) + +/// Helper methods called from an event reaction or from a leave mode. +pub struct Helper {} + +impl Helper { + /// Move down one row if possible. + pub fn down_one_row(tab: &mut Tab) { + match tab.mode { + Mode::Normal => { + tab.path_content.unselect_current(); + tab.path_content.next(); + tab.path_content.select_current(); + tab.window.scroll_down_one(tab.path_content.index) + } + Mode::Preview => tab.window.scroll_down_one(tab.window.bottom), + _ => (), } - Mode::Preview => tab.window.scroll_down_one(tab.window.bottom), - _ => (), } -} -/// Move up one row if possible. -pub fn helper_up_one_row(tab: &mut Tab) { - match tab.mode { - Mode::Normal => { - tab.path_content.unselect_current(); - tab.path_content.prev(); - tab.path_content.select_current(); - tab.window.scroll_up_one(tab.path_content.index) + /// Move up one row if possible. + pub fn up_one_row(tab: &mut Tab) { + match tab.mode { + Mode::Normal => { + tab.path_content.unselect_current(); + tab.path_content.prev(); + tab.path_content.select_current(); + tab.window.scroll_up_one(tab.path_content.index) + } + Mode::Preview => tab.window.scroll_up_one(tab.window.top), + _ => (), } - Mode::Preview => tab.window.scroll_up_one(tab.window.top), - _ => (), } -} -/// Move to the top of the current directory. -pub fn helper_go_top(tab: &mut Tab) { - match tab.mode { - Mode::Normal => tab.path_content.select_index(0), - Mode::Preview => (), - _ => { - return; + /// Move to the top of the current directory. + pub fn go_top(tab: &mut Tab) { + match tab.mode { + Mode::Normal => tab.path_content.select_index(0), + Mode::Preview => (), + _ => { + return; + } } + tab.window.scroll_to(0); } - tab.window.scroll_to(0); -} -/// Move to parent directory if there's one. -/// Does -/// Add the starting directory to history. -pub fn helper_move_to_parent(tab: &mut Tab) -> Result<()> { - tab.move_to_parent() -} + /// Move to parent directory if there's one. + /// Does + /// Add the starting directory to history. + pub fn move_to_parent(tab: &mut Tab) -> Result<()> { + tab.move_to_parent() + } -/// Move the cursor left one block. -pub fn helper_move_cursor_left(tab: &mut Tab) { - tab.input.cursor_left() -} + /// Move the cursor left one block. + pub fn move_cursor_left(tab: &mut Tab) { + tab.input.cursor_left() + } -/// Move the cursor to the right in input string. -pub fn helper_move_cursor_right(tab: &mut Tab) { - tab.input.cursor_right() -} + /// Move the cursor to the right in input string. + pub fn move_cursor_right(tab: &mut Tab) { + tab.input.cursor_right() + } -/// Delete the char to the left in input string. -pub fn helper_delete_char_left(tab: &mut Tab) { - tab.input.delete_char_left() -} + /// Delete the char to the left in input string. + pub fn delete_char_left(tab: &mut Tab) { + tab.input.delete_char_left() + } -/// Delete all chars right of the cursor in input string. -pub fn helper_delete_chars_right(tab: &mut Tab) { - tab.input.delete_chars_right() -} + /// Delete all chars right of the cursor in input string. + pub fn delete_chars_right(tab: &mut Tab) { + tab.input.delete_chars_right() + } -/// Add a char to input string, look for a possible completion. -pub fn helper_text_insert_and_complete(tab: &mut Tab, c: char) -> Result<()> { - helper_text_insertion(tab, c); - tab.fill_completion() -} + /// Add a char to input string, look for a possible completion. + pub fn text_insert_and_complete(tab: &mut Tab, c: char) -> Result<()> { + Helper::text_insertion(tab, c); + tab.fill_completion() + } -/// Insert a char in the input string. -pub fn helper_text_insertion(tab: &mut Tab, c: char) { - tab.input.insert(c); -} + /// Insert a char in the input string. + pub fn text_insertion(tab: &mut Tab, c: char) { + tab.input.insert(c); + } -/// Fold every child node in the tree. -/// Recursively explore the tree and fold every node. Reset the display. -pub fn helper_tree_go_to_root(tab: &mut Tab, colors: &Colors) -> Result<()> { - tab.directory.tree.reset_required_height(); - tab.tree_select_root(colors) -} + /// Fold every child node in the tree. + /// Recursively explore the tree and fold every node. Reset the display. + pub fn tree_go_to_root(tab: &mut Tab, colors: &Colors) -> Result<()> { + tab.directory.tree.reset_required_height(); + tab.tree_select_root(colors) + } -/// Select the first child of the current node and reset the display. -pub fn helper_select_first_child(tab: &mut Tab, colors: &Colors) -> Result<()> { - tab.directory.tree.increase_required_height(); - tab.tree_select_first_child(colors) -} + /// Select the first child of the current node and reset the display. + pub fn select_first_child(tab: &mut Tab, colors: &Colors) -> Result<()> { + tab.directory.tree.increase_required_height(); + tab.tree_select_first_child(colors) + } -/// Select the parent of the current node and reset the display. -/// Move to the parent and reset the tree if we were in the root node. -pub fn helper_select_parent(tab: &mut Tab, colors: &Colors) -> Result<()> { - tab.tree_select_parent(colors) -} + /// Select the parent of the current node and reset the display. + /// Move to the parent and reset the tree if we were in the root node. + pub fn select_parent(tab: &mut Tab, colors: &Colors) -> Result<()> { + tab.tree_select_parent(colors) + } -/// Select the next sibling of the current node. -pub fn helper_select_next(tab: &mut Tab, colors: &Colors) -> Result<()> { - tab.directory.tree.increase_required_height(); - tab.tree_select_next(colors) -} + /// Select the next sibling of the current node. + pub fn select_next(tab: &mut Tab, colors: &Colors) -> Result<()> { + tab.directory.tree.increase_required_height(); + tab.tree_select_next(colors) + } -/// Select the previous sibling of the current node. -pub fn helper_select_prev(tab: &mut Tab, colors: &Colors) -> Result<()> { - tab.directory.tree.decrease_required_height(); - tab.tree_select_prev(colors) -} + /// Select the previous sibling of the current node. + pub fn select_prev(tab: &mut Tab, colors: &Colors) -> Result<()> { + tab.directory.tree.decrease_required_height(); + tab.tree_select_prev(colors) + } -/// Move up 10 lines in the tree -pub fn helper_tree_page_up(tab: &mut Tab, colors: &Colors) -> Result<()> { - tab.directory.tree.decrease_required_height_by_ten(); - tab.tree_page_up(colors) -} + /// Move up 10 lines in the tree + pub fn tree_page_up(tab: &mut Tab, colors: &Colors) -> Result<()> { + tab.directory.tree.decrease_required_height_by_ten(); + tab.tree_page_up(colors) + } -/// Move down 10 lines in the tree -pub fn helper_tree_page_down(tab: &mut Tab, colors: |