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
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
//! `rich banner TEXT`: a FIGlet banner, with a font, a style or colour
//! gradient, alignment and width fitting (0.0.19, #708). Not upstream: a
//! binary-boundary convenience (see `docs/PORTING.md`) over rs-rich-art's
//! public FIGlet API (`FigletFont`, `figlet::render`) and rs-rich-ext's
//! `palette::gradient`, behind the CLI's `art` feature.
//!
//! The bundled font is `standard`; `--font` also takes a FIGfont (`.flf`)
//! file, or the name of one in `$FIGLET_FONTDIR` or figlet's own font
//! directories, so an installed figlet's fonts work by name.
use super::*;

use rich::{Color, Segment, Style};
use rich_art::figlet::{self, FigletFont};
use rich_art::Justify as FigletJustify;
use rich_ext::cli_doc::{ArgSpec, CommandSpec};

/// The largest font file read: real FIGfonts are tens of kilobytes.
const FONT_LIMIT: u64 = 4 * 1024 * 1024;

/// The widest `--width`: wide enough for any terminal, small enough that a
/// layout of that many columns stays cheap.
const MAX_WIDTH: usize = 10_000;

/// Named gradients for `--gradient`.
const PRESETS: &[(&str, &[&str])] = &[
    (
        "rainbow",
        &[
            "#ff0000", "#ff8000", "#ffff00", "#00c000", "#0080ff", "#8000ff",
        ],
    ),
    ("sunset", &["#ff5f6d", "#ffc371"]),
    ("ocean", &["#2193b0", "#6dd5ed"]),
    ("forest", &["#134e5e", "#71b280"]),
    ("fire", &["#f12711", "#f5af19"]),
];

/// Whether the command line is `rich banner …` (not `rich -p banner`).
pub(super) fn requested(args: &[String]) -> bool {
    subcommand_word(args) == Some("banner")
}

/// The command's help: registered with the root spec in `cli_spec`.
pub(super) fn command() -> CommandSpec {
    let presets = PRESETS
        .iter()
        .map(|(name, _)| *name)
        .collect::<Vec<_>>()
        .join(", ");
    CommandSpec::new("banner")
        .about(
            "Draw TEXT as a FIGlet banner: a font, a style or colour gradient, alignment and \
             width fitting. Built with the `art` feature",
        )
        .usage("banner [OPTIONS] [TEXT]...")
        .arg(ArgSpec::positional("text").multiple(true).help(
            "The words to draw, joined with spaces; none (or `-`) reads stdin. A newline \
             starts a new banner row",
        ))
        .arg(ArgSpec::option("font").value_name("NAME|FILE").help(
            "standard (bundled, the default), a FIGfont .flf file, or the name of one in \
             $FIGLET_FONTDIR, /usr/share/figlet or /usr/local/share/figlet",
        ))
        .arg(
            ArgSpec::flag("list-fonts").help(
                "List the bundled font and the .flf fonts found in those directories, then exit",
            ),
        )
        .arg(
            ArgSpec::option("style")
                .value_name("STYLE")
                .help("A style for every row, such as `bold cyan` or `white on blue`"),
        )
        .arg(
            ArgSpec::option("gradient")
                .value_name("COLORS")
                .help(format!(
                    "Colour the strokes along a gradient: two or more comma-separated colours \
                     (`#ff0000,blue`), or a preset: {presets}. Overrides --style's colour"
                )),
        )
        .arg(
            ArgSpec::option("direction")
                .value_name("D")
                .help("Which way --gradient runs (default horizontal)")
                .choices(["horizontal", "vertical", "diagonal"]),
        )
        .arg(
            ArgSpec::option("align")
                .value_name("A")
                .help("Position the banner within the width (default left)")
                .choices(["left", "center", "right"]),
        )
        .arg(
            ArgSpec::option("width")
                .short('w')
                .value_name("N")
                .help("Lay the banner out to N columns (default: the terminal's width)"),
        )
        .arg(
            ArgSpec::option("fit")
                .value_name("F")
                .help(
                    "When the banner is wider than the width: wrap it at spaces onto more rows \
                     (the default, as figlet does), print TEXT as plain text instead (plain), or \
                     let it overflow (none)",
                )
                .choices(["wrap", "plain", "none"]),
        )
        .arg(ArgSpec::flag("force-terminal").help(
            "Write colour and styles even when stdout is not a terminal (for a pager or a file)",
        ))
        .example("rich banner Hello", "A banner in the standard font")
        .example(
            "rich banner --gradient rainbow --align center 'Ship it'",
            "Centred, in rainbow colours",
        )
        .example(
            "rich banner --font slant --fit plain Release",
            "An installed figlet font; plain text on a narrow terminal",
        )
}

