rs-rich-cli 0.0.23

The `rich` executable: a Rust port of Python `rich-cli` 1.8.1
Documentation
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
//! `rich diff` for text, ANSI captures, source, patches and merge conflicts
//! (`--conflicts`), and `rich ansi explain`: CLI glue over `rich_ext::diff` and
//! `rich_ext::ansi_explain`.
//!
//! Neither exists upstream (upstream's CLI has no diff; ours compared images
//! only). Both compose public rich-ext renderables, so they are binary-boundary
//! conveniences like `inspect.rs`.
use rich::measure::Measurement;
use rich::text::Text;
use rich::{Console, ConsoleOptions, Renderable, Segment};
use rich_ext::ansi_explain::{explain, Explanation, ExplanationView, ViewMode};
use rich_ext::diff::{git, ConflictLayout, ConflictView, DiffView, Layout, SourceDiff};

/// Options for the text diff and `ansi explain`.
#[derive(Clone, Debug, Default)]
pub(crate) struct ToolOptions {
    side_by_side: bool,
    conflicts: bool,
    context: Option<usize>,
    language: Option<String>,
    ansi_inline: bool,
    escapes_only: bool,
}

impl ToolOptions {
    /// Consume one of these options; anything else stays with the main parser.
    pub(crate) fn parse_option<'a>(
        &mut self,
        arg: &str,
        rest: &mut impl Iterator<Item = &'a String>,
    ) -> Result<bool, String> {
        match arg {
            "--side-by-side" => self.side_by_side = true,
            "--conflicts" => self.conflicts = true,
            "--context" => {
                self.context = Some(
                    rest.next()
                        .and_then(|v| v.parse::<usize>().ok())
                        .ok_or("--context requires a number of lines")?,
                );
            }
            "--language" => {
                self.language = Some(rest.next().ok_or("--language requires a name")?.clone());
            }
            "--ansi-inline" => self.ansi_inline = true,
            "--escapes-only" => self.escapes_only = true,
            _ => return Ok(false),
        }
        Ok(true)
    }

    /// The first text-diff option given, for the "only has an effect" check.
    pub(crate) fn diff_option(&self) -> Option<&'static str> {
        [
            ("--side-by-side", self.side_by_side),
            ("--conflicts", self.conflicts),
            ("--context", self.context.is_some()),
            ("--language", self.language.is_some()),
        ]
        .into_iter()
        .find_map(|(flag, given)| given.then_some(flag))
    }

    /// Whether `--conflicts` was given: one file's merge conflicts.
    pub(crate) fn conflicts(&self) -> bool {
        self.conflicts
    }

    /// The first `ansi explain` option given.
    pub(crate) fn ansi_option(&self) -> Option<&'static str> {
        [
            ("--ansi-inline", self.ansi_inline),
            ("--escapes-only", self.escapes_only),
        ]
        .into_iter()
        .find_map(|(flag, given)| given.then_some(flag))
    }

    fn layout(&self) -> Layout {
        if self.side_by_side {
            Layout::SideBySide
        } else {
            Layout::Unified
        }
    }
}

/// Renderables printed one after another.
struct Stack(Vec<Box<dyn Renderable>>);

impl Renderable for Stack {
    fn rich_render(&self, console: &Console, options: &ConsoleOptions) -> Vec<Segment> {
        let mut out: Vec<Segment> = Vec::new();
        for part in &self.0 {
            // Each part starts on its own line, whether or not the previous
            // one ended with a newline.
            if out.last().is_some_and(|last| !last.text.ends_with('\n')) {
                out.push(Segment::line());
            }
            out.extend(part.rich_render(console, options));
        }
        out
    }

    fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement {
        self.0
            .iter()
            .map(|part| part.measure(console, options))
            .fold(Measurement::new(0, 0), |a, b| {
                Measurement::new(a.minimum.max(b.minimum), a.maximum.max(b.maximum))
            })
    }
}

/// A text diff ready to print, and whether it trips `--threshold`.
pub(crate) struct TextDiffOutcome {
    pub renderable: Box<dyn Renderable>,
    pub failed: bool,
}

