musichoard/src/tui/listener.rs

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 IEventListener {
fn spawn(self) -> thread::JoinHandle<EventError>;
}
pub struct EventListener {
events: EventSender,
}
// GRCOV_EXCL_START
impl EventListener {
pub fn new(events: EventSender) -> Self {
EventListener { events }
}
}
impl IEventListener for EventListener {
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