mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-27 15:56:28 +00: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,24 +35,44 @@ 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 {
|
||||||
|
KeyCode::Enter => {
|
||||||
|
if last_input_valid {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
InputEvent::Unrecognized
|
||||||
|
}
|
||||||
|
KeyCode::Char(c) => {
|
||||||
let input_event = match c {
|
let input_event = match c {
|
||||||
'n' => InputEvent::Next,
|
'n' => InputEvent::Next,
|
||||||
'h' => InputEvent::Hint,
|
'h' => InputEvent::Hint,
|
||||||
'l' => break InputEvent::List,
|
'l' => break InputEvent::List,
|
||||||
'q' => break InputEvent::Quit,
|
'q' => break InputEvent::Quit,
|
||||||
'r' if manual_run => InputEvent::Run,
|
'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() {
|
if tx.send(WatchEvent::Input(input_event)).is_err() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Event::Resize(_, _) => {
|
Event::Resize(_, _) => {
|
||||||
if tx.send(WatchEvent::TerminalResize).is_err() {
|
if tx.send(WatchEvent::TerminalResize).is_err() {
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in a new issue