diff --git a/src/alejandra_cli/Cargo.toml b/src/alejandra_cli/Cargo.toml index df93e8d..d948fce 100644 --- a/src/alejandra_cli/Cargo.toml +++ b/src/alejandra_cli/Cargo.toml @@ -7,6 +7,7 @@ alejandra = { path = "../alejandra" } clap = { version = "*", default-features = false, features = [ "color", "derive", + "env", "std", "strsim", ] } diff --git a/src/alejandra_cli/src/cli.rs b/src/alejandra_cli/src/cli.rs index dee76ac..0ef0496 100644 --- a/src/alejandra_cli/src/cli.rs +++ b/src/alejandra_cli/src/cli.rs @@ -1,4 +1,3 @@ -use std::env; use std::fs::read_to_string; use std::io::Read; @@ -49,7 +48,11 @@ struct CLIArgs { /// Number of formatting threads to spawn. Defaults to the number of /// physical CPUs. - #[clap(long, short, value_parser = value_parser!(u8).range(1..))] + #[clap( + long, short, + env = "ALEJANDRA_THREADS", + value_parser = value_parser!(u8).range(1..), + )] threads: Option, /// Use once to hide informational messages, @@ -146,15 +149,8 @@ pub fn main() -> ! { let include: Vec<&str> = args.include.iter().map(String::as_str).collect::>(); - // Try CLI value, then env var, then fall back to number of physical CPUs. - let threads: usize = if let Some(cli_threads) = args.threads { - cli_threads.into() // convert u8 to usize - } else { - env::var("ALEJANDRA_THREADS") - .ok() - .and_then(|v| v.parse::().ok()) - .unwrap_or_else(num_cpus::get_physical) - }; + let threads = + args.threads.map_or_else(num_cpus::get_physical, Into::::into); let verbosity = match args.quiet { 0 => Verbosity::Everything, diff --git a/src/alejandra_cli/tests/cli.rs b/src/alejandra_cli/tests/cli.rs index 3af89c8..a7b4963 100644 --- a/src/alejandra_cli/tests/cli.rs +++ b/src/alejandra_cli/tests/cli.rs @@ -128,7 +128,7 @@ fn cases() { output_got.push_str(&format!("args: {:?}\n", case.args)); let mut child = Command::new("cargo") - .env("ALEJANDRA_THREADS", "4") + .env("ALEJANDRA_THREADS", "1") .args(["run", "--quiet", "--"]) .args(case.args) .stdin(Stdio::piped()) diff --git a/src/alejandra_cli/tests/output.txt b/src/alejandra_cli/tests/output.txt index 6b9e39e..17c9b77 100644 --- a/src/alejandra_cli/tests/output.txt +++ b/src/alejandra_cli/tests/output.txt @@ -33,7 +33,7 @@ stdout: -t, --threads Number of formatting threads to spawn. Defaults to the number of - physical CPUs + physical CPUs [env: ALEJANDRA_THREADS=1] -V, --version Print version information @@ -184,7 +184,7 @@ exit code: Some(1) === args: [".", "--exclude", "."] stderr: - Checking style in 0 files using 4 threads. + Checking style in 0 files using 1 thread. Congratulations! Your code complies with the Alejandra style. @@ -203,7 +203,7 @@ stdout: Requires formatting: tests/inputs/changed.nix stderr: - Checking style in 1 file using 4 threads. + Checking style in 1 file using 1 thread. Alert! 1 file requires formatting. @@ -218,7 +218,7 @@ stdout: Requires formatting: tests/inputs/changed.nix stderr: - Checking style in 1 file using 4 threads. + Checking style in 1 file using 1 thread. Alert! 1 file requires formatting. @@ -233,7 +233,7 @@ exit code: Some(2) === args: ["-c", "tests/inputs/unchanged.nix"] stderr: - Checking style in 1 file using 4 threads. + Checking style in 1 file using 1 thread. Congratulations! Your code complies with the Alejandra style. @@ -249,7 +249,7 @@ exit code: Some(0) === args: ["--check", "tests/inputs/error.nix"] stderr: - Checking style in 1 file using 4 threads. + Checking style in 1 file using 1 thread. Failed! 1 error found at: