2024-04-11 03:51:02 +03:00
|
|
|
use anyhow::Result;
|
2024-08-24 01:14:12 +03:00
|
|
|
use crossterm::{
|
2024-08-26 00:53:50 +03:00
|
|
|
style::{
|
|
|
|
Attribute, Attributes, Color, ResetColor, SetAttribute, SetAttributes, SetForegroundColor,
|
|
|
|
},
|
|
|
|
terminal, QueueableCommand,
|
2024-04-07 20:29:16 +03:00
|
|
|
};
|
2024-04-12 19:58:01 +03:00
|
|
|
use std::io::{self, StdoutLock, Write};
|
2024-04-07 20:29:16 +03:00
|
|
|
|
2024-04-12 16:27:29 +03:00
|
|
|
use crate::{
|
|
|
|
app_state::{AppState, ExercisesProgress},
|
2024-04-30 02:41:08 +03:00
|
|
|
clear_terminal,
|
2024-08-26 00:53:50 +03:00
|
|
|
exercise::{solution_link_line, RunnableExercise, OUTPUT_CAPACITY},
|
|
|
|
term::{progress_bar, terminal_file_link},
|
2024-04-12 16:27:29 +03:00
|
|
|
};
|
2024-04-07 20:29:16 +03:00
|
|
|
|
2024-05-13 03:45:12 +03:00
|
|
|
#[derive(PartialEq, Eq)]
|
2024-04-24 03:52:30 +03:00
|
|
|
enum DoneStatus {
|
|
|
|
DoneWithSolution(String),
|
|
|
|
DoneWithoutSolution,
|
|
|
|
Pending,
|
|
|
|
}
|
|
|
|
|
2024-04-07 20:29:16 +03:00
|
|
|
pub struct WatchState<'a> {
|
2024-08-26 00:53:50 +03:00
|
|
|
stdout: StdoutLock<'a>,
|
2024-04-11 03:51:02 +03:00
|
|
|
app_state: &'a mut AppState,
|
2024-04-25 02:56:01 +03:00
|
|
|
output: Vec<u8>,
|
2024-04-12 03:45:54 +03:00
|
|
|
show_hint: bool,
|
2024-04-24 03:52:30 +03:00
|
|
|
done_status: DoneStatus,
|
2024-04-14 18:10:53 +03:00
|
|
|
manual_run: bool,
|
2024-04-07 20:29:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> WatchState<'a> {
|
2024-04-14 18:10:53 +03:00
|
|
|
pub fn new(app_state: &'a mut AppState, manual_run: bool) -> Self {
|
2024-08-26 00:53:50 +03:00
|
|
|
// TODO: Take stdout as arg.
|
|
|
|
let stdout = io::stdout().lock();
|
2024-04-07 20:29:16 +03:00
|
|
|
|
|
|
|
Self {
|
2024-08-26 00:53:50 +03:00
|
|
|
stdout,
|
2024-04-11 03:51:02 +03:00
|
|
|
app_state,
|
2024-04-25 02:56:01 +03:00
|
|
|
output: Vec::with_capacity(OUTPUT_CAPACITY),
|
2024-04-12 03:45:54 +03:00
|
|
|
show_hint: false,
|
2024-04-24 03:52:30 +03:00
|
|
|
done_status: DoneStatus::Pending,
|
2024-04-14 18:10:53 +03:00
|
|
|
manual_run,
|
2024-04-07 20:29:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn into_writer(self) -> StdoutLock<'a> {
|
2024-08-26 00:53:50 +03:00
|
|
|
self.stdout
|
2024-04-07 20:29:16 +03:00
|
|
|
}
|
|
|
|
|
2024-04-12 03:45:54 +03:00
|
|
|
pub fn run_current_exercise(&mut self) -> Result<()> {
|
|
|
|
self.show_hint = false;
|
|
|
|
|
2024-08-08 23:48:53 +03:00
|
|
|
writeln!(
|
2024-08-26 00:53:50 +03:00
|
|
|
self.stdout,
|
2024-08-08 23:48:53 +03:00
|
|
|
"\nChecking the exercise `{}`. Please wait…",
|
|
|
|
self.app_state.current_exercise().name,
|
|
|
|
)?;
|
2024-08-20 17:05:52 +03:00
|
|
|
|
2024-04-27 05:14:59 +03:00
|
|
|
let success = self
|
|
|
|
.app_state
|
|
|
|
.current_exercise()
|
2024-08-01 16:23:54 +03:00
|
|
|
.run_exercise(Some(&mut self.output), self.app_state.cmd_runner())?;
|
2024-08-20 17:05:52 +03:00
|
|
|
self.output.push(b'\n');
|
2024-04-25 02:56:01 +03:00
|
|
|
if success {
|
2024-04-24 03:52:30 +03:00
|
|
|
self.done_status =
|
|
|
|
if let Some(solution_path) = self.app_state.current_solution_path()? {
|
|
|
|
DoneStatus::DoneWithSolution(solution_path)
|
|
|
|
} else {
|
|
|
|
DoneStatus::DoneWithoutSolution
|
|
|
|
};
|
2024-04-12 03:45:54 +03:00
|
|
|
} else {
|
2024-04-12 19:57:04 +03:00
|
|
|
self.app_state
|
|
|
|
.set_pending(self.app_state.current_exercise_ind())?;
|
|
|
|
|
2024-04-24 03:52:30 +03:00
|
|
|
self.done_status = DoneStatus::Pending;
|
2024-04-07 20:29:16 +03:00
|
|
|
}
|
|
|
|
|
2024-08-24 01:14:12 +03:00
|
|
|
self.render()?;
|
|
|
|
Ok(())
|
2024-04-07 20:29:16 +03:00
|
|
|
}
|
|
|
|
|
2024-05-13 18:06:11 +03: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(());
|
|
|
|
}
|
|
|
|
|
2024-04-11 03:51:02 +03:00
|
|
|
self.app_state.set_current_exercise_ind(exercise_ind)?;
|
|
|
|
self.run_current_exercise()
|
2024-04-07 20:29:16 +03:00
|
|
|
}
|
|
|
|
|
2024-05-14 02:49:22 +03:00
|
|
|
/// Move on to the next exercise if the current one is done.
|
2024-04-12 19:57:04 +03:00
|
|
|
pub fn next_exercise(&mut self) -> Result<ExercisesProgress> {
|
2024-05-13 03:45:12 +03:00
|
|
|
if self.done_status == DoneStatus::Pending {
|
2024-05-13 03:32:25 +03:00
|
|
|
return Ok(ExercisesProgress::CurrentPending);
|
2024-04-12 16:27:29 +03:00
|
|
|
}
|
|
|
|
|
2024-08-26 00:53:50 +03:00
|
|
|
self.app_state.done_current_exercise(&mut self.stdout)
|
2024-04-12 16:27:29 +03:00
|
|
|
}
|
|
|
|
|
2024-04-11 03:51:02 +03:00
|
|
|
fn show_prompt(&mut self) -> io::Result<()> {
|
2024-04-14 18:10:53 +03:00
|
|
|
if self.manual_run {
|
2024-08-26 00:53:50 +03:00
|
|
|
self.stdout.queue(SetAttribute(Attribute::Bold))?;
|
|
|
|
self.stdout.write_all(b"r")?;
|
|
|
|
self.stdout.queue(ResetColor)?;
|
|
|
|
self.stdout.write_all(b":run / ")?;
|
2024-04-14 18:10:53 +03:00
|
|
|
}
|
|
|
|
|
2024-05-13 03:45:12 +03:00
|
|
|
if self.done_status != DoneStatus::Pending {
|
2024-08-26 00:53:50 +03:00
|
|
|
self.stdout.queue(SetAttribute(Attribute::Bold))?;
|
|
|
|
self.stdout.write_all(b"n")?;
|
|
|
|
self.stdout.queue(ResetColor)?;
|
|
|
|
self.stdout.write_all(b":")?;
|
|
|
|
self.stdout.queue(SetAttribute(Attribute::Underlined))?;
|
|
|
|
self.stdout.write_all(b"next")?;
|
|
|
|
self.stdout.queue(ResetColor)?;
|
|
|
|
self.stdout.write_all(b" / ")?;
|
2024-04-12 16:27:29 +03:00
|
|
|
}
|
|
|
|
|
2024-04-12 03:45:54 +03:00
|
|
|
if !self.show_hint {
|
2024-08-26 00:53:50 +03:00
|
|
|
self.stdout.queue(SetAttribute(Attribute::Bold))?;
|
|
|
|
self.stdout.write_all(b"h")?;
|
|
|
|
self.stdout.queue(ResetColor)?;
|
|
|
|
self.stdout.write_all(b":hint / ")?;
|
2024-04-10 16:56:38 +03:00
|
|
|
}
|
|
|
|
|
2024-08-26 00:53:50 +03:00
|
|
|
self.stdout.queue(SetAttribute(Attribute::Bold))?;
|
|
|
|
self.stdout.write_all(b"l")?;
|
|
|
|
self.stdout.queue(ResetColor)?;
|
|
|
|
self.stdout.write_all(b":list / ")?;
|
|
|
|
|
|
|
|
self.stdout.queue(SetAttribute(Attribute::Bold))?;
|
|
|
|
self.stdout.write_all(b"q")?;
|
|
|
|
self.stdout.queue(ResetColor)?;
|
|
|
|
self.stdout.write_all(b":quit ? ")?;
|
2024-04-10 16:56:38 +03:00
|
|
|
|
2024-08-26 00:53:50 +03:00
|
|
|
self.stdout.flush()
|
2024-04-07 20:29:16 +03:00
|
|
|
}
|
|
|
|
|
2024-08-24 01:14:12 +03:00
|
|
|
pub fn render(&mut self) -> io::Result<()> {
|
2024-05-14 02:49:22 +03:00
|
|
|
// Prevent having the first line shifted if clearing wasn't successful.
|
2024-08-26 00:53:50 +03:00
|
|
|
self.stdout.write_all(b"\n")?;
|
|
|
|
clear_terminal(&mut self.stdout)?;
|
2024-04-07 20:29:16 +03:00
|
|
|
|
2024-08-26 00:53:50 +03:00
|
|
|
self.stdout.write_all(&self.output)?;
|
2024-04-10 16:56:38 +03:00
|
|
|
|
2024-04-12 03:45:54 +03:00
|
|
|
if self.show_hint {
|
2024-08-26 00:53:50 +03:00
|
|
|
self.stdout
|
|
|
|
.queue(SetAttributes(
|
|
|
|
Attributes::from(Attribute::Bold).with(Attribute::Underlined),
|
|
|
|
))?
|
|
|
|
.queue(SetForegroundColor(Color::Cyan))?;
|
|
|
|
self.stdout.write_all(b"Hint\n")?;
|
|
|
|
self.stdout.queue(ResetColor)?;
|
|
|
|
|
|
|
|
self.stdout
|
|
|
|
.write_all(self.app_state.current_exercise().hint.as_bytes())?;
|
|
|
|
self.stdout.write_all(b"\n\n")?;
|
2024-04-10 16:56:38 +03:00
|
|
|
}
|
|
|
|
|
2024-05-13 03:45:12 +03:00
|
|
|
if self.done_status != DoneStatus::Pending {
|
2024-08-26 00:53:50 +03:00
|
|
|
self.stdout
|
|
|
|
.queue(SetAttribute(Attribute::Bold))?
|
|
|
|
.queue(SetForegroundColor(Color::Green))?;
|
|
|
|
self.stdout.write_all("Exercise done ✓\n".as_bytes())?;
|
|
|
|
self.stdout.queue(ResetColor)?;
|
2024-08-20 17:04:29 +03:00
|
|
|
|
|
|
|
if let DoneStatus::DoneWithSolution(solution_path) = &self.done_status {
|
2024-08-26 00:53:50 +03:00
|
|
|
solution_link_line(&mut self.stdout, solution_path)?;
|
2024-08-20 17:04:29 +03:00
|
|
|
}
|
2024-04-12 03:45:54 +03:00
|
|
|
|
2024-04-25 03:03:26 +03:00
|
|
|
writeln!(
|
2024-08-26 00:53:50 +03:00
|
|
|
self.stdout,
|
2024-08-20 17:04:29 +03:00
|
|
|
"When done experimenting, enter `n` to move on to the next exercise 🦀\n",
|
2024-04-25 03:03:26 +03:00
|
|
|
)?;
|
2024-04-24 03:52:30 +03:00
|
|
|
}
|
|
|
|
|
2024-05-14 02:49:22 +03:00
|
|
|
let line_width = terminal::size()?.0;
|
2024-08-24 01:14:12 +03:00
|
|
|
progress_bar(
|
2024-08-26 00:53:50 +03:00
|
|
|
&mut self.stdout,
|
2024-04-11 03:51:02 +03:00
|
|
|
self.app_state.n_done(),
|
|
|
|
self.app_state.exercises().len() as u16,
|
|
|
|
line_width,
|
|
|
|
)?;
|
2024-08-26 00:53:50 +03:00
|
|
|
|
|
|
|
self.stdout.write_all(b"\nCurrent exercise: ")?;
|
|
|
|
terminal_file_link(
|
|
|
|
&mut self.stdout,
|
|
|
|
self.app_state.current_exercise().path,
|
|
|
|
Color::Blue,
|
2024-04-25 03:03:26 +03:00
|
|
|
)?;
|
2024-08-26 00:53:50 +03:00
|
|
|
self.stdout.write_all(b"\n\n")?;
|
2024-04-10 15:29:31 +03:00
|
|
|
|
2024-04-09 22:07:53 +03:00
|
|
|
self.show_prompt()?;
|
|
|
|
|
|
|
|
Ok(())
|
2024-04-07 20:29:16 +03:00
|
|
|
}
|
|
|
|
|
2024-08-24 01:14:12 +03:00
|
|
|
pub fn show_hint(&mut self) -> io::Result<()> {
|
2024-04-12 03:45:54 +03:00
|
|
|
self.show_hint = true;
|
2024-04-10 16:56:38 +03:00
|
|
|
self.render()
|
2024-04-07 20:29:16 +03:00
|
|
|
}
|
|
|
|
}
|