diff options
author | Canop <cano.petrole@gmail.com> | 2020-09-22 18:56:47 +0200 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2020-09-22 18:56:47 +0200 |
commit | 60b742fceda175bd1b357280535b27ba5e915d94 (patch) | |
tree | 30c48e578a42db9428b82effaead61be4ce87af1 /src/verb/builtin.rs | |
parent | 5edab5fd73ac38df12ff4ce7db21091f6bd8acbb (diff) |
refactor: move the invocation pattern from execution to verb
Diffstat (limited to 'src/verb/builtin.rs')
-rw-r--r-- | src/verb/builtin.rs | 185 |
1 files changed, 119 insertions, 66 deletions
diff --git a/src/verb/builtin.rs b/src/verb/builtin.rs index 90375ed..0c7092f 100644 --- a/src/verb/builtin.rs +++ b/src/verb/builtin.rs @@ -1,132 +1,185 @@ use { - super::Verb, - crate::keys::*, + super::*, + crate::{ + app::SelectionType, + keys::*, + }, crossterm::event::{KeyCode, KeyEvent, KeyModifiers}, }; +fn build_internal( + internal: Internal, + bang: bool, +) -> Verb { + let invocation = internal.invocation_pattern(); + let execution = VerbExecution::Internal( + InternalExecution::from_internal_bang(internal, bang) + ); + let description = VerbDescription::from_text(internal.description().to_string()); + Verb::new(Some(invocation), execution, description).unwrap() +} + +fn internal( + internal: Internal, +) -> Verb { + build_internal(internal, false) +} + +fn internal_bang( + internal: Internal, +) -> Verb { + build_internal(internal, true) +} + +fn external( + invocation_str: &str, + execution_str: &str, + exec_mode: ExternalExecutionMode, +) -> Verb { + let execution = VerbExecution::External(ExternalExecution::new( + execution_str, + exec_mode, + )); + Verb::new( + Some(invocation_str), + execution, + VerbDescription::from_code(execution_str.to_string()), + ).unwrap() +} + /// declare the built_in verbs, the ones which are available /// in standard (they still may be overriden by configuration) pub fn builtin_verbs() -> Vec<Verb> { use super::{ExternalExecutionMode::*, Internal::*}; vec![ - Verb::internal(back), - Verb::from(super::cd::CD.clone()) - .with_description("change directory and quit (mapped to *alt*-*enter*)"), + internal(back), + + // those two operations are mapped on ALT-ENTER, one + // for directories and the other one for the other files + external( + "cd", + "cd {directory}", + FromParentShell, + ) + .with_stype(SelectionType::Directory) + .with_key(ALT_ENTER) + .with_description("change directory and quit"), + internal(open_leave) // calls the system open + .with_key(ALT_ENTER) + .with_shortcut("ol"), + #[cfg(unix)] - Verb::external( + external( "chmod {args}", "chmod {args} {file}", StayInBroot, - ).unwrap(), - Verb::internal(open_preview), - Verb::internal(close_preview), - Verb::internal(toggle_preview), - Verb::internal(preview_image), - Verb::internal(preview_text), - Verb::internal(preview_binary), - Verb::internal(close_panel_ok), - Verb::internal(close_panel_cancel) + ), + internal(open_preview), + internal(close_preview), + internal(toggle_preview), + internal(preview_image), + internal(preview_text), + internal(preview_binary), + internal(close_panel_ok), + internal(close_panel_cancel) .with_key(BACK_TAB) .with_control_key('w'), - Verb::external( + external( "copy {newpath:path-from-parent}", "/bin/cp -r {file} {newpath:path-from-parent}", StayInBroot, - ).unwrap() + ) .with_shortcut("cp"), #[cfg(feature="clipboard")] - Verb::internal(copy_path) + internal(copy_path) .with_alt_key('c'), - Verb::external( + external( "copy_to_panel", "/bin/cp -r {file} {other-panel-directory}", StayInBroot, - ).unwrap() + ) .with_shortcut("cpp"), // :focus is also hardcoded on Enter on directories // but ctrl-f is useful for focusing on a file's parent // (and keep the filter) - Verb::internal(focus) + internal(focus) .with_control_key('f'), - Verb::internal(help).with_key(F1).with_shortcut("?"), + internal(help).with_key(F1).with_shortcut("?"), #[cfg(feature="clipboard")] - Verb::internal(input_paste) + internal(input_paste) .with_control_key('v'), - Verb::internal(line_down).with_key(DOWN), - Verb::internal(line_up).with_key(UP), - Verb::external( + internal(line_down).with_key(DOWN), + internal(line_up).with_key(UP), + external( "mkdir {subpath}", "/bin/mkdir -p {subpath:path-from-directory}", StayInBroot, - ).unwrap() + ) .with_shortcut("md"), - Verb::external( + external( "move {newpath:path-from-parent}", "/bin/mv {file} {newpath:path-from-parent}", StayInBroot, - ).unwrap() + ) .with_shortcut("mv"), - Verb::external( + external( "move_to_panel", "/bin/mv {file} {other-panel-directory}", StayInBroot, - ).unwrap() + ) .with_shortcut("mvp"), - Verb::internal_bang(start_end_panel) + internal_bang(start_end_panel) .with_control_key('p'), - Verb::internal(next_match) + internal(next_match) .with_key(TAB), - Verb::internal(no_sort) + internal(no_sort) .with_shortcut("ns"), - Verb::internal(open_stay) + internal(open_stay) .with_key(ENTER) .with_shortcut("os"), - Verb::internal(open_stay_filter) + internal(open_stay_filter) .with_shortcut("osf"), - Verb::internal(open_leave) - .with_key(ALT_ENTER) - .with_shortcut("ol"), - Verb::internal(parent).with_shortcut("p"), - Verb::internal(page_down).with_key(PAGE_DOWN), - Verb::internal(page_up).with_key(PAGE_UP), - Verb::internal(panel_left) + internal(parent).with_shortcut("p"), + internal(page_down).with_key(PAGE_DOWN), + internal(page_up).with_key(PAGE_UP), + internal(panel_left) .with_key(KeyEvent { code: KeyCode::Left, modifiers: KeyModifiers::CONTROL, }), - Verb::internal(panel_right) + internal(panel_right) .with_key(KeyEvent { code: KeyCode::Right, modifiers: KeyModifiers::CONTROL, }), - Verb::internal(print_path).with_shortcut("pp"), - Verb::internal(print_relative_path).with_shortcut("prp"), - Verb::internal(print_tree).with_shortcut("pt"), - Verb::internal(quit) + internal(print_path).with_shortcut("pp"), + internal(print_relative_path).with_shortcut("prp"), + internal(print_tree).with_shortcut("pt"), + internal(quit) .with_control_key('c') .with_control_key('q') .with_shortcut("q"), - Verb::internal(refresh).with_key(F5), - Verb::internal(sort_by_count).with_shortcut("sc"), - Verb::internal(sort_by_date).with_shortcut("sd"), - Verb::internal(sort_by_size).with_shortcut("ss"), - Verb::external( + internal(refresh).with_key(F5), + internal(sort_by_count).with_shortcut("sc"), + internal(sort_by_date).with_shortcut("sd"), + internal(sort_by_size).with_shortcut("ss"), + external( "rm", "/bin/rm -rf {file}", StayInBroot, - ).unwrap(), - Verb::internal(toggle_counts).with_shortcut("counts"), - Verb::internal(toggle_dates).with_shortcut("dates"), - Verb::internal(toggle_files).with_shortcut("files"), - Verb::internal(toggle_git_ignore).with_shortcut("gi"), - Verb::internal(toggle_git_file_info).with_shortcut("gf"), - Verb::internal(toggle_git_status).with_shortcut("gs"), - Verb::internal(toggle_hidden).with_shortcut("h"), + ), + internal(toggle_counts).with_shortcut("counts"), + internal(toggle_dates).with_shortcut("dates"), + internal(toggle_files).with_shortcut("files"), + internal(toggle_git_ignore).with_shortcut("gi"), + internal(toggle_git_file_info).with_shortcut("gf"), + internal(toggle_git_status).with_shortcut("gs"), + internal(toggle_hidden).with_shortcut("h"), #[cfg(unix)] - Verb::internal(toggle_perm).with_shortcut("perm"), - Verb::internal(toggle_sizes).with_shortcut("sizes"), - Verb::internal(toggle_trim_root), - Verb::internal(total_search).with_control_key('s'), - Verb::internal(up_tree).with_shortcut("up"), + internal(toggle_perm).with_shortcut("perm"), + internal(toggle_sizes).with_shortcut("sizes"), + internal(toggle_trim_root), + internal(total_search).with_control_key('s'), + internal(up_tree).with_shortcut("up"), ] } |