Skip to main content

oxinav_core/
command.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub enum AppCommand {
3    SwitchDarkLightMode,
4    ToggleHiddenFiles,
5    CreateDirectory,
6    CreateFile,
7    CreateSymlink,
8    ExtractArchive,
9    JumpToPath,
10    HistoryBack,
11    HistoryForward,
12    SaveBookmark,
13    GoToBookmark,
14    ShowShortcuts,
15    CopyToOtherPane,
16    MoveToOtherPane,
17    Delete,
18    Rename,
19    ViewFile,
20    FilterFiles,
21    ClearFilter,
22    SortByName,
23    SortBySize,
24    SortByModified,
25}
26
27impl AppCommand {
28    pub const fn all() -> &'static [Self] {
29        &[
30            Self::SwitchDarkLightMode,
31            Self::ToggleHiddenFiles,
32            Self::CreateDirectory,
33            Self::CreateFile,
34            Self::CreateSymlink,
35            Self::ExtractArchive,
36            Self::JumpToPath,
37            Self::HistoryBack,
38            Self::HistoryForward,
39            Self::SaveBookmark,
40            Self::GoToBookmark,
41            Self::ShowShortcuts,
42            Self::CopyToOtherPane,
43            Self::MoveToOtherPane,
44            Self::Delete,
45            Self::Rename,
46            Self::ViewFile,
47            Self::FilterFiles,
48            Self::ClearFilter,
49            Self::SortByName,
50            Self::SortBySize,
51            Self::SortByModified,
52        ]
53    }
54
55    pub const fn title(self) -> &'static str {
56        match self {
57            Self::SwitchDarkLightMode => "switch dark/light mode",
58            Self::ToggleHiddenFiles => "show/hide hidden files",
59            Self::CreateDirectory => "create directory",
60            Self::CreateFile => "create file",
61            Self::CreateSymlink => "create symlink",
62            Self::ExtractArchive => "extract archive",
63            Self::JumpToPath => "go to path",
64            Self::HistoryBack => "back",
65            Self::HistoryForward => "forward",
66            Self::SaveBookmark => "save bookmark",
67            Self::GoToBookmark => "go to bookmark",
68            Self::ShowShortcuts => "show keyboard shortcuts",
69            Self::CopyToOtherPane => "copy to other pane",
70            Self::MoveToOtherPane => "move to other pane",
71            Self::Delete => "delete",
72            Self::Rename => "rename",
73            Self::ViewFile => "view file",
74            Self::FilterFiles => "filter files",
75            Self::ClearFilter => "clear filter",
76            Self::SortByName => "sort by name",
77            Self::SortBySize => "sort by size",
78            Self::SortByModified => "sort by modified",
79        }
80    }
81
82    pub const fn description(self) -> &'static str {
83        match self {
84            Self::SwitchDarkLightMode => "Toggle the application theme",
85            Self::ToggleHiddenFiles => "Toggle visibility of dot-prefixed files",
86            Self::CreateDirectory => "Create a directory in the active pane (mkdir)",
87            Self::CreateFile => "Create an empty file in the active pane (touch)",
88            Self::CreateSymlink => {
89                "Create a symlink in the other pane pointing at the selected entry"
90            }
91            Self::ExtractArchive => "Extract the selected zip or tar/tar.gz into the active pane",
92            Self::JumpToPath => "Move the active pane to a typed path (~ expands to home)",
93            Self::HistoryBack => "Return to the previously visited location",
94            Self::HistoryForward => "Go forward again after going back",
95            Self::SaveBookmark => "Save the active pane's location under a name",
96            Self::GoToBookmark => "Move the active pane to a saved bookmark",
97            Self::ShowShortcuts => "List every keyboard shortcut the app responds to",
98            Self::CopyToOtherPane => "Copy selected item into the other pane",
99            Self::MoveToOtherPane => "Move selected item into the other pane",
100            Self::Delete => "Delete the selected item (permanent)",
101            Self::Rename => "Rename the selected item",
102            Self::ViewFile => "View the selected file read-only, without leaving the app",
103            Self::FilterFiles => "Restrict the active pane to names matching a pattern (e.g. *.rs)",
104            Self::ClearFilter => "Remove the active pane's name filter",
105            Self::SortByName => "Sort the active pane by name; invoke again to reverse direction",
106            Self::SortBySize => "Sort the active pane by size; invoke again to reverse direction",
107            Self::SortByModified => {
108                "Sort the active pane by modified time; invoke again to reverse direction"
109            }
110        }
111    }
112
113    pub const fn shortcut(self) -> Option<&'static str> {
114        match self {
115            Self::SwitchDarkLightMode => None,
116            Self::ToggleHiddenFiles => Some("."),
117            Self::CreateDirectory => Some("F7"),
118            Self::CreateFile => Some("Shift+F7"),
119            Self::CreateSymlink => Some("Ctrl+L"),
120            Self::ExtractArchive => Some("Ctrl+E"),
121            Self::JumpToPath => Some("Ctrl+G"),
122            Self::HistoryBack => Some("Alt+Left"),
123            Self::HistoryForward => Some("Alt+Right"),
124            Self::SaveBookmark => Some("Ctrl+D"),
125            Self::GoToBookmark => Some("Ctrl+B"),
126            Self::ShowShortcuts => Some("?"),
127            Self::CopyToOtherPane => Some("F5"),
128            Self::MoveToOtherPane => Some("F6"),
129            Self::Delete => Some("Del"),
130            Self::Rename => Some("Shift+F6"),
131            Self::ViewFile => Some("F3"),
132            Self::FilterFiles => Some("Ctrl+F"),
133            Self::ClearFilter => Some("Ctrl+Shift+F"),
134            Self::SortByName => Some("Ctrl+1"),
135            Self::SortBySize => Some("Ctrl+2"),
136            Self::SortByModified => Some("Ctrl+3"),
137        }
138    }
139
140    /// The sort key this command selects, for commands that set the active
141    /// pane's sort key.
142    pub const fn sort_key(self) -> Option<crate::pane::SortKey> {
143        match self {
144            Self::SortByName => Some(crate::pane::SortKey::Name),
145            Self::SortBySize => Some(crate::pane::SortKey::Size),
146            Self::SortByModified => Some(crate::pane::SortKey::Modified),
147            _ => None,
148        }
149    }
150
151    pub fn matches_query(self, query: &str) -> bool {
152        let query = query.trim().to_lowercase();
153
154        query.is_empty()
155            || self.title().contains(&query)
156            || self.description().to_lowercase().contains(&query)
157    }
158}
159
160pub fn filter_commands(query: &str) -> Vec<AppCommand> {
161    let mut commands: Vec<AppCommand> = AppCommand::all()
162        .iter()
163        .copied()
164        .filter(|command| command.matches_query(query))
165        .collect();
166    commands.sort_by_key(|command| command.title());
167    commands
168}
169
170#[cfg(test)]
171mod tests {
172    use super::*;
173
174    #[test]
175    fn lists_switch_dark_light_mode_command() {
176        assert_eq!(
177            AppCommand::all(),
178            &[
179                AppCommand::SwitchDarkLightMode,
180                AppCommand::ToggleHiddenFiles,
181                AppCommand::CreateDirectory,
182                AppCommand::CreateFile,
183                AppCommand::CreateSymlink,
184                AppCommand::ExtractArchive,
185                AppCommand::JumpToPath,
186                AppCommand::HistoryBack,
187                AppCommand::HistoryForward,
188                AppCommand::SaveBookmark,
189                AppCommand::GoToBookmark,
190                AppCommand::ShowShortcuts,
191                AppCommand::CopyToOtherPane,
192                AppCommand::MoveToOtherPane,
193                AppCommand::Delete,
194                AppCommand::Rename,
195                AppCommand::ViewFile,
196                AppCommand::FilterFiles,
197                AppCommand::ClearFilter,
198                AppCommand::SortByName,
199                AppCommand::SortBySize,
200                AppCommand::SortByModified,
201            ]
202        );
203        assert_eq!(
204            AppCommand::SwitchDarkLightMode.title(),
205            "switch dark/light mode"
206        );
207    }
208
209    #[test]
210    fn lists_toggle_hidden_files_command() {
211        assert_eq!(
212            AppCommand::ToggleHiddenFiles.title(),
213            "show/hide hidden files"
214        );
215        assert_eq!(AppCommand::ToggleHiddenFiles.shortcut(), Some("."));
216    }
217
218    #[test]
219    fn lists_create_directory_command() {
220        assert_eq!(AppCommand::CreateDirectory.title(), "create directory");
221        assert_eq!(AppCommand::CreateDirectory.shortcut(), Some("F7"));
222    }
223
224    #[test]
225    fn lists_show_shortcuts_command() {
226        assert_eq!(AppCommand::ShowShortcuts.title(), "show keyboard shortcuts");
227        assert_eq!(AppCommand::ShowShortcuts.shortcut(), Some("?"));
228    }
229
230    #[test]
231    fn filters_commands_by_title() {
232        assert_eq!(
233            filter_commands("dark"),
234            vec![AppCommand::SwitchDarkLightMode]
235        );
236        assert!(filter_commands("missing").is_empty());
237    }
238
239    #[test]
240    fn filters_commands_empty_query() {
241        assert_eq!(
242            filter_commands(""),
243            vec![
244                AppCommand::HistoryBack,
245                AppCommand::ClearFilter,
246                AppCommand::CopyToOtherPane,
247                AppCommand::CreateDirectory,
248                AppCommand::CreateFile,
249                AppCommand::CreateSymlink,
250                AppCommand::Delete,
251                AppCommand::ExtractArchive,
252                AppCommand::FilterFiles,
253                AppCommand::HistoryForward,
254                AppCommand::GoToBookmark,
255                AppCommand::JumpToPath,
256                AppCommand::MoveToOtherPane,
257                AppCommand::Rename,
258                AppCommand::SaveBookmark,
259                AppCommand::ShowShortcuts,
260                AppCommand::ToggleHiddenFiles,
261                AppCommand::SortByModified,
262                AppCommand::SortByName,
263                AppCommand::SortBySize,
264                AppCommand::SwitchDarkLightMode,
265                AppCommand::ViewFile,
266            ]
267        );
268    }
269
270    #[test]
271    fn filters_commands_whitespace_only_query() {
272        assert_eq!(
273            filter_commands("   "),
274            vec![
275                AppCommand::HistoryBack,
276                AppCommand::ClearFilter,
277                AppCommand::CopyToOtherPane,
278                AppCommand::CreateDirectory,
279                AppCommand::CreateFile,
280                AppCommand::CreateSymlink,
281                AppCommand::Delete,
282                AppCommand::ExtractArchive,
283                AppCommand::FilterFiles,
284                AppCommand::HistoryForward,
285                AppCommand::GoToBookmark,
286                AppCommand::JumpToPath,
287                AppCommand::MoveToOtherPane,
288                AppCommand::Rename,
289                AppCommand::SaveBookmark,
290                AppCommand::ShowShortcuts,
291                AppCommand::ToggleHiddenFiles,
292                AppCommand::SortByModified,
293                AppCommand::SortByName,
294                AppCommand::SortBySize,
295                AppCommand::SwitchDarkLightMode,
296                AppCommand::ViewFile,
297            ]
298        );
299    }
300
301    #[test]
302    fn filters_commands_are_sorted_alphabetically_by_title() {
303        let titles: Vec<&str> = filter_commands("").iter().map(|c| c.title()).collect();
304        let mut sorted_titles = titles.clone();
305        sorted_titles.sort_unstable();
306        assert_eq!(titles, sorted_titles);
307    }
308
309    #[test]
310    fn lists_delete_command() {
311        assert_eq!(AppCommand::Delete.title(), "delete");
312        assert_eq!(AppCommand::Delete.shortcut(), Some("Del"));
313    }
314
315    #[test]
316    fn lists_rename_command() {
317        assert_eq!(AppCommand::Rename.title(), "rename");
318        assert_eq!(AppCommand::Rename.shortcut(), Some("Shift+F6"));
319    }
320
321    #[test]
322    fn lists_copy_to_other_pane_command() {
323        assert_eq!(AppCommand::CopyToOtherPane.title(), "copy to other pane");
324        assert_eq!(AppCommand::CopyToOtherPane.shortcut(), Some("F5"));
325    }
326
327    #[test]
328    fn lists_move_to_other_pane_command() {
329        assert_eq!(AppCommand::MoveToOtherPane.title(), "move to other pane");
330        assert_eq!(AppCommand::MoveToOtherPane.shortcut(), Some("F6"));
331    }
332
333    #[test]
334    fn filters_commands_by_description() {
335        assert_eq!(
336            filter_commands("theme"),
337            vec![AppCommand::SwitchDarkLightMode]
338        );
339    }
340
341    #[test]
342    fn filters_commands_by_hidden_files_title() {
343        assert_eq!(
344            filter_commands("hidden"),
345            vec![AppCommand::ToggleHiddenFiles]
346        );
347    }
348
349    #[test]
350    fn filters_commands_by_create_directory_title() {
351        assert_eq!(filter_commands("mkdir"), vec![AppCommand::CreateDirectory]);
352        assert_eq!(
353            filter_commands("directory"),
354            vec![AppCommand::CreateDirectory]
355        );
356    }
357
358    #[test]
359    fn lists_sort_by_name_command() {
360        assert_eq!(AppCommand::SortByName.title(), "sort by name");
361        assert_eq!(AppCommand::SortByName.shortcut(), Some("Ctrl+1"));
362        assert_eq!(
363            AppCommand::SortByName.sort_key(),
364            Some(crate::pane::SortKey::Name)
365        );
366    }
367
368    #[test]
369    fn lists_sort_by_size_command() {
370        assert_eq!(AppCommand::SortBySize.title(), "sort by size");
371        assert_eq!(AppCommand::SortBySize.shortcut(), Some("Ctrl+2"));
372        assert_eq!(
373            AppCommand::SortBySize.sort_key(),
374            Some(crate::pane::SortKey::Size)
375        );
376    }
377
378    #[test]
379    fn lists_sort_by_modified_command() {
380        assert_eq!(AppCommand::SortByModified.title(), "sort by modified");
381        assert_eq!(AppCommand::SortByModified.shortcut(), Some("Ctrl+3"));
382        assert_eq!(
383            AppCommand::SortByModified.sort_key(),
384            Some(crate::pane::SortKey::Modified)
385        );
386    }
387
388    #[test]
389    fn non_sort_commands_have_no_sort_key() {
390        assert_eq!(AppCommand::Delete.sort_key(), None);
391    }
392
393    #[test]
394    fn filters_commands_by_sort_title() {
395        assert_eq!(
396            filter_commands("sort"),
397            vec![
398                AppCommand::SortByModified,
399                AppCommand::SortByName,
400                AppCommand::SortBySize,
401            ]
402        );
403        assert_eq!(
404            filter_commands("sort by size"),
405            vec![AppCommand::SortBySize]
406        );
407    }
408}