summaryrefslogtreecommitdiff
path: root/src/watch/state.rs
diff options
context:
space:
mode:
authorMo <76752051+mo8it@users.noreply.github.com>2024-04-24 02:56:20 +0200
committerGitHub <noreply@github.com>2024-04-24 02:56:20 +0200
commit53fdb9044d54dbdf5f0aa6c309cd285829a04b3a (patch)
treeb45b6f11597130690232b2eaa9621f25ecf98200 /src/watch/state.rs
parent86684b7fc9dd5e8bedad6056565645d1d980925c (diff)
parent8a085a0a85c759029cd57c28364867bde817e738 (diff)
Merge pull request #1955 from rust-lang/solutions
Solutions
Diffstat (limited to 'src/watch/state.rs')
-rw-r--r--src/watch/state.rs35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/watch/state.rs b/src/watch/state.rs
index c0f6c53..5f4abf3 100644
--- a/src/watch/state.rs
+++ b/src/watch/state.rs
@@ -1,6 +1,6 @@
use anyhow::Result;
use crossterm::{
- style::Stylize,
+ style::{style, Stylize},
terminal::{size, Clear, ClearType},
ExecutableCommand,
};
@@ -9,15 +9,22 @@ use std::io::{self, StdoutLock, Write};
use crate::{
app_state::{AppState, ExercisesProgress},
progress_bar::progress_bar,
+ terminal_link::TerminalFileLink,
};
+enum DoneStatus {
+ DoneWithSolution(String),
+ DoneWithoutSolution,
+ Pending,
+}
+
pub struct WatchState<'a> {
writer: StdoutLock<'a>,
app_state: &'a mut AppState,
stdout: Option<Vec<u8>>,
stderr: Option<Vec<u8>>,
show_hint: bool,
- show_done: bool,
+ done_status: DoneStatus,
manual_run: bool,
}
@@ -31,7 +38,7 @@ impl<'a> WatchState<'a> {
stdout: None,
stderr: None,
show_hint: false,
- show_done: false,
+ done_status: DoneStatus::Pending,
manual_run,
}
}
@@ -49,13 +56,18 @@ impl<'a> WatchState<'a> {
if output.status.success() {
self.stderr = None;
- self.show_done = true;
+ self.done_status =
+ if let Some(solution_path) = self.app_state.current_solution_path()? {
+ DoneStatus::DoneWithSolution(solution_path)
+ } else {
+ DoneStatus::DoneWithoutSolution
+ };
} else {
self.app_state
.set_pending(self.app_state.current_exercise_ind())?;
self.stderr = Some(output.stderr);
- self.show_done = false;
+ self.done_status = DoneStatus::Pending;
}
self.render()
@@ -67,7 +79,7 @@ impl<'a> WatchState<'a> {
}
pub fn next_exercise(&mut self) -> Result<ExercisesProgress> {
- if !self.show_done {
+ if matches!(self.done_status, DoneStatus::Pending) {
self.writer
.write_all(b"The current exercise isn't done yet\n")?;
self.show_prompt()?;
@@ -84,7 +96,7 @@ impl<'a> WatchState<'a> {
self.writer.write_fmt(format_args!("{}un/", 'r'.bold()))?;
}
- if self.show_done {
+ if !matches!(self.done_status, DoneStatus::Pending) {
self.writer.write_fmt(format_args!("{}ext/", 'n'.bold()))?;
}
@@ -124,7 +136,7 @@ impl<'a> WatchState<'a> {
))?;
}
- if self.show_done {
+ if !matches!(self.done_status, DoneStatus::Pending) {
self.writer.write_fmt(format_args!(
"{}\n\n",
"Exercise done ✓
@@ -134,6 +146,13 @@ When you are done experimenting, enter `n` or `next` to go to the next exercise
))?;
}
+ if let DoneStatus::DoneWithSolution(solution_path) = &self.done_status {
+ self.writer.write_fmt(format_args!(
+ "A solution file can be found at {}\n\n",
+ style(TerminalFileLink(solution_path)).underlined().green()
+ ))?;
+ }
+
let line_width = size()?.0;
let progress_bar = progress_bar(
self.app_state.n_done(),