diff --git a/src/lib.rs b/src/lib.rs index 73f23a7..3c5a60c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ -use crossterm::event::{self, Event}; -use std::error::Error; +use crossterm::event::{self, Event, KeyCode}; +use std::{error::Error, time::Duration}; pub mod cli; pub mod pomodoro; @@ -8,13 +8,35 @@ pub mod ui; use ui::draw_ui; pub fn run() -> Result<(), Box> { + let mut keep_running; let mut terminal = ratatui::init(); loop { draw_ui(&mut terminal)?; - if matches!(event::read().expect("failed to read event"), Event::Key(_)) { + + keep_running = process_events()?; + + if !keep_running { break; } } ratatui::restore(); Ok(()) } + +fn process_events() -> Result> { + if !event::poll(Duration::from_secs(0))? { + return Ok(true); + } + + if let Event::Key(key) = event::read()? { + return Ok(handle_key(key.code)); + } + Ok(true) +} + +fn handle_key(code: KeyCode) -> bool { + if let KeyCode::Char('q') = code { + return false; + } + true +}