fn has_ansi(text: &str) -> bool {
    text.contains('\u{1b}')
}

fn file_name(path: &str) -> &str {
    path.rsplit(['/', '\\']).next().unwrap_or(path)
}

/// `rich diff OLD NEW` for anything that is not an image pair, or
/// `rich diff PATCH` for a unified diff such as `git diff` output.
pub(crate) fn text_diff(
    options: &ToolOptions,
    names: &[String],
    contents: &[String],
    threshold: Option<f32>,
    sanitize: bool,
) -> Result<TextDiffOutcome, String> {
    if sanitize {
        // Names and contents both reach the terminal. ANSI captures keep the
        // styles they are compared by; everything else in them is shown.
        let ansi = contents.len() == 2 && contents.iter().any(|c| has_ansi(c));
        let names: Vec<String> = names
            .iter()
            .map(|n| rich_ext::sanitize_terminal_controls(n))
            .collect();
        let contents: Vec<String> = contents
            .iter()
            .map(|c| {
                if ansi {
                    crate::controls::neutralize_for_decoder(c)
                } else {
                    rich_ext::sanitize_terminal_controls(c)
                }
            })
            .collect();
        return text_diff(options, &names, &contents, threshold, false);
    }
    if options.conflicts {
        let [content] = contents else {
            return Err("--conflicts needs exactly one file: rich diff --conflicts FILE".into());
        };
        return conflicts(options, &names[0], content);
    }
    if let [patch] = contents {
        let parsed = git::parse_unified(patch).map_err(|err| {
            format!(
                "{}: not a unified diff (line {}: {}); give two files to compare",
                names[0], err.line, err.message
            )
        })?;
        if parsed.files.is_empty() {
            return Err(format!(
                "{}: no file changes found in it; give two files to compare",
                names[0]
            ));
        }
        let (added, removed) = parsed.stats();
        let view = git::PatchView::new(parsed).layout(options.layout());
        let summary = Text::from_markup(&format!("[dim]{} added, {} removed[/]", added, removed))
            .map_err(|err| err.to_string())?;
        return Ok(TextDiffOutcome {
            renderable: Box::new(Stack(vec![Box::new(view), Box::new(summary)])),
            failed: false,
        });
    }
    let [old, new] = contents else {
        return Err("--diff needs two files to compare, or one patch".into());
    };
    let (old_name, new_name) = (names[0].as_str(), names[1].as_str());
    let context = options.context.unwrap_or(3);
    let ansi = has_ansi(old) || has_ansi(new);
    let (view, stats): (Box<dyn Renderable>, (usize, usize)) = if ansi {
        let view = DiffView::ansi(old, new)
            .layout(options.layout())
            .context(context)
            .titles(old_name, new_name);
        let (added, removed, restyled) = view.stats();
        (Box::new(view), (added + restyled, removed + restyled))
    } else {
        let mut source = SourceDiff::new(old.as_str(), new.as_str())
            .paths(old_name, new_name)
            .layout(options.layout())
            .context(context);
        if let Some(language) = &options.language {
            source = source.language(language.as_str());
        }
        let (added, removed, _) = source.view().stats();
        (Box::new(source), (added, removed))
    };
    let (added, removed) = stats;
    let total = old.lines().count() + new.lines().count();
    let changed = if total == 0 {
        0.0
    } else {
        (added + removed) as f32 * 100.0 / total as f32
    };
    let mut summary = format!(
        "[dim]{} → {}: {added} added, {removed} removed ({changed:.1}% of lines changed)[/]",
        rich::markup::escape(file_name(old_name)),
        rich::markup::escape(file_name(new_name)),
    );
    let mut failed = false;
    if let Some(limit) = threshold {
        failed = super::diff_threshold_exceeded(changed, limit);
        summary.push('\n');
        summary.push_str(&if failed {
            format!("[bold red]FAIL[/] {changed:.1}% changed, limit {limit:.1}%")
        } else {
            format!("[bold green]OK[/] {changed:.1}% changed, within {limit:.1}%")
        });
    }
    let summary = Text::from_markup(&summary).map_err(|err| err.to_string())?;
    Ok(TextDiffOutcome {
        renderable: Box::new(Stack(vec![view, Box::new(summary)])),
        failed,
    })
}