/// How the banner meets the width.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Fit {
    Wrap,
    Plain,
    None,
}

/// Which way a gradient runs.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Direction {
    Horizontal,
    Vertical,
    Diagonal,
}

#[derive(Debug)]
struct Args {
    help: bool,
    list_fonts: bool,
    words: Vec<String>,
    font: Option<String>,
    style: Option<Style>,
    gradient: Vec<Color>,
    direction: Direction,
    align: FigletJustify,
    width: Option<usize>,
    fit: Fit,
    force_terminal: bool,
}

/// The colours a `--gradient` value names.
fn parse_gradient(value: &str) -> Result<Vec<Color>, String> {
    let lower = value.trim().to_ascii_lowercase();
    if let Some((_, stops)) = PRESETS.iter().find(|(name, _)| *name == lower) {
        return Ok(stops
            .iter()
            .map(|stop| Color::parse(stop).expect("preset colours parse"))
            .collect());
    }
    let stops = value
        .split(',')
        .map(|stop| {
            Color::parse(stop.trim()).map_err(|_| {
                format!(
                    "--gradient: {stop:?} is not a colour (use names, #rrggbb or rgb(r,g,b), or a \
                     preset: {})",
                    PRESETS
                        .iter()
                        .map(|(name, _)| *name)
                        .collect::<Vec<_>>()
                        .join(", ")
                )
            })
        })
        .collect::<Result<Vec<_>, _>>()?;
    if stops.len() < 2 {
        return Err(format!(
            "--gradient needs two or more colours, or a preset; for one colour use --style \
             (got {value:?})"
        ));
    }
    if stops.iter().any(|stop| stop.get_truecolor().is_none()) {
        return Err("--gradient cannot use `default`: it has no RGB value to blend".into());
    }
    Ok(stops)
}

fn parse_args(args: &[String]) -> Result<Args, String> {
    let mut parsed = Args {
        help: false,
        list_fonts: false,
        words: Vec::new(),
        font: None,
        style: None,
        gradient: Vec::new(),
        direction: Direction::Horizontal,
        align: FigletJustify::Left,
        width: None,
        fit: Fit::Wrap,
        force_terminal: false,
    };
    let mut seen_command = false;
    let mut iter = args.iter();
    while let Some(arg) = iter.next() {
        if !arg.starts_with('-') || arg == "-" {
            if !seen_command && arg == "banner" {
                seen_command = true;
            } else {
                parsed.words.push(arg.clone());
            }
            continue;
        }
        let (name, inline) = match arg.split_once('=') {
            Some((name, value)) if name.starts_with("--") => (name, Some(value.to_string())),
            _ => (arg.as_str(), None),
        };
        let mut value = || -> Result<String, String> {
            match inline.clone() {
                Some(value) => Ok(value),
                None => iter
                    .next()
                    .cloned()
                    .ok_or_else(|| format!("{name} needs a value")),
            }
        };
        match name {
            "--" => {
                parsed.words.extend(iter.cloned());
                break;
            }
            "--font" => parsed.font = Some(value()?),
            "--list-fonts" => parsed.list_fonts = true,
            "--style" => {
                let text = value()?;
                parsed.style = Some(
                    Style::parse(&text)
                        .map_err(|error| format!("--style: invalid style {text:?}: {error}"))?,
                );
            }
            "--gradient" => parsed.gradient = parse_gradient(&value()?)?,
            "--direction" => {
                parsed.direction = match value()?.as_str() {
                    "horizontal" => Direction::Horizontal,
                    "vertical" => Direction::Vertical,
                    "diagonal" => Direction::Diagonal,
                    other => {
                        return Err(format!(
                            "--direction takes horizontal, vertical or diagonal, not {other:?}"
                        ))
                    }
                }
            }
            "--align" => {
                parsed.align = match value()?.as_str() {
                    "left" => FigletJustify::Left,
                    "center" | "centre" => FigletJustify::Center,
                    "right" => FigletJustify::Right,
                    other => {
                        return Err(format!(
                            "--align takes left, center or right, not {other:?}"
                        ))
                    }
                }
            }
            "-w" | "--width" => {
                let text = value()?;
                match text.parse::<usize>() {
                    Ok(n) if (1..=MAX_WIDTH).contains(&n) => parsed.width = Some(n),
                    _ => {
                        return Err(format!(
                            "--width takes a column count from 1 to {MAX_WIDTH}, not {text:?}"
                        ))
                    }
                }
            }
            "--fit" => {
                parsed.fit = match value()?.as_str() {
                    "wrap" => Fit::Wrap,
                    "plain" => Fit::Plain,
                    "none" => Fit::None,
                    other => return Err(format!("--fit takes wrap, plain or none, not {other:?}")),
                }
            }
            "--force-terminal" => parsed.force_terminal = true,
            "--help" => parsed.help = true,
            "--no-color" | "--color" | "--no-config" => {}
            "--report" => {
                value()?;
            }
            other => return Err(format!("unknown option {other} for `rich banner`")),
        }
    }
    Ok(parsed)
}

