use tuika::{Keymap, Layer};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum GlobalAction {
ReverseSearch,
Interrupt,
Quit,
ToggleBackground,
PasteImage,
}
pub(crate) fn global_keymap() -> Keymap<GlobalAction> {
Keymap::new().layer(
Layer::new("global")
.bind_labeled("ctrl+r", "search history", GlobalAction::ReverseSearch)
.bind_labeled("ctrl+c", "interrupt", GlobalAction::Interrupt)
.bind_labeled("ctrl+d", "quit", GlobalAction::Quit)
.bind_labeled("ctrl+b", "background tasks", GlobalAction::ToggleBackground)
.bind_labeled("ctrl+v", "paste image", GlobalAction::PasteImage),
)
}
#[cfg(test)]
mod tests {
use super::*;
use tuika::Dispatch;
use tuika::event::{Key, KeyCode};
fn ctrl(c: char) -> Key {
Key {
code: KeyCode::Char(c),
ctrl: true,
alt: false,
shift: false,
}
}
#[test]
fn binds_every_global_chord() {
let mut keymap = global_keymap();
assert_eq!(
keymap.dispatch(ctrl('r')),
Dispatch::Command(GlobalAction::ReverseSearch)
);
assert_eq!(
keymap.dispatch(ctrl('c')),
Dispatch::Command(GlobalAction::Interrupt)
);
assert_eq!(
keymap.dispatch(ctrl('d')),
Dispatch::Command(GlobalAction::Quit)
);
assert_eq!(
keymap.dispatch(ctrl('b')),
Dispatch::Command(GlobalAction::ToggleBackground)
);
assert_eq!(
keymap.dispatch(ctrl('v')),
Dispatch::Command(GlobalAction::PasteImage)
);
}
#[test]
fn leaves_unbound_keys_for_the_composer() {
let mut keymap = global_keymap();
assert_eq!(
keymap.dispatch(Key::new(KeyCode::Char('a'))),
Dispatch::Unmatched
);
assert_eq!(keymap.dispatch(ctrl('z')), Dispatch::Unmatched);
}
#[test]
fn every_binding_carries_a_help_label() {
let hints = global_keymap().hints();
assert_eq!(hints.len(), 5);
assert!(hints.iter().all(|hint| hint.label.is_some()));
assert!(hints.iter().any(|hint| hint.keys == "Ctrl+R"));
}
}