use std::sync::mpsc::{self, Receiver};
use std::thread;
use std::time::Duration;
use crossterm::event::{self, Event, KeyEventKind};
pub enum AppEvent {
Key(crossterm::event::KeyEvent),
Tick,
Resize,
}
pub fn setup_event_channel() -> Receiver<AppEvent> {
let (tx, rx) = mpsc::channel::<AppEvent>();
let tx_tick = tx.clone();
thread::spawn(move || loop {
thread::sleep(Duration::from_millis(50));
if tx_tick.send(AppEvent::Tick).is_err() {
break;
}
});
thread::spawn(move || loop {
match event::read() {
Ok(Event::Key(key))
if key.kind == KeyEventKind::Press && tx.send(AppEvent::Key(key)).is_err() => {
break;
}
Ok(Event::Resize(_, _))
if tx.send(AppEvent::Resize).is_err() => {
break;
}
_ => {}
}
});
rx
}