oxinav-core 0.4.0

Core logic for OxiNav: file operations, pane state, archives and viewing, with no UI dependency
Documentation
//! The catalog behind the `?` help panel.
//!
//! The app's keys are spread over `key_subscription` in the UI crate, and only
//! the palette-executable subset has an [`AppCommand`]. This module is the one
//! place that names *every* key, so the help panel has something to render and
//! the listing can be unit-tested without a window.
//!
//! Keys that back an [`AppCommand`] get a row of their own, spelled exactly as
//! [`AppCommand::shortcut`] spells them — `every_command_shortcut_is_listed`
//! relies on that to catch a command whose key never reached this file.
//!
//! [`AppCommand`]: crate::command::AppCommand
//! [`AppCommand::shortcut`]: crate::command::AppCommand::shortcut

/// One row of the help panel: the keys on the left, what they do on the right.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Shortcut {
    pub keys: &'static str,
    pub action: &'static str,
}

/// A titled run of rows, rendered as one section of the panel.
#[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 }
}

/// Deliberately first: the palette can reach every command by name, so it is
/// the one key that makes the rest optional.
const COMMAND_PALETTE: &[Shortcut] = &[key(
    ":",
    "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("Ctrl+G", "Go to a typed path (~ expands)"),
    key("/", "Jump to an entry by prefix"),
    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("F5", "Copy to the other pane"),
    key("F6", "Move to the other pane"),
    key("Shift+F6", "Rename"),
    key("F7", "Create directory"),
    key("Shift+F7", "Create file"),
    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("?", "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,
    },
];

/// Every shortcut, in the order the help panel shows them.
pub const fn groups() -> &'static [ShortcutGroup] {
    GROUPS
}

/// Group headers plus rows — what the panel needs to size its scroll area.
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()
    }

    /// The help panel and the command palette quote the same keys, so a
    /// command whose shortcut never made it into this catalog is a bug the
    /// user would only notice by missing it.
    #[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()
            );
        }
    }

    /// The palette is the only key that leads to every other action, so the
    /// panel opens with it rather than burying it among the odds and ends.
    #[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, ":");
    }

    #[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());
    }
}