mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 00:00:03 +03:00
Simplify handling terminal events for unbuffered stdin
This commit is contained in:
parent
2d0860fe1b
commit
aa3eda70e5
|
@ -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)?;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
Loading…
Reference in a new issue