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