rustlings/src/run.rs

52 lines
1.4 KiB
Rust
Raw Normal View History

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