mirror of
https://github.com/notohh/rustlings.git
synced 2024-10-30 08:49:12 -04:00
Only send Unrecognized
on ENTER if the last input wasn't valid
This commit is contained in:
parent
56eb4a5d65
commit
a7bc6d53a5
1 changed files with 34 additions and 11 deletions
|
@ -13,6 +13,9 @@ pub enum InputEvent {
|
||||||
}
|
}
|
||||||
|
|
||||||
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,
|
||||||
|
@ -32,22 +35,42 @@ pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if key.modifiers != KeyModifiers::NONE {
|
if key.modifiers != KeyModifiers::NONE {
|
||||||
|
last_input_valid = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let KeyCode::Char(c) = key.code {
|
let input_event = match key.code {
|
||||||
let input_event = match c {
|
KeyCode::Enter => {
|
||||||
'n' => InputEvent::Next,
|
if last_input_valid {
|
||||||
'h' => InputEvent::Hint,
|
continue;
|
||||||
'l' => break InputEvent::List,
|
}
|
||||||
'q' => break InputEvent::Quit,
|
|
||||||
'r' if manual_run => InputEvent::Run,
|
|
||||||
_ => InputEvent::Unrecognized,
|
|
||||||
};
|
|
||||||
|
|
||||||
if tx.send(WatchEvent::Input(input_event)).is_err() {
|
InputEvent::Unrecognized
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
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() {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Resize(_, _) => {
|
Event::Resize(_, _) => {
|
||||||
|
|
Loading…
Reference in a new issue