butteraugli-cli 0.9.0

Command-line tool for butteraugli perceptual image quality comparison
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
//! butteraugli CLI - Perceptual image quality metric
//!
//! Compare two images and compute a butteraugli distance score.

use std::io::{self, IsTerminal, Write};
use std::path::{Path, PathBuf};
use std::process::ExitCode;

use butteraugli::{ButteraugliParams, ButteraugliResult, Img, ImgVec, RGB8, butteraugli};
use clap::{ArgAction, ColorChoice, Parser, ValueEnum};
use colored::Colorize;
use image::GenericImageView;
use serde::Serialize;

/// Butteraugli perceptual image quality metric
///
/// Computes the perceptual distance between two images. Lower scores mean
/// the images are more similar. A score of 0 means identical images.
///
/// Score interpretation:
///   0.0       - Identical images
///   0.0 - 0.5 - Imperceptible difference
///   0.5 - 1.0 - Barely noticeable
///   1.0 - 2.0 - Noticeable but acceptable
///   2.0 - 3.0 - Clearly visible difference
///   3.0+      - Large difference
#[derive(Parser, Debug)]
#[command(name = "butteraugli")]
#[command(author, version, about, long_about = None)]
#[command(after_help = "EXAMPLES:
    Compare two images:
        butteraugli original.png compressed.jpg

    Show quality rating with colors:
        butteraugli -q original.png compressed.jpg

    CI mode - fail if score exceeds threshold:
        butteraugli --max-score 1.5 original.png compressed.jpg

    Compare all PNGs in two directories:
        butteraugli --batch dir1/ dir2/

    Output JSON for scripting:
        butteraugli --json original.png compressed.jpg

    Save difference heatmap:
        butteraugli --diffmap diff.png original.png compressed.jpg

    HDR content (higher intensity target):
        butteraugli --intensity-target 250 hdr_ref.png hdr_test.png

EXIT CODES:
    0 - Success (score within threshold if --max-score specified)
    1 - Score exceeded threshold (--max-score)
    2 - Error (file not found, invalid image, etc.)")]
struct Cli {
    /// Reference image or directory (original/source)
    #[arg(value_name = "REFERENCE")]
    reference: PathBuf,

    /// Distorted image or directory (compressed/modified)
    #[arg(value_name = "DISTORTED")]
    distorted: PathBuf,

    /// Output format
    #[arg(short, long, value_enum, default_value = "text")]
    format: OutputFormat,

    /// Output JSON (shorthand for --format json)
    #[arg(long, conflicts_with = "format")]
    json: bool,

    /// Show quality rating with colors (shorthand for --format quality)
    #[arg(short, long, conflicts_with = "format")]
    quality: bool,

    /// Save diffmap (heatmap) to file
    #[arg(short, long, value_name = "FILE")]
    diffmap: Option<PathBuf>,

    /// Maximum acceptable score (exit code 1 if exceeded)
    ///
    /// Useful for CI pipelines to enforce quality thresholds.
    /// Common thresholds: 1.0 (good), 1.5 (acceptable), 2.0 (bad)
    #[arg(long, value_name = "SCORE")]
    max_score: Option<f64>,

    /// Batch mode: compare matching files in two directories
    #[arg(long, short = 'b')]
    batch: bool,

    /// File extensions to include in batch mode (comma-separated)
    #[arg(
        long,
        default_value = "png,jpg,jpeg,webp,gif,bmp",
        value_delimiter = ','
    )]
    extensions: Vec<String>,

    /// Intensity target (viewing conditions, default: 80 nits)
    ///
    /// Lower values make the metric more sensitive to differences
    /// in dark regions. Default is 80 (typical indoor viewing).
    /// Use 250+ for HDR content.
    #[arg(long, default_value = "80.0", value_name = "NITS")]
    intensity_target: f32,

    /// High-frequency asymmetry factor
    ///
    /// Controls sensitivity to high-frequency artifacts.
    /// Higher values penalize blurring more than ringing.
    #[arg(long, default_value = "1.0", value_name = "FACTOR")]
    hf_asymmetry: f32,

    /// X channel multiplier (color sensitivity)
    #[arg(long, default_value = "1.0", value_name = "FACTOR")]
    xmul: f32,

    /// Quiet mode - only output the score number
    #[arg(long, short = 's', action = ArgAction::SetTrue)]
    quiet: bool,

    /// Control color output
    #[arg(long, value_enum, default_value = "auto")]
    color: ColorChoice,

    /// Continue on errors in batch mode
    #[arg(long)]
    keep_going: bool,

    /// Show summary statistics in batch mode
    #[arg(long)]
    summary: bool,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum)]
