1 //! declare the internal functions which may be used in verbs.
2 //! They don't take any user argument other than the selection
3 //! (this may change if the needs arise).
4 //! They can be called as ":some_name" from builtin verbs and
5 //! from configured verbs.
6 
7 use {
8     crate::errors::ConfError,
9 };
10 
11 macro_rules! Internals {
12     (
13         $($name:ident: $description:literal $need_path:literal,)*
14     ) => {
15         #[derive(Debug, Clone, Copy, PartialEq)]
16         #[allow(non_camel_case_types)]
17         pub enum Internal {
18             $($name,)*
19         }
20         impl Internal {
21             pub fn try_from(verb: &str) -> Result<Internal, ConfError> {
22                 use Internal::*;
23                 match verb {
24                     $(stringify!($name) => Ok($name),)*
25                     _ => Err(ConfError::UnknownInternal{ verb: verb.to_string() }),
26                 }
27             }
28         }
29         impl Internal {
30             pub fn name(self) -> &'static str {
31                 use Internal::*;
32                 match self {
33                     $($name => stringify!($name),)*
34                 }
35             }
36             pub fn description(self) -> &'static str {
37                 use Internal::*;
38                 match self {
39                     $($name => $description,)*
40                 }
41             }
42             pub fn need_path(self) -> bool {
43                 use Internal::*;
44                 match self {
45                     $($name => $need_path,)*
46                 }
47             }
48         }
49     }
50 }
51 
52 
53 // internals:
54 //  name: "description" needs_a_path
55 Internals! {
56     back: "revert to the previous state (mapped to *esc*)" false,
57     close_panel_ok: "close the panel, validating the selected path" false,
58     close_panel_cancel: "close the panel, not using the selected path" false,
59     copy_line: "copy selected line (in tree or preview)" true,
60     copy_path: "copy path to system clipboard" true,
61     filesystems: "list mounted filesystems" false,
62     focus: "display the directory (mapped to *enter*)" true,
63     help: "display broot's help" false,
64     input_clear: "empty the input" false,
65     input_del_char_left: "delete the char left of the cursor" false,
66     input_del_char_below: "delete the char left at the cursor's position" false,
67     input_del_word_left: "delete the word left of the cursor" false,
68     input_del_word_right: "delete the word right of the cursor" false,
69     input_go_to_end: "move the cursor to the end of input" false,
70     input_go_left: "move the cursor to the left" false,
71     input_go_right: "move the cursor to the right" false,
72     input_go_to_start: "move the cursor to the start of input" false,
73     input_go_word_left: "move the cursor one word to the left" false,
74     input_go_word_right: "move the cursor one word to the right" false,
75     input_selection_copy: "copy the selected part of the input into the selection" false,
76     input_selection_cut: "cut the selected part of the input into the selection" false,
77     input_paste: "paste the clipboard content into the input" false,
78     line_down: "move one line down" false,
79     line_up: "move one line up" false,
80     line_down_no_cycle: "move one line down" false,
81     line_up_no_cycle: "move one line up" false,
82     open_stay: "open file or directory according to OS (stay in broot)" true,
83     open_stay_filter: "display the directory, keeping the current pattern" true,
84     open_leave: "open file or directory according to OS (quit broot)" true,
85     mode_input: "enter the input mode" false,
86     mode_command: "enter the command mode" false,
87     next_match: "select the next match" false,
88     next_same_depth: "select the next file at the same depth" false,
89     no_sort: "don't sort" false,
90     page_down: "scroll one page down" false,
91     page_up: "scroll one page up" false,
92     parent: "move to the parent directory" false,
93     panel_left: "focus panel on left" false,
94     panel_right: "focus panel on right" false,
95     previous_match: "select the previous match" false,
96     previous_same_depth: "select the previous file at the same depth" false,
97     open_preview: "open the preview panel" true,
98     close_preview: "close the preview panel" false,
99     toggle_preview: "open/close the preview panel" false,
100     preview_image: "preview the selection as image" true,
101     preview_text: "preview the selection as text" true,
102     preview_binary: "preview the selection as binary" true,
103     print_path: "print path and leaves broot" true,
104     print_relative_path: "print relative path and leaves broot" true,
105     print_tree: "print tree and leaves broot" true,
106     start_end_panel: "either open or close an additional panel" true,
107     quit: "quit Broot" false,
108     refresh: "refresh tree and clear size cache" false,
109     root_up: "move tree root up" true,
110     root_down: "move tree root down" true,
111     //restore_pattern: "restore a pattern which was just removed" false,
112     select_first: "select the first item" false,
113     select_last: "select the last item" false,
114     sort_by_count: "sort by count" false,
115     sort_by_date: "sort by date" false,
116     sort_by_size: "sort by size" false,
117     clear_stage: "empty the staging area" false,
118     stage: "add selection to staging area" true,
119     unstage: "remove selection from staging area" true,
120     open_staging_area: "open the staging area" false,
121     close_staging_area: "close the staging area panel" false,
122     toggle_staging_area: "open/close the staging area panel" false,
123     toggle_stage: "add or remove selection to staging area" true,
124     toggle_counts: "toggle showing number of files in directories" false,
125     toggle_dates: "toggle showing last modified dates" false,
126     toggle_device_id: "toggle showing device id" false,
127     toggle_files: "toggle showing files (or just folders)" false,
128     toggle_git_ignore: "toggle use of .gitignore" false,
129     toggle_git_file_info: "toggle display of git file information" false,
130     toggle_git_status: "toggle showing only files relevant for git status" false,
131     toggle_root_fs: "toggle showing filesystem info on top" false,
132     toggle_hidden: "toggle showing hidden files" false,
133     toggle_perm: "toggle showing file permissions" false,
134     toggle_sizes: "toggle showing sizes" false,
135     toggle_trim_root: "toggle removing nodes at first level too" false,
136     toggle_second_tree: "toggle display of a second tree panel" true,
137     total_search: "search again but on all children" false,
138     up_tree: "focus the parent of the current root" true,
139 }
140 
141 impl Internal {
invocation_pattern(self) -> &'static str142     pub fn invocation_pattern(self) -> &'static str {
143         match self {
144             Internal::focus => r"focus (?P<path>.*)?",
145             Internal::line_down => r"line_down (?P<count>\d*)?",
146             Internal::line_up => r"line_up (?P<count>\d*)?",
147             Internal::line_down_no_cycle => r"line_down_no_cycle (?P<count>\d*)?",
148             Internal::line_up_no_cycle => r"line_up_no_cycle (?P<count>\d*)?",
149             _ => self.name(),
150         }
151     }
exec_pattern(self) -> &'static str152     pub fn exec_pattern(self) -> &'static str {
153         match self {
154             Internal::focus => r"focus {path}",
155             Internal::line_down => r"line_down {count}",
156             Internal::line_up => r"line_up {count}",
157             Internal::line_down_no_cycle => r"line_down_no_cycle {count}",
158             Internal::line_up_no_cycle => r"line_up_no_cycle {count}",
159             _ => self.name(),
160         }
161     }
needs_selection(self, arg: &Option<String>) -> bool162     pub fn needs_selection(self, arg: &Option<String>) -> bool {
163         match self {
164             Internal::focus => arg.is_none(),
165             _ => self.need_path(),
166         }
167     }
168 }
169