use crate::modes::Keymap;
use mouser_mode_api::MenuEntry;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Cx {
Folder,
File,
Media,
Empty,
Image,
FsImage,
Player,
FsPlayer,
Text,
}
use Cx::*;
pub struct Act {
pub id: &'static str,
pub arg: i32,
pub label: &'static str,
pub cx: &'static [Cx],
}
const fn a(id: &'static str, arg: i32, label: &'static str, cx: &'static [Cx]) -> Act {
Act { id, arg, label, cx }
}
const ITEMS: &[Cx] = &[Folder, File, Media];
const VIEWS: &[Cx] = &[Image, FsImage, Player, FsPlayer, Text];
const IMAGE: &[Cx] = &[Image, FsImage];
const PLAYER: &[Cx] = &[Player, FsPlayer];
const LIST: &[Cx] = &[Folder, File, Media, Empty];
pub const ACTIONS: &[Act] = &[
a("open", 0, "open", &[Folder, File]),
a("open", 0, "play", &[Media]),
a("open_fullscreen", 0, "play / view fullscreen", &[Media]),
a("sync_item", 0, "sync the shell here", &[Folder]),
a("copy", 0, "copy (paste in another folder)", ITEMS),
a("copy_to", 0, "copy to…", ITEMS),
a("move_to", 0, "move to…", ITEMS),
a("rename", 0, "rename…", ITEMS),
a("delete", 0, "delete…", ITEMS),
a("copy_path", 0, "copy path", &[Folder, File, Media, Image, FsImage, Player, FsPlayer, Text]),
a("mark", 0, "mark / unmark", ITEMS),
a("pipe", 0, "pipe into a command…", ITEMS),
a("play_pause", 0, "play / pause", PLAYER),
a("seek_back", 0, "back 10 s", PLAYER),
a("seek_forward", 0, "forward 10 s", PLAYER),
a("volume_down", 0, "volume down", PLAYER),
a("volume_up", 0, "volume up", PLAYER),
a("zoom_in", 0, "zoom in", IMAGE),
a("zoom_out", 0, "zoom out", IMAGE),
a("zoom_fit", 0, "fit", IMAGE),
a("zoom_actual", 0, "100% (actual pixels)", IMAGE),
a("left", 0, "pan left / previous image", IMAGE),
a("right", 0, "pan right / next image", IMAGE),
a("up", 0, "pan up", IMAGE),
a("down", 0, "pan down", IMAGE),
a("up", 0, "scroll up", &[Text]),
a("down", 0, "scroll down", &[Text]),
a("page_up", 0, "page up", &[Text]),
a("page_down", 0, "page down", &[Text]),
a("copy_text", 0, "copy text", &[Text]),
a("left", 0, "previous file", &[Text]),
a("right", 0, "next file", &[Text]),
a("fullscreen", 0, "fullscreen", &[Image, Player]),
a("fullscreen", 0, "exit fullscreen", &[FsImage, FsPlayer]),
a("close", 0, "close", VIEWS),
a("parent", 0, "back", LIST),
a("sync", 0, "sync the shell to this folder", &[Empty]),
a("new_folder", 0, "new folder…", &[Empty]),
a("paste", 0, "paste", &[Folder, Empty]),
a("sort_name", 0, "sort by name", &[Empty]),
a("sort_modified", 0, "sort by modified", &[Empty]),
a("sort_size", 0, "sort by size", &[Empty]),
a("reverse", 0, "reverse order", &[Empty]),
a("switch_view", 0, "switch view", &[Empty]),
a("view_list", 0, "list view", &[Empty]),
a("view_grid", 0, "grid view", &[Empty]),
a("view_carousel", 0, "carousel view", &[Empty]),
a("bigger", 0, "larger tiles", &[Empty]),
a("smaller", 0, "smaller tiles", &[Empty]),
a("hidden", 0, "show / hide dotfiles", &[Empty]),
a("search", 0, "search…", &[Empty]),
a("mark_all", 0, "mark all / clear marks", &[Empty]),
a("first", 0, "first item", &[Empty]),
a("last", 0, "last item", &[Empty]),
a("options", 0, "options", &[Empty]),
a("man", 0, "manual", &[Empty]),
a("keys", 0, "keyboard help", &[Folder, File, Media, Empty, Image, FsImage, Player, FsPlayer, Text]),
];
pub const NOT_IN_MENUS: &[&str] = &["menu", "back", "help"];
pub type Enabled<'a> = &'a dyn Fn(&str) -> bool;
pub fn entries(cx: Cx, item: Option<usize>, keys: &Keymap, enabled: Enabled<'_>) -> Vec<MenuEntry> {
ACTIONS
.iter()
.filter(|x| x.cx.contains(&cx))
.map(|x| {
let key = keys.keys.iter().find(|k| k.0 == x.id).map(|k| k.1.clone()).unwrap_or_default();
let arg = item.map_or(x.arg, |i| i as i32);
MenuEntry { id: format!("menu:{}", x.id), arg, label: x.label.into(), key, enabled: enabled(x.id) }
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn keys() -> Keymap {
Keymap::new(crate::commander::BINDINGS)
}
fn ids(cx: Cx) -> Vec<(String, String)> {
entries(cx, Some(0), &keys(), &|_| true).into_iter().map(|e| (e.id.trim_start_matches("menu:").to_string(), e.key)).collect()
}
fn has(cx: Cx, want: &[(&str, &str)]) {
let got = ids(cx);
for (id, key) in want {
assert!(got.iter().any(|(i, k)| i == id && k == key), "{cx:?}: {id} ({key}) missing from {got:?}");
}
}
#[test]
fn fullscreen_image() {
has(
FsImage,
&[
("close", "q"),
("zoom_in", "="),
("zoom_out", "-"),
("zoom_fit", "0"),
("zoom_actual", "1"),
("left", "left"),
("up", "up"),
("fullscreen", "f"),
],
);
assert!(!ids(FsImage).iter().any(|(i, _)| i == "play_pause" || i == "delete"));
}
#[test]
fn playing_media_fullscreen_and_not() {
for cx in [FsPlayer, Player] {
has(
cx,
&[
("play_pause", "k"),
("seek_back", "j"),
("seek_forward", "l"),
("volume_down", "-"),
("volume_up", "="),
("fullscreen", "f"),
("close", "q"),
],
);
assert!(!ids(cx).iter().any(|(i, _)| i == "zoom_in"));
}
let fs = entries(FsPlayer, None, &keys(), &|_| true);
assert!(fs.iter().any(|e| e.label == "exit fullscreen"));
}
#[test]
fn text_view() {
has(
Text,
&[
("up", "up"),
("down", "down"),
("page_up", "pageup"),
("page_down", "pagedown"),
("close", "q"),
("copy_text", "T"),
("copy_path", "Y"),
("keys", "?"),
],
);
}
#[test]
fn list_file_media_and_folder() {
let item = [
("open", "enter"),
("copy", "y"),
("copy_to", "c"),
("move_to", "M"),
("rename", "r"),
("delete", "d"),
("copy_path", "Y"),
("mark", "space"),
("pipe", "|"),
];
has(File, &item);
has(Media, &item);
has(Media, &[("open_fullscreen", "O")]);
assert!(entries(Media, Some(3), &keys(), &|_| true).iter().any(|e| e.label == "play" && e.arg == 3), "media: play, on that item");
has(Folder, &[("open", "enter"), ("parent", "u"), ("sync_item", "e"), ("delete", "d"), ("paste", "p")]);
assert!(!ids(File).iter().any(|(i, _)| i == "play_pause" || i == "new_folder"));
}
#[test]
fn empty_area() {
has(
Empty,
&[
("new_folder", "n"),
("paste", "p"),
("sort_name", "N"),
("sort_modified", "D"),
("sort_size", "S"),
("reverse", "R"),
("switch_view", "v"),
("view_list", "L"),
("view_grid", "G"),
("view_carousel", "C"),
("sync", "s"),
("hidden", "h"),
("search", "/"),
("keys", "?"),
],
);
assert!(!ids(Empty).iter().any(|(i, _)| i == "delete" || i == "open"));
}
#[test]
fn rebinding_shows_in_the_menu() {
let mut k = keys();
k.apply("commander", "[commander.keys]\nplay_pause = \"p\"\n").unwrap();
let e = entries(Player, None, &k, &|_| true);
assert_eq!(e.iter().find(|e| e.id == "menu:play_pause").unwrap().key, "p");
}
#[test]
fn disabled_entries_are_greyed() {
let e = entries(File, Some(0), &keys(), &|id| id != "delete");
assert!(!e.iter().find(|e| e.id == "menu:delete").unwrap().enabled);
}
#[test]
fn registry_covers_every_binding() {
for b in crate::commander::BINDINGS {
assert!(NOT_IN_MENUS.contains(&b.id) || ACTIONS.iter().any(|a| a.id == b.id), "{} is in no menu", b.id);
}
for a in ACTIONS {
assert!(crate::commander::BINDINGS.iter().any(|b| b.id == a.id), "{} has no key", a.id);
}
}
}