2024-04-04 18:44:43 -04:00
|
|
|
use anyhow::{bail, Result};
|
2024-07-25 10:26:48 -04:00
|
|
|
use ratatui::crossterm::style::{style, Stylize};
|
2024-04-12 12:57:04 -04:00
|
|
|
use std::io::{self, Write};
|
2022-08-17 10:31:53 -04:00
|
|
|
|
2024-04-23 20:52:30 -04:00
|
|
|
use crate::{
|
|
|
|
app_state::{AppState, ExercisesProgress},
|
2024-06-01 15:48:15 -04:00
|
|
|
exercise::{RunnableExercise, OUTPUT_CAPACITY},
|
2024-04-23 20:52:30 -04:00
|
|
|
terminal_link::TerminalFileLink,
|
|
|
|
};
|
2019-01-09 14:33:43 -05:00
|
|
|
|
2024-04-10 20:51:02 -04:00
|
|
|
pub fn run(app_state: &mut AppState) -> Result<()> {
|
|
|
|
let exercise = app_state.current_exercise();
|
2024-04-24 19:56:01 -04:00
|
|
|
let mut output = Vec::with_capacity(OUTPUT_CAPACITY);
|
2024-08-01 09:23:54 -04:00
|
|
|
let success = exercise.run_exercise(Some(&mut output), app_state.cmd_runner())?;
|
2019-03-06 15:47:00 -05:00
|
|
|
|
2024-04-12 12:57:04 -04:00
|
|
|
let mut stdout = io::stdout().lock();
|
2024-04-24 19:56:01 -04:00
|
|
|
stdout.write_all(&output)?;
|
2024-04-04 15:06:11 -04:00
|
|
|
|
2024-04-24 19:56:01 -04:00
|
|
|
if !success {
|
2024-04-12 13:24:26 -04:00
|
|
|
app_state.set_pending(app_state.current_exercise_ind())?;
|
|
|
|
|
2024-04-13 20:41:19 -04:00
|
|
|
bail!(
|
|
|
|
"Ran {} with errors",
|
|
|
|
app_state.current_exercise().terminal_link(),
|
|
|
|
);
|
2019-01-09 14:33:43 -05:00
|
|
|
}
|
2024-03-31 10:55:33 -04:00
|
|
|
|
2024-04-24 20:03:26 -04:00
|
|
|
writeln!(
|
|
|
|
stdout,
|
|
|
|
"{}{}",
|
2024-04-10 20:51:02 -04:00
|
|
|
"✓ Successfully ran ".green(),
|
2024-04-13 20:41:19 -04:00
|
|
|
exercise.path.green(),
|
2024-04-24 20:03:26 -04:00
|
|
|
)?;
|
2024-04-10 20:51:02 -04:00
|
|
|
|
2024-04-23 20:52:30 -04: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 12:57:04 -04:00
|
|
|
match app_state.done_current_exercise(&mut stdout)? {
|
|
|
|
ExercisesProgress::AllDone => (),
|
2024-05-13 19:49:22 -04:00
|
|
|
ExercisesProgress::CurrentPending | ExercisesProgress::NewPending => println!(
|
2024-04-13 20:41:19 -04:00
|
|
|
"Next exercise: {}",
|
|
|
|
app_state.current_exercise().terminal_link(),
|
|
|
|
),
|
2024-04-10 20:51:02 -04:00
|
|
|
}
|
2024-04-04 18:44:43 -04:00
|
|
|
|
2024-03-31 10:55:33 -04:00
|
|
|
Ok(())
|
2019-01-09 14:33:43 -05:00
|
|
|
}
|