rustlings/src/watch/state.rs

177 lines
4.9 KiB
Rust
Raw Normal View History

use anyhow::Result;
use ratatui::crossterm::{
2024-04-23 20:52:30 -04:00
style::{style, Stylize},
2024-05-13 19:49:22 -04:00
terminal,
2024-04-07 13:29:16 -04:00
};
2024-04-12 12:58:01 -04:00
use std::io::{self, StdoutLock, Write};
2024-04-07 13:29:16 -04:00
2024-04-12 09:27:29 -04:00
use crate::{
app_state::{AppState, ExercisesProgress},
2024-04-29 19:41:08 -04:00
clear_terminal,
exercise::{RunnableExercise, OUTPUT_CAPACITY},
2024-04-12 09:27:29 -04:00
progress_bar::progress_bar,
2024-04-23 20:52:30 -04:00
terminal_link::TerminalFileLink,
2024-04-12 09:27:29 -04:00
};
2024-04-07 13:29:16 -04:00
2024-05-12 20:45:12 -04:00
#[derive(PartialEq, Eq)]
2024-04-23 20:52:30 -04:00
enum DoneStatus {
DoneWithSolution(String),
DoneWithoutSolution,
Pending,
}
2024-04-07 13:29:16 -04:00
pub struct WatchState<'a> {
writer: StdoutLock<'a>,
app_state: &'a mut AppState,
2024-04-24 19:56:01 -04:00
output: Vec<u8>,
2024-04-11 20:45:54 -04:00
show_hint: bool,
2024-04-23 20:52:30 -04:00
done_status: DoneStatus,
2024-04-14 11:10:53 -04:00
manual_run: bool,
2024-04-07 13:29:16 -04:00
}
impl<'a> WatchState<'a> {
2024-04-14 11:10:53 -04:00
pub fn new(app_state: &'a mut AppState, manual_run: bool) -> Self {
2024-04-07 13:29:16 -04:00
let writer = io::stdout().lock();
Self {
writer,
app_state,
2024-04-24 19:56:01 -04:00
output: Vec::with_capacity(OUTPUT_CAPACITY),
2024-04-11 20:45:54 -04:00
show_hint: false,
2024-04-23 20:52:30 -04:00
done_status: DoneStatus::Pending,
2024-04-14 11:10:53 -04:00
manual_run,
2024-04-07 13:29:16 -04:00
}
}
#[inline]
pub fn into_writer(self) -> StdoutLock<'a> {
self.writer
}
2024-04-11 20:45:54 -04:00
pub fn run_current_exercise(&mut self) -> Result<()> {
self.show_hint = false;
self.writer
.write_all(b"\nChecking the exercise, please wait...")?;
let success = self
.app_state
.current_exercise()
2024-08-01 09:23:54 -04:00
.run_exercise(Some(&mut self.output), self.app_state.cmd_runner())?;
2024-04-24 19:56:01 -04:00
if success {
2024-04-23 20:52:30 -04:00
self.done_status =
if let Some(solution_path) = self.app_state.current_solution_path()? {
DoneStatus::DoneWithSolution(solution_path)
} else {
DoneStatus::DoneWithoutSolution
};
2024-04-11 20:45:54 -04:00
} else {
self.app_state
.set_pending(self.app_state.current_exercise_ind())?;
2024-04-23 20:52:30 -04:00
self.done_status = DoneStatus::Pending;
2024-04-07 13:29:16 -04:00
}
2024-04-11 20:45:54 -04:00
self.render()
2024-04-07 13:29:16 -04:00
}
2024-05-13 11:06:11 -04:00
pub fn handle_file_change(&mut self, exercise_ind: usize) -> Result<()> {
// Don't skip exercises on file changes to avoid confusion from missing exercises.
// Skipping exercises must be explicit in the interactive list.
// But going back to an earlier exercise on file change is fine.
if self.app_state.current_exercise_ind() < exercise_ind {
return Ok(());
}
self.app_state.set_current_exercise_ind(exercise_ind)?;
self.run_current_exercise()
2024-04-07 13:29:16 -04:00
}
2024-05-13 19:49:22 -04:00
/// Move on to the next exercise if the current one is done.
pub fn next_exercise(&mut self) -> Result<ExercisesProgress> {
2024-05-12 20:45:12 -04:00
if self.done_status == DoneStatus::Pending {
2024-05-12 20:32:25 -04:00
return Ok(ExercisesProgress::CurrentPending);
2024-04-12 09:27:29 -04:00
}
self.app_state.done_current_exercise(&mut self.writer)
2024-04-12 09:27:29 -04:00
}
fn show_prompt(&mut self) -> io::Result<()> {
2024-04-11 20:45:54 -04:00
self.writer.write_all(b"\n")?;
2024-04-14 11:10:53 -04:00
if self.manual_run {
write!(self.writer, "{}:run / ", 'r'.bold())?;
2024-04-14 11:10:53 -04:00
}
2024-05-12 20:45:12 -04:00
if self.done_status != DoneStatus::Pending {
2024-07-01 19:50:05 -04:00
write!(self.writer, "{}:{} / ", 'n'.bold(), "next".underlined())?;
2024-04-12 09:27:29 -04:00
}
2024-04-11 20:45:54 -04:00
if !self.show_hint {
write!(self.writer, "{}:hint / ", 'h'.bold())?;
}
write!(self.writer, "{}:list / {}:quit ? ", 'l'.bold(), 'q'.bold())?;
2024-04-07 13:29:16 -04:00
self.writer.flush()
}
pub fn render(&mut self) -> Result<()> {
2024-05-13 19:49:22 -04:00
// Prevent having the first line shifted if clearing wasn't successful.
2024-04-09 21:56:41 -04:00
self.writer.write_all(b"\n")?;
2024-04-29 19:41:08 -04:00
clear_terminal(&mut self.writer)?;
2024-04-07 13:29:16 -04:00
2024-04-24 19:56:01 -04:00
self.writer.write_all(&self.output)?;
self.writer.write_all(b"\n")?;
2024-04-11 20:45:54 -04:00
if self.show_hint {
2024-04-24 20:03:26 -04:00
writeln!(
self.writer,
"{}\n{}\n",
"Hint".bold().cyan().underlined(),
self.app_state.current_exercise().hint,
2024-04-24 20:03:26 -04:00
)?;
}
2024-05-12 20:45:12 -04:00
if self.done_status != DoneStatus::Pending {
2024-04-24 20:03:26 -04:00
writeln!(
self.writer,
"{}\n",
2024-04-11 20:45:54 -04:00
"Exercise done ✓
2024-05-12 20:37:32 -04:00
When you are done experimenting, enter `n` to move on to the next exercise 🦀"
2024-04-11 20:45:54 -04:00
.bold()
.green(),
2024-04-24 20:03:26 -04:00
)?;
2024-04-11 20:45:54 -04:00
}
2024-04-23 20:52:30 -04:00
if let DoneStatus::DoneWithSolution(solution_path) = &self.done_status {
2024-04-24 20:03:26 -04:00
writeln!(
self.writer,
"A solution file can be found at {}\n",
2024-05-13 19:49:22 -04:00
style(TerminalFileLink(solution_path)).underlined().green(),
2024-04-24 20:03:26 -04:00
)?;
2024-04-23 20:52:30 -04:00
}
2024-05-13 19:49:22 -04:00
let line_width = terminal::size()?.0;
let progress_bar = progress_bar(
self.app_state.n_done(),
self.app_state.exercises().len() as u16,
line_width,
)?;
2024-04-24 20:03:26 -04:00
writeln!(
self.writer,
"{progress_bar}Current exercise: {}",
2024-04-13 20:41:19 -04:00
self.app_state.current_exercise().terminal_link(),
2024-04-24 20:03:26 -04:00
)?;
2024-04-10 08:29:31 -04:00
self.show_prompt()?;
Ok(())
2024-04-07 13:29:16 -04:00
}
pub fn show_hint(&mut self) -> Result<()> {
2024-04-11 20:45:54 -04:00
self.show_hint = true;
self.render()
2024-04-07 13:29:16 -04:00
}
}