Merge pull request #1504 from rb5014/success-hints-feature
feat(main, verify): Show hint(s) after exercise completion
This commit is contained in:
commit
b07680108b
16
src/main.rs
16
src/main.rs
|
@ -61,7 +61,11 @@ struct VerifyArgs {}
|
||||||
#[derive(FromArgs, PartialEq, Debug)]
|
#[derive(FromArgs, PartialEq, Debug)]
|
||||||
#[argh(subcommand, name = "watch")]
|
#[argh(subcommand, name = "watch")]
|
||||||
/// Reruns `verify` when files were edited
|
/// Reruns `verify` when files were edited
|
||||||
struct WatchArgs {}
|
struct WatchArgs {
|
||||||
|
/// show hints on success
|
||||||
|
#[argh(switch)]
|
||||||
|
success_hints: bool,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(FromArgs, PartialEq, Debug)]
|
#[derive(FromArgs, PartialEq, Debug)]
|
||||||
#[argh(subcommand, name = "run")]
|
#[argh(subcommand, name = "run")]
|
||||||
|
@ -229,7 +233,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Subcommands::Verify(_subargs) => {
|
Subcommands::Verify(_subargs) => {
|
||||||
verify(&exercises, (0, exercises.len()), verbose)
|
verify(&exercises, (0, exercises.len()), verbose, false)
|
||||||
.unwrap_or_else(|_| std::process::exit(1));
|
.unwrap_or_else(|_| std::process::exit(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -252,7 +256,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Subcommands::Watch(_subargs) => match watch(&exercises, verbose) {
|
Subcommands::Watch(_subargs) => match watch(&exercises, verbose, _subargs.success_hints) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!(
|
println!(
|
||||||
"Error: Could not watch your progress. Error message was {:?}.",
|
"Error: Could not watch your progress. Error message was {:?}.",
|
||||||
|
@ -348,7 +352,7 @@ enum WatchStatus {
|
||||||
Unfinished,
|
Unfinished,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<WatchStatus> {
|
fn watch(exercises: &[Exercise], verbose: bool, success_hints: bool) -> notify::Result<WatchStatus> {
|
||||||
/* Clears the terminal with an ANSI escape code.
|
/* Clears the terminal with an ANSI escape code.
|
||||||
Works in UNIX and newer Windows terminals. */
|
Works in UNIX and newer Windows terminals. */
|
||||||
fn clear_screen() {
|
fn clear_screen() {
|
||||||
|
@ -364,7 +368,7 @@ fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<WatchStatus> {
|
||||||
clear_screen();
|
clear_screen();
|
||||||
|
|
||||||
let to_owned_hint = |t: &Exercise| t.hint.to_owned();
|
let to_owned_hint = |t: &Exercise| t.hint.to_owned();
|
||||||
let failed_exercise_hint = match verify(exercises.iter(), (0, exercises.len()), verbose) {
|
let failed_exercise_hint = match verify(exercises.iter(), (0, exercises.len()), verbose, success_hints) {
|
||||||
Ok(_) => return Ok(WatchStatus::Finished),
|
Ok(_) => return Ok(WatchStatus::Finished),
|
||||||
Err(exercise) => Arc::new(Mutex::new(Some(to_owned_hint(exercise)))),
|
Err(exercise) => Arc::new(Mutex::new(Some(to_owned_hint(exercise)))),
|
||||||
};
|
};
|
||||||
|
@ -386,7 +390,7 @@ fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<WatchStatus> {
|
||||||
);
|
);
|
||||||
let num_done = exercises.iter().filter(|e| e.looks_done()).count();
|
let num_done = exercises.iter().filter(|e| e.looks_done()).count();
|
||||||
clear_screen();
|
clear_screen();
|
||||||
match verify(pending_exercises, (num_done, exercises.len()), verbose) {
|
match verify(pending_exercises, (num_done, exercises.len()), verbose, success_hints) {
|
||||||
Ok(_) => return Ok(WatchStatus::Finished),
|
Ok(_) => return Ok(WatchStatus::Finished),
|
||||||
Err(exercise) => {
|
Err(exercise) => {
|
||||||
let mut failed_exercise_hint = failed_exercise_hint.lock().unwrap();
|
let mut failed_exercise_hint = failed_exercise_hint.lock().unwrap();
|
||||||
|
|
|
@ -12,6 +12,7 @@ pub fn verify<'a>(
|
||||||
exercises: impl IntoIterator<Item = &'a Exercise>,
|
exercises: impl IntoIterator<Item = &'a Exercise>,
|
||||||
progress: (usize, usize),
|
progress: (usize, usize),
|
||||||
verbose: bool,
|
verbose: bool,
|
||||||
|
success_hints: bool,
|
||||||
) -> Result<(), &'a Exercise> {
|
) -> Result<(), &'a Exercise> {
|
||||||
let (num_done, total) = progress;
|
let (num_done, total) = progress;
|
||||||
let bar = ProgressBar::new(total as u64);
|
let bar = ProgressBar::new(total as u64);
|
||||||
|
@ -25,9 +26,9 @@ pub fn verify<'a>(
|
||||||
|
|
||||||
for exercise in exercises {
|
for exercise in exercises {
|
||||||
let compile_result = match exercise.mode {
|
let compile_result = match exercise.mode {
|
||||||
Mode::Test => compile_and_test(exercise, RunMode::Interactive, verbose),
|
Mode::Test => compile_and_test(exercise, RunMode::Interactive, verbose, success_hints),
|
||||||
Mode::Compile => compile_and_run_interactively(exercise),
|
Mode::Compile => compile_and_run_interactively(exercise, success_hints),
|
||||||
Mode::Clippy => compile_only(exercise),
|
Mode::Clippy => compile_only(exercise, success_hints),
|
||||||
};
|
};
|
||||||
if !compile_result.unwrap_or(false) {
|
if !compile_result.unwrap_or(false) {
|
||||||
return Err(exercise);
|
return Err(exercise);
|
||||||
|
@ -46,12 +47,12 @@ enum RunMode {
|
||||||
|
|
||||||
// Compile and run the resulting test harness of the given Exercise
|
// Compile and run the resulting test harness of the given Exercise
|
||||||
pub fn test(exercise: &Exercise, verbose: bool) -> Result<(), ()> {
|
pub fn test(exercise: &Exercise, verbose: bool) -> Result<(), ()> {
|
||||||
compile_and_test(exercise, RunMode::NonInteractive, verbose)?;
|
compile_and_test(exercise, RunMode::NonInteractive, verbose, false)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoke the rust compiler without running the resulting binary
|
// Invoke the rust compiler without running the resulting binary
|
||||||
fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
|
fn compile_only(exercise: &Exercise, success_hints: bool) -> Result<bool, ()> {
|
||||||
let progress_bar = ProgressBar::new_spinner();
|
let progress_bar = ProgressBar::new_spinner();
|
||||||
progress_bar.set_message(format!("Compiling {exercise}..."));
|
progress_bar.set_message(format!("Compiling {exercise}..."));
|
||||||
progress_bar.enable_steady_tick(100);
|
progress_bar.enable_steady_tick(100);
|
||||||
|
@ -59,11 +60,11 @@ fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
|
||||||
let _ = compile(exercise, &progress_bar)?;
|
let _ = compile(exercise, &progress_bar)?;
|
||||||
progress_bar.finish_and_clear();
|
progress_bar.finish_and_clear();
|
||||||
|
|
||||||
Ok(prompt_for_completion(exercise, None))
|
Ok(prompt_for_completion(exercise, None, success_hints))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compile the given Exercise and run the resulting binary in an interactive mode
|
// Compile the given Exercise and run the resulting binary in an interactive mode
|
||||||
fn compile_and_run_interactively(exercise: &Exercise) -> Result<bool, ()> {
|
fn compile_and_run_interactively(exercise: &Exercise, success_hints: bool) -> Result<bool, ()> {
|
||||||
let progress_bar = ProgressBar::new_spinner();
|
let progress_bar = ProgressBar::new_spinner();
|
||||||
progress_bar.set_message(format!("Compiling {exercise}..."));
|
progress_bar.set_message(format!("Compiling {exercise}..."));
|
||||||
progress_bar.enable_steady_tick(100);
|
progress_bar.enable_steady_tick(100);
|
||||||
|
@ -84,12 +85,12 @@ fn compile_and_run_interactively(exercise: &Exercise) -> Result<bool, ()> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(prompt_for_completion(exercise, Some(output.stdout)))
|
Ok(prompt_for_completion(exercise, Some(output.stdout), success_hints))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compile the given Exercise as a test harness and display
|
// Compile the given Exercise as a test harness and display
|
||||||
// the output if verbose is set to true
|
// the output if verbose is set to true
|
||||||
fn compile_and_test(exercise: &Exercise, run_mode: RunMode, verbose: bool) -> Result<bool, ()> {
|
fn compile_and_test(exercise: &Exercise, run_mode: RunMode, verbose: bool, success_hints: bool) -> Result<bool, ()> {
|
||||||
let progress_bar = ProgressBar::new_spinner();
|
let progress_bar = ProgressBar::new_spinner();
|
||||||
progress_bar.set_message(format!("Testing {exercise}..."));
|
progress_bar.set_message(format!("Testing {exercise}..."));
|
||||||
progress_bar.enable_steady_tick(100);
|
progress_bar.enable_steady_tick(100);
|
||||||
|
@ -104,7 +105,7 @@ fn compile_and_test(exercise: &Exercise, run_mode: RunMode, verbose: bool) -> Re
|
||||||
println!("{}", output.stdout);
|
println!("{}", output.stdout);
|
||||||
}
|
}
|
||||||
if let RunMode::Interactive = run_mode {
|
if let RunMode::Interactive = run_mode {
|
||||||
Ok(prompt_for_completion(exercise, None))
|
Ok(prompt_for_completion(exercise, None, success_hints))
|
||||||
} else {
|
} else {
|
||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
|
@ -142,12 +143,11 @@ fn compile<'a, 'b>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>) -> bool {
|
fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>, success_hints: bool) -> bool {
|
||||||
let context = match exercise.state() {
|
let context = match exercise.state() {
|
||||||
State::Done => return true,
|
State::Done => return true,
|
||||||
State::Pending(context) => context,
|
State::Pending(context) => context,
|
||||||
};
|
};
|
||||||
|
|
||||||
match exercise.mode {
|
match exercise.mode {
|
||||||
Mode::Compile => success!("Successfully ran {}!", exercise),
|
Mode::Compile => success!("Successfully ran {}!", exercise),
|
||||||
Mode::Test => success!("Successfully tested {}!", exercise),
|
Mode::Test => success!("Successfully tested {}!", exercise),
|
||||||
|
@ -167,7 +167,6 @@ fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>) ->
|
||||||
Mode::Test => "The code is compiling, and the tests pass!",
|
Mode::Test => "The code is compiling, and the tests pass!",
|
||||||
Mode::Clippy => clippy_success_msg,
|
Mode::Clippy => clippy_success_msg,
|
||||||
};
|
};
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
if no_emoji {
|
if no_emoji {
|
||||||
println!("~*~ {success_msg} ~*~")
|
println!("~*~ {success_msg} ~*~")
|
||||||
|
@ -183,6 +182,13 @@ fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>) ->
|
||||||
println!("{}", separator());
|
println!("{}", separator());
|
||||||
println!();
|
println!();
|
||||||
}
|
}
|
||||||
|
if success_hints {
|
||||||
|
println!("Hints:");
|
||||||
|
println!("{}", separator());
|
||||||
|
println!("{}", exercise.hint);
|
||||||
|
println!("{}", separator());
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
|
||||||
println!("You can keep working on this exercise,");
|
println!("You can keep working on this exercise,");
|
||||||
println!(
|
println!(
|
||||||
|
|
Loading…
Reference in a new issue