Merge pull request 'Non Blocking Events' (#2) from wesley/non-blocking-events into development
Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
28
src/lib.rs
28
src/lib.rs
@@ -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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user