enum OutputFormat {
    /// Plain text output with score
    Text,
    /// JSON output with all metrics
    Json,
    /// Include quality rating interpretation (with colors)
    Quality,
    /// Minimal - just the score number
    Score,
}

#[derive(Serialize)]
struct JsonOutput {
    score: f64,
    quality_rating: String,
    quality_description: String,
    reference: String,
    distorted: String,
    width: u32,
    height: u32,
    params: JsonParams,
    #[serde(skip_serializing_if = "Option::is_none")]
    threshold_exceeded: Option<bool>,
}

#[derive(Serialize)]
struct JsonParams {
    intensity_target: f32,
    hf_asymmetry: f32,
    xmul: f32,
}

#[derive(Serialize)]
struct BatchJsonOutput {
    results: Vec<JsonOutput>,
    summary: BatchSummary,
}

#[derive(Serialize)]
struct BatchSummary {
    total: usize,
    passed: usize,
    failed: usize,
    errors: usize,
    min_score: f64,
    max_score: f64,
    mean_score: f64,
}

struct ComparisonResult {
    reference: PathBuf,
    distorted: PathBuf,
    result: Result<(ButteraugliResult, u32, u32), String>,
}

fn main() -> ExitCode {
    let cli = Cli::parse();

    // Set up color output
    setup_colors(&cli);

    if cli.batch || (cli.reference.is_dir() && cli.distorted.is_dir()) {
        run_batch(&cli)
    } else {
        run_single(&cli)
    }
}

fn setup_colors(cli: &Cli) {
    match cli.color {
        ColorChoice::Always => colored::control::set_override(true),
        ColorChoice::Never => colored::control::set_override(false),
        ColorChoice::Auto => {
            // Disable colors if not a terminal
            if !io::stdout().is_terminal() {
                colored::control::set_override(false);
            }
        }
    }
}

fn run_single(cli: &Cli) -> ExitCode {
    match compare_images(cli, &cli.reference, &cli.distorted) {
        Ok((result, width, height)) => {
            // Save diffmap if requested
            if let Some(diffmap_path) = &cli.diffmap
                && let Some(diffmap) = &result.diffmap
            {
                if let Err(e) = save_diffmap(diffmap, diffmap_path) {
                    if !cli.quiet {
                        eprintln!("{}: {}", "error".red().bold(), e);
                    }
                    return ExitCode::from(2);
                }
                if !cli.quiet && get_format(cli) != OutputFormat::Json {
                    eprintln!("Diffmap saved to: {}", diffmap_path.display());
                }
            }

            // Output results
            if let Err(e) = output_single_result(cli, &result, width, height) {
                if !cli.quiet {
                    eprintln!("{}: {}", "error".red().bold(), e);
                }
                return ExitCode::from(2);
            }

            // Check threshold
            if let Some(max_score) = cli.max_score
                && result.score > max_score
            {
                return ExitCode::from(1);
            }

            ExitCode::SUCCESS
        }
        Err(e) => {
            if !cli.quiet {
                eprintln!("{}: {}", "error".red().bold(), e);
            }
            ExitCode::from(2)
        }
    }
}

fn run_batch(cli: &Cli) -> ExitCode {
    if !cli.reference.is_dir() {
        eprintln!(
            "{}: reference path '{}' is not a directory",
            "error".red().bold(),
            cli.reference.display()
        );
        return ExitCode::from(2);
    }
    if !cli.distorted.is_dir() {
        eprintln!(
            "{}: distorted path '{}' is not a directory",
            "error".red().bold(),
            cli.distorted.display()
        );
        return ExitCode::from(2);
    }

    // Find matching files
    let pairs = match find_matching_files(&cli.reference, &cli.distorted, &cli.extensions) {
        Ok(pairs) => pairs,
        Err(e) => {
            eprintln!("{}: {}", "error".red().bold(), e);
            return ExitCode::from(2);
        }
    };

    if pairs.is_empty() {
        eprintln!(
            "{}: no matching image files found",
            "warning".yellow().bold()
        );
        return ExitCode::from(2);
    }

    // Compare all pairs
    let mut results: Vec<ComparisonResult> = Vec::new();
    let mut had_errors = false;
    let mut threshold_exceeded = false;

    for (ref_path, dist_path) in &pairs {
        let comparison = compare_images(cli, ref_path, dist_path);

        if let Err(ref e) = comparison {
            had_errors = true;
            if !cli.keep_going {
                eprintln!("{}: {}: {}", "error".red().bold(), ref_path.display(), e);
                return ExitCode::from(2);
            }
        }

        if let Ok((ref result, _, _)) = comparison
            && let Some(max_score) = cli.max_score
            && result.score > max_score
        {
            threshold_exceeded = true;
        }

        results.push(ComparisonResult {
            reference: ref_path.clone(),
            distorted: dist_path.clone(),
            result: comparison,
        });
    }

    // Output results
    if let Err(e) = output_batch_results(cli, &results) {
        eprintln!("{}: {}", "error".red().bold(), e);
        return ExitCode::from(2);
    }

    if threshold_exceeded {
        ExitCode::from(1)
    } else if had_errors {
        ExitCode::from(2)
    } else {
        ExitCode::SUCCESS
    }
}

