diff --git a/Cargo.lock b/Cargo.lock index 786f802..2f16347 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -316,6 +316,12 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hermit-abi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" + [[package]] name = "humantime" version = "2.1.0" @@ -342,6 +348,17 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "is-terminal" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "is_ci" version = "1.2.0" @@ -389,7 +406,7 @@ dependencies = [ "cfg-if", "miette-derive", "owo-colors", - "supports-color", + "supports-color 3.0.0", "supports-hyperlinks", "supports-unicode", "terminal_size", @@ -445,9 +462,13 @@ dependencies = [ [[package]] name = "owo-colors" -version = "4.0.0" +version = "4.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "caff54706df99d2a78a5a4e3455ff45448d81ef1bb63c22cd14052ca0e993a3f" +checksum = "fb37767f6569cd834a413442455e0f066d0d522de8630436e2a1761d9726ba56" +dependencies = [ + "supports-color 2.1.0", + "supports-color 3.0.0", +] [[package]] name = "phf" @@ -700,6 +721,16 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "supports-color" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6398cde53adc3c4557306a96ce67b302968513830a77a95b2b17305d9719a89" +dependencies = [ + "is-terminal", + "is_ci", +] + [[package]] name = "supports-color" version = "3.0.0" @@ -992,6 +1023,7 @@ dependencies = [ "glob", "log", "miette", + "owo-colors", "phf", "serde", "toml", diff --git a/Cargo.toml b/Cargo.toml index e863526..b9af73f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ phf = { version = "0.11.2", features = ["macros"] } clap = { version = "4.5.16", features = ["derive"] } log = "0.4.22" env_logger = "0.11.5" +owo-colors = { version = "4.1.0", features = ["supports-colors"] } # Grammars tree-sitter-go = "0.23.1" diff --git a/README.md b/README.md index 55e68c2..9db91a8 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ $ unicop example-files/homoglyph.js example-files/invisible.js example-files/not 6 │ ]; ╰──── Error while scanning example-files/not-utf-8-file.ts: Failed to read file (stream did not contain valid UTF-8) + Scanned 486 unicode code points in 2 files, resulting in 3 rule violations Failed to scan 1 file diff --git a/src/main.rs b/src/main.rs index 95bc76c..d08e0fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -202,23 +202,55 @@ fn main() -> anyhow::Result<()> { } } - println!( + let found_issue = global_scan_stats.num_rule_violations > 0 || num_failed_files > 0; + + // If any errors have been reported, print an empty line. Visually separates + // the below stats summary from the above error printing + if found_issue { + println!(); + } + let summary_print_style = message_style(found_issue); + + let scan_stats_msg = format!( "Scanned {} unicode code points in {} files, resulting in {} rule violations", global_scan_stats.num_unicode_code_points, num_files_scanned, global_scan_stats.num_rule_violations, ); + print_with_style(&scan_stats_msg, summary_print_style); + match num_failed_files { 0 => (), - 1 => println!("Failed to scan 1 file"), - 2.. => println!("Failed to scan {num_failed_files} files"), + 1 => print_with_style("Failed to scan 1 file", summary_print_style), + 2.. => print_with_style( + &format!("Failed to scan {num_failed_files} files"), + summary_print_style, + ), } - if global_scan_stats.num_rule_violations > 0 || num_failed_files > 0 { + + if found_issue { std::process::exit(1); } Ok(()) } +fn print_with_style(msg: &str, style: owo_colors::Style) { + use owo_colors::{OwoColorize, Stream}; + println!( + "{}", + msg.if_supports_color(Stream::Stdout, |text| text.style(style)) + ); +} + +fn message_style(found_issue: bool) -> owo_colors::Style { + let base_style = owo_colors::Style::new().bold(); + if found_issue { + base_style.red() + } else { + base_style.green() + } +} + #[derive(Debug)] enum ScanError { /// Failed to read the source code file