/// `rich diff --conflicts FILE`: the merge conflicts left in one file,
/// numbered, with ours, base and theirs side by side when the width allows
/// (always with `--side-by-side`), and a summary line. Like every other text
/// diff it succeeds whether or not there are conflicts; markers that do not
/// parse are a data error naming the line.
fn conflicts(options: &ToolOptions, name: &str, content: &str) -> Result<TextDiffOutcome, String> {
    let view = ConflictView::parse(content)
        .map_err(|err| format!("{name}: {err}"))?
        .path(name)
        .layout(if options.side_by_side {
            ConflictLayout::SideBySide
        } else {
            ConflictLayout::Auto
        })
        .context(options.context.unwrap_or(3));
    let view = match &options.language {
        Some(language) => view.language(language.as_str()),
        None => view,
    };
    let count = view.file().conflicts().len();
    let with_base = view
        .file()
        .conflicts()
        .iter()
        .filter(|c| c.base.is_some())
        .count();
    let mut summary = format!(
        "{}: {count} conflict{}",
        rich::markup::escape(file_name(name)),
        if count == 1 { "" } else { "s" }
    );
    if with_base > 0 {
        summary.push_str(&format!(" ({with_base} with a base)"));
    }
    let summary =
        Text::from_markup(&format!("[dim]{summary}[/]")).map_err(|err| err.to_string())?;
    Ok(TextDiffOutcome {
        renderable: Box::new(Stack(vec![Box::new(view), Box::new(summary)])),
        failed: false,
    })
}

/// `ExplanationView` borrows its explanation; this owns it for the CLI.
struct Explained {
    explanation: Explanation,
    inline: bool,
    escapes_only: bool,
}

impl Explained {
    fn view(&self) -> ExplanationView<'_> {
        let mode = if self.inline {
            ViewMode::Inline
        } else {
            ViewMode::Table
        };
        ExplanationView::new(&self.explanation)
            .mode(mode)
            .escapes_only(self.escapes_only)
    }
}

impl Renderable for Explained {
    fn rich_render(&self, console: &Console, options: &ConsoleOptions) -> Vec<Segment> {
        self.view().rich_render(console, options)
    }

    fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement {
        self.view().measure(console, options)
    }
}

/// `rich ansi explain`: every escape sequence in `content`, decoded.
pub(crate) fn ansi_explain(options: &ToolOptions, content: &str) -> Box<dyn Renderable> {
    Box::new(Explained {
        explanation: explain(content),
        inline: options.ansi_inline,
        escapes_only: options.escapes_only,
    })
}

/// Whether `bench` is the first positional, with no render mode selected (as
/// for `rich docs`: `rich -p bench` still prints the word).
fn is_bench(args: &[String]) -> bool {
    super::subcommand_word(args) == Some("bench")
}

