#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Key {
Char(char),
Up,
Down,
Left,
Right,
Enter,
Tab,
Esc,
PageUp,
PageDown,
Home,
End,
Interrupt,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Action {
Up,
Down,
PageUp,
PageDown,
First,
Last,
Collapse,
Expand,
SwitchLens,
OpenDetail,
CloseDetail,
Quit,
}
pub(super) fn action_for(key: Key) -> Option<Action> {
match key {
Key::Up | Key::Char('k') => Some(Action::Up),
Key::Down | Key::Char('j') => Some(Action::Down),
Key::PageUp => Some(Action::PageUp),
Key::PageDown => Some(Action::PageDown),
Key::Home | Key::Char('g') => Some(Action::First),
Key::End | Key::Char('G') => Some(Action::Last),
Key::Left | Key::Char('h') => Some(Action::Collapse),
Key::Right | Key::Char('l') => Some(Action::Expand),
Key::Tab => Some(Action::SwitchLens),
Key::Enter => Some(Action::OpenDetail),
Key::Esc => Some(Action::CloseDetail),
Key::Char('q') | Key::Interrupt => Some(Action::Quit),
Key::Char(_) => None,
}
}
#[cfg(test)]
mod tests {
use super::{Action, Key, action_for};
#[test]
fn every_key_the_viewer_reads_names_its_action() {
let table = [
(Key::Char('k'), Some(Action::Up)),
(Key::Up, Some(Action::Up)),
(Key::Char('j'), Some(Action::Down)),
(Key::Down, Some(Action::Down)),
(Key::PageUp, Some(Action::PageUp)),
(Key::PageDown, Some(Action::PageDown)),
(Key::Char('g'), Some(Action::First)),
(Key::Home, Some(Action::First)),
(Key::Char('G'), Some(Action::Last)),
(Key::End, Some(Action::Last)),
(Key::Char('h'), Some(Action::Collapse)),
(Key::Left, Some(Action::Collapse)),
(Key::Char('l'), Some(Action::Expand)),
(Key::Right, Some(Action::Expand)),
(Key::Tab, Some(Action::SwitchLens)),
(Key::Enter, Some(Action::OpenDetail)),
(Key::Esc, Some(Action::CloseDetail)),
(Key::Char('q'), Some(Action::Quit)),
(Key::Interrupt, Some(Action::Quit)),
(Key::Char('x'), None),
];
for (key, action) in table {
assert_eq!(action_for(key), action, "{key:?}");
}
}
}