summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-04-07 03:03:37 +0200
committermo8it <mo8it@proton.me>2024-04-07 03:03:37 +0200
commitf6db88aca860b229e97712a612cee8ab4436b764 (patch)
tree58d15420643310d2f1860f4f24a63a10c7e174bf /src/main.rs
parent0819bbe21fc86315d3acdcdb2bc14b21f3acb788 (diff)
Started with list
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs96
1 files changed, 4 insertions, 92 deletions
diff --git a/src/main.rs b/src/main.rs
index e8218ef..34d1784 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,7 +6,6 @@ use crate::verify::verify;
use anyhow::{bail, Context, Result};
use clap::{Parser, Subcommand};
use state::State;
-use std::io::Write;
use std::path::Path;
use std::process::exit;
use verify::VerifyState;
@@ -15,6 +14,7 @@ mod consts;
mod embedded;
mod exercise;
mod init;
+mod list;
mod run;
mod state;
mod verify;
@@ -52,24 +52,7 @@ enum Subcommands {
name: String,
},
/// List the exercises available in Rustlings
- List {
- /// Show only the paths of the exercises
- #[arg(short, long)]
- paths: bool,
- /// Show only the names of the exercises
- #[arg(short, long)]
- names: bool,
- /// Provide a string to match exercise names.
- /// Comma separated patterns are accepted
- #[arg(short, long)]
- filter: Option<String>,
- /// Display only exercises not yet solved
- #[arg(short, long)]
- unsolved: bool,
- /// Display only exercises that have been solved
- #[arg(short, long)]
- solved: bool,
- },
+ List,
}
fn main() -> Result<()> {
@@ -110,79 +93,8 @@ If you are just starting with Rustlings, run the command `rustlings init` to ini
}
// `Init` is handled above.
Some(Subcommands::Init) => (),
- Some(Subcommands::List {
- paths,
- names,
- filter,
- unsolved,
- solved,
- }) => {
- if !paths && !names {
- println!("{:<17}\t{:<46}\t{:<7}", "Name", "Path", "Status");
- }
- let mut exercises_done: u16 = 0;
- let lowercase_filter = filter
- .as_ref()
- .map(|s| s.to_lowercase())
- .unwrap_or_default();
- let filters = lowercase_filter
- .split(',')
- .filter_map(|f| {
- let f = f.trim();
- if f.is_empty() {
- None
- } else {
- Some(f)
- }
- })
- .collect::<Vec<_>>();
-
- for exercise in &exercises {
- let fname = exercise.path.to_string_lossy();
- let filter_cond = filters
- .iter()
- .any(|f| exercise.name.contains(f) || fname.contains(f));
- let looks_done = exercise.looks_done()?;
- let status = if looks_done {
- exercises_done += 1;
- "Done"
- } else {
- "Pending"
- };
- let solve_cond =
- (looks_done && solved) || (!looks_done && unsolved) || (!solved && !unsolved);
- if solve_cond && (filter_cond || filter.is_none()) {
- let line = if paths {
- format!("{fname}\n")
- } else if names {
- format!("{}\n", exercise.name)
- } else {
- format!("{:<17}\t{fname:<46}\t{status:<7}\n", exercise.name)
- };
- // Somehow using println! leads to the binary panicking
- // when its output is piped.
- // So, we're handling a Broken Pipe error and exiting with 0 anyway
- let stdout = std::io::stdout();
- {
- let mut handle = stdout.lock();
- handle.write_all(line.as_bytes()).unwrap_or_else(|e| {
- match e.kind() {
- std::io::ErrorKind::BrokenPipe => exit(0),
- _ => exit(1),
- };
- });
- }
- }
- }
-
- let percentage_progress = exercises_done as f32 / exercises.len() as f32 * 100.0;
- println!(
- "Progress: You completed {} / {} exercises ({:.1} %).",
- exercises_done,
- exercises.len(),
- percentage_progress
- );
- exit(0);
+ Some(Subcommands::List) => {
+ list::list(&state, &exercises)?;
}
Some(Subcommands::Run { name }) => {
let exercise = find_exercise(&name, &exercises)?;