2024-09-05 11:23:56 -04:00
|
|
|
use crossterm::event::{self, Event, KeyCode, KeyEventKind};
|
2024-04-10 10:02:12 -04:00
|
|
|
use std::sync::mpsc::Sender;
|
|
|
|
|
|
|
|
use super::WatchEvent;
|
|
|
|
|
|
|
|
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-04-14 11:10:53 -04:00
|
|
|
pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
|
2024-04-10 10:02:12 -04:00
|
|
|
let last_input_event = loop {
|
|
|
|
let terminal_event = match event::read() {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
|
|
|
// If `send` returns an error, then the receiver is dropped and
|
|
|
|
// a shutdown has been already initialized.
|
|
|
|
let _ = tx.send(WatchEvent::TerminalEventErr(e));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match terminal_event {
|
|
|
|
Event::Key(key) => {
|
|
|
|
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-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,
|
|
|
|
KeyCode::Char('l') => break InputEvent::List,
|
|
|
|
KeyCode::Char('q') => break InputEvent::Quit,
|
|
|
|
KeyCode::Char('r') if manual_run => InputEvent::Run,
|
|
|
|
_ => continue,
|
2024-05-13 10:39:38 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if tx.send(WatchEvent::Input(input_event)).is_err() {
|
|
|
|
return;
|
2024-04-10 10:02:12 -04:00
|
|
|
}
|
|
|
|
}
|
2024-09-05 11:45:27 -04:00
|
|
|
Event::Resize(width, _) => {
|
|
|
|
if tx.send(WatchEvent::TerminalResize { width }).is_err() {
|
2024-04-10 10:02:12 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-08-23 18:14:12 -04:00
|
|
|
Event::FocusGained | Event::FocusLost | Event::Mouse(_) => continue,
|
2024-04-10 10:02:12 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let _ = tx.send(WatchEvent::Input(last_input_event));
|
|
|
|
}
|