use crossterm::event::{self, Event, KeyEvent, KeyEventKind};
use std::{
path::PathBuf,
sync::mpsc::Sender,
thread::{self, JoinHandle},
time::Duration,
};
#[derive(Debug)]
pub enum AppEvent {
Key(KeyEvent),
Tick,
Log(String),
FileStarted(usize),
FileDone(usize, bool),
AllDone { succeeded: usize, failed: usize },
CancelAll,
CancelledWithResidual(Vec<PathBuf>),
}
pub fn spawn_input_thread(tx: Sender<AppEvent>) -> JoinHandle<()> {
thread::spawn(move || loop {
const POLL_TIMEOUT: Duration = Duration::from_millis(200);
let polled = event::poll(POLL_TIMEOUT).unwrap_or(false);
if polled {
if let Ok(Event::Key(key)) = event::read() {
if key.kind == KeyEventKind::Press && tx.send(AppEvent::Key(key)).is_err() {
break;
}
}
} else if tx.send(AppEvent::Tick).is_err() {
break;
}
})
}