/// `rich bench compare BASELINE CANDIDATE [--threshold PCT]`: compare two
/// benchmark runs (`rich_ext::qa::bench` JSON files, or criterion output
/// directories) and exit 5 when any benchmark regressed. `Ok(false)` when the
/// command line is not this command.
pub(crate) fn bench_dispatch(args: &[String]) -> Result<bool, String> {
    use rich_ext::qa::bench::{compare, BenchRun, CompareOptions, ComparisonView};
    if !is_bench(args) {
        return Ok(false);
    }
    // `rich bench --help` and `rich bench compare --help` show that command.
    if args
        .iter()
        .take_while(|arg| *arg != "--")
        .any(|arg| arg == "--help")
    {
        let mut path = vec!["bench"];
        let mut words = args.iter().filter(|arg| !arg.starts_with('-'));
        if words.nth(1).is_some_and(|word| word == "compare") {
            path.push("compare");
        }
        let no_color = super::cli_spec::no_color_requested(args);
        if let Some(help) = super::cli_spec::subcommand_help(&path, no_color) {
            super::authoring::out(&format!("{help}\n"));
        }
        return Ok(true);
    }
    let mut positionals = Vec::new();
    let mut threshold = None;
    let mut width = None;
    let mut no_color = std::env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty());
    let mut iter = args.iter();
    while let Some(arg) = iter.next() {
        match arg.as_str() {
            "--threshold" => {
                threshold = Some(
                    iter.next()
                        .and_then(|v| v.parse::<f64>().ok())
                        .filter(|v| v.is_finite() && *v >= 0.0)
                        .ok_or("--threshold requires a percentage")?,
                );
            }
            "-w" | "--width" => {
                width = Some(
                    iter.next()
                        .and_then(|v| v.parse::<usize>().ok())
                        .filter(|v| *v > 0)
                        .ok_or(format!("{arg} requires a positive number of columns"))?,
                );
            }
            "--no-color" => no_color = true,
            "--color" => no_color = false,
            "--no-config" | "--machine-json" => {}
            "--report" => {
                iter.next();
            }
            other if other.starts_with('-') && other != "-" => {
                return Err(format!(
                    "unknown option {other:?} for rich bench compare; it takes --threshold PCT \
                     and --width N"
                ));
            }
            other => positionals.push(other.to_string()),
        }
    }
    let [_, command, baseline, candidate] = positionals.as_slice() else {
        return Err("usage: rich bench compare BASELINE CANDIDATE [--threshold PCT]".into());
    };
    if command != "compare" {
        return Err(format!("unknown bench command {command:?}; use compare"));
    }
    let load = |path: &str| {
        let file = super::fs_path(path);
        let result = if file.is_dir() {
            BenchRun::from_criterion_dir(&file)
        } else {
            BenchRun::load(&file)
        };
        result.map_err(|err| format!("cannot read benchmark run {path}: {err}"))
    };
    let (mut baseline, mut candidate) = match (load(baseline), load(candidate)) {
        (Ok(a), Ok(b)) => (a, b),
        (Err(err), _) | (_, Err(err)) => {
            let _ = super::emit_error(
                super::wants_json_report(args),
                super::ExitClass::Input,
                &err,
            );
            std::process::exit(i32::from(super::ExitClass::Input.code()));
        }
    };
    let mut options = CompareOptions::default();
    if let Some(threshold) = threshold {
        options.threshold_pct = threshold;
    }
    // Benchmark names come from the files; show any controls in them.
    for run in [&mut baseline, &mut candidate] {
        for measurement in &mut run.measurements {
            measurement.name = rich_ext::sanitize_terminal_controls(&measurement.name);
        }
    }
    let mut comparison = compare(&baseline, &candidate, &options);
    flag_changes_from_zero(&mut comparison, &options);
    let mut console = crate::console_builder().no_color(no_color);
    if let Some(width) = width {
        console = console.width(width);
    }
    console.build().print(&ComparisonView::new(&comparison));
    if comparison.has_regressions() {
        let _ = super::emit_error(
            super::wants_json_report(args),
            super::ExitClass::Gate,
            "benchmark regression",
        );
        std::process::exit(i32::from(super::ExitClass::Gate.code()));
    }
    if super::wants_json_report(args) {
        super::emit_success_report(super::ReportFormat::Json);
    }
    Ok(true)
}

