|
1 | 1 | use anyhow::{bail, Context, Result};
|
2 | 2 | use app_state::StateFileStatus;
|
3 | 3 | use clap::{Parser, Subcommand};
|
| 4 | +use crossterm::{ |
| 5 | + style::{Color, Print, ResetColor, SetForegroundColor}, |
| 6 | + QueueableCommand, |
| 7 | +}; |
4 | 8 | use std::{
|
5 | 9 | io::{self, IsTerminal, Write},
|
6 | 10 | path::Path,
|
@@ -47,6 +51,8 @@ enum Subcommands {
|
47 | 51 | /// The name of the exercise
|
48 | 52 | name: Option<String>,
|
49 | 53 | },
|
| 54 | + /// Run all the exercises, marking them as done or pending accordingly. |
| 55 | + RunAll, |
50 | 56 | /// Reset a single exercise
|
51 | 57 | Reset {
|
52 | 58 | /// The name of the exercise
|
@@ -138,6 +144,36 @@ fn main() -> Result<()> {
|
138 | 144 | }
|
139 | 145 | run::run(&mut app_state)?;
|
140 | 146 | }
|
| 147 | + Some(Subcommands::RunAll) => { |
| 148 | + let mut stdout = io::stdout().lock(); |
| 149 | + if let Some(first_fail) = app_state.check_all_exercises(&mut stdout, false)? { |
| 150 | + let pending = app_state |
| 151 | + .exercises() |
| 152 | + .iter() |
| 153 | + .filter(|exercise| !exercise.done) |
| 154 | + .count(); |
| 155 | + if app_state.current_exercise().done { |
| 156 | + app_state.set_current_exercise_ind(first_fail)?; |
| 157 | + } |
| 158 | + stdout |
| 159 | + .queue(Print("\n"))? |
| 160 | + .queue(SetForegroundColor(Color::Red))? |
| 161 | + .queue(Print(format!("{pending}")))? |
| 162 | + .queue(ResetColor)?; |
| 163 | + if pending == 1 { |
| 164 | + stdout.queue(Print(" exercise has some errors: "))?; |
| 165 | + } else { |
| 166 | + stdout.queue(Print(" exercises have errors, including "))?; |
| 167 | + } |
| 168 | + app_state |
| 169 | + .current_exercise() |
| 170 | + .terminal_file_link(&mut stdout)?; |
| 171 | + stdout.write_all(b".\n")?; |
| 172 | + exit(1); |
| 173 | + } else { |
| 174 | + app_state.render_final_message(&mut stdout)?; |
| 175 | + } |
| 176 | + } |
141 | 177 | Some(Subcommands::Reset { name }) => {
|
142 | 178 | app_state.set_current_exercise_by_name(&name)?;
|
143 | 179 | let exercise_path = app_state.reset_current_exercise()?;
|
|
0 commit comments