Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇

4.2 KB
Raw Blame
// ©AngelaMos | 2026
// input.rs

use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};

use crate::app::{Action, Tab};

#[must_use]
pub fn action_of(event: &Event) -> Action {
    match event {
        Event::Key(key) if key.kind == KeyEventKind::Press => key_action(*key),
        _ => Action::Ignore,
    }
}

#[must_use]
pub fn key_action(key: KeyEvent) -> Action {
    if key.modifiers.contains(KeyModifiers::CONTROL) && matches!(key.code, KeyCode::Char('c')) {
        return Action::Quit;
    }
    match key.code {
        KeyCode::Char('q') | KeyCode::Esc => Action::Quit,
        KeyCode::Tab | KeyCode::Right | KeyCode::Char('l') => Action::NextTab,
        KeyCode::BackTab | KeyCode::Left | KeyCode::Char('h') => Action::PreviousTab,
        KeyCode::Char('1') => Action::SelectTab(Tab::Overview),
        KeyCode::Char('2') => Action::SelectTab(Tab::Findings),
        KeyCode::Char('3') => Action::SelectTab(Tab::Instances),
        KeyCode::Char('j') | KeyCode::Down => Action::Down,
        KeyCode::Char('k') | KeyCode::Up => Action::Up,
        KeyCode::Char('g') | KeyCode::Home => Action::First,
        KeyCode::Char('G') | KeyCode::End => Action::Last,
        KeyCode::Char('J') | KeyCode::PageDown => Action::ScrollDetailDown,
        KeyCode::Char('K') | KeyCode::PageUp => Action::ScrollDetailUp,
        KeyCode::Char('p' | ' ') => Action::TogglePause,
        KeyCode::Char('r') => Action::Refresh,
        _ => Action::Ignore,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn press(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    #[test]
    fn quitting_works_from_every_documented_key() {
        for code in [KeyCode::Char('q'), KeyCode::Esc] {
            assert_eq!(key_action(press(code)), Action::Quit);
        }
        assert_eq!(
            key_action(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)),
            Action::Quit,
            "the alternate screen swallows the terminal's own interrupt, so the app must handle it"
        );
    }

    #[test]
    fn a_plain_c_is_not_a_quit() {
        assert_eq!(key_action(press(KeyCode::Char('c'))), Action::Ignore);
    }

    #[test]
    fn every_panel_has_a_direct_number_key() {
        assert_eq!(
            key_action(press(KeyCode::Char('1'))),
            Action::SelectTab(Tab::Overview)
        );
        assert_eq!(
            key_action(press(KeyCode::Char('2'))),
            Action::SelectTab(Tab::Findings)
        );
        assert_eq!(
            key_action(press(KeyCode::Char('3'))),
            Action::SelectTab(Tab::Instances)
        );
    }

    #[test]
    fn vim_keys_and_arrows_agree() {
        assert_eq!(key_action(press(KeyCode::Char('j'))), Action::Down);
        assert_eq!(key_action(press(KeyCode::Down)), Action::Down);
        assert_eq!(key_action(press(KeyCode::Char('k'))), Action::Up);
        assert_eq!(key_action(press(KeyCode::Up)), Action::Up);
        assert_eq!(key_action(press(KeyCode::Tab)), Action::NextTab);
        assert_eq!(key_action(press(KeyCode::BackTab)), Action::PreviousTab);
    }

    #[test]
    fn shifted_movement_scrolls_the_report_rather_than_changing_the_selection() {
        assert_eq!(
            key_action(press(KeyCode::Char('J'))),
            Action::ScrollDetailDown
        );
        assert_eq!(
            key_action(press(KeyCode::Char('K'))),
            Action::ScrollDetailUp
        );
    }

    #[test]
    fn an_unmapped_key_does_nothing_rather_than_something_surprising() {
        assert_eq!(key_action(press(KeyCode::Char('z'))), Action::Ignore);
        assert_eq!(key_action(press(KeyCode::F(7))), Action::Ignore);
    }

    #[test]
    fn a_key_release_is_not_a_key_press() {
        let release = Event::Key(KeyEvent::new_with_kind(
            KeyCode::Char('q'),
            KeyModifiers::NONE,
            KeyEventKind::Release,
        ));
        assert_eq!(
            action_of(&release),
            Action::Ignore,
            "windows terminals deliver both press and release, and acting on both fires every action twice"
        );
    }

    #[test]
    fn a_resize_is_not_an_action() {
        assert_eq!(action_of(&Event::Resize(80, 24)), Action::Ignore);
    }
}