/// Where `--font NAME` is looked for, after the path itself.
fn font_dirs() -> Vec<PathBuf> {
    let mut dirs = Vec::new();
    if let Some(dir) = std::env::var_os("FIGLET_FONTDIR").filter(|dir| !dir.is_empty()) {
        dirs.push(PathBuf::from(dir));
    }
    dirs.push(PathBuf::from("/usr/share/figlet"));
    dirs.push(PathBuf::from("/usr/local/share/figlet"));
    dirs
}

/// The font `--font` names: (class, message) on failure.
fn load_font(name: Option<&str>) -> Result<FigletFont, (ExitClass, String)> {
    let Some(name) = name.filter(|name| *name != "standard") else {
        return Ok(FigletFont::standard());
    };
    let given = fs_path(name);
    let path = if given.is_file() {
        Some(given)
    } else if !name.contains(['/', '\\']) {
        let file = if name.ends_with(".flf") {
            name.to_string()
        } else {
            format!("{name}.flf")
        };
        font_dirs()
            .into_iter()
            .map(|dir| dir.join(&file))
            .find(|path| path.is_file())
    } else {
        None
    };
    let Some(path) = path else {
        return Err((
            ExitClass::Input,
            format!(
                "no font {name:?}: not a file, and no {name}.flf in $FIGLET_FONTDIR, \
                 /usr/share/figlet or /usr/local/share/figlet (try --list-fonts)"
            ),
        ));
    };
    let mut bytes = Vec::new();
    std::fs::File::open(&path)
        .and_then(|file| file.take(FONT_LIMIT + 1).read_to_end(&mut bytes))
        .map_err(|error| {
            (
                ExitClass::Input,
                format!("cannot read font {}: {error}", path.display()),
            )
        })?;
    if bytes.len() as u64 > FONT_LIMIT {
        return Err((
            ExitClass::Input,
            format!("font {} is larger than 4 MiB", path.display()),
        ));
    }
    let source = String::from_utf8_lossy(&bytes);
    FigletFont::parse(&source).map_err(|error| {
        (
            ExitClass::Data,
            format!("cannot use font {}: {error}", path.display()),
        )
    })
}

/// `--list-fonts`: the bundled font, then each directory's `.flf` files.
fn list_fonts() -> String {
    let mut out = String::from("standard (bundled)\n");
    for dir in font_dirs() {
        let Ok(entries) = std::fs::read_dir(&dir) else {
            continue;
        };
        let mut names: Vec<String> = entries
            .flatten()
            .filter_map(|entry| {
                let name = entry.file_name().to_string_lossy().into_owned();
                name.strip_suffix(".flf").map(str::to_string)
            })
            .collect();
        names.sort();
        for name in names {
            out.push_str(&format!("{name} ({})\n", dir.display()));
        }
    }
    out
}

