50 lines
1.5 KiB
Rust
50 lines
1.5 KiB
Rust
|
use crossterm::event::{self, Event as CrosstermEvent};
|
||
|
use std::thread;
|
||
|
|
||
|
#[cfg(test)]
|
||
|
use mockall::automock;
|
||
|
|
||
|
use super::event::{Event, EventError, EventSender};
|
||
|
|
||
|
#[cfg_attr(test, automock)]
|
||
|
pub trait EventListener {
|
||
|
fn spawn(self) -> thread::JoinHandle<EventError>;
|
||
|
}
|
||
|
|
||
|
pub struct TuiEventListener {
|
||
|
events: EventSender,
|
||
|
}
|
||
|
|
||
|
// GRCOV_EXCL_START
|
||
|
impl TuiEventListener {
|
||
|
pub fn new(events: EventSender) -> Self {
|
||
|
TuiEventListener { events }
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl EventListener for TuiEventListener {
|
||
|
fn spawn(self) -> thread::JoinHandle<EventError> {
|
||
|
thread::spawn(move || {
|
||
|
loop {
|
||
|
// Put this inside an if event::poll {...} if the display needs to be refreshed on a
|
||
|
// periodic basis. See
|
||
|
// https://github.com/tui-rs-revival/rust-tui-template/blob/master/src/event.rs.
|
||
|
match event::read() {
|
||
|
Ok(event) => {
|
||
|
if let Err(err) = match event {
|
||
|
CrosstermEvent::Key(e) => self.events.send(Event::Key(e)),
|
||
|
CrosstermEvent::Mouse(e) => self.events.send(Event::Mouse(e)),
|
||
|
CrosstermEvent::Resize(w, h) => self.events.send(Event::Resize(w, h)),
|
||
|
_ => unimplemented!(),
|
||
|
} {
|
||
|
return err;
|
||
|
}
|
||
|
}
|
||
|
Err(err) => return EventError::Io(err),
|
||
|
};
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
// GRCOV_EXCL_STOP
|