mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-01-28 00:00:03 +03:00
add command to go back to the previous exercise
This commit is contained in:
parent
1aec7c1152
commit
085d91d9b7
|
@ -95,6 +95,9 @@ fn run_watch(
|
||||||
|
|
||||||
while let Ok(event) = watch_event_receiver.recv() {
|
while let Ok(event) = watch_event_receiver.recv() {
|
||||||
match event {
|
match event {
|
||||||
|
WatchEvent::Input(InputEvent::Previous) => {
|
||||||
|
watch_state.previous_exercise(&mut stdout)?
|
||||||
|
}
|
||||||
WatchEvent::Input(InputEvent::Next) => match watch_state.next_exercise(&mut stdout)? {
|
WatchEvent::Input(InputEvent::Next) => match watch_state.next_exercise(&mut stdout)? {
|
||||||
ExercisesProgress::AllDone => break,
|
ExercisesProgress::AllDone => break,
|
||||||
ExercisesProgress::NewPending => watch_state.run_current_exercise(&mut stdout)?,
|
ExercisesProgress::NewPending => watch_state.run_current_exercise(&mut stdout)?,
|
||||||
|
|
|
@ -92,12 +92,7 @@ impl<'a> WatchState<'a> {
|
||||||
.run_exercise(Some(&mut self.output), self.app_state.cmd_runner())?;
|
.run_exercise(Some(&mut self.output), self.app_state.cmd_runner())?;
|
||||||
self.output.push(b'\n');
|
self.output.push(b'\n');
|
||||||
if success {
|
if success {
|
||||||
self.done_status =
|
self.done_status = self.completed_exercise_status()?;
|
||||||
if let Some(solution_path) = self.app_state.current_solution_path()? {
|
|
||||||
DoneStatus::DoneWithSolution(solution_path)
|
|
||||||
} else {
|
|
||||||
DoneStatus::DoneWithoutSolution
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
self.app_state
|
self.app_state
|
||||||
.set_pending(self.app_state.current_exercise_ind())?;
|
.set_pending(self.app_state.current_exercise_ind())?;
|
||||||
|
@ -170,6 +165,17 @@ impl<'a> WatchState<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn show_prompt(&self, stdout: &mut StdoutLock) -> io::Result<()> {
|
fn show_prompt(&self, stdout: &mut StdoutLock) -> io::Result<()> {
|
||||||
|
if self.app_state.current_exercise_ind() != 0 {
|
||||||
|
stdout.queue(SetAttribute(Attribute::Bold))?;
|
||||||
|
stdout.write_all(b"p")?;
|
||||||
|
stdout.queue(ResetColor)?;
|
||||||
|
stdout.write_all(b":")?;
|
||||||
|
stdout.queue(SetAttribute(Attribute::Underlined))?;
|
||||||
|
stdout.write_all(b"previous")?;
|
||||||
|
stdout.queue(ResetColor)?;
|
||||||
|
stdout.write_all(b" / ")?;
|
||||||
|
}
|
||||||
|
|
||||||
if self.done_status != DoneStatus::Pending {
|
if self.done_status != DoneStatus::Pending {
|
||||||
stdout.queue(SetAttribute(Attribute::Bold))?;
|
stdout.queue(SetAttribute(Attribute::Bold))?;
|
||||||
stdout.write_all(b"n")?;
|
stdout.write_all(b"n")?;
|
||||||
|
@ -295,4 +301,31 @@ impl<'a> WatchState<'a> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn completed_exercise_status(&self) -> Result<DoneStatus> {
|
||||||
|
if let Some(solution_path) = self.app_state.current_solution_path()? {
|
||||||
|
Ok(DoneStatus::DoneWithSolution(solution_path))
|
||||||
|
} else {
|
||||||
|
Ok(DoneStatus::DoneWithoutSolution)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn previous_exercise(&mut self, stdout: &mut StdoutLock) -> Result<()> {
|
||||||
|
let current_exercise_ind = self.app_state.current_exercise_ind();
|
||||||
|
|
||||||
|
if current_exercise_ind == 0 {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
self.app_state
|
||||||
|
.set_current_exercise_ind(current_exercise_ind - 1)?;
|
||||||
|
self.done_status = if self.app_state.current_exercise().done {
|
||||||
|
self.completed_exercise_status()?
|
||||||
|
} else {
|
||||||
|
DoneStatus::Pending
|
||||||
|
};
|
||||||
|
self.render(stdout)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ use std::sync::{
|
||||||
use super::{WatchEvent, EXERCISE_RUNNING};
|
use super::{WatchEvent, EXERCISE_RUNNING};
|
||||||
|
|
||||||
pub enum InputEvent {
|
pub enum InputEvent {
|
||||||
|
Previous,
|
||||||
Next,
|
Next,
|
||||||
Run,
|
Run,
|
||||||
Hint,
|
Hint,
|
||||||
|
@ -34,6 +35,7 @@ pub fn terminal_event_handler(
|
||||||
}
|
}
|
||||||
|
|
||||||
let input_event = match key.code {
|
let input_event = match key.code {
|
||||||
|
KeyCode::Char('p') => InputEvent::Previous,
|
||||||
KeyCode::Char('n') => InputEvent::Next,
|
KeyCode::Char('n') => InputEvent::Next,
|
||||||
KeyCode::Char('r') if manual_run => InputEvent::Run,
|
KeyCode::Char('r') if manual_run => InputEvent::Run,
|
||||||
KeyCode::Char('h') => InputEvent::Hint,
|
KeyCode::Char('h') => InputEvent::Hint,
|
||||||
|
|
Loading…
Reference in a new issue