Non Blocking Events #2

Merged
wesley merged 1 commits from wesley/non-blocking-events into development 2025-09-09 23:04:45 -04:00

View File

@@ -1,5 +1,5 @@
use crossterm::event::{self, Event}; use crossterm::event::{self, Event, KeyCode};
use std::error::Error; use std::{error::Error, time::Duration};
pub mod cli; pub mod cli;
pub mod pomodoro; pub mod pomodoro;
@@ -8,13 +8,35 @@ pub mod ui;
use ui::draw_ui; use ui::draw_ui;
pub fn run() -> Result<(), Box<dyn Error>> { pub fn run() -> Result<(), Box<dyn Error>> {
let mut keep_running;
let mut terminal = ratatui::init(); let mut terminal = ratatui::init();
loop { loop {
draw_ui(&mut terminal)?; draw_ui(&mut terminal)?;
if matches!(event::read().expect("failed to read event"), Event::Key(_)) {
keep_running = process_events()?;
if !keep_running {
break; break;
} }
} }
ratatui::restore(); ratatui::restore();
Ok(()) Ok(())
} }
fn process_events() -> Result<bool, Box<dyn Error>> {
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
}