Show done message

This commit is contained in:
mo8it 2024-04-12 02:45:54 +02:00
parent 2a95a3e966
commit 6807e63c5f
2 changed files with 31 additions and 25 deletions

View File

@ -54,9 +54,7 @@ pub fn watch(app_state: &mut AppState) -> Result<WatchExit> {
let mut watch_state = WatchState::new(app_state); let mut watch_state = WatchState::new(app_state);
// TODO: bool
watch_state.run_current_exercise()?; watch_state.run_current_exercise()?;
watch_state.render()?;
thread::spawn(move || terminal_event_handler(tx)); thread::spawn(move || terminal_event_handler(tx));
@ -76,9 +74,7 @@ pub fn watch(app_state: &mut AppState) -> Result<WatchExit> {
watch_state.handle_invalid_cmd(&cmd)?; watch_state.handle_invalid_cmd(&cmd)?;
} }
WatchEvent::FileChange { exercise_ind } => { WatchEvent::FileChange { exercise_ind } => {
// TODO: bool
watch_state.run_exercise_with_ind(exercise_ind)?; watch_state.run_exercise_with_ind(exercise_ind)?;
watch_state.render()?;
} }
WatchEvent::NotifyErr(e) => { WatchEvent::NotifyErr(e) => {
return Err(Error::from(e).context("Exercise file watcher failed")) return Err(Error::from(e).context("Exercise file watcher failed"))

View File

@ -13,8 +13,8 @@ pub struct WatchState<'a> {
app_state: &'a mut AppState, app_state: &'a mut AppState,
stdout: Option<Vec<u8>>, stdout: Option<Vec<u8>>,
stderr: Option<Vec<u8>>, stderr: Option<Vec<u8>>,
message: Option<String>, show_hint: bool,
hint_displayed: bool, show_done: bool,
} }
impl<'a> WatchState<'a> { impl<'a> WatchState<'a> {
@ -26,8 +26,8 @@ impl<'a> WatchState<'a> {
app_state, app_state,
stdout: None, stdout: None,
stderr: None, stderr: None,
message: None, show_hint: false,
hint_displayed: false, show_done: false,
} }
} }
@ -36,29 +36,32 @@ impl<'a> WatchState<'a> {
self.writer self.writer
} }
pub fn run_current_exercise(&mut self) -> Result<bool> { pub fn run_current_exercise(&mut self) -> Result<()> {
self.show_hint = false;
let output = self.app_state.current_exercise().run()?; let output = self.app_state.current_exercise().run()?;
self.stdout = Some(output.stdout); self.stdout = Some(output.stdout);
if !output.status.success() { if output.status.success() {
self.stderr = Some(output.stderr);
return Ok(false);
}
self.stderr = None; self.stderr = None;
self.show_done = true;
Ok(true) } else {
self.stderr = Some(output.stderr);
self.show_done = false;
} }
pub fn run_exercise_with_ind(&mut self, exercise_ind: usize) -> Result<bool> { self.render()
}
pub fn run_exercise_with_ind(&mut self, exercise_ind: usize) -> Result<()> {
self.app_state.set_current_exercise_ind(exercise_ind)?; self.app_state.set_current_exercise_ind(exercise_ind)?;
self.run_current_exercise() self.run_current_exercise()
} }
fn show_prompt(&mut self) -> io::Result<()> { fn show_prompt(&mut self) -> io::Result<()> {
self.writer.write_all(b"\n\n")?; self.writer.write_all(b"\n")?;
if !self.hint_displayed { if !self.show_hint {
self.writer.write_fmt(format_args!("{}int/", 'h'.bold()))?; self.writer.write_fmt(format_args!("{}int/", 'h'.bold()))?;
} }
@ -84,20 +87,26 @@ impl<'a> WatchState<'a> {
self.writer.write_all(b"\n")?; self.writer.write_all(b"\n")?;
} }
if let Some(message) = &self.message {
self.writer.write_all(message.as_bytes())?;
}
self.writer.write_all(b"\n")?; self.writer.write_all(b"\n")?;
if self.hint_displayed { if self.show_hint {
self.writer self.writer
.write_fmt(format_args!("\n{}\n", "Hint".bold().cyan().underlined()))?; .write_fmt(format_args!("{}\n", "Hint".bold().cyan().underlined()))?;
self.writer self.writer
.write_all(self.app_state.current_exercise().hint.as_bytes())?; .write_all(self.app_state.current_exercise().hint.as_bytes())?;
self.writer.write_all(b"\n\n")?; self.writer.write_all(b"\n\n")?;
} }
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(),
))?;
}
let line_width = size()?.0; let line_width = size()?.0;
let progress_bar = progress_bar( let progress_bar = progress_bar(
self.app_state.n_done(), self.app_state.n_done(),
@ -108,7 +117,7 @@ impl<'a> WatchState<'a> {
self.writer.write_all(b"Current exercise: ")?; self.writer.write_all(b"Current exercise: ")?;
self.writer.write_fmt(format_args!( self.writer.write_fmt(format_args!(
"{}", "{}\n",
self.app_state self.app_state
.current_exercise() .current_exercise()
.path .path
@ -122,7 +131,7 @@ impl<'a> WatchState<'a> {
} }
pub fn show_hint(&mut self) -> Result<()> { pub fn show_hint(&mut self) -> Result<()> {
self.hint_displayed = true; self.show_hint = true;
self.render() self.render()
} }
@ -133,6 +142,7 @@ impl<'a> WatchState<'a> {
self.writer self.writer
.write_all(b" (confusing input can occur after resizing the terminal)")?; .write_all(b" (confusing input can occur after resizing the terminal)")?;
} }
self.writer.write_all(b"\n")?;
self.show_prompt() self.show_prompt()
} }
} }