Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the stat printing *pop* with colors and newlines #37

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading