Skip to content

Commit

Permalink
Add color to the printed stats to make them pop
Browse files Browse the repository at this point in the history
  • Loading branch information
faern committed Dec 12, 2024
1 parent 81d7e1b commit d59f07e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 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
39 changes: 33 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,28 +202,55 @@ fn main() -> anyhow::Result<()> {
}
}

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 global_scan_stats.num_rule_violations > 0 || num_failed_files > 0 {
println!("");
if found_issue {
println!();
}
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 d59f07e

Please sign in to comment.