Cleaned up imports a bit and added rustfmt.toml

This commit is contained in:
2023-08-08 10:50:04 -06:00
parent 0d4e283c21
commit 155675b596
6 changed files with 182 additions and 179 deletions
+32 -31
View File
@@ -9,44 +9,45 @@ use crossterm::event::Event as CrosstermEvent;
use crate::event::Key;
pub enum InputEvent<T> {
KeyEvent(T),
Tick
KeyEvent(T),
Tick,
}
pub struct Events {
_tx: Sender<InputEvent<Key>>,
rx: Receiver<InputEvent<Key>>
_tx: Sender<InputEvent<Key>>,
rx: Receiver<InputEvent<Key>>,
}
impl Events {
pub fn new() -> Self {
let (tx, rx) = mpsc::channel();
let tick_rate: Duration = Duration::from_millis(250);
pub fn new() -> Self {
let (tx, rx) = mpsc::channel();
let tick_rate: Duration = Duration::from_millis(250);
let event_tx = tx.clone();
thread::spawn(move || {
let mut last_tick = Instant::now();
loop {
let timeout = tick_rate.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
if event::poll(timeout).unwrap() {
if let CrosstermEvent::Key(key) = event::read().unwrap() {
let key = Key::from(key);
event_tx.send(InputEvent::KeyEvent(key)).unwrap();
}
}
let event_tx = tx.clone();
thread::spawn(move || {
let mut last_tick = Instant::now();
loop {
let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
if event::poll(timeout).unwrap() {
if let CrosstermEvent::Key(key) = event::read().unwrap() {
let key = Key::from(key);
event_tx.send(InputEvent::KeyEvent(key)).unwrap();
}
}
if last_tick.elapsed() >= tick_rate {
event_tx.send(InputEvent::Tick).unwrap();
last_tick = Instant::now();
}
}
});
if last_tick.elapsed() >= tick_rate {
event_tx.send(InputEvent::Tick).unwrap();
last_tick = Instant::now();
}
}
});
Events { _tx: tx, rx }
}
Events { _tx: tx, rx }
}
pub fn next(&self) -> Result<InputEvent<Key>, mpsc::RecvError> {
self.rx.recv()
}
}
pub fn next(&self) -> Result<InputEvent<Key>, mpsc::RecvError> {
self.rx.recv()
}
}