/// The banner's lines, or the text's own lines under `--fit plain`.
fn layout(text: &str, font: &FigletFont, width: usize, args: &Args) -> Vec<String> {
    let lines = |banner: String| -> Vec<String> {
        banner
            .strip_suffix('\n')
            .unwrap_or(&banner)
            .split('\n')
            .map(str::to_string)
            .collect()
    };
    // Laid out with room to spare, it never wraps: the natural banner.
    let natural = lines(figlet::render(
        text,
        font,
        usize::MAX / 4,
        FigletJustify::Left,
    ));
    let natural_width = natural
        .iter()
        .map(|line| rich::cells::cell_len(line))
        .max()
        .unwrap_or(0);
    // FIGlet keeps each row one column short of its width.
    let fits = natural_width < width;
    match args.fit {
        Fit::Plain if !fits => text.lines().map(str::to_string).collect(),
        Fit::None => lines(figlet::render(
            text,
            font,
            width.max(natural_width + 1),
            args.align,
        )),
        _ => lines(figlet::render(text, font, width, args.align)),
    }
}

/// The banner's lines painted: `style` under every column, and the
/// gradient's colour on every stroke.
struct Painted {
    lines: Vec<String>,
    style: Option<Style>,
    gradient: Vec<Color>,
    direction: Direction,
}

impl Painted {
    fn colour_at(&self, row: usize, column: usize, width: usize) -> Color {
        let share = |at: usize, of: usize| {
            if of <= 1 {
                0.0
            } else {
                at as f64 / (of - 1) as f64
            }
        };
        let t = match self.direction {
            Direction::Horizontal => share(column, width),
            Direction::Vertical => share(row, self.lines.len()),
            Direction::Diagonal => (share(column, width) + share(row, self.lines.len())) / 2.0,
        };
        rich_ext::palette::gradient(&self.gradient, t)
    }
}

impl Renderable for Painted {
    fn rich_render(&self, _console: &Console, _options: &rich::ConsoleOptions) -> Vec<Segment> {
        // The gradient spans the drawn strokes, not the trailing blanks.
        let width = self
            .lines
            .iter()
            .map(|line| rich::cells::cell_len(line.trim_end()))
            .max()
            .unwrap_or(0);
        let base = self.style.clone();
        let mut out = Vec::new();
        for (row, line) in self.lines.iter().enumerate() {
            if row > 0 {
                out.push(Segment::line());
            }
            if self.gradient.is_empty() {
                out.push(Segment::new(line.clone(), base.clone()));
                continue;
            }
            let mut column = 0;
            for ch in line.chars() {
                let style = if ch.is_whitespace() {
                    base.clone()
                } else {
                    let colour = Style::new().with_color(self.colour_at(row, column, width));
                    Some(match &base {
                        Some(base) => base.combine(&colour),
                        None => colour,
                    })
                };
                out.push(Segment::new(ch.to_string(), style));
                column += rich::cells::cell_len(ch.encode_utf8(&mut [0; 4]));
            }
        }
        // Merge neighbours that ended up with the same style.
        Segment::simplify(&out)
    }
}

