2024-04-05 01:44:43 +03:00
|
|
|
use anyhow::{bail, Result};
|
2024-04-09 18:15:12 +03:00
|
|
|
use crossterm::style::Stylize;
|
2024-04-12 19:57:04 +03:00
|
|
|
use std::io::{self, Write};
|
2022-08-17 17:31:53 +03:00
|
|
|
|
2024-04-11 03:51:02 +03:00
|
|
|
use crate::app_state::{AppState, ExercisesProgress};
|
2019-01-09 22:33:43 +03:00
|
|
|
|
2024-04-11 03:51:02 +03:00
|
|
|
pub fn run(app_state: &mut AppState) -> Result<()> {
|
|
|
|
let exercise = app_state.current_exercise();
|
2024-03-31 17:55:33 +03:00
|
|
|
let output = exercise.run()?;
|
2019-03-06 23:47:00 +03:00
|
|
|
|
2024-04-12 19:57:04 +03:00
|
|
|
let mut stdout = io::stdout().lock();
|
|
|
|
stdout.write_all(&output.stdout)?;
|
|
|
|
stdout.write_all(b"\n")?;
|
|
|
|
stdout.write_all(&output.stderr)?;
|
|
|
|
stdout.flush()?;
|
2024-04-04 22:06:11 +03:00
|
|
|
|
2024-04-05 01:44:43 +03:00
|
|
|
if !output.status.success() {
|
2024-04-12 20:24:26 +03:00
|
|
|
app_state.set_pending(app_state.current_exercise_ind())?;
|
|
|
|
|
2024-04-14 02:15:43 +03:00
|
|
|
bail!("Ran {} with errors", app_state.current_exercise());
|
2019-01-09 22:33:43 +03:00
|
|
|
}
|
2024-03-31 17:55:33 +03:00
|
|
|
|
2024-04-12 19:57:04 +03:00
|
|
|
stdout.write_fmt(format_args!(
|
2024-04-12 20:30:36 +03:00
|
|
|
"{}{}\n",
|
2024-04-11 03:51:02 +03:00
|
|
|
"✓ Successfully ran ".green(),
|
|
|
|
exercise.path.to_string_lossy().green(),
|
2024-04-12 19:57:04 +03:00
|
|
|
))?;
|
2024-04-11 03:51:02 +03:00
|
|
|
|
2024-04-12 19:57:04 +03:00
|
|
|
match app_state.done_current_exercise(&mut stdout)? {
|
|
|
|
ExercisesProgress::AllDone => (),
|
2024-04-11 03:51:02 +03:00
|
|
|
ExercisesProgress::Pending => println!("Next exercise: {}", app_state.current_exercise()),
|
|
|
|
}
|
2024-04-05 01:44:43 +03:00
|
|
|
|
2024-03-31 17:55:33 +03:00
|
|
|
Ok(())
|
2019-01-09 22:33:43 +03:00
|
|
|
}
|