From 59608ce5f25f89de3e5325df34f68de6208750bd Mon Sep 17 00:00:00 2001 From: Wesley Irvin Date: Tue, 9 Sep 2025 23:01:31 -0400 Subject: [PATCH] Non Blocking Events Changed how we process events by polling first to see if there are events to process. This keeps the app real-time and not hanging waiting for keypress. --- src/lib.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) 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 +} -- 2.49.1