rustlings/src/run.rs

52 lines
1.3 KiB
Rust
Raw Normal View History

2024-04-04 18:44:43 -04:00
use anyhow::{bail, Result};
2024-04-23 20:52:30 -04:00
use crossterm::style::{style, Stylize};
use std::io::{self, Write};
2024-04-23 20:52:30 -04:00
use crate::{
app_state::{AppState, ExercisesProgress},
terminal_link::TerminalFileLink,
};
2019-01-09 14:33:43 -05:00
pub fn run(app_state: &mut AppState) -> Result<()> {
let exercise = app_state.current_exercise();
2024-03-31 10:55:33 -04:00
let output = exercise.run()?;
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 15:06:11 -04:00
2024-04-04 18:44:43 -04:00
if !output.status.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
stdout.write_fmt(format_args!(
2024-04-12 13:30:36 -04:00
"{}{}\n",
"✓ Successfully ran ".green(),
2024-04-13 20:41:19 -04:00
exercise.path.green(),
))?;
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(),
);
}
match app_state.done_current_exercise(&mut stdout)? {
ExercisesProgress::AllDone => (),
2024-04-13 20:41:19 -04:00
ExercisesProgress::Pending => println!(
"Next exercise: {}",
app_state.current_exercise().terminal_link(),
),
}
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
}