use std::path::Path;
use colored::Colorize;
use crate::analysis::Issue;
use crate::cli::Cli;
pub struct Reporter<'a> {
cli: &'a Cli,
}
impl<'a> Reporter<'a> {
pub fn new(cli: &'a Cli) -> Self {
if cli.no_color {
colored::control::set_override(false);
}
Self { cli }
}
pub fn report_file_issues(&self, path: &Path, issues: &[Issue]) {
if self.cli.quiet {
return;
}
println!("{}", path.display().to_string().bold());
for issue in issues {
if issue.is_fixable() {
println!(
" {} {}",
"error:".red().bold(),
issue.description().red()
);
} else {
println!(
" {} {}",
"warning:".yellow().bold(),
issue.description().yellow()
);
}
}
}
pub fn report_file_ok(&self, path: &Path) {
if !self.cli.verbose || self.cli.quiet {
return;
}
println!("{} {}", "✓".green(), path.display());
}
pub fn report_error(&self, path: &Path, error: &anyhow::Error) {
if self.cli.quiet {
return;
}
eprintln!(
"{} {}: {}",
"error:".red().bold(),
path.display(),
error
);
}
pub fn report_would_fix(&self, path: &Path, issues: &[Issue]) {
if self.cli.quiet {
return;
}
println!("{} {}", "Would fix:".blue(), path.display());
if self.cli.verbose {
for issue in issues {
println!(" {} {}", "•".yellow(), issue.description());
}
}
}
pub fn report_fixed(&self, path: &Path, issues: &[Issue]) {
if self.cli.quiet {
return;
}
println!("{} {}", "Fixed:".green(), path.display());
if self.cli.verbose {
for issue in issues {
println!(" {} {}", "•".yellow(), issue.description());
}
}
}
pub fn report_summary(
&self,
files_with_issues: usize,
error_count: usize,
warning_count: usize,
) {
if self.cli.quiet {
return;
}
println!();
let total_issues = error_count + warning_count;
if total_issues == 0 {
println!("{}", "No issues found.".green());
} else {
let mut parts = Vec::new();
if error_count > 0 {
parts.push(format!("{} errors", error_count).red().to_string());
}
if warning_count > 0 {
parts.push(format!("{} warnings", warning_count).yellow().to_string());
}
println!(
"Found {} in {} files.",
parts.join(", "),
files_with_issues
);
}
}
pub fn report_fix_summary(&self, fixed: usize, failed: usize, dry_run: bool) {
if self.cli.quiet {
return;
}
println!();
if dry_run {
if fixed == 0 {
println!("{}", "No files would be modified.".green());
} else {
println!(
"{}",
format!("Would fix {} files.", fixed).blue()
);
}
} else {
if fixed > 0 {
println!(
"{}",
format!("Fixed {} files.", fixed).green()
);
}
if failed > 0 {
println!(
"{}",
format!("Failed to fix {} files.", failed).red()
);
}
if fixed == 0 && failed == 0 {
println!("{}", "No files needed fixing.".green());
}
}
}
}