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