Skip to content

6. The CLI

cargo install rs-rich-cli      # installs a binary called `rich`

Rendering a file

With no mode flag, the type is detected from the extension:

rich README.md          # markdown
rich data.json          # pretty JSON
rich main.rs            # syntax highlighted
rich table.csv          # a table
rich notebook.ipynb     # a Jupyter notebook

Force a mode when the extension lies, or when reading stdin:

rich --markdown notes.txt
cat main.rs | rich --syntax -

- means standard input.

Printing markup

rich -p "[bold red]Alert[/] disk at [bold]91%[/]"

Bad markup is reported rather than printed literally:

$ rich -p "[/nope]"
rich: markup error: closing tag '[/nope]' at position 0 doesn't match any open tag
$ echo $?
1

Fetching a URL

rich https://raw.githubusercontent.com/Textualize/rich/master/README.md

The render mode comes from the flag, else the URL's extension, else the response Content-Type.

Decorating output

rich --panel rounded --title "Notes" --style "bold blue" notes.md
rich --padding 1,4 --center report.md
rich --width 60 --rule "Section"

Exporting

Both exports write to a path and leave the terminal output intact, so you get both at once:

rich -m README.md --export-html readme.html
rich -m README.md --export-svg readme.svg
rich -m README.md -o readme.html --export-svg readme.svg   # both

-o is short for --export-html. The output is self-contained — no external CSS, fonts or images — which is exactly how every picture on this site is made.

Paging

rich --pager long-document.md

Pages through $PAGER (falling back to less -R, then more), keeping the styling, unlike piping to a pager yourself.

Full option list

rich 0.0.1 — Rust port of the rich-cli terminal toolbox

USAGE:
rich [OPTIONS] [RESOURCE]

RESOURCE is a file path, an http(s) URL, or `-` for stdin.

RENDER MODE (choose at most one; default auto-detects by extension):
-p, --print      Interpret RESOURCE as console markup
-m, --markdown   Render RESOURCE as Markdown
-j, --json       Pretty-print RESOURCE as JSON
-x, --syntax     Syntax-highlight RESOURCE (language from its extension)
--csv        Render RESOURCE as a CSV/TSV table
--ipynb      Render RESOURCE as a Jupyter notebook
--gif        Animate one or more GIFs (several play side by side)
--loop N     With --gif, repeat N times (0 = forever)
--rule       Draw a horizontal rule (RESOURCE is its title)

OPTIONS:
-w, --width N     Set the output width
--left        Left-justify output
--center      Center output
--right       Right-justify output
-o, --export-html PATH
Also write a self-contained HTML document to PATH
--export-svg PATH
Also write a self-contained SVG document to PATH
--panel BOX   Wrap output in a panel (none/ascii/ascii2/square/rounded/heavy/double)
--padding P   Wrap output in padding (1, 2, or 4 comma-separated ints)
--title T     Panel title (with --panel)
--caption T   Panel caption/subtitle (with --panel)
--style S     Panel border style, e.g. "bold red" (with --panel)
--pager       Page the output through the system pager ($PAGER, else less/more)
--no-color    Disable colored output
-h, --help        Show this help
-V, --version     Show the version (mirrors upstream rich-cli)

With no RESOURCE and no mode flag, a capability demo is shown.