diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 52 |
1 files changed, 43 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs index fe4b3dc..c8bcd2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use clap::{Parser, Subcommand}; use std::{ io::{self, IsTerminal, Write}, path::Path, - process::exit, + process::ExitCode, }; use term::{clear_terminal, press_enter_prompt}; @@ -47,6 +47,8 @@ enum Subcommands { /// The name of the exercise name: Option<String>, }, + /// Check all the exercises, marking them as done or pending accordingly. + CheckAll, /// Reset a single exercise Reset { /// The name of the exercise @@ -62,22 +64,26 @@ enum Subcommands { Dev(DevCommands), } -fn main() -> Result<()> { +fn main() -> Result<ExitCode> { let args = Args::parse(); if cfg!(not(debug_assertions)) && Path::new("dev/rustlings-repo.txt").exists() { bail!("{OLD_METHOD_ERR}"); } - match args.command { - Some(Subcommands::Init) => return init::init().context("Initialization failed"), - Some(Subcommands::Dev(dev_command)) => return dev_command.run(), - _ => (), + 'priority_cmd: { + match args.command { + Some(Subcommands::Init) => init::init().context("Initialization failed")?, + Some(Subcommands::Dev(dev_command)) => dev_command.run()?, + _ => break 'priority_cmd, + } + + return Ok(ExitCode::SUCCESS); } if !Path::new("exercises").is_dir() { println!("{PRE_INIT_MSG}"); - exit(1); + return Ok(ExitCode::FAILURE); } let info_file = InfoFile::parse()?; @@ -136,7 +142,35 @@ fn main() -> Result<()> { if let Some(name) = name { app_state.set_current_exercise_by_name(&name)?; } - run::run(&mut app_state)?; + return run::run(&mut app_state); + } + Some(Subcommands::CheckAll) => { + let mut stdout = io::stdout().lock(); + if let Some(first_pending_exercise_ind) = app_state.check_all_exercises(&mut stdout)? { + if app_state.current_exercise().done { + app_state.set_current_exercise_ind(first_pending_exercise_ind)?; + } + + stdout.write_all(b"\n\n")?; + let pending = app_state.n_pending(); + if pending == 1 { + stdout.write_all(b"One exercise pending: ")?; + } else { + write!( + stdout, + "{pending}/{} exercises pending. The first: ", + app_state.exercises().len(), + )?; + } + app_state + .current_exercise() + .terminal_file_link(&mut stdout)?; + stdout.write_all(b"\n")?; + + return Ok(ExitCode::FAILURE); + } else { + app_state.render_final_message(&mut stdout)?; + } } Some(Subcommands::Reset { name }) => { app_state.set_current_exercise_by_name(&name)?; @@ -153,7 +187,7 @@ fn main() -> Result<()> { Some(Subcommands::Init | Subcommands::Dev(_)) => (), } - Ok(()) + Ok(ExitCode::SUCCESS) } const OLD_METHOD_ERR: &str = |
