add command to go back to the previous exercise

This commit is contained in:
doublevoid 2025-01-01 21:58:07 -03:00
parent 1aec7c1152
commit 085d91d9b7
3 changed files with 44 additions and 6 deletions

View file

@ -95,6 +95,9 @@ fn run_watch(
while let Ok(event) = watch_event_receiver.recv() {
match event {
WatchEvent::Input(InputEvent::Previous) => {
watch_state.previous_exercise(&mut stdout)?
}
WatchEvent::Input(InputEvent::Next) => match watch_state.next_exercise(&mut stdout)? {
ExercisesProgress::AllDone => break,
ExercisesProgress::NewPending => watch_state.run_current_exercise(&mut stdout)?,

View file

@ -92,12 +92,7 @@ impl<'a> WatchState<'a> {
.run_exercise(Some(&mut self.output), self.app_state.cmd_runner())?;
self.output.push(b'\n');
if success {
self.done_status =
if let Some(solution_path) = self.app_state.current_solution_path()? {
DoneStatus::DoneWithSolution(solution_path)
} else {
DoneStatus::DoneWithoutSolution
};
self.done_status = self.completed_exercise_status()?;
} else {
self.app_state
.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<()> {
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 {
stdout.queue(SetAttribute(Attribute::Bold))?;
stdout.write_all(b"n")?;
@ -295,4 +301,31 @@ impl<'a> WatchState<'a> {
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(())
}
}

View file

@ -7,6 +7,7 @@ use std::sync::{
use super::{WatchEvent, EXERCISE_RUNNING};
pub enum InputEvent {
Previous,
Next,
Run,
Hint,
@ -34,6 +35,7 @@ pub fn terminal_event_handler(
}
let input_event = match key.code {
KeyCode::Char('p') => InputEvent::Previous,
KeyCode::Char('n') => InputEvent::Next,
KeyCode::Char('r') if manual_run => InputEvent::Run,
KeyCode::Char('h') => InputEvent::Hint,