1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2024-11-08 09:09:17 +01:00

Show the invalid command to avoid confusion after resizing the terminal

This commit is contained in:
mo8it 2024-04-10 04:08:40 +02:00
parent a46d66134b
commit 6255efe8b2
2 changed files with 17 additions and 16 deletions

@ -27,13 +27,12 @@ pub enum WatchExit {
List, List,
} }
#[derive(Copy, Clone)]
enum InputEvent { enum InputEvent {
Hint, Hint,
Clear, Clear,
List, List,
Quit, Quit,
Unrecognized, Unrecognized(String),
} }
enum WatchEvent { enum WatchEvent {
@ -85,7 +84,7 @@ impl notify_debouncer_mini::DebounceEventHandler for DebouceEventHandler {
fn terminal_event_handler(tx: Sender<WatchEvent>) { fn terminal_event_handler(tx: Sender<WatchEvent>) {
let mut input = String::with_capacity(8); let mut input = String::with_capacity(8);
loop { let last_input_event = loop {
let terminal_event = match event::read() { let terminal_event = match event::read() {
Ok(v) => v, Ok(v) => v,
Err(e) => { Err(e) => {
@ -108,20 +107,15 @@ fn terminal_event_handler(tx: Sender<WatchEvent>) {
let input_event = match input.trim() { let input_event = match input.trim() {
"h" | "hint" => InputEvent::Hint, "h" | "hint" => InputEvent::Hint,
"c" | "clear" => InputEvent::Clear, "c" | "clear" => InputEvent::Clear,
"l" | "list" => InputEvent::List, "l" | "list" => break InputEvent::List,
"q" | "quit" => InputEvent::Quit, "q" | "quit" => break InputEvent::Quit,
_ => InputEvent::Unrecognized, _ => InputEvent::Unrecognized(input.clone()),
}; };
if tx.send(WatchEvent::Input(input_event)).is_err() { if tx.send(WatchEvent::Input(input_event)).is_err() {
return; return;
} }
match input_event {
InputEvent::List | InputEvent::Quit => return,
_ => (),
}
input.clear(); input.clear();
} }
KeyCode::Char(c) => { KeyCode::Char(c) => {
@ -137,7 +131,9 @@ fn terminal_event_handler(tx: Sender<WatchEvent>) {
} }
Event::FocusGained | Event::FocusLost | Event::Mouse(_) | Event::Paste(_) => continue, Event::FocusGained | Event::FocusLost | Event::Mouse(_) | Event::Paste(_) => continue,
} }
} };
let _ = tx.send(WatchEvent::Input(last_input_event));
} }
pub fn watch(state_file: &mut StateFile, exercises: &'static [Exercise]) -> Result<WatchExit> { pub fn watch(state_file: &mut StateFile, exercises: &'static [Exercise]) -> Result<WatchExit> {
@ -173,8 +169,8 @@ pub fn watch(state_file: &mut StateFile, exercises: &'static [Exercise]) -> Resu
watch_state.render()?; watch_state.render()?;
} }
WatchEvent::Input(InputEvent::Quit) => break, WatchEvent::Input(InputEvent::Quit) => break,
WatchEvent::Input(InputEvent::Unrecognized) => { WatchEvent::Input(InputEvent::Unrecognized(cmd)) => {
watch_state.handle_invalid_cmd()?; watch_state.handle_invalid_cmd(&cmd)?;
} }
WatchEvent::FileChange { exercise_ind } => { WatchEvent::FileChange { exercise_ind } => {
// TODO: bool // TODO: bool

@ -159,8 +159,13 @@ You can keep working on this exercise or jump into the next one by removing the
self.show_prompt() self.show_prompt()
} }
pub fn handle_invalid_cmd(&mut self) -> io::Result<()> { pub fn handle_invalid_cmd(&mut self, cmd: &str) -> io::Result<()> {
self.writer.write_all(b"Invalid command")?; self.writer.write_all(b"Invalid command: ")?;
self.writer.write_all(cmd.as_bytes())?;
if cmd.len() > 1 {
self.writer
.write_all(b" (confusing input can occur after resizing the terminal)")?;
}
self.show_prompt() self.show_prompt()
} }
} }