fn find_matching_files(
    ref_dir: &Path,
    dist_dir: &Path,
    extensions: &[String],
) -> Result<Vec<(PathBuf, PathBuf)>, String> {
    let extensions: Vec<String> = extensions.iter().map(|e| e.to_lowercase()).collect();

    let mut pairs = Vec::new();

    let entries = std::fs::read_dir(ref_dir)
        .map_err(|e| format!("failed to read directory '{}': {}", ref_dir.display(), e))?;

    for entry in entries {
        let entry = entry.map_err(|e| format!("failed to read directory entry: {}", e))?;
        let path = entry.path();

        if !path.is_file() {
            continue;
        }

        // Check extension
        let ext = path
            .extension()
            .and_then(|e| e.to_str())
            .map(|e| e.to_lowercase())
            .unwrap_or_default();

        if !extensions.contains(&ext) {
            continue;
        }

        // Find matching file in distorted directory
        let filename = path.file_name().unwrap();
        let dist_path = dist_dir.join(filename);

        if dist_path.exists() {
            pairs.push((path, dist_path));
        }
    }

    pairs.sort_by(|a, b| a.0.cmp(&b.0));
    Ok(pairs)
}

fn compare_images(
    cli: &Cli,
    ref_path: &Path,
    dist_path: &Path,
) -> Result<(ButteraugliResult, u32, u32), String> {
    // Load images
    let ref_img = image::open(ref_path)
        .map_err(|e| format!("failed to load '{}': {}", ref_path.display(), e))?;
    let dist_img = image::open(dist_path)
        .map_err(|e| format!("failed to load '{}': {}", dist_path.display(), e))?;

    // Check dimensions match
    let (ref_w, ref_h) = ref_img.dimensions();
    let (dist_w, dist_h) = dist_img.dimensions();

    if ref_w != dist_w || ref_h != dist_h {
        return Err(format!(
            "dimension mismatch: {}x{} vs {}x{}",
            ref_w, ref_h, dist_w, dist_h
        ));
    }

    // Convert to RGB8
    let ref_rgb = ref_img.to_rgb8();
    let dist_rgb = dist_img.to_rgb8();

    // Convert to RGB8 pixels for butteraugli
    let ref_pixels: Vec<RGB8> = ref_rgb
        .pixels()
        .map(|p| RGB8::new(p.0[0], p.0[1], p.0[2]))
        .collect();
    let dist_pixels: Vec<RGB8> = dist_rgb
        .pixels()
        .map(|p| RGB8::new(p.0[0], p.0[1], p.0[2]))
        .collect();

    let ref_imgref = Img::new(ref_pixels, ref_w as usize, ref_h as usize);
    let dist_imgref = Img::new(dist_pixels, dist_w as usize, dist_h as usize);

    // Set up parameters
    let params = ButteraugliParams::default()
        .with_intensity_target(cli.intensity_target)
        .with_hf_asymmetry(cli.hf_asymmetry)
        .with_xmul(cli.xmul)
        .with_compute_diffmap(cli.diffmap.is_some());

    // Compute butteraugli
    let result = butteraugli(ref_imgref.as_ref(), dist_imgref.as_ref(), &params)
        .map_err(|e| format!("butteraugli failed: {e}"))?;

    Ok((result, ref_w, ref_h))
}

fn get_format(cli: &Cli) -> OutputFormat {
    if cli.json {
        OutputFormat::Json
    } else if cli.quality {
        OutputFormat::Quality
    } else if cli.quiet {
        OutputFormat::Score
    } else {
        cli.format
    }
}

