Non Blocking Events #2
28
src/lib.rs
28
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<dyn Error>> {
|
||||
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<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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user