mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-22 05:52:23 -05:00
Fix invisible input on Windows
This commit is contained in:
parent
11fda5d70f
commit
0525739046
3 changed files with 24 additions and 37 deletions
|
@ -92,8 +92,8 @@ pub fn watch(
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
WatchEvent::Input(InputEvent::Run) => watch_state.run_current_exercise()?,
|
WatchEvent::Input(InputEvent::Run) => watch_state.run_current_exercise()?,
|
||||||
WatchEvent::Input(InputEvent::Unrecognized(cmd)) => {
|
WatchEvent::Input(InputEvent::Unrecognized(input)) => {
|
||||||
watch_state.handle_invalid_cmd(&cmd)?;
|
watch_state.handle_invalid_input(input)?;
|
||||||
}
|
}
|
||||||
WatchEvent::FileChange { exercise_ind } => {
|
WatchEvent::FileChange { exercise_ind } => {
|
||||||
watch_state.run_exercise_with_ind(exercise_ind)?;
|
watch_state.run_exercise_with_ind(exercise_ind)?;
|
||||||
|
|
|
@ -166,14 +166,11 @@ When you are done experimenting, enter `n` (or `next`) to move on to the next ex
|
||||||
self.render()
|
self.render()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_invalid_cmd(&mut self, cmd: &str) -> io::Result<()> {
|
pub fn handle_invalid_input(&mut self, input: char) -> io::Result<()> {
|
||||||
self.writer.write_all(b"Invalid command: ")?;
|
writeln!(
|
||||||
self.writer.write_all(cmd.as_bytes())?;
|
self.writer,
|
||||||
if cmd.len() > 1 {
|
"Invalid input: {input} (confusing input can occur after resizing the terminal)",
|
||||||
self.writer
|
)?;
|
||||||
.write_all(b" (confusing input can occur after resizing the terminal)")?;
|
|
||||||
}
|
|
||||||
self.writer.write_all(b"\n")?;
|
|
||||||
self.show_prompt()
|
self.show_prompt()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,12 +9,10 @@ pub enum InputEvent {
|
||||||
Hint,
|
Hint,
|
||||||
List,
|
List,
|
||||||
Quit,
|
Quit,
|
||||||
Unrecognized(String),
|
Unrecognized(char),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
|
pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
|
||||||
let mut input = String::with_capacity(8);
|
|
||||||
|
|
||||||
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,
|
||||||
|
@ -28,36 +26,28 @@ pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
|
||||||
|
|
||||||
match terminal_event {
|
match terminal_event {
|
||||||
Event::Key(key) => {
|
Event::Key(key) => {
|
||||||
|
match key.kind {
|
||||||
|
KeyEventKind::Release | KeyEventKind::Repeat => continue,
|
||||||
|
KeyEventKind::Press => (),
|
||||||
|
}
|
||||||
|
|
||||||
if key.modifiers != KeyModifiers::NONE {
|
if key.modifiers != KeyModifiers::NONE {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
match key.kind {
|
if let KeyCode::Char(c) = key.code {
|
||||||
KeyEventKind::Release => continue,
|
let input_event = match c {
|
||||||
KeyEventKind::Press | KeyEventKind::Repeat => (),
|
'n' => InputEvent::Next,
|
||||||
}
|
'h' => InputEvent::Hint,
|
||||||
|
'l' => break InputEvent::List,
|
||||||
|
'q' => break InputEvent::Quit,
|
||||||
|
'r' if manual_run => InputEvent::Run,
|
||||||
|
_ => InputEvent::Unrecognized(c),
|
||||||
|
};
|
||||||
|
|
||||||
match key.code {
|
if tx.send(WatchEvent::Input(input_event)).is_err() {
|
||||||
KeyCode::Enter => {
|
return;
|
||||||
let input_event = match input.trim() {
|
|
||||||
"n" | "next" => InputEvent::Next,
|
|
||||||
"h" | "hint" => InputEvent::Hint,
|
|
||||||
"l" | "list" => break InputEvent::List,
|
|
||||||
"q" | "quit" => break InputEvent::Quit,
|
|
||||||
"r" | "run" if manual_run => InputEvent::Run,
|
|
||||||
_ => InputEvent::Unrecognized(input.clone()),
|
|
||||||
};
|
|
||||||
|
|
||||||
if tx.send(WatchEvent::Input(input_event)).is_err() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
input.clear();
|
|
||||||
}
|
}
|
||||||
KeyCode::Char(c) => {
|
|
||||||
input.push(c);
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Resize(_, _) => {
|
Event::Resize(_, _) => {
|
||||||
|
|
Loading…
Reference in a new issue