rustlings/src/run.rs

57 lines
1.7 KiB
Rust
Raw Normal View History

2024-08-25 17:53:50 -04:00
use anyhow::Result;
use crossterm::{
style::{Color, ResetColor, SetForegroundColor},
QueueableCommand,
};
use std::{
io::{self, Write},
process::exit,
};
2024-04-23 20:52:30 -04:00
use crate::{
app_state::{AppState, ExercisesProgress},
2024-08-25 17:53:50 -04:00
exercise::{solution_link_line, RunnableExercise, OUTPUT_CAPACITY},
term::terminal_file_link,
2024-04-23 20:52:30 -04:00
};
2019-01-09 14:33:43 -05: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())?;
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-08-25 17:53:50 -04:00
stdout.write_all(b"Ran ")?;
terminal_file_link(&mut stdout, app_state.current_exercise().path, Color::Blue)?;
stdout.write_all(b" with errors\n")?;
exit(1);
2019-01-09 14:33:43 -05:00
}
2024-03-31 10:55:33 -04:00
2024-08-25 17:53:50 -04:00
stdout.queue(SetForegroundColor(Color::Green))?;
stdout.write_all("✓ Successfully ran ".as_bytes())?;
stdout.write_all(exercise.path.as_bytes())?;
stdout.queue(ResetColor)?;
stdout.write_all(b"\n")?;
2024-04-23 20:52:30 -04:00
if let Some(solution_path) = app_state.current_solution_path()? {
2024-08-25 17:53:50 -04:00
stdout.write_all(b"\n")?;
solution_link_line(&mut stdout, &solution_path)?;
stdout.write_all(b"\n")?;
2024-04-23 20:52:30 -04:00
}
match app_state.done_current_exercise(&mut stdout)? {
2024-08-25 17:53:50 -04:00
ExercisesProgress::CurrentPending | ExercisesProgress::NewPending => {
stdout.write_all(b"Next exercise: ")?;
terminal_file_link(&mut stdout, app_state.current_exercise().path, Color::Blue)?;
stdout.write_all(b"\n")?;
}
ExercisesProgress::AllDone => (),
}
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
}