Skip to content

Commit

Permalink
Merge branch 'improve-stats-summary'
Browse files Browse the repository at this point in the history
  • Loading branch information
faern committed Dec 12, 2024
2 parents d915677 + d59f07e commit 9a315d5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
40 changes: 36 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 36 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9a315d5

Please sign in to comment.