rustlings/src/watch/terminal_event.rs

74 lines
2.3 KiB
Rust
Raw Normal View History

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,
CheckAll,
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;
}
let input_event = match key.code {
KeyCode::Char('n') => InputEvent::Next,
2024-09-26 19:15:45 +03:00
KeyCode::Char('r') if manual_run => InputEvent::Run,
KeyCode::Char('h') => InputEvent::Hint,
2024-09-12 18:45:42 +03:00
KeyCode::Char('l') => break WatchEvent::Input(InputEvent::List),
KeyCode::Char('c') => InputEvent::CheckAll,
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),
_ => continue,
};
2024-09-12 18:45:42 +03:00
if sender.send(WatchEvent::Input(input_event)).is_err() {
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
}