fm/event/
action_map.rs

1use anyhow::Result;
2use strum::IntoEnumIterator;
3use strum_macros::{Display, EnumIter, EnumString};
4
5use crate::app::Status;
6use crate::config::Bindings;
7use crate::event::EventAction;
8
9/// Different kind of action which can be mapped to a key.
10/// All those actions are mapped to a key and this enum
11/// makes the junction between received Key events and
12/// actions in the application.
13#[derive(Clone, Debug, Display, EnumString, EnumIter)]
14pub enum ActionMap {
15    Action,
16    Back,
17    Backspace,
18    Bulk,
19    Cd,
20    Chmod,
21    ClearFlags,
22    CliMenu,
23    CloudDrive,
24    Compress,
25    Context,
26    CopyContent,
27    CopyFilename,
28    CopyFilepath,
29    CopyPaste,
30    CutPaste,
31    Delete,
32    DeleteLeft,
33    DeleteLine,
34    DisplayFlagged,
35    End,
36    Enter,
37    Exec,
38    Filter,
39    FlagAll,
40    FlaggedToClipboard,
41    FlaggedFromClipboard,
42    FocusGoLeft,
43    FocusGoRight,
44    FocusGoDown,
45    FocusGoUp,
46    FuzzyFind,
47    FuzzyFindHelp,
48    FuzzyFindLine,
49    GoRoot,
50    GoStart,
51    Help,
52    History,
53    Home,
54    KeyHome,
55    Log,
56    MarksJump,
57    MarksNew,
58    MoveDown,
59    MoveLeft,
60    MoveRight,
61    MoveUp,
62    Mount,
63    NextThing,
64    NextWord,
65    NewDir,
66    NewFile,
67    Nothing,
68    NvimFilepicker,
69    NvimSetAddress,
70    OpenConfig,
71    OpenFile,
72    OpenAll,
73    PageDown,
74    PageUp,
75    Preview,
76    PreviousThing,
77    PreviousWord,
78    Quit,
79    RefreshIfNeeded,
80    RefreshView,
81    RegexMatch,
82    RemoteMount,
83    Rename,
84    ResetMode,
85    ReverseFlags,
86    Search,
87    SearchNext,
88    Shell,
89    ShellCommand,
90    TempMarksJump,
91    TempMarksNew,
92    TuiMenu,
93    Shortcut,
94    Sort,
95    Symlink,
96    SyncLTR,
97    Tab,
98    ToggleDisplayFull,
99    ToggleDualPane,
100    ToggleFlag,
101    ToggleFlagChildren,
102    ToggleHidden,
103    TogglePreviewSecond,
104    TrashEmpty,
105    TrashMoveFile,
106    TrashOpen,
107    TrashRestoreFile,
108    Tree,
109    TreeDepthDecr,
110    TreeDepthIncr,
111    TreeFold,
112    TreeFoldAll,
113    TreeUnFoldAll,
114    ToggleVisual,
115    Custom(String),
116}
117
118impl ActionMap {
119    /// Makes the junction between `Actions` and `Events`.
120    /// Every Action links to a different `EventExec` method.
121    pub fn matcher(&self, status: &mut Status, binds: &Bindings) -> Result<()> {
122        match self {
123            Self::Action => EventAction::action(status),
124            Self::Back => EventAction::back(status),
125            Self::Backspace => EventAction::backspace(status),
126            Self::Bulk => EventAction::bulk(status),
127            Self::Cd => EventAction::cd(status),
128            Self::Chmod => EventAction::chmod(status),
129            Self::ClearFlags => EventAction::clear_flags(status),
130            Self::CliMenu => EventAction::cli_menu(status),
131            Self::CloudDrive => EventAction::cloud_drive(status),
132            Self::Compress => EventAction::compress(status),
133            Self::Context => EventAction::context(status),
134            Self::CopyContent => EventAction::copy_content(status),
135            Self::CopyFilename => EventAction::copy_filename(status),
136            Self::CopyFilepath => EventAction::copy_filepath(status),
137            Self::CopyPaste => EventAction::copy_paste(status),
138            Self::CutPaste => EventAction::cut_paste(status),
139            Self::Delete => EventAction::delete(status),
140            Self::DeleteLeft => EventAction::delete_left(status),
141            Self::DeleteLine => EventAction::delete_line(status),
142            Self::DisplayFlagged => EventAction::display_flagged(status),
143            Self::End => EventAction::end(status),
144            Self::Enter => EventAction::enter(status, binds),
145            Self::Exec => EventAction::exec(status),
146            Self::Filter => EventAction::filter(status),
147            Self::FlagAll => EventAction::flag_all(status),
148            Self::FlaggedToClipboard => EventAction::flagged_to_clipboard(status),
149            Self::FlaggedFromClipboard => EventAction::flagged_from_clipboard(status),
150            Self::FocusGoLeft => EventAction::focus_go_left(status),
151            Self::FocusGoRight => EventAction::focus_go_right(status),
152            Self::FocusGoDown => EventAction::focus_go_down(status),
153            Self::FocusGoUp => EventAction::focus_go_up(status),
154            Self::FuzzyFind => EventAction::fuzzyfind(status),
155            Self::FuzzyFindHelp => EventAction::fuzzyfind_help(status, binds),
156            Self::FuzzyFindLine => EventAction::fuzzyfind_line(status),
157            Self::GoRoot => EventAction::go_root(status),
158            Self::GoStart => EventAction::go_start(status),
159            Self::Help => EventAction::help(status, binds),
160            Self::History => EventAction::history(status),
161            Self::Home => EventAction::home(status),
162            Self::KeyHome => EventAction::key_home(status),
163            Self::Log => EventAction::log(status),
164            Self::MarksJump => EventAction::marks_jump(status),
165            Self::MarksNew => EventAction::marks_new(status),
166            Self::Mount => EventAction::mount(status),
167            Self::MoveDown => EventAction::move_down(status),
168            Self::MoveLeft => EventAction::move_left(status),
169            Self::MoveRight => EventAction::move_right(status),
170            Self::MoveUp => EventAction::move_up(status),
171            Self::NextThing => EventAction::next_thing(status),
172            Self::NextWord => EventAction::next_word(status),
173            Self::NewDir => EventAction::new_dir(status),
174            Self::NewFile => EventAction::new_file(status),
175            Self::NvimFilepicker => EventAction::nvim_filepicker(status),
176            Self::NvimSetAddress => EventAction::set_nvim_server(status),
177            Self::OpenConfig => EventAction::open_config(status),
178            Self::OpenFile => EventAction::open_file(status),
179            Self::OpenAll => EventAction::open_all(status),
180            Self::PageDown => EventAction::page_down(status),
181            Self::PageUp => EventAction::page_up(status),
182            Self::Preview => EventAction::preview(status),
183            Self::PreviousThing => EventAction::previous_thing(status),
184            Self::PreviousWord => EventAction::previous_word(status),
185            Self::Quit => EventAction::quit(status),
186            Self::RefreshIfNeeded => EventAction::refresh_if_needed(status),
187            Self::RefreshView => EventAction::refresh_view(status),
188            Self::RegexMatch => EventAction::regex_match(status),
189            Self::RemoteMount => EventAction::remote_mount(status),
190            Self::Rename => EventAction::rename(status),
191            Self::ResetMode => EventAction::reset_mode(status),
192            Self::ReverseFlags => EventAction::reverse_flags(status),
193            Self::Search => EventAction::search(status),
194            Self::SearchNext => EventAction::search_next(status),
195            Self::Shell => EventAction::shell(status),
196            Self::ShellCommand => EventAction::shell_command(status),
197            Self::Shortcut => EventAction::shortcut(status),
198            Self::Sort => EventAction::sort(status),
199            Self::Symlink => EventAction::symlink(status),
200            Self::SyncLTR => EventAction::sync_ltr(status),
201            Self::Tab => EventAction::tab(status),
202            Self::TempMarksJump => EventAction::temp_marks_jump(status),
203            Self::TempMarksNew => EventAction::temp_marks_new(status),
204            Self::ToggleDisplayFull => EventAction::toggle_display_full(status),
205            Self::ToggleDualPane => EventAction::toggle_dualpane(status),
206            Self::ToggleFlag => EventAction::toggle_flag(status),
207            Self::ToggleFlagChildren => EventAction::toggle_flag_children(status),
208            Self::ToggleHidden => EventAction::toggle_hidden(status),
209            Self::TogglePreviewSecond => EventAction::toggle_preview_second(status),
210            Self::TrashEmpty => EventAction::trash_empty(status),
211            Self::TrashMoveFile => EventAction::trash_move_file(status),
212            Self::TrashOpen => EventAction::trash_open(status),
213            Self::TrashRestoreFile => EventAction::trash_restore(status),
214            Self::Tree => EventAction::tree(status),
215            Self::TreeDepthDecr => EventAction::tree_depth_decr(status),
216            Self::TreeDepthIncr => EventAction::tree_depth_incr(status),
217            Self::TreeFold => EventAction::tree_fold(status),
218            Self::TreeFoldAll => EventAction::tree_fold_all(status),
219            Self::TreeUnFoldAll => EventAction::tree_unfold_all(status),
220            Self::TuiMenu => EventAction::tui_menu(status),
221            Self::ToggleVisual => EventAction::visual(status),
222
223            Self::Custom(string) => EventAction::custom(status, string),
224
225            Self::Nothing => Ok(()),
226        }
227    }
228
229    pub fn description(&self) -> &'static str {
230        match self {
231            Self::Action => "ACTION",
232            Self::Back => "move back to previous dir",
233            Self::Backspace => "delete previous char",
234            Self::Bulk => "BULK",
235            Self::Cd => "CD",
236            Self::Chmod => "CHMOD ",
237            Self::ClearFlags => "clear flags",
238            Self::CliMenu => "CLI APPS",
239            Self::Compress => "compress into an archive",
240            Self::Context => "CONTEXT",
241            Self::CopyContent => "copy text file content to clipbloard",
242            Self::CopyFilename => "copy filename to clipboard",
243            Self::CopyFilepath => "copy filepath to clipboard",
244            Self::CopyPaste => "copy to current dir",
245            Self::CloudDrive => "navigate into a cloud drive",
246            Self::Custom(_) => "custom command",
247            Self::CutPaste => "move to current dir",
248            Self::Delete => "delete files permanently",
249            Self::DeleteLeft => "delete a word to the left",
250            Self::DeleteLine => "delete the whole line / Sync left tab from right tab",
251            Self::DisplayFlagged => "FLAGGED",
252            Self::End => "go to last line",
253            Self::Enter => "Execute mode then NORMAL",
254            Self::Exec => "OPEN WITH ",
255            Self::Filter => "FILTER ",
256            Self::FlagAll => "flag all",
257            Self::FlaggedFromClipboard => "flag existing files from primary clipboard",
258            Self::FlaggedToClipboard => "copy flagged files to primary clipbloard",
259            Self::FocusGoDown => "move focus to bottom",
260            Self::FocusGoLeft => "move focus to left",
261            Self::FocusGoRight => "move focus to right",
262            Self::FocusGoUp => "move focus to up",
263            Self::FuzzyFind => "fuzzy finder for file",
264            Self::FuzzyFindHelp => "fuzzy finder from help",
265            Self::FuzzyFindLine => "fuzzy finder for line",
266            Self::GoRoot => "move to root (/)",
267            Self::GoStart => "move to starting point",
268            Self::Help => "help",
269            Self::History => "HISTORY",
270            Self::Home => "move to $HOME",
271            Self::KeyHome => "go to first line",
272            Self::Log => "open the logs",
273            Self::MarksJump => "MARKS: Jump",
274            Self::MarksNew => "MARKS: Save",
275            Self::Mount => "MOUNTS",
276            Self::MoveDown => "one line down",
277            Self::MoveLeft => "cd to parent directory ",
278            Self::MoveRight => "cd to child directory",
279            Self::MoveUp => "one line up  ",
280            Self::NewDir => "NEWDIR ",
281            Self::NewFile => "NEWFILE",
282            Self::NextThing => "select next 'thing'",
283            Self::NextWord => "go to next word in input",
284            Self::Nothing => "do nothing",
285            Self::NvimFilepicker => "open in current nvim session",
286            Self::NvimSetAddress => "setup the nvim rpc address",
287            Self::OpenAll => "open all flagged files",
288            Self::OpenConfig => "open the config file",
289            Self::OpenFile => {
290                "open the selected file with :
291    - default       Self::Default
292    - audio         Self::Audio
293    - images        Self::Bitmap
294    - office        Self::Office
295    - pdf, ebooks   Self::Readable
296    - text          Self::Text
297    - video         Self::Video
298    - vectorials    Self::Vectorial
299    - compressed files are decompressed
300    - iso images are mounted"
301            }
302            Self::PageDown => "10 lines down",
303            Self::PageUp => "10 lines up",
304            Self::Preview => "preview this file",
305            Self::PreviousThing => "select previous 'thing'",
306            Self::PreviousWord => "go to previous word in input",
307            Self::Quit => "quit",
308            Self::RefreshIfNeeded => "refresh the terminal if we have to",
309            Self::RefreshView => "refresh view",
310            Self::RegexMatch => "REGEXMATCH",
311            Self::RemoteMount => "MOUNT REMOTE PATH",
312            Self::Rename => "RENAME",
313            Self::ResetMode => "NORMAL",
314            Self::ReverseFlags => "reverse flags",
315            Self::Search => "SEARCH",
316            Self::SearchNext => "search next matching element",
317            Self::Shell => "shell in current directory",
318            Self::ShellCommand => "run a shell command",
319            Self::Shortcut => "SHORTCUT",
320            Self::Sort => "SORT",
321            Self::Symlink => "symlink to current dir",
322            Self::SyncLTR => "Sync right tab from left tab path",
323            Self::TempMarksJump => "TEMP MARKS: Jump",
324            Self::TempMarksNew => "TEMP MARKS: Save",
325            Self::Tab => "cycle tab",
326            Self::ToggleDisplayFull => "toggle full metadata display of files",
327            Self::ToggleDualPane => "toggle dual pane - if the width is sufficiant",
328            Self::ToggleFlag => "toggle flag on a file",
329            Self::ToggleFlagChildren => "toggle flag on every children file of a directory",
330            Self::ToggleHidden => "toggle hidden",
331            Self::TogglePreviewSecond => "toggle a preview on the second pane",
332            Self::TrashEmpty => "Empty the trash",
333            Self::TrashMoveFile => "move to trash",
334            Self::TrashOpen => "Open the trash (enter to restore, del clear)",
335            Self::TrashRestoreFile => "restore the trash file",
336            Self::Tree => "Toggle tree mode",
337            Self::TreeDepthDecr => "Decrease depth of tree",
338            Self::TreeDepthIncr => "Increase depth of tree",
339            Self::TreeFold => "Fold a node",
340            Self::TreeFoldAll => "Fold every node",
341            Self::TreeUnFoldAll => "Unfold every node",
342            Self::TuiMenu => "TUI APPS",
343            Self::ToggleVisual => "Visual selection",
344        }
345    }
346
347    pub fn actions_matching(key: String) -> Vec<String> {
348        Self::iter()
349            .filter(|action| action.to_string().to_lowercase().contains(&key))
350            .map(|action| action.to_string())
351            .collect()
352    }
353}