Simplify handling terminal events for unbuffered stdin

This commit is contained in:
mo8it 2024-09-05 17:12:26 +02:00
parent 2d0860fe1b
commit aa3eda70e5
2 changed files with 6 additions and 37 deletions

View file

@ -95,7 +95,6 @@ fn run_watch(
break; break;
} }
WatchEvent::Input(InputEvent::Run) => watch_state.run_current_exercise(&mut stdout)?, WatchEvent::Input(InputEvent::Run) => watch_state.run_current_exercise(&mut stdout)?,
WatchEvent::Input(InputEvent::Unrecognized) => watch_state.render(&mut stdout)?,
WatchEvent::FileChange { exercise_ind } => { WatchEvent::FileChange { exercise_ind } => {
watch_state.handle_file_change(exercise_ind, &mut stdout)?; watch_state.handle_file_change(exercise_ind, &mut stdout)?;
} }

View file

@ -9,13 +9,9 @@ pub enum InputEvent {
Hint, Hint,
List, List,
Quit, Quit,
Unrecognized,
} }
pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) { pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
// Only send `Unrecognized` on ENTER if the last input wasn't valid.
let mut last_input_valid = false;
let last_input_event = loop { let last_input_event = loop {
let terminal_event = match event::read() { let terminal_event = match event::read() {
Ok(v) => v, Ok(v) => v,
@ -34,39 +30,13 @@ pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
KeyEventKind::Press => (), KeyEventKind::Press => (),
} }
if key.modifiers != KeyModifiers::NONE {
last_input_valid = false;
continue;
}
let input_event = match key.code { let input_event = match key.code {
KeyCode::Enter => { KeyCode::Char('n') => InputEvent::Next,
if last_input_valid { KeyCode::Char('h') => InputEvent::Hint,
continue; KeyCode::Char('l') => break InputEvent::List,
} KeyCode::Char('q') => break InputEvent::Quit,
KeyCode::Char('r') if manual_run => InputEvent::Run,
InputEvent::Unrecognized _ => continue,
}
KeyCode::Char(c) => {
let input_event = match c {
'n' => InputEvent::Next,
'h' => InputEvent::Hint,
'l' => break InputEvent::List,
'q' => break InputEvent::Quit,
'r' if manual_run => InputEvent::Run,
_ => {
last_input_valid = false;
continue;
}
};
last_input_valid = true;
input_event
}
_ => {
last_input_valid = false;
continue;
}
}; };
if tx.send(WatchEvent::Input(input_event)).is_err() { if tx.send(WatchEvent::Input(input_event)).is_err() {