|
| 1 | +mod compare; |
| 2 | +mod dir; |
| 3 | +mod terminal; |
| 4 | + |
| 5 | +use anyhow::Result; |
| 6 | +use clap::Parser; |
| 7 | +use colored::*; |
| 8 | +use std::path::PathBuf; |
| 9 | + |
| 10 | +#[derive(Parser, Debug)] |
| 11 | +#[command(author, version, about, long_about = None)] |
| 12 | +struct Args { |
| 13 | + /// First image or directory |
| 14 | + path_a: PathBuf, |
| 15 | + |
| 16 | + /// Second image or directory |
| 17 | + path_b: PathBuf, |
| 18 | + |
| 19 | + /// Threshold for difference (0.0 to 1.0) |
| 20 | + #[arg(short, long, default_value_t = 0.1)] |
| 21 | + threshold: f32, |
| 22 | + |
| 23 | + /// Output path for diff overlay image (single file mode only) |
| 24 | + #[arg(short, long)] |
| 25 | + output: Option<PathBuf>, |
| 26 | + |
| 27 | + /// Print preview in terminal |
| 28 | + #[arg(short, long)] |
| 29 | + preview: bool, |
| 30 | + |
| 31 | + /// Fail if any difference is found (non-zero exit code) |
| 32 | + #[arg(long)] |
| 33 | + fail_on_diff: bool, |
| 34 | +} |
| 35 | + |
| 36 | +fn main() -> Result<()> { |
| 37 | + let args = Args::parse(); |
| 38 | + |
| 39 | + if args.path_a.is_dir() && args.path_b.is_dir() { |
| 40 | + run_dir_diff(&args) |
| 41 | + } else { |
| 42 | + run_file_diff(&args) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +fn run_file_diff(args: &Args) -> Result<()> { |
| 47 | + let res = compare::compare_images( |
| 48 | + &args.path_a, |
| 49 | + &args.path_b, |
| 50 | + args.threshold, |
| 51 | + args.output.is_some() || args.preview, |
| 52 | + )?; |
| 53 | + |
| 54 | + println!("{}", "Comparison Result:".bold()); |
| 55 | + println!(" Similarity: {:.2}%", res.score * 100.0); |
| 56 | + println!(" Diff Pixels: {}", res.diff_pixels); |
| 57 | + println!(" Total Pixels: {}", res.total_pixels); |
| 58 | + |
| 59 | + if let Some(diff_img) = &res.diff_image { |
| 60 | + if let Some(output_path) = &args.output { |
| 61 | + diff_img.save(output_path)?; |
| 62 | + println!(" Diff image saved to: {}", output_path.display().to_string().cyan()); |
| 63 | + } |
| 64 | + |
| 65 | + if args.preview { |
| 66 | + println!("\n{}", "Terminal Preview:".bold()); |
| 67 | + let dynamic_img = image::DynamicImage::ImageRgba8(diff_img.clone()); |
| 68 | + terminal::print_preview(&dynamic_img); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if args.fail_on_diff && res.diff_pixels > 0 { |
| 73 | + std::process::exit(1); |
| 74 | + } |
| 75 | + |
| 76 | + Ok(()) |
| 77 | +} |
| 78 | + |
| 79 | +fn run_dir_diff(args: &Args) -> Result<()> { |
| 80 | + let items = dir::compare_directories(&args.path_a, &args.path_b, args.threshold)?; |
| 81 | + |
| 82 | + println!("\n{:<40} {:<10} {:<10}", "File", "Score", "Status"); |
| 83 | + println!("{}", "-".repeat(65)); |
| 84 | + |
| 85 | + let mut diff_count = 0; |
| 86 | + |
| 87 | + let total_files = items.len(); |
| 88 | + for item in items { |
| 89 | + match item.status { |
| 90 | + dir::DirDiffStatus::Match(res) => { |
| 91 | + let status = if res.diff_pixels > 0 { |
| 92 | + diff_count += 1; |
| 93 | + "DIFF".red() |
| 94 | + } else { |
| 95 | + "OK".green() |
| 96 | + }; |
| 97 | + println!("{:<40} {:<10.2}% {:<10}", |
| 98 | + item.relative_path.display().to_string(), |
| 99 | + res.score * 100.0, |
| 100 | + status |
| 101 | + ); |
| 102 | + } |
| 103 | + dir::DirDiffStatus::MissingInB => { |
| 104 | + diff_count += 1; |
| 105 | + println!("{:<40} {:<10} {:<10}", |
| 106 | + item.relative_path.display().to_string(), |
| 107 | + "-".dimmed(), |
| 108 | + "MISSING".yellow() |
| 109 | + ); |
| 110 | + } |
| 111 | + dir::DirDiffStatus::Error(e) => { |
| 112 | + println!("{:<40} {:<10} {:<10}", |
| 113 | + item.relative_path.display().to_string(), |
| 114 | + "ERROR".red(), |
| 115 | + e.yellow() |
| 116 | + ); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + println!("\nSummary: {} files compared, {} differences found.", |
| 122 | + total_files, |
| 123 | + diff_count |
| 124 | + ); |
| 125 | + |
| 126 | + if args.fail_on_diff && diff_count > 0 { |
| 127 | + std::process::exit(1); |
| 128 | + } |
| 129 | + |
| 130 | + Ok(()) |
| 131 | +} |
0 commit comments