Only send Unrecognized on ENTER if the last input wasn't valid

This commit is contained in:
mo8it 2024-05-13 16:39:38 +02:00
parent 56eb4a5d65
commit a7bc6d53a5

View file

@ -13,6 +13,9 @@ pub enum InputEvent {
}
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 terminal_event = match event::read() {
Ok(v) => v,
@ -32,24 +35,44 @@ pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
}
if key.modifiers != KeyModifiers::NONE {
last_input_valid = false;
continue;
}
if let KeyCode::Char(c) = key.code {
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,
_ => InputEvent::Unrecognized,
_ => {
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() {
return;
}
}
}
Event::Resize(_, _) => {
if tx.send(WatchEvent::TerminalResize).is_err() {
return;