1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub struct Shortcut {
18 pub keys: &'static str,
19 pub action: &'static str,
20}
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub struct ShortcutGroup {
25 pub title: &'static str,
26 pub shortcuts: &'static [Shortcut],
27}
28
29const fn key(keys: &'static str, action: &'static str) -> Shortcut {
30 Shortcut { keys, action }
31}
32
33const COMMAND_PALETTE: &[Shortcut] = &[key(
36 ": / Ctrl+P",
37 "Open the command palette \u{2014} every action, by name",
38)];
39
40const NAVIGATION: &[Shortcut] = &[
41 key("\u{2191} / \u{2193}", "Move the cursor"),
42 key("PgUp / PgDn", "Move the cursor a page"),
43 key("Enter", "Enter the directory under the cursor"),
44 key("Backspace", "Go to the parent directory"),
45 key("Tab", "Switch the active pane"),
46 key("Alt+Left", "Back to the previously visited location"),
47 key("Alt+Right", "Forward again after going back"),
48 key("Alt+I", "Show this directory in the other pane"),
49 key("Ctrl+G", "Go to a typed path (~ expands)"),
50 key("a\u{2013}z\u{2026}", "Type a name to jump to it"),
51 key("/", "Jump prompt, empty (for names starting with . ? / :)"),
52 key("Ctrl+B", "Go to a bookmark"),
53 key("Ctrl+D", "Save the active pane's location as a bookmark"),
54];
55
56const SELECTION: &[Shortcut] = &[
57 key("Space", "Tag or untag the entry under the cursor"),
58 key(
59 "Shift+\u{2191} / Shift+\u{2193}",
60 "Extend the tagged selection",
61 ),
62];
63
64const FILE_OPERATIONS: &[Shortcut] = &[
65 key("F3", "View the selected file (read-only)"),
66 key("F4", "Edit the file under the cursor"),
67 key("F5", "Copy to the other pane"),
68 key(
69 "Alt+F5",
70 "Pack the selection into an archive in the other pane",
71 ),
72 key("F6", "Move to the other pane"),
73 key("Shift+F6", "Rename"),
74 key("F7", "Create directory"),
75 key("Shift+F7", "Create file"),
76 key("Alt+F7", "Search file contents under this directory"),
77 key("Del", "Delete (permanent)"),
78 key("Ctrl+L", "Symlink the selection into the other pane"),
79 key("Ctrl+E", "Extract the selected zip or tar/tar.gz"),
80];
81
82const VIEW: &[Shortcut] = &[
83 key(".", "Show or hide hidden files"),
84 key("Ctrl+F", "Filter the pane by a pattern (e.g. *.rs)"),
85 key("Ctrl+Shift+F", "Clear the filter"),
86 key("Ctrl+1", "Sort by name"),
87 key("Ctrl+2", "Sort by size"),
88 key("Ctrl+3", "Sort by modified time"),
89];
90
91const OTHER: &[Shortcut] = &[
92 key("Shift+F4", "Open a terminal here"),
93 key("?", "Show the keyboard shortcuts"),
94 key("Esc", "Close a prompt, or cancel a running operation"),
95 key("s", "Skip conflicting files (overwrite prompt only)"),
96];
97
98const GROUPS: &[ShortcutGroup] = &[
99 ShortcutGroup {
100 title: "Command palette",
101 shortcuts: COMMAND_PALETTE,
102 },
103 ShortcutGroup {
104 title: "Navigation",
105 shortcuts: NAVIGATION,
106 },
107 ShortcutGroup {
108 title: "Selection",
109 shortcuts: SELECTION,
110 },
111 ShortcutGroup {
112 title: "File operations",
113 shortcuts: FILE_OPERATIONS,
114 },
115 ShortcutGroup {
116 title: "View",
117 shortcuts: VIEW,
118 },
119 ShortcutGroup {
120 title: "Other",
121 shortcuts: OTHER,
122 },
123];
124
125pub const fn groups() -> &'static [ShortcutGroup] {
127 GROUPS
128}
129
130pub fn row_count() -> usize {
132 GROUPS.iter().map(|group| 1 + group.shortcuts.len()).sum()
133}
134
135#[cfg(test)]
136mod tests {
137 use super::*;
138 use crate::command::AppCommand;
139
140 fn all_keys() -> Vec<&'static str> {
141 groups()
142 .iter()
143 .flat_map(|group| group.shortcuts.iter())
144 .map(|shortcut| shortcut.keys)
145 .collect()
146 }
147
148 #[test]
152 fn every_command_shortcut_is_listed() {
153 let keys = all_keys();
154 for command in AppCommand::all() {
155 let Some(shortcut) = command.shortcut() else {
156 continue;
157 };
158 assert!(
159 keys.contains(&shortcut),
160 "{shortcut} ({}) is missing from the shortcuts help",
161 command.title()
162 );
163 }
164 }
165
166 #[test]
169 fn palette_is_the_first_shortcut() {
170 let first_group = groups().first().expect("the catalog has groups");
171 let first = first_group
172 .shortcuts
173 .first()
174 .expect("the first group has rows");
175 assert_eq!(first.keys, ": / Ctrl+P");
176 }
177
178 #[test]
179 fn no_duplicate_keys() {
180 let mut keys = all_keys();
181 let total = keys.len();
182 keys.sort_unstable();
183 keys.dedup();
184 assert_eq!(keys.len(), total, "a key is listed twice");
185 }
186
187 #[test]
188 fn every_group_is_non_empty() {
189 for group in groups() {
190 assert!(
191 !group.shortcuts.is_empty(),
192 "group {} has no shortcuts",
193 group.title
194 );
195 }
196 }
197
198 #[test]
199 fn counts_headers_and_rows() {
200 let rows: usize = groups().iter().map(|group| group.shortcuts.len()).sum();
201 assert_eq!(row_count(), rows + groups().len());
202 }
203}