use std::sync::mpsc::{self, Receiver, Sender};
use std::thread;
use blubat_core::Snapshot;
use crossterm::event::{self, Event as Terminal, KeyCode, KeyEventKind, KeyModifiers};
use super::app::{Event, Key};
pub fn events(readings: Receiver<Snapshot>) -> (Sender<Event>, Receiver<Event>) {
let (sender, events) = mpsc::channel();
let keys = sender.clone();
let others = sender.clone();
thread::spawn(move || forward(readings.into_iter().map(Event::Reading), &sender));
thread::spawn(move || forward(keypresses(), &keys));
(others, events)
}
fn forward(source: impl Iterator<Item = Event>, sink: &Sender<Event>) {
for event in source {
if sink.send(event).is_err() {
break;
}
}
}
fn keypresses() -> impl Iterator<Item = Event> {
std::iter::from_fn(|| {
loop {
match event::read() {
Ok(terminal) => {
if let Some(event) = pressed(&terminal) {
return Some(event);
}
}
Err(_) => return None,
}
}
})
}
fn pressed(terminal: &Terminal) -> Option<Event> {
match terminal {
Terminal::Key(key) if key.kind == KeyEventKind::Press => {
let control = key.modifiers.contains(KeyModifiers::CONTROL);
match key.code {
KeyCode::Char('c') if control => Some(Event::Interrupt),
KeyCode::Char(_) if control => None,
KeyCode::Char(key) => Some(Event::Key(Key::Char(key))),
KeyCode::Enter => Some(Event::Key(Key::Enter)),
KeyCode::Esc => Some(Event::Key(Key::Escape)),
KeyCode::Backspace => Some(Event::Key(Key::Backspace)),
_ => None,
}
}
_ => None,
}
}
#[cfg(test)]
mod tests {
use crossterm::event::KeyEvent;
use super::*;
fn key(code: KeyCode, kind: KeyEventKind) -> Terminal {
Terminal::Key(KeyEvent::new_with_kind(code, KeyModifiers::NONE, kind))
}
fn control(code: KeyCode) -> Terminal {
Terminal::Key(KeyEvent::new_with_kind(
code,
KeyModifiers::CONTROL,
KeyEventKind::Press,
))
}
#[test]
fn a_source_reaches_the_loop_in_order() {
let (sender, events) = mpsc::channel();
forward(
['j', 'k', 'q']
.into_iter()
.map(|key| Event::Key(Key::Char(key))),
&sender,
);
drop(sender);
assert_eq!(
events.into_iter().collect::<Vec<_>>(),
[
Event::Key(Key::Char('j')),
Event::Key(Key::Char('k')),
Event::Key(Key::Char('q'))
]
);
}
#[test]
fn a_source_stops_once_the_loop_does() {
let (sender, events) = mpsc::channel();
let mut produced = 0;
drop(events);
forward(
std::iter::repeat_with(|| {
produced += 1;
Event::Key(Key::Char('j'))
}),
&sender,
);
assert_eq!(produced, 1, "an endless source stops at the first failure");
}
#[test]
fn only_a_pressed_key_the_dashboard_binds_reaches_it() {
for (code, expected) in [
(KeyCode::Char('q'), Key::Char('q')),
(KeyCode::Enter, Key::Enter),
(KeyCode::Esc, Key::Escape),
(KeyCode::Backspace, Key::Backspace),
] {
assert_eq!(
pressed(&key(code, KeyEventKind::Press)),
Some(Event::Key(expected))
);
}
assert_eq!(
pressed(&key(KeyCode::Char('q'), KeyEventKind::Release)),
None
);
assert_eq!(pressed(&key(KeyCode::Tab, KeyEventKind::Press)), None);
assert_eq!(pressed(&Terminal::Resize(80, 24)), None);
}
#[test]
fn ctrl_c_interrupts_and_no_other_chord_types_a_character() {
assert_eq!(
pressed(&control(KeyCode::Char('c'))),
Some(Event::Interrupt)
);
assert_eq!(
pressed(&control(KeyCode::Char('u'))),
None,
"a chord blubat does not bind is not the letter in it"
);
assert_eq!(
pressed(&key(KeyCode::Char('c'), KeyEventKind::Press)),
Some(Event::Key(Key::Char('c'))),
"and c on its own is still a c"
);
}
}