/// `compare` calls any change from a 0 ns baseline 0% and so unchanged, but
/// 0 → 5 ns is a change of unbounded size: show it as `+inf%` (or `-inf%`)
/// and judge it as a regression (or improvement) unless it is within the
/// noise.
fn flag_changes_from_zero(
    comparison: &mut rich_ext::qa::bench::Comparison,
    options: &rich_ext::qa::bench::CompareOptions,
) {
    use rich_ext::qa::bench::{Noise, Statistic, Verdict};
    for row in &mut comparison.rows {
        let (Some(b), Some(c)) = (&row.baseline, &row.candidate) else {
            continue;
        };
        let value = |m: &rich_ext::qa::bench::Measurement| match options.statistic {
            Statistic::Mean => m.mean,
            Statistic::Median => m.median,
        };
        let (bv, cv) = (value(b), value(c));
        if bv > 0.0 || cv == bv {
            continue;
        }
        let noisy = match options.noise {
            Noise::Stddev => (cv - bv).abs() <= (b.stddev.powi(2) + c.stddev.powi(2)).sqrt(),
            Noise::Ignore => false,
        };
        let up = cv > bv;
        row.change_pct = Some(if up { f64::INFINITY } else { f64::NEG_INFINITY });
        if !noisy {
            row.verdict = if up {
                Verdict::Regression
            } else {
                Verdict::Improvement
            };
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn render(renderable: &dyn Renderable) -> String {
        Console::builder()
            .width(80)
            .no_color(true)
            .build()
            .render_export(renderable)
    }

    fn names(items: &[&str]) -> Vec<String> {
        items.iter().map(|s| s.to_string()).collect()
    }

    #[test]
    fn text_diff_reports_changes_and_the_threshold() {
        let contents = names(&["a\nb\nc\n", "a\nB\nc\n"]);
        let outcome = text_diff(
            &ToolOptions::default(),
            &names(&["old.txt", "new.txt"]),
            &contents,
            Some(10.0),
            false,
        )
        .unwrap();
        let out = render(outcome.renderable.as_ref());
        assert!(out.contains("-") && out.contains("B"), "{out}");
        assert!(
            out.contains("1 added, 1 removed (33.3% of lines changed)"),
            "{out}"
        );
        assert!(out.contains("FAIL 33.3% changed, limit 10.0%"), "{out}");
        assert!(outcome.failed);
    }

    #[test]
    fn a_single_patch_renders_its_files() {
        let patch = "diff --git a/x.rs b/x.rs\n--- a/x.rs\n+++ b/x.rs\n@@ -1 +1 @@\n-fn a() {}\n+fn b() {}\n";
        let outcome = text_diff(
            &ToolOptions::default(),
            &names(&["change.patch"]),
            &names(&[patch]),
            None,
            false,
        )
        .unwrap();
        let out = render(outcome.renderable.as_ref());
        assert!(out.contains("x.rs") && out.contains("fn b()"), "{out}");
        assert!(out.contains("1 added, 1 removed"), "{out}");

        let err = text_diff(
            &ToolOptions::default(),
            &names(&["notes.txt"]),
            &names(&["just text\n"]),
            None,
            false,
        )
        .err()
        .unwrap();
        assert!(err.contains("give two files to compare"), "{err}");
    }

    #[test]
    fn ansi_inputs_diff_on_visible_text_and_styles() {
        let old = "\u{1b}[31mred\u{1b}[0m\nsame\n";
        let new = "\u{1b}[32mred\u{1b}[0m\nsame\n";
        // Sanitizing keeps the styles an ANSI capture is compared by.
        for sanitize in [false, true] {
            let outcome = text_diff(
                &ToolOptions::default(),
                &names(&["a.ansi", "b.ansi"]),
                &names(&[old, new]),
                None,
                sanitize,
            )
            .unwrap();
            let out = render(outcome.renderable.as_ref());
            assert!(out.contains('~'), "a style-only change must show:\n{out}");
        }
    }

    #[test]
    fn bench_is_a_command_word_only_in_first_position() {
        let args = |items: &[&str]| names(items);
        assert!(is_bench(&args(&["bench", "compare", "a", "b"])));
        assert!(is_bench(&args(&["--no-color", "bench"])));
        assert!(!is_bench(&args(&["--title", "bench", "file.txt"])));
        assert!(!is_bench(&args(&["-p", "bench"])));
        assert!(!is_bench(&args(&["notes.txt", "bench"])));
    }

    #[test]
    fn ansi_explain_decodes_sgr() {
        let out = render(ansi_explain(&ToolOptions::default(), "\u{1b}[1;31mhi\u{1b}[0m").as_ref());
        assert!(out.contains("bold"), "{out}");
        assert!(out.contains("hi"), "{out}");
    }
}