#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Shortcut {
pub keys: &'static str,
pub action: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ShortcutGroup {
pub title: &'static str,
pub shortcuts: &'static [Shortcut],
}
const fn key(keys: &'static str, action: &'static str) -> Shortcut {
Shortcut { keys, action }
}
const COMMAND_PALETTE: &[Shortcut] = &[key(
": / Ctrl+P",
"Open the command palette \u{2014} every action, by name",
)];
const NAVIGATION: &[Shortcut] = &[
key("\u{2191} / \u{2193}", "Move the cursor"),
key("PgUp / PgDn", "Move the cursor a page"),
key("Enter", "Enter the directory under the cursor"),
key("Backspace", "Go to the parent directory"),
key("Tab", "Switch the active pane"),
key("Alt+Left", "Back to the previously visited location"),
key("Alt+Right", "Forward again after going back"),
key("Alt+I", "Show this directory in the other pane"),
key("Ctrl+G", "Go to a typed path (~ expands)"),
key("a\u{2013}z\u{2026}", "Type a name to jump to it"),
key("/", "Jump prompt, empty (for names starting with . ? / :)"),
key("Ctrl+B", "Go to a bookmark"),
key("Ctrl+D", "Save the active pane's location as a bookmark"),
];
const SELECTION: &[Shortcut] = &[
key("Space", "Tag or untag the entry under the cursor"),
key(
"Shift+\u{2191} / Shift+\u{2193}",
"Extend the tagged selection",
),
];
const FILE_OPERATIONS: &[Shortcut] = &[
key("F3", "View the selected file (read-only)"),
key("F4", "Edit the file under the cursor"),
key("F5", "Copy to the other pane"),
key(
"Alt+F5",
"Pack the selection into an archive in the other pane",
),
key("F6", "Move to the other pane"),
key("Shift+F6", "Rename"),
key("F7", "Create directory"),
key("Shift+F7", "Create file"),
key("Alt+F7", "Search file contents under this directory"),
key("Del", "Delete (permanent)"),
key("Ctrl+L", "Symlink the selection into the other pane"),
key("Ctrl+E", "Extract the selected zip or tar/tar.gz"),
];
const VIEW: &[Shortcut] = &[
key(".", "Show or hide hidden files"),
key("Ctrl+F", "Filter the pane by a pattern (e.g. *.rs)"),
key("Ctrl+Shift+F", "Clear the filter"),
key("Ctrl+1", "Sort by name"),
key("Ctrl+2", "Sort by size"),
key("Ctrl+3", "Sort by modified time"),
];
const OTHER: &[Shortcut] = &[
key("Shift+F4", "Open a terminal here"),
key("?", "Show the keyboard shortcuts"),
key("Esc", "Close a prompt, or cancel a running operation"),
key("s", "Skip conflicting files (overwrite prompt only)"),
];
const GROUPS: &[ShortcutGroup] = &[
ShortcutGroup {
title: "Command palette",
shortcuts: COMMAND_PALETTE,
},
ShortcutGroup {
title: "Navigation",
shortcuts: NAVIGATION,
},
ShortcutGroup {
title: "Selection",
shortcuts: SELECTION,
},
ShortcutGroup {
title: "File operations",
shortcuts: FILE_OPERATIONS,
},
ShortcutGroup {
title: "View",
shortcuts: VIEW,
},
ShortcutGroup {
title: "Other",
shortcuts: OTHER,
},
];
pub const fn groups() -> &'static [ShortcutGroup] {
GROUPS
}
pub fn row_count() -> usize {
GROUPS.iter().map(|group| 1 + group.shortcuts.len()).sum()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::command::AppCommand;
fn all_keys() -> Vec<&'static str> {
groups()
.iter()
.flat_map(|group| group.shortcuts.iter())
.map(|shortcut| shortcut.keys)
.collect()
}
#[test]
fn every_command_shortcut_is_listed() {
let keys = all_keys();
for command in AppCommand::all() {
let Some(shortcut) = command.shortcut() else {
continue;
};
assert!(
keys.contains(&shortcut),
"{shortcut} ({}) is missing from the shortcuts help",
command.title()
);
}
}
#[test]
fn palette_is_the_first_shortcut() {
let first_group = groups().first().expect("the catalog has groups");
let first = first_group
.shortcuts
.first()
.expect("the first group has rows");
assert_eq!(first.keys, ": / Ctrl+P");
}
#[test]
fn no_duplicate_keys() {
let mut keys = all_keys();
let total = keys.len();
keys.sort_unstable();
keys.dedup();
assert_eq!(keys.len(), total, "a key is listed twice");
}
#[test]
fn every_group_is_non_empty() {
for group in groups() {
assert!(
!group.shortcuts.is_empty(),
"group {} has no shortcuts",
group.title
);
}
}
#[test]
fn counts_headers_and_rows() {
let rows: usize = groups().iter().map(|group| group.shortcuts.len()).sum();
assert_eq!(row_count(), rows + groups().len());
}
}