Skip to content

Commit

Permalink
fix: Improve color env variable support
Browse files Browse the repository at this point in the history
- `CLICOLOR=1` now works correctly
- `NO_COLOR=` now works correctly
- Auto-enable colors in CI


For running `typos` on the Linux kernel (176,210 typos to be printed), we went from 20.082s to
<20.450s.  Where in that range is unclear due to jitter in my system.
```console
$ hyperfine -L typos ./typos-main,./typos-anstream "{typos} ../../../linux" -i
Benchmark 1: ./typos-main ../../../linux
  Time (mean ± σ):     20.082 s ±  0.111 s    [User: 39.668 s, System: 0.474 s]
  Range (min … max):   19.961 s … 20.331 s    10 runs

  Warning: Ignoring non-zero exit code.

Benchmark 2: ./typos-anstream ../../../linux
  Time (mean ± σ):     20.426 s ±  0.104 s    [User: 40.301 s, System: 0.523 s]
  Range (min … max):   20.316 s … 20.661 s    10 runs

  Warning: Ignoring non-zero exit code.

Summary
  './typos-main ../../../linux' ran
    1.02 ± 0.01 times faster than './typos-anstream ../../../linux'

$ CLICOLOR_FORCE=1 hyperfine -L typos ./typos-anstream "{typos} ../../../linux" -i
Benchmark 1: ./typos-anstream ../../../linux
  Time (mean ± σ):     20.262 s ±  0.075 s    [User: 39.961 s, System: 0.542 s]
  Range (min … max):   20.154 s … 20.420 s    10 runs

  Warning: Ignoring non-zero exit code.

$ CLICOLOR=0 hyperfine -L typos ./typos-anstream "{typos} ../../../linux" -i
Benchmark 1: ./typos-anstream ../../../linux
  Time (mean ± σ):     20.296 s ±  0.065 s    [User: 40.003 s, System: 0.565 s]
  Range (min … max):   20.169 s … 20.383 s    10 runs

  Warning: Ignoring non-zero exit code.
```
  • Loading branch information
epage committed Mar 14, 2023
1 parent 28e7f17 commit 3a29410
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 83 deletions.
90 changes: 82 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions crates/typos-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ toml = "0.7.2"
log = "0.4"
env_logger = { version = "0.10", default-features = false, features = ["auto-color"] }
atty = "0.2.14"
yansi = "0.5.1"
concolor = { version = "0.0.12" }
concolor-clap = { version = "0.0.14", features = ["api_unstable"] }
concolor = { version = "0.1.1" }
concolor-clap = { version = "0.1.0", features = ["api"] }
bstr = "1.3"
once_cell = "1.17.1"
ahash = "0.8"
Expand All @@ -79,6 +78,8 @@ unicode-width = "0.1.10"
unic-emoji-char = "0.9.0"
thread_local = "1.1.7"
globset = "0.4.10"
anstyle = "0.3.1"
anstyle-stream = "0.2.0"

[dev-dependencies]
assert_fs = "1.0"
Expand Down
21 changes: 4 additions & 17 deletions crates/typos-cli/src/bin/typos-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,12 @@ fn run() -> proc_exit::ExitResult {

init_logging(args.verbose.log_level());

let stdout_palette = if concolor::get(concolor::Stream::Stdout).ansi_color() {
report::Palette::colored()
} else {
report::Palette::plain()
};
let stderr_palette = if concolor::get(concolor::Stream::Stderr).ansi_color() {
report::Palette::colored()
} else {
report::Palette::plain()
};

if let Some(output_path) = args.dump_config.as_ref() {
run_dump_config(&args, output_path)
} else if args.type_list {
run_type_list(&args)
} else {
run_checks(&args, stdout_palette, stderr_palette)
run_checks(&args)
}
}

Expand Down Expand Up @@ -149,11 +138,7 @@ fn run_type_list(args: &args::Args) -> proc_exit::ExitResult {
Ok(())
}

fn run_checks(
args: &args::Args,
stdout_palette: report::Palette,
stderr_palette: report::Palette,
) -> proc_exit::ExitResult {
fn run_checks(args: &args::Args) -> proc_exit::ExitResult {
let global_cwd = std::env::current_dir().to_sysexits()?;

let storage = typos_cli::policy::ConfigStorage::new();
Expand Down Expand Up @@ -219,6 +204,8 @@ fn run_checks(
let output_reporter = if args.diff {
Box::new(crate::report::PrintSilent)
} else {
let stdout_palette = report::Palette::colored();
let stderr_palette = report::Palette::colored();
args.format.reporter(stdout_palette, stderr_palette)
};
let status_reporter = report::MessageStatus::new(output_reporter.as_ref());
Expand Down
Loading

0 comments on commit 3a29410

Please sign in to comment.