pub(super) fn dispatch(args: &[String]) -> ExitCode {
    let json = wants_json_report(args);
    let no_color = cli_spec::no_color_requested(args);
    let parsed = match parse_args(args) {
        Ok(parsed) => parsed,
        Err(message) => {
            return emit_error(
                json,
                ExitClass::Usage,
                &format!("{message} (try `rich banner --help`)"),
            )
        }
    };
    if parsed.help {
        if let Some(help) = cli_spec::subcommand_help(&["banner"], no_color) {
            authoring::out(&format!("{help}\n"));
        }
        return ExitCode::SUCCESS;
    }
    if parsed.list_fonts {
        authoring::out(&list_fonts());
        return ExitCode::SUCCESS;
    }
    let font = match load_font(parsed.font.as_deref()) {
        Ok(font) => font,
        Err((class, message)) => return emit_error(json, class, &message),
    };
    let text = if parsed.words.is_empty() || parsed.words == ["-"] {
        let mut input = String::new();
        if let Err(error) = std::io::stdin().read_to_string(&mut input) {
            return emit_error(
                json,
                ExitClass::Input,
                &format!("cannot read stdin: {error}"),
            );
        }
        input.trim_end_matches(['\n', '\r']).replace("\r\n", "\n")
    } else {
        parsed.words.join(" ")
    };
    if text.trim().is_empty() {
        return emit_error(
            json,
            ExitClass::Usage,
            "`rich banner` needs TEXT to draw (try `rich banner --help`)",
        );
    }
    // A control character would be drawn as nothing and could run as one.
    let text: String = text
        .chars()
        .filter(|ch| *ch == '\n' || !ch.is_control())
        .collect();
    let builder = || {
        let builder = crate::console_builder().no_color(no_color);
        let builder = if parsed.force_terminal {
            builder.force_terminal(true)
        } else {
            builder
        };
        match parsed.width {
            Some(width) => builder.width(width),
            None => builder,
        }
    };
    let width = builder().build().width();
    let lines = layout(&text, &font, width, &parsed);
    let painted = Painted {
        lines,
        style: parsed.style.clone(),
        gradient: parsed.gradient.clone(),
        direction: parsed.direction,
    };
    // Never let the console wrap a row: `--fit none` may be wider.
    let widest = painted
        .lines
        .iter()
        .map(|line| rich::cells::cell_len(line))
        .max()
        .unwrap_or(0);
    let console = builder().width(width.max(widest)).build();
    authoring::out(&format!("{}\n", console.render_to_string(&painted)));
    ExitCode::SUCCESS
}

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

    fn args(words: &[&str]) -> Vec<String> {
        words.iter().map(|word| word.to_string()).collect()
    }

    #[test]
    fn parses_every_option() {
        let parsed = parse_args(&args(&[
            "banner",
            "--font",
            "standard",
            "--style=bold",
            "--gradient",
            "red,#0000ff",
            "--direction",
            "vertical",
            "--align",
            "center",
            "-w",
            "40",
            "--fit",
            "plain",
            "Hello",
            "world",
        ]))
        .unwrap();
        assert_eq!(parsed.words, ["Hello", "world"]);
        assert_eq!(parsed.gradient.len(), 2);
        assert_eq!(parsed.direction, Direction::Vertical);
        assert_eq!(parsed.align, FigletJustify::Center);
        assert_eq!(parsed.width, Some(40));
        assert_eq!(parsed.fit, Fit::Plain);
        assert!(parsed.style.is_some());
    }

    #[test]
    fn rejects_bad_values() {
        for bad in [
            &["banner", "--gradient", "red"][..],
            &["banner", "--gradient", "red,notacolour"],
            &["banner", "--gradient", "default,red"],
            &["banner", "--align", "middle"],
            &["banner", "--fit", "squash"],
            &["banner", "--width", "0"],
            &["banner", "--direction", "up"],
            &["banner", "--style", "not a style"],
            &["banner", "--bogus"],
            &["banner", "--font"],
        ] {
            assert!(parse_args(&args(bad)).is_err(), "{bad:?}");
        }
    }

    #[test]
    fn presets_resolve() {
        for (name, stops) in PRESETS {
            assert_eq!(parse_gradient(name).unwrap().len(), stops.len());
        }
        assert_eq!(parse_gradient("RAINBOW").unwrap().len(), 6);
    }

    #[test]
    fn fit_modes_lay_out_differently() {
        let font = FigletFont::standard();
        let mut parsed = parse_args(&args(&["banner"])).unwrap();
        let wrapped = layout("Hello world", &font, 30, &parsed);
        assert_eq!(wrapped.len(), 12, "two banner rows: {wrapped:#?}");
        parsed.fit = Fit::Plain;
        assert_eq!(layout("Hello world", &font, 30, &parsed), ["Hello world"]);
        // Plain only when it does not fit.
        assert_eq!(layout("Hi", &font, 30, &parsed).len(), 6);
        parsed.fit = Fit::None;
        let wide = layout("Hello world", &font, 30, &parsed);
        assert_eq!(wide.len(), 6);
        assert!(wide.iter().any(|line| line.len() > 30));
    }

    #[test]
    fn gradients_run_each_way() {
        let painted = |direction| Painted {
            lines: vec!["ab".into(), "cd".into()],
            style: None,
            gradient: parse_gradient("#000000,#ffffff").unwrap(),
            direction,
        };
        let hex = |c: Color| c.get_truecolor().unwrap().hex();
        let horizontal = painted(Direction::Horizontal);
        assert_eq!(hex(horizontal.colour_at(0, 0, 2)), "#000000");
        assert_eq!(hex(horizontal.colour_at(1, 1, 2)), "#ffffff");
        let vertical = painted(Direction::Vertical);
        assert_eq!(hex(vertical.colour_at(0, 1, 2)), "#000000");
        assert_eq!(hex(vertical.colour_at(1, 0, 2)), "#ffffff");
        let diagonal = painted(Direction::Diagonal);
        assert_eq!(hex(diagonal.colour_at(0, 1, 2)), "#808080");
    }
}