use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; use super::{app::App, event::{Event, EventReceiver}}; pub struct EventHandler { events: EventReceiver, } impl EventHandler { pub fn new(events: EventReceiver) -> Self { EventHandler { events } } pub fn handle_next_event(&mut self, app: &mut App) { match self.events.recv() { Event::Key(key_event) => Self::handle_key_event(app, key_event), Event::Mouse(_) => {} Event::Resize(_, _) => {} } } fn handle_key_event(app: &mut App, key_event: KeyEvent) { match key_event.code { // Exit application on `ESC` or `q`. KeyCode::Esc | KeyCode::Char('q') => { app.quit(); } // Exit application on `Ctrl-C`. KeyCode::Char('c') | KeyCode::Char('C') => { if key_event.modifiers == KeyModifiers::CONTROL { app.quit(); } } // Category change. KeyCode::Left => { app.decrement_category(); } KeyCode::Right => { app.increment_category(); } // Selection change. KeyCode::Up => { app.decrement_selection(); } KeyCode::Down => { app.increment_selection(); } // Other keys. _ => {} } } }