2024-04-10 20:51:02 -04:00
|
|
|
use anyhow::Result;
|
2024-04-07 13:29:16 -04:00
|
|
|
use crossterm::{
|
2024-04-10 20:51:02 -04:00
|
|
|
style::Stylize,
|
2024-04-09 15:07:53 -04:00
|
|
|
terminal::{size, Clear, ClearType},
|
2024-04-07 13:29:16 -04:00
|
|
|
ExecutableCommand,
|
|
|
|
};
|
2024-04-10 20:51:02 -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},
|
|
|
|
progress_bar::progress_bar,
|
|
|
|
};
|
2024-04-07 13:29:16 -04:00
|
|
|
|
|
|
|
pub struct WatchState<'a> {
|
|
|
|
writer: StdoutLock<'a>,
|
2024-04-10 20:51:02 -04:00
|
|
|
app_state: &'a mut AppState,
|
2024-04-07 13:29:16 -04:00
|
|
|
stdout: Option<Vec<u8>>,
|
|
|
|
stderr: Option<Vec<u8>>,
|
2024-04-11 20:45:54 -04:00
|
|
|
show_hint: bool,
|
|
|
|
show_done: bool,
|
2024-04-07 13:29:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> WatchState<'a> {
|
2024-04-10 20:51:02 -04:00
|
|
|
pub fn new(app_state: &'a mut AppState) -> Self {
|
2024-04-07 13:29:16 -04:00
|
|
|
let writer = io::stdout().lock();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
writer,
|
2024-04-10 20:51:02 -04:00
|
|
|
app_state,
|
2024-04-07 13:29:16 -04:00
|
|
|
stdout: None,
|
|
|
|
stderr: None,
|
2024-04-11 20:45:54 -04:00
|
|
|
show_hint: false,
|
|
|
|
show_done: false,
|
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;
|
|
|
|
|
2024-04-10 20:51:02 -04:00
|
|
|
let output = self.app_state.current_exercise().run()?;
|
2024-04-09 16:20:12 -04:00
|
|
|
self.stdout = Some(output.stdout);
|
2024-04-07 13:29:16 -04:00
|
|
|
|
2024-04-11 20:45:54 -04:00
|
|
|
if output.status.success() {
|
|
|
|
self.stderr = None;
|
|
|
|
self.show_done = true;
|
|
|
|
} else {
|
2024-04-07 13:29:16 -04:00
|
|
|
self.stderr = Some(output.stderr);
|
2024-04-11 20:45:54 -04:00
|
|
|
self.show_done = false;
|
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-04-11 20:45:54 -04:00
|
|
|
pub fn run_exercise_with_ind(&mut self, exercise_ind: usize) -> Result<()> {
|
2024-04-10 20:51:02 -04:00
|
|
|
self.app_state.set_current_exercise_ind(exercise_ind)?;
|
|
|
|
self.run_current_exercise()
|
2024-04-07 13:29:16 -04:00
|
|
|
}
|
|
|
|
|
2024-04-12 09:27:29 -04:00
|
|
|
pub fn next_exercise(&mut self) -> Result<()> {
|
|
|
|
if !self.show_done {
|
|
|
|
self.writer
|
|
|
|
.write_all(b"The current exercise isn't done yet\n")?;
|
|
|
|
self.show_prompt()?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.app_state.done_current_exercise()? {
|
|
|
|
ExercisesProgress::AllDone => todo!(),
|
|
|
|
ExercisesProgress::Pending => self.run_current_exercise(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-10 20:51:02 -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-10 09:56:38 -04:00
|
|
|
|
2024-04-12 09:27:29 -04:00
|
|
|
if self.show_done {
|
|
|
|
self.writer.write_fmt(format_args!("{}ext/", 'n'.bold()))?;
|
|
|
|
}
|
|
|
|
|
2024-04-11 20:45:54 -04:00
|
|
|
if !self.show_hint {
|
2024-04-10 09:56:38 -04:00
|
|
|
self.writer.write_fmt(format_args!("{}int/", 'h'.bold()))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.writer
|
|
|
|
.write_fmt(format_args!("{}ist/{}uit? ", 'l'.bold(), 'q'.bold()))?;
|
|
|
|
|
2024-04-07 13:29:16 -04:00
|
|
|
self.writer.flush()
|
|
|
|
}
|
|
|
|
|
2024-04-09 15:07:53 -04:00
|
|
|
pub fn render(&mut self) -> Result<()> {
|
2024-04-09 21:56:41 -04:00
|
|
|
// Prevent having the first line shifted after clearing because of the prompt.
|
|
|
|
self.writer.write_all(b"\n")?;
|
|
|
|
|
2024-04-07 13:29:16 -04:00
|
|
|
self.writer.execute(Clear(ClearType::All))?;
|
|
|
|
|
|
|
|
if let Some(stdout) = &self.stdout {
|
|
|
|
self.writer.write_all(stdout)?;
|
2024-04-10 09:56:38 -04:00
|
|
|
self.writer.write_all(b"\n")?;
|
2024-04-07 13:29:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(stderr) = &self.stderr {
|
|
|
|
self.writer.write_all(stderr)?;
|
2024-04-10 09:56:38 -04:00
|
|
|
self.writer.write_all(b"\n")?;
|
2024-04-07 13:29:16 -04:00
|
|
|
}
|
|
|
|
|
2024-04-09 16:06:55 -04:00
|
|
|
self.writer.write_all(b"\n")?;
|
2024-04-10 09:56:38 -04:00
|
|
|
|
2024-04-11 20:45:54 -04:00
|
|
|
if self.show_hint {
|
2024-04-10 09:56:38 -04:00
|
|
|
self.writer
|
2024-04-11 20:45:54 -04:00
|
|
|
.write_fmt(format_args!("{}\n", "Hint".bold().cyan().underlined()))?;
|
2024-04-10 20:51:02 -04:00
|
|
|
self.writer
|
|
|
|
.write_all(self.app_state.current_exercise().hint.as_bytes())?;
|
2024-04-10 09:56:38 -04:00
|
|
|
self.writer.write_all(b"\n\n")?;
|
|
|
|
}
|
|
|
|
|
2024-04-11 20:45:54 -04:00
|
|
|
if self.show_done {
|
|
|
|
self.writer.write_fmt(format_args!(
|
|
|
|
"{}\n\n",
|
|
|
|
"Exercise done ✓
|
|
|
|
When you are done experimenting, enter `n` or `next` to go to the next exercise 🦀"
|
|
|
|
.bold()
|
|
|
|
.green(),
|
|
|
|
))?;
|
|
|
|
}
|
|
|
|
|
2024-04-09 15:07:53 -04:00
|
|
|
let line_width = size()?.0;
|
2024-04-10 20:51:02 -04:00
|
|
|
let progress_bar = progress_bar(
|
|
|
|
self.app_state.n_done(),
|
|
|
|
self.app_state.exercises().len() as u16,
|
|
|
|
line_width,
|
|
|
|
)?;
|
2024-04-09 15:07:53 -04:00
|
|
|
self.writer.write_all(progress_bar.as_bytes())?;
|
|
|
|
|
2024-04-10 08:29:31 -04:00
|
|
|
self.writer.write_all(b"Current exercise: ")?;
|
|
|
|
self.writer.write_fmt(format_args!(
|
2024-04-11 20:45:54 -04:00
|
|
|
"{}\n",
|
2024-04-10 20:51:02 -04:00
|
|
|
self.app_state
|
|
|
|
.current_exercise()
|
|
|
|
.path
|
|
|
|
.to_string_lossy()
|
|
|
|
.bold(),
|
2024-04-10 08:29:31 -04:00
|
|
|
))?;
|
|
|
|
|
2024-04-09 15:07:53 -04:00
|
|
|
self.show_prompt()?;
|
|
|
|
|
|
|
|
Ok(())
|
2024-04-07 13:29:16 -04:00
|
|
|
}
|
|
|
|
|
2024-04-10 09:56:38 -04:00
|
|
|
pub fn show_hint(&mut self) -> Result<()> {
|
2024-04-11 20:45:54 -04:00
|
|
|
self.show_hint = true;
|
2024-04-10 09:56:38 -04:00
|
|
|
self.render()
|
2024-04-07 13:29:16 -04:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:08:40 -04:00
|
|
|
pub fn handle_invalid_cmd(&mut self, cmd: &str) -> io::Result<()> {
|
|
|
|
self.writer.write_all(b"Invalid command: ")?;
|
|
|
|
self.writer.write_all(cmd.as_bytes())?;
|
|
|
|
if cmd.len() > 1 {
|
|
|
|
self.writer
|
|
|
|
.write_all(b" (confusing input can occur after resizing the terminal)")?;
|
|
|
|
}
|
2024-04-11 20:45:54 -04:00
|
|
|
self.writer.write_all(b"\n")?;
|
2024-04-07 13:29:16 -04:00
|
|
|
self.show_prompt()
|
|
|
|
}
|
|
|
|
}
|