fn save_diffmap(diffmap: &ImgVec<f32>, path: &Path) -> Result<(), String> {
    let width = diffmap.width();
    let height = diffmap.height();

    // Convert diffmap to RGB heatmap
    let mut rgb_data = Vec::with_capacity(width * height * 3);

    // Find max value for normalization
    let max_val = diffmap
        .buf()
        .iter()
        .copied()
        .fold(0.0f32, f32::max)
        .max(1.0);

    for y in 0..height {
        for x in 0..width {
            let val = diffmap.buf()[y * width + x];
            let normalized = (val / max_val).clamp(0.0, 1.0);
            let (r, g, b) = heatmap_color(normalized);
            rgb_data.push(r);
            rgb_data.push(g);
            rgb_data.push(b);
        }
    }

    // Save as PNG
    image::save_buffer(
        path,
        &rgb_data,
        width as u32,
        height as u32,
        image::ColorType::Rgb8,
    )
    .map_err(|e| format!("failed to save diffmap: {e}"))
}

/// Convert a value 0-1 to a heatmap color (blue -> cyan -> green -> yellow -> red)
fn heatmap_color(val: f32) -> (u8, u8, u8) {
    let v = val.clamp(0.0, 1.0);

    if v < 0.25 {
        // Blue to Cyan
        let t = v / 0.25;
        (0, (t * 255.0) as u8, 255)
    } else if v < 0.5 {
        // Cyan to Green
        let t = (v - 0.25) / 0.25;
        (0, 255, (255.0 * (1.0 - t)) as u8)
    } else if v < 0.75 {
        // Green to Yellow
        let t = (v - 0.5) / 0.25;
        ((t * 255.0) as u8, 255, 0)
    } else {
        // Yellow to Red
        let t = (v - 0.75) / 0.25;
        (255, (255.0 * (1.0 - t)) as u8, 0)
    }
}

fn quality_rating(score: f64) -> (&'static str, &'static str, colored::Color) {
    use colored::Color;
    if score < 0.5 {
        ("excellent", "Imperceptible difference", Color::Green)
    } else if score < 1.0 {
        ("good", "Barely noticeable difference", Color::Green)
    } else if score < 2.0 {
        ("acceptable", "Noticeable but acceptable", Color::Yellow)
    } else if score < 3.0 {
        ("poor", "Clearly visible difference", Color::Red)
    } else {
        ("bad", "Large, obvious difference", Color::Red)
    }
}

fn output_single_result(
    cli: &Cli,
    result: &ButteraugliResult,
    width: u32,
    height: u32,
) -> Result<(), String> {
    let format = get_format(cli);
    let (rating, description, color) = quality_rating(result.score);

    match format {
        OutputFormat::Score => {
            println!("{:.6}", result.score);
        }
        OutputFormat::Text => {
            let score_str = format!("{:.4}", result.score);
            if let Some(max_score) = cli.max_score {
                if result.score > max_score {
                    println!(
                        "Butteraugli score: {} (exceeds threshold {})",
                        score_str.color(color),
                        max_score
                    );
                } else {
                    println!("Butteraugli score: {}", score_str.color(color));
                }
            } else {
                println!("Butteraugli score: {}", score_str.color(color));
            }
        }
        OutputFormat::Quality => {
            let score_str = format!("{:.4}", result.score);
            let rating_colored = rating.color(color).bold();
            println!(
                "Butteraugli score: {} ({})",
                score_str.color(color),
                rating_colored
            );
            println!("Quality: {}", description);

            if let Some(max_score) = cli.max_score {
                if result.score > max_score {
                    println!(
                        "{}",
                        format!("Threshold exceeded: {:.4} > {}", result.score, max_score)
                            .red()
                            .bold()
                    );
                } else {
                    println!(
                        "{}",
                        format!("Threshold passed: {:.4} <= {}", result.score, max_score).green()
                    );
                }
            }
        }
        OutputFormat::Json => {
            let threshold_exceeded = cli.max_score.map(|max| result.score > max);
            let output = JsonOutput {
                score: result.score,
                quality_rating: rating.to_string(),
                quality_description: description.to_string(),
                reference: cli.reference.display().to_string(),
                distorted: cli.distorted.display().to_string(),
                width,
                height,
                params: JsonParams {
                    intensity_target: cli.intensity_target,
                    hf_asymmetry: cli.hf_asymmetry,
                    xmul: cli.xmul,
                },
                threshold_exceeded,
            };
            let json = serde_json::to_string_pretty(&output)
                .map_err(|e| format!("failed to serialize JSON: {e}"))?;
            println!("{json}");
        }
    }

    Ok(())
}

