From aa3eda70e5727f9665a97fadcce081a96909e9b1 Mon Sep 17 00:00:00 2001 From: mo8it Date: Thu, 5 Sep 2024 17:12:26 +0200 Subject: [PATCH] Simplify handling terminal events for unbuffered stdin --- src/watch.rs | 1 - src/watch/terminal_event.rs | 42 ++++++------------------------------- 2 files changed, 6 insertions(+), 37 deletions(-) diff --git a/src/watch.rs b/src/watch.rs index be8409f..ee5dd74 100644 --- a/src/watch.rs +++ b/src/watch.rs @@ -95,7 +95,6 @@ fn run_watch( break; } WatchEvent::Input(InputEvent::Run) => watch_state.run_current_exercise(&mut stdout)?, - WatchEvent::Input(InputEvent::Unrecognized) => watch_state.render(&mut stdout)?, WatchEvent::FileChange { exercise_ind } => { watch_state.handle_file_change(exercise_ind, &mut stdout)?; } diff --git a/src/watch/terminal_event.rs b/src/watch/terminal_event.rs index 3e8c272..8e2e241 100644 --- a/src/watch/terminal_event.rs +++ b/src/watch/terminal_event.rs @@ -9,13 +9,9 @@ pub enum InputEvent { Hint, List, Quit, - Unrecognized, } pub fn terminal_event_handler(tx: Sender, 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 terminal_event = match event::read() { Ok(v) => v, @@ -34,39 +30,13 @@ pub fn terminal_event_handler(tx: Sender, manual_run: bool) { KeyEventKind::Press => (), } - if key.modifiers != KeyModifiers::NONE { - last_input_valid = false; - continue; - } - let input_event = match key.code { - KeyCode::Enter => { - if last_input_valid { - continue; - } - - InputEvent::Unrecognized - } - 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; - } + 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, }; if tx.send(WatchEvent::Input(input_event)).is_err() {