mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 00:00:03 +03:00
Improve input handling
This commit is contained in:
parent
0525739046
commit
f9e35a4344
|
@ -21,8 +21,12 @@ const BAD_INDEX_ERR: &str = "The current exercise index is higher than the numbe
|
|||
|
||||
#[must_use]
|
||||
pub enum ExercisesProgress {
|
||||
// All exercises are done.
|
||||
AllDone,
|
||||
Pending,
|
||||
// The current exercise failed and is still pending.
|
||||
CurrentPending,
|
||||
// A new exercise is now pending.
|
||||
NewPending,
|
||||
}
|
||||
|
||||
pub enum StateFileStatus {
|
||||
|
@ -343,7 +347,7 @@ impl AppState {
|
|||
if let Some(ind) = self.next_pending_exercise_ind() {
|
||||
self.set_current_exercise_ind(ind)?;
|
||||
|
||||
return Ok(ExercisesProgress::Pending);
|
||||
return Ok(ExercisesProgress::NewPending);
|
||||
}
|
||||
|
||||
writer.write_all(RERUNNING_ALL_EXERCISES_MSG)?;
|
||||
|
@ -366,7 +370,7 @@ impl AppState {
|
|||
|
||||
self.write()?;
|
||||
|
||||
return Ok(ExercisesProgress::Pending);
|
||||
return Ok(ExercisesProgress::NewPending);
|
||||
}
|
||||
|
||||
writeln!(writer, "{}", "ok".green())?;
|
||||
|
|
|
@ -41,7 +41,11 @@ pub fn run(app_state: &mut AppState) -> Result<()> {
|
|||
|
||||
match app_state.done_current_exercise(&mut stdout)? {
|
||||
ExercisesProgress::AllDone => (),
|
||||
ExercisesProgress::Pending => println!(
|
||||
ExercisesProgress::CurrentPending => println!(
|
||||
"Current exercise: {}",
|
||||
app_state.current_exercise().terminal_link(),
|
||||
),
|
||||
ExercisesProgress::NewPending => println!(
|
||||
"Next exercise: {}",
|
||||
app_state.current_exercise().terminal_link(),
|
||||
),
|
||||
|
|
|
@ -79,7 +79,8 @@ pub fn watch(
|
|||
match event {
|
||||
WatchEvent::Input(InputEvent::Next) => match watch_state.next_exercise()? {
|
||||
ExercisesProgress::AllDone => break,
|
||||
ExercisesProgress::Pending => watch_state.run_current_exercise()?,
|
||||
ExercisesProgress::CurrentPending => watch_state.render()?,
|
||||
ExercisesProgress::NewPending => watch_state.run_current_exercise()?,
|
||||
},
|
||||
WatchEvent::Input(InputEvent::Hint) => {
|
||||
watch_state.show_hint()?;
|
||||
|
@ -92,9 +93,7 @@ pub fn watch(
|
|||
break;
|
||||
}
|
||||
WatchEvent::Input(InputEvent::Run) => watch_state.run_current_exercise()?,
|
||||
WatchEvent::Input(InputEvent::Unrecognized(input)) => {
|
||||
watch_state.handle_invalid_input(input)?;
|
||||
}
|
||||
WatchEvent::Input(InputEvent::Unrecognized) => watch_state.render()?,
|
||||
WatchEvent::FileChange { exercise_ind } => {
|
||||
watch_state.run_exercise_with_ind(exercise_ind)?;
|
||||
}
|
||||
|
|
|
@ -78,10 +78,7 @@ impl<'a> WatchState<'a> {
|
|||
|
||||
pub fn next_exercise(&mut self) -> Result<ExercisesProgress> {
|
||||
if matches!(self.done_status, DoneStatus::Pending) {
|
||||
self.writer
|
||||
.write_all(b"The current exercise isn't done yet\n")?;
|
||||
self.show_prompt()?;
|
||||
return Ok(ExercisesProgress::Pending);
|
||||
return Ok(ExercisesProgress::CurrentPending);
|
||||
}
|
||||
|
||||
self.app_state.done_current_exercise(&mut self.writer)
|
||||
|
@ -165,12 +162,4 @@ When you are done experimenting, enter `n` (or `next`) to move on to the next ex
|
|||
self.show_hint = true;
|
||||
self.render()
|
||||
}
|
||||
|
||||
pub fn handle_invalid_input(&mut self, input: char) -> io::Result<()> {
|
||||
writeln!(
|
||||
self.writer,
|
||||
"Invalid input: {input} (confusing input can occur after resizing the terminal)",
|
||||
)?;
|
||||
self.show_prompt()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ pub enum InputEvent {
|
|||
Hint,
|
||||
List,
|
||||
Quit,
|
||||
Unrecognized(char),
|
||||
Unrecognized,
|
||||
}
|
||||
|
||||
pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
|
||||
|
@ -42,7 +42,7 @@ pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
|
|||
'l' => break InputEvent::List,
|
||||
'q' => break InputEvent::Quit,
|
||||
'r' if manual_run => InputEvent::Run,
|
||||
_ => InputEvent::Unrecognized(c),
|
||||
_ => InputEvent::Unrecognized,
|
||||
};
|
||||
|
||||
if tx.send(WatchEvent::Input(input_event)).is_err() {
|
||||
|
|
Loading…
Reference in a new issue