2024-09-05 18:23:56 +03:00
|
|
|
use crossterm::event::{self, Event, KeyCode, KeyEventKind};
|
2024-09-26 19:15:45 +03:00
|
|
|
use std::sync::{
|
|
|
|
atomic::Ordering::Relaxed,
|
|
|
|
mpsc::{Receiver, Sender},
|
|
|
|
};
|
2024-04-10 17:02:12 +03:00
|
|
|
|
2024-09-18 02:43:48 +03:00
|
|
|
use super::{WatchEvent, EXERCISE_RUNNING};
|
2024-09-12 18:45:42 +03:00
|
|
|
|
2024-04-10 17:02:12 +03:00
|
|
|
pub enum InputEvent {
|
2024-04-12 16:27:29 +03:00
|
|
|
Next,
|
2024-09-26 19:15:45 +03:00
|
|
|
Run,
|
2024-04-10 17:02:12 +03:00
|
|
|
Hint,
|
|
|
|
List,
|
2024-09-26 19:15:45 +03:00
|
|
|
Reset,
|
2024-04-10 17:02:12 +03:00
|
|
|
Quit,
|
|
|
|
}
|
|
|
|
|
2024-09-26 19:15:45 +03:00
|
|
|
pub fn terminal_event_handler(
|
|
|
|
sender: Sender<WatchEvent>,
|
|
|
|
unpause_receiver: Receiver<()>,
|
|
|
|
manual_run: bool,
|
|
|
|
) {
|
2024-09-12 18:45:42 +03:00
|
|
|
let last_watch_event = loop {
|
|
|
|
match event::read() {
|
|
|
|
Ok(Event::Key(key)) => {
|
2024-04-10 17:02:12 +03:00
|
|
|
match key.kind {
|
2024-05-13 03:20:04 +03:00
|
|
|
KeyEventKind::Release | KeyEventKind::Repeat => continue,
|
|
|
|
KeyEventKind::Press => (),
|
2024-04-10 17:02:12 +03:00
|
|
|
}
|
|
|
|
|
2024-09-18 02:43:48 +03:00
|
|
|
if EXERCISE_RUNNING.load(Relaxed) {
|
2024-09-12 18:45:42 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-05-13 17:39:38 +03:00
|
|
|
let input_event = match key.code {
|
2024-09-05 18:12:26 +03:00
|
|
|
KeyCode::Char('n') => InputEvent::Next,
|
2024-09-26 19:15:45 +03:00
|
|
|
KeyCode::Char('r') if manual_run => InputEvent::Run,
|
2024-09-05 18:12:26 +03:00
|
|
|
KeyCode::Char('h') => InputEvent::Hint,
|
2024-09-12 18:45:42 +03:00
|
|
|
KeyCode::Char('l') => break WatchEvent::Input(InputEvent::List),
|
2024-09-26 19:15:45 +03:00
|
|
|
KeyCode::Char('x') => {
|
|
|
|
if sender.send(WatchEvent::Input(InputEvent::Reset)).is_err() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pause input until quitting the confirmation prompt.
|
|
|
|
if unpause_receiver.recv().is_err() {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2024-09-12 18:45:42 +03:00
|
|
|
KeyCode::Char('q') => break WatchEvent::Input(InputEvent::Quit),
|
2024-09-05 18:12:26 +03:00
|
|
|
_ => continue,
|
2024-05-13 17:39:38 +03:00
|
|
|
};
|
|
|
|
|
2024-09-12 18:45:42 +03:00
|
|
|
if sender.send(WatchEvent::Input(input_event)).is_err() {
|
2024-05-13 17:39:38 +03:00
|
|
|
return;
|
2024-04-10 17:02:12 +03:00
|
|
|
}
|
|
|
|
}
|
2024-09-12 18:45:42 +03:00
|
|
|
Ok(Event::Resize(width, _)) => {
|
|
|
|
if sender.send(WatchEvent::TerminalResize { width }).is_err() {
|
2024-04-10 17:02:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-09-12 18:45:42 +03:00
|
|
|
Ok(Event::FocusGained | Event::FocusLost | Event::Mouse(_)) => continue,
|
|
|
|
Err(e) => break WatchEvent::TerminalEventErr(e),
|
2024-04-10 17:02:12 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-12 18:45:42 +03:00
|
|
|
let _ = sender.send(last_watch_event);
|
2024-04-10 17:02:12 +03:00
|
|
|
}
|