2024-04-04 23:44:43 +01:00
|
|
|
use anyhow::{bail, Result};
|
2024-04-24 01:52:30 +01:00
|
|
|
use crossterm::style::{style, Stylize};
|
2024-04-12 17:57:04 +01:00
|
|
|
use std::io::{self, Write};
|
2022-08-17 15:31:53 +01:00
|
|
|
|
2024-04-24 01:52:30 +01:00
|
|
|
use crate::{
|
|
|
|
app_state::{AppState, ExercisesProgress},
|
2024-04-25 00:56:01 +01:00
|
|
|
exercise::OUTPUT_CAPACITY,
|
2024-04-24 01:52:30 +01:00
|
|
|
terminal_link::TerminalFileLink,
|
|
|
|
};
|
2019-01-09 19:33:43 +00:00
|
|
|
|
2024-04-11 01:51:02 +01:00
|
|
|
pub fn run(app_state: &mut AppState) -> Result<()> {
|
|
|
|
let exercise = app_state.current_exercise();
|
2024-04-25 00:56:01 +01:00
|
|
|
let mut output = Vec::with_capacity(OUTPUT_CAPACITY);
|
2024-04-27 03:14:59 +01:00
|
|
|
let success = exercise.run(&mut output, app_state.target_dir())?;
|
2019-03-06 20:47:00 +00:00
|
|
|
|
2024-04-12 17:57:04 +01:00
|
|
|
let mut stdout = io::stdout().lock();
|
2024-04-25 00:56:01 +01:00
|
|
|
stdout.write_all(&output)?;
|
2024-04-04 20:06:11 +01:00
|
|
|
|
2024-04-25 00:56:01 +01:00
|
|
|
if !success {
|
2024-04-12 18:24:26 +01:00
|
|
|
app_state.set_pending(app_state.current_exercise_ind())?;
|
|
|
|
|
2024-04-14 01:41:19 +01:00
|
|
|
bail!(
|
|
|
|
"Ran {} with errors",
|
|
|
|
app_state.current_exercise().terminal_link(),
|
|
|
|
);
|
2019-01-09 19:33:43 +00:00
|
|
|
}
|
2024-03-31 15:55:33 +01:00
|
|
|
|
2024-04-25 01:03:26 +01:00
|
|
|
writeln!(
|
|
|
|
stdout,
|
|
|
|
"{}{}",
|
2024-04-11 01:51:02 +01:00
|
|
|
"✓ Successfully ran ".green(),
|
2024-04-14 01:41:19 +01:00
|
|
|
exercise.path.green(),
|
2024-04-25 01:03:26 +01:00
|
|
|
)?;
|
2024-04-11 01:51:02 +01:00
|
|
|
|
2024-04-24 01:52:30 +01:00
|
|
|
if let Some(solution_path) = app_state.current_solution_path()? {
|
|
|
|
println!(
|
|
|
|
"\nA solution file can be found at {}\n",
|
|
|
|
style(TerminalFileLink(&solution_path)).underlined().green(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-12 17:57:04 +01:00
|
|
|
match app_state.done_current_exercise(&mut stdout)? {
|
|
|
|
ExercisesProgress::AllDone => (),
|
2024-05-13 01:32:25 +01:00
|
|
|
ExercisesProgress::CurrentPending => println!(
|
|
|
|
"Current exercise: {}",
|
|
|
|
app_state.current_exercise().terminal_link(),
|
|
|
|
),
|
|
|
|
ExercisesProgress::NewPending => println!(
|
2024-04-14 01:41:19 +01:00
|
|
|
"Next exercise: {}",
|
|
|
|
app_state.current_exercise().terminal_link(),
|
|
|
|
),
|
2024-04-11 01:51:02 +01:00
|
|
|
}
|
2024-04-04 23:44:43 +01:00
|
|
|
|
2024-03-31 15:55:33 +01:00
|
|
|
Ok(())
|
2019-01-09 19:33:43 +00:00
|
|
|
}
|