mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-27 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]
|
#[must_use]
|
||||||
pub enum ExercisesProgress {
|
pub enum ExercisesProgress {
|
||||||
|
// All exercises are done.
|
||||||
AllDone,
|
AllDone,
|
||||||
Pending,
|
// The current exercise failed and is still pending.
|
||||||
|
CurrentPending,
|
||||||
|
// A new exercise is now pending.
|
||||||
|
NewPending,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum StateFileStatus {
|
pub enum StateFileStatus {
|
||||||
|
@ -343,7 +347,7 @@ impl AppState {
|
||||||
if let Some(ind) = self.next_pending_exercise_ind() {
|
if let Some(ind) = self.next_pending_exercise_ind() {
|
||||||
self.set_current_exercise_ind(ind)?;
|
self.set_current_exercise_ind(ind)?;
|
||||||
|
|
||||||
return Ok(ExercisesProgress::Pending);
|
return Ok(ExercisesProgress::NewPending);
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.write_all(RERUNNING_ALL_EXERCISES_MSG)?;
|
writer.write_all(RERUNNING_ALL_EXERCISES_MSG)?;
|
||||||
|
@ -366,7 +370,7 @@ impl AppState {
|
||||||
|
|
||||||
self.write()?;
|
self.write()?;
|
||||||
|
|
||||||
return Ok(ExercisesProgress::Pending);
|
return Ok(ExercisesProgress::NewPending);
|
||||||
}
|
}
|
||||||
|
|
||||||
writeln!(writer, "{}", "ok".green())?;
|
writeln!(writer, "{}", "ok".green())?;
|
||||||
|
|
|
@ -41,7 +41,11 @@ pub fn run(app_state: &mut AppState) -> Result<()> {
|
||||||
|
|
||||||
match app_state.done_current_exercise(&mut stdout)? {
|
match app_state.done_current_exercise(&mut stdout)? {
|
||||||
ExercisesProgress::AllDone => (),
|
ExercisesProgress::AllDone => (),
|
||||||
ExercisesProgress::Pending => println!(
|
ExercisesProgress::CurrentPending => println!(
|
||||||
|
"Current exercise: {}",
|
||||||
|
app_state.current_exercise().terminal_link(),
|
||||||
|
),
|
||||||
|
ExercisesProgress::NewPending => println!(
|
||||||
"Next exercise: {}",
|
"Next exercise: {}",
|
||||||
app_state.current_exercise().terminal_link(),
|
app_state.current_exercise().terminal_link(),
|
||||||
),
|
),
|
||||||
|
|
|
@ -79,7 +79,8 @@ pub fn watch(
|
||||||
match event {
|
match event {
|
||||||
WatchEvent::Input(InputEvent::Next) => match watch_state.next_exercise()? {
|
WatchEvent::Input(InputEvent::Next) => match watch_state.next_exercise()? {
|
||||||
ExercisesProgress::AllDone => break,
|
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) => {
|
WatchEvent::Input(InputEvent::Hint) => {
|
||||||
watch_state.show_hint()?;
|
watch_state.show_hint()?;
|
||||||
|
@ -92,9 +93,7 @@ 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(input)) => {
|
WatchEvent::Input(InputEvent::Unrecognized) => watch_state.render()?,
|
||||||
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)?;
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,10 +78,7 @@ impl<'a> WatchState<'a> {
|
||||||
|
|
||||||
pub fn next_exercise(&mut self) -> Result<ExercisesProgress> {
|
pub fn next_exercise(&mut self) -> Result<ExercisesProgress> {
|
||||||
if matches!(self.done_status, DoneStatus::Pending) {
|
if matches!(self.done_status, DoneStatus::Pending) {
|
||||||
self.writer
|
return Ok(ExercisesProgress::CurrentPending);
|
||||||
.write_all(b"The current exercise isn't done yet\n")?;
|
|
||||||
self.show_prompt()?;
|
|
||||||
return Ok(ExercisesProgress::Pending);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.app_state.done_current_exercise(&mut self.writer)
|
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.show_hint = true;
|
||||||
self.render()
|
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,
|
Hint,
|
||||||
List,
|
List,
|
||||||
Quit,
|
Quit,
|
||||||
Unrecognized(char),
|
Unrecognized,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
|
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,
|
'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(c),
|
_ => InputEvent::Unrecognized,
|
||||||
};
|
};
|
||||||
|
|
||||||
if tx.send(WatchEvent::Input(input_event)).is_err() {
|
if tx.send(WatchEvent::Input(input_event)).is_err() {
|
||||||
|
|
Loading…
Reference in a new issue