#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AppCommand {
SwitchDarkLightMode,
ToggleHiddenFiles,
CreateDirectory,
CreateFile,
CreateSymlink,
ExtractArchive,
JumpToPath,
HistoryBack,
HistoryForward,
SaveBookmark,
GoToBookmark,
ShowShortcuts,
CopyToOtherPane,
MoveToOtherPane,
Delete,
Rename,
ViewFile,
FilterFiles,
ClearFilter,
SortByName,
SortBySize,
SortByModified,
}
impl AppCommand {
pub const fn all() -> &'static [Self] {
&[
Self::SwitchDarkLightMode,
Self::ToggleHiddenFiles,
Self::CreateDirectory,
Self::CreateFile,
Self::CreateSymlink,
Self::ExtractArchive,
Self::JumpToPath,
Self::HistoryBack,
Self::HistoryForward,
Self::SaveBookmark,
Self::GoToBookmark,
Self::ShowShortcuts,
Self::CopyToOtherPane,
Self::MoveToOtherPane,
Self::Delete,
Self::Rename,
Self::ViewFile,
Self::FilterFiles,
Self::ClearFilter,
Self::SortByName,
Self::SortBySize,
Self::SortByModified,
]
}
pub const fn title(self) -> &'static str {
match self {
Self::SwitchDarkLightMode => "switch dark/light mode",
Self::ToggleHiddenFiles => "show/hide hidden files",
Self::CreateDirectory => "create directory",
Self::CreateFile => "create file",
Self::CreateSymlink => "create symlink",
Self::ExtractArchive => "extract archive",
Self::JumpToPath => "go to path",
Self::HistoryBack => "back",
Self::HistoryForward => "forward",
Self::SaveBookmark => "save bookmark",
Self::GoToBookmark => "go to bookmark",
Self::ShowShortcuts => "show keyboard shortcuts",
Self::CopyToOtherPane => "copy to other pane",
Self::MoveToOtherPane => "move to other pane",
Self::Delete => "delete",
Self::Rename => "rename",
Self::ViewFile => "view file",
Self::FilterFiles => "filter files",
Self::ClearFilter => "clear filter",
Self::SortByName => "sort by name",
Self::SortBySize => "sort by size",
Self::SortByModified => "sort by modified",
}
}
pub const fn description(self) -> &'static str {
match self {
Self::SwitchDarkLightMode => "Toggle the application theme",
Self::ToggleHiddenFiles => "Toggle visibility of dot-prefixed files",
Self::CreateDirectory => "Create a directory in the active pane (mkdir)",
Self::CreateFile => "Create an empty file in the active pane (touch)",
Self::CreateSymlink => {
"Create a symlink in the other pane pointing at the selected entry"
}
Self::ExtractArchive => "Extract the selected zip or tar/tar.gz into the active pane",
Self::JumpToPath => "Move the active pane to a typed path (~ expands to home)",
Self::HistoryBack => "Return to the previously visited location",
Self::HistoryForward => "Go forward again after going back",
Self::SaveBookmark => "Save the active pane's location under a name",
Self::GoToBookmark => "Move the active pane to a saved bookmark",
Self::ShowShortcuts => "List every keyboard shortcut the app responds to",
Self::CopyToOtherPane => "Copy selected item into the other pane",
Self::MoveToOtherPane => "Move selected item into the other pane",
Self::Delete => "Delete the selected item (permanent)",
Self::Rename => "Rename the selected item",
Self::ViewFile => "View the selected file read-only, without leaving the app",
Self::FilterFiles => "Restrict the active pane to names matching a pattern (e.g. *.rs)",
Self::ClearFilter => "Remove the active pane's name filter",
Self::SortByName => "Sort the active pane by name; invoke again to reverse direction",
Self::SortBySize => "Sort the active pane by size; invoke again to reverse direction",
Self::SortByModified => {
"Sort the active pane by modified time; invoke again to reverse direction"
}
}
}
pub const fn shortcut(self) -> Option<&'static str> {
match self {
Self::SwitchDarkLightMode => None,
Self::ToggleHiddenFiles => Some("."),
Self::CreateDirectory => Some("F7"),
Self::CreateFile => Some("Shift+F7"),
Self::CreateSymlink => Some("Ctrl+L"),
Self::ExtractArchive => Some("Ctrl+E"),
Self::JumpToPath => Some("Ctrl+G"),
Self::HistoryBack => Some("Alt+Left"),
Self::HistoryForward => Some("Alt+Right"),
Self::SaveBookmark => Some("Ctrl+D"),
Self::GoToBookmark => Some("Ctrl+B"),
Self::ShowShortcuts => Some("?"),
Self::CopyToOtherPane => Some("F5"),
Self::MoveToOtherPane => Some("F6"),
Self::Delete => Some("Del"),
Self::Rename => Some("Shift+F6"),
Self::ViewFile => Some("F3"),
Self::FilterFiles => Some("Ctrl+F"),
Self::ClearFilter => Some("Ctrl+Shift+F"),
Self::SortByName => Some("Ctrl+1"),
Self::SortBySize => Some("Ctrl+2"),
Self::SortByModified => Some("Ctrl+3"),
}
}
pub const fn sort_key(self) -> Option<crate::pane::SortKey> {
match self {
Self::SortByName => Some(crate::pane::SortKey::Name),
Self::SortBySize => Some(crate::pane::SortKey::Size),
Self::SortByModified => Some(crate::pane::SortKey::Modified),
_ => None,
}
}
pub fn matches_query(self, query: &str) -> bool {
let query = query.trim().to_lowercase();
query.is_empty()
|| self.title().contains(&query)
|| self.description().to_lowercase().contains(&query)
}
}
pub fn filter_commands(query: &str) -> Vec<AppCommand> {
let mut commands: Vec<AppCommand> = AppCommand::all()
.iter()
.copied()
.filter(|command| command.matches_query(query))
.collect();
commands.sort_by_key(|command| command.title());
commands
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lists_switch_dark_light_mode_command() {
assert_eq!(
AppCommand::all(),
&[
AppCommand::SwitchDarkLightMode,
AppCommand::ToggleHiddenFiles,
AppCommand::CreateDirectory,
AppCommand::CreateFile,
AppCommand::CreateSymlink,
AppCommand::ExtractArchive,
AppCommand::JumpToPath,
AppCommand::HistoryBack,
AppCommand::HistoryForward,
AppCommand::SaveBookmark,
AppCommand::GoToBookmark,
AppCommand::ShowShortcuts,
AppCommand::CopyToOtherPane,
AppCommand::MoveToOtherPane,
AppCommand::Delete,
AppCommand::Rename,
AppCommand::ViewFile,
AppCommand::FilterFiles,
AppCommand::ClearFilter,
AppCommand::SortByName,
AppCommand::SortBySize,
AppCommand::SortByModified,
]
);
assert_eq!(
AppCommand::SwitchDarkLightMode.title(),
"switch dark/light mode"
);
}
#[test]
fn lists_toggle_hidden_files_command() {
assert_eq!(
AppCommand::ToggleHiddenFiles.title(),
"show/hide hidden files"
);
assert_eq!(AppCommand::ToggleHiddenFiles.shortcut(), Some("."));
}
#[test]
fn lists_create_directory_command() {
assert_eq!(AppCommand::CreateDirectory.title(), "create directory");
assert_eq!(AppCommand::CreateDirectory.shortcut(), Some("F7"));
}
#[test]
fn lists_show_shortcuts_command() {
assert_eq!(AppCommand::ShowShortcuts.title(), "show keyboard shortcuts");
assert_eq!(AppCommand::ShowShortcuts.shortcut(), Some("?"));
}
#[test]
fn filters_commands_by_title() {
assert_eq!(
filter_commands("dark"),
vec![AppCommand::SwitchDarkLightMode]
);
assert!(filter_commands("missing").is_empty());
}
#[test]
fn filters_commands_empty_query() {
assert_eq!(
filter_commands(""),
vec![
AppCommand::HistoryBack,
AppCommand::ClearFilter,
AppCommand::CopyToOtherPane,
AppCommand::CreateDirectory,
AppCommand::CreateFile,
AppCommand::CreateSymlink,
AppCommand::Delete,
AppCommand::ExtractArchive,
AppCommand::FilterFiles,
AppCommand::HistoryForward,
AppCommand::GoToBookmark,
AppCommand::JumpToPath,
AppCommand::MoveToOtherPane,
AppCommand::Rename,
AppCommand::SaveBookmark,
AppCommand::ShowShortcuts,
AppCommand::ToggleHiddenFiles,
AppCommand::SortByModified,
AppCommand::SortByName,
AppCommand::SortBySize,
AppCommand::SwitchDarkLightMode,
AppCommand::ViewFile,
]
);
}
#[test]
fn filters_commands_whitespace_only_query() {
assert_eq!(
filter_commands(" "),
vec![
AppCommand::HistoryBack,
AppCommand::ClearFilter,
AppCommand::CopyToOtherPane,
AppCommand::CreateDirectory,
AppCommand::CreateFile,
AppCommand::CreateSymlink,
AppCommand::Delete,
AppCommand::ExtractArchive,
AppCommand::FilterFiles,
AppCommand::HistoryForward,
AppCommand::GoToBookmark,
AppCommand::JumpToPath,
AppCommand::MoveToOtherPane,
AppCommand::Rename,
AppCommand::SaveBookmark,
AppCommand::ShowShortcuts,
AppCommand::ToggleHiddenFiles,
AppCommand::SortByModified,
AppCommand::SortByName,
AppCommand::SortBySize,
AppCommand::SwitchDarkLightMode,
AppCommand::ViewFile,
]
);
}
#[test]
fn filters_commands_are_sorted_alphabetically_by_title() {
let titles: Vec<&str> = filter_commands("").iter().map(|c| c.title()).collect();
let mut sorted_titles = titles.clone();
sorted_titles.sort_unstable();
assert_eq!(titles, sorted_titles);
}
#[test]
fn lists_delete_command() {
assert_eq!(AppCommand::Delete.title(), "delete");
assert_eq!(AppCommand::Delete.shortcut(), Some("Del"));
}
#[test]
fn lists_rename_command() {
assert_eq!(AppCommand::Rename.title(), "rename");
assert_eq!(AppCommand::Rename.shortcut(), Some("Shift+F6"));
}
#[test]
fn lists_copy_to_other_pane_command() {
assert_eq!(AppCommand::CopyToOtherPane.title(), "copy to other pane");
assert_eq!(AppCommand::CopyToOtherPane.shortcut(), Some("F5"));
}
#[test]
fn lists_move_to_other_pane_command() {
assert_eq!(AppCommand::MoveToOtherPane.title(), "move to other pane");
assert_eq!(AppCommand::MoveToOtherPane.shortcut(), Some("F6"));
}
#[test]
fn filters_commands_by_description() {
assert_eq!(
filter_commands("theme"),
vec![AppCommand::SwitchDarkLightMode]
);
}
#[test]
fn filters_commands_by_hidden_files_title() {
assert_eq!(
filter_commands("hidden"),
vec![AppCommand::ToggleHiddenFiles]
);
}
#[test]
fn filters_commands_by_create_directory_title() {
assert_eq!(filter_commands("mkdir"), vec![AppCommand::CreateDirectory]);
assert_eq!(
filter_commands("directory"),
vec![AppCommand::CreateDirectory]
);
}
#[test]
fn lists_sort_by_name_command() {
assert_eq!(AppCommand::SortByName.title(), "sort by name");
assert_eq!(AppCommand::SortByName.shortcut(), Some("Ctrl+1"));
assert_eq!(
AppCommand::SortByName.sort_key(),
Some(crate::pane::SortKey::Name)
);
}
#[test]
fn lists_sort_by_size_command() {
assert_eq!(AppCommand::SortBySize.title(), "sort by size");
assert_eq!(AppCommand::SortBySize.shortcut(), Some("Ctrl+2"));
assert_eq!(
AppCommand::SortBySize.sort_key(),
Some(crate::pane::SortKey::Size)
);
}
#[test]
fn lists_sort_by_modified_command() {
assert_eq!(AppCommand::SortByModified.title(), "sort by modified");
assert_eq!(AppCommand::SortByModified.shortcut(), Some("Ctrl+3"));
assert_eq!(
AppCommand::SortByModified.sort_key(),
Some(crate::pane::SortKey::Modified)
);
}
#[test]
fn non_sort_commands_have_no_sort_key() {
assert_eq!(AppCommand::Delete.sort_key(), None);
}
#[test]
fn filters_commands_by_sort_title() {
assert_eq!(
filter_commands("sort"),
vec![
AppCommand::SortByModified,
AppCommand::SortByName,
AppCommand::SortBySize,
]
);
assert_eq!(
filter_commands("sort by size"),
vec![AppCommand::SortBySize]
);
}
}