First one-shot pass

This commit is contained in:
2026-02-10 14:29:23 -05:00
parent 739d79d6a2
commit f65e3d8413
48 changed files with 5409 additions and 2 deletions

49
src/event.rs Normal file
View File

@@ -0,0 +1,49 @@
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use crossterm::event::{self, Event, KeyEvent};
pub enum AppEvent {
Key(KeyEvent),
Tick,
Resize(#[allow(dead_code)] u16, #[allow(dead_code)] u16),
}
pub struct EventHandler {
rx: mpsc::Receiver<AppEvent>,
_tx: mpsc::Sender<AppEvent>,
}
impl EventHandler {
pub fn new(tick_rate: Duration) -> Self {
let (tx, rx) = mpsc::channel();
let _tx = tx.clone();
thread::spawn(move || loop {
if event::poll(tick_rate).unwrap_or(false) {
match event::read() {
Ok(Event::Key(key)) => {
if tx.send(AppEvent::Key(key)).is_err() {
return;
}
}
Ok(Event::Resize(w, h)) => {
if tx.send(AppEvent::Resize(w, h)).is_err() {
return;
}
}
_ => {}
}
} else if tx.send(AppEvent::Tick).is_err() {
return;
}
});
Self { rx, _tx }
}
pub fn next(&self) -> anyhow::Result<AppEvent> {
Ok(self.rx.recv()?)
}
}