fn output_batch_results(cli: &Cli, results: &[ComparisonResult]) -> Result<(), String> {
    let format = get_format(cli);

    // Collect scores for summary
    let mut scores: Vec<f64> = Vec::new();
    let mut passed = 0;
    let mut failed = 0;
    let mut errors = 0;

    for cr in results {
        match &cr.result {
            Ok((result, _, _)) => {
                scores.push(result.score);
                if let Some(max_score) = cli.max_score {
                    if result.score > max_score {
                        failed += 1;
                    } else {
                        passed += 1;
                    }
                } else {
                    passed += 1;
                }
            }
            Err(_) => {
                errors += 1;
            }
        }
    }

    let min_score = scores.iter().copied().fold(f64::INFINITY, f64::min);
    let max_score = scores.iter().copied().fold(f64::NEG_INFINITY, f64::max);
    let mean_score = if scores.is_empty() {
        0.0
    } else {
        scores.iter().sum::<f64>() / scores.len() as f64
    };

    match format {
        OutputFormat::Json => {
            let mut json_results = Vec::new();
            for cr in results {
                match &cr.result {
                    Ok((result, width, height)) => {
                        let (rating, desc, _) = quality_rating(result.score);
                        let threshold_exceeded = cli.max_score.map(|max| result.score > max);
                        json_results.push(JsonOutput {
                            score: result.score,
                            quality_rating: rating.to_string(),
                            quality_description: desc.to_string(),
                            reference: cr.reference.display().to_string(),
                            distorted: cr.distorted.display().to_string(),
                            width: *width,
                            height: *height,
                            params: JsonParams {
                                intensity_target: cli.intensity_target,
                                hf_asymmetry: cli.hf_asymmetry,
                                xmul: cli.xmul,
                            },
                            threshold_exceeded,
                        });
                    }
                    Err(_) => {
                        // Skip errors in JSON output
                    }
                }
            }

            let batch_output = BatchJsonOutput {
                results: json_results,
                summary: BatchSummary {
                    total: results.len(),
                    passed,
                    failed,
                    errors,
                    min_score: if min_score.is_finite() {
                        min_score
                    } else {
                        0.0
                    },
                    max_score: if max_score.is_finite() {
                        max_score
                    } else {
                        0.0
                    },
                    mean_score,
                },
            };

            let json = serde_json::to_string_pretty(&batch_output)
                .map_err(|e| format!("failed to serialize JSON: {e}"))?;
            println!("{json}");
        }
        OutputFormat::Score => {
            for cr in results {
                if let Ok((result, _, _)) = &cr.result {
                    println!("{:.6}", result.score);
                }
            }
        }
        _ => {
            // Text or Quality format
            let name_width = results
                .iter()
                .map(|cr| cr.reference.file_name().unwrap_or_default().len())
                .max()
                .unwrap_or(20);

            for cr in results {
                let filename = cr
                    .reference
                    .file_name()
                    .and_then(|s| s.to_str())
                    .unwrap_or("?");

                match &cr.result {
                    Ok((result, _, _)) => {
                        let (rating, _, color) = quality_rating(result.score);
                        let score_str = format!("{:.4}", result.score);

                        let status = if let Some(max) = cli.max_score {
                            if result.score > max {
                                "FAIL".red().bold()
                            } else {
                                "PASS".green().bold()
                            }
                        } else {
                            rating.color(color).bold()
                        };

                        println!(
                            "{:width$}  {:>8}  {}",
                            filename,
                            score_str.color(color),
                            status,
                            width = name_width
                        );
                    }
                    Err(e) => {
                        println!(
                            "{:width$}  {:>8}  {}",
                            filename,
                            "-".dimmed(),
                            format!("ERROR: {}", e).red(),
                            width = name_width
                        );
                    }
                }
            }

            // Summary
            if cli.summary || results.len() > 1 {
                println!();
                println!("{}", "Summary:".bold());
                println!(
                    "  Total: {}  Passed: {}  Failed: {}  Errors: {}",
                    results.len(),
                    passed.to_string().green(),
                    if failed > 0 {
                        failed.to_string().red()
                    } else {
                        failed.to_string().normal()
                    },
                    if errors > 0 {
                        errors.to_string().red()
                    } else {
                        errors.to_string().normal()
                    }
                );
                if !scores.is_empty() {
                    println!(
                        "  Scores: min={:.4}  max={:.4}  mean={:.4}",
                        min_score, max_score, mean_score
                    );
                }
            }
        }
    }

    // Flush stdout
    let _ = io::stdout().flush();

    Ok(())
}