2024-09-05 11:23:56 -04:00
|
|
|
use crossterm::event::{self, Event, KeyCode, KeyEventKind};
|
2024-09-12 11:45:42 -04:00
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicBool, Ordering::Relaxed},
|
|
|
|
mpsc::Sender,
|
|
|
|
};
|
2024-04-10 10:02:12 -04:00
|
|
|
|
|
|
|
use super::WatchEvent;
|
|
|
|
|
2024-09-12 11:45:42 -04:00
|
|
|
static INPUT_PAUSED: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
|
|
|
// Private unit type to force using the constructor function.
|
|
|
|
#[must_use = "When the guard is dropped, the input is unpaused"]
|
|
|
|
pub struct InputPauseGuard(());
|
|
|
|
|
|
|
|
impl InputPauseGuard {
|
|
|
|
#[inline]
|
|
|
|
pub fn scoped_pause() -> Self {
|
|
|
|
INPUT_PAUSED.store(true, Relaxed);
|
|
|
|
Self(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for InputPauseGuard {
|
|
|
|
#[inline]
|
|
|
|
fn drop(&mut self) {
|
|
|
|
INPUT_PAUSED.store(false, Relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-10 10:02:12 -04:00
|
|
|
pub enum InputEvent {
|
2024-04-14 11:10:53 -04:00
|
|
|
Run,
|
2024-04-12 09:27:29 -04:00
|
|
|
Next,
|
2024-04-10 10:02:12 -04:00
|
|
|
Hint,
|
|
|
|
List,
|
|
|
|
Quit,
|
|
|
|
}
|
|
|
|
|
2024-09-12 11:45:42 -04:00
|
|
|
pub fn terminal_event_handler(sender: Sender<WatchEvent>, manual_run: bool) {
|
|
|
|
let last_watch_event = loop {
|
|
|
|
match event::read() {
|
|
|
|
Ok(Event::Key(key)) => {
|
2024-04-10 10:02:12 -04:00
|
|
|
match key.kind {
|
2024-05-12 20:20:04 -04:00
|
|
|
KeyEventKind::Release | KeyEventKind::Repeat => continue,
|
|
|
|
KeyEventKind::Press => (),
|
2024-04-10 10:02:12 -04:00
|
|
|
}
|
|
|
|
|
2024-09-12 11:45:42 -04:00
|
|
|
if INPUT_PAUSED.load(Relaxed) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-05-13 10:39:38 -04:00
|
|
|
let input_event = match key.code {
|
2024-09-05 11:12:26 -04:00
|
|
|
KeyCode::Char('n') => InputEvent::Next,
|
|
|
|
KeyCode::Char('h') => InputEvent::Hint,
|
2024-09-12 11:45:42 -04:00
|
|
|
KeyCode::Char('l') => break WatchEvent::Input(InputEvent::List),
|
|
|
|
KeyCode::Char('q') => break WatchEvent::Input(InputEvent::Quit),
|
2024-09-05 11:12:26 -04:00
|
|
|
KeyCode::Char('r') if manual_run => InputEvent::Run,
|
|
|
|
_ => continue,
|
2024-05-13 10:39:38 -04:00
|
|
|
};
|
|
|
|
|
2024-09-12 11:45:42 -04:00
|
|
|
if sender.send(WatchEvent::Input(input_event)).is_err() {
|
2024-05-13 10:39:38 -04:00
|
|
|
return;
|
2024-04-10 10:02:12 -04:00
|
|
|
}
|
|
|
|
}
|
2024-09-12 11:45:42 -04:00
|
|
|
Ok(Event::Resize(width, _)) => {
|
|
|
|
if sender.send(WatchEvent::TerminalResize { width }).is_err() {
|
2024-04-10 10:02:12 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-09-12 11:45:42 -04:00
|
|
|
Ok(Event::FocusGained | Event::FocusLost | Event::Mouse(_)) => continue,
|
|
|
|
Err(e) => break WatchEvent::TerminalEventErr(e),
|
2024-04-10 10:02:12 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-12 11:45:42 -04:00
|
|
|
let _ = sender.send(last_watch_event);
|
2024-04-10 10:02:12 -04:00
|
|
|
}
|