API Reference¶
CLI entry point¶
rich_gradient_cli ¶
rich-gradient-cli: render text, files, URLs, and stdin with rich-gradient.
The package exposes a single Typer application, :data:app, installed as the
gradient console script.
app
module-attribute
¶
app = Typer(add_completion=False, rich_markup_mode='rich', no_args_is_help=False, context_settings={'help_option_names': ['--help']}, help='Render text, files, URLs, or stdin with a [b]rich-gradient[/b] gradient.\n\nRESOURCE is a file path, an http(s) URL, [b]-[/b] for stdin, or literal text. The renderable is auto-detected from the file extension unless a mode flag such as [b]--markdown[/b] is given.', epilog='https://maxludden.github.io/rich-gradient-cli')
entrypoint ¶
Console-script entry point for gradient.
Runs the app, then sends the opt-in telemetry event only when the command
completed successfully (not for --help, --telemetry or usage errors).
Command¶
rich_gradient_cli.cli ¶
The gradient command: one command, modeled on Textualize/rich-cli.
app
module-attribute
¶
app = Typer(add_completion=False, rich_markup_mode='rich', no_args_is_help=False, context_settings={'help_option_names': ['--help']}, help='Render text, files, URLs, or stdin with a [b]rich-gradient[/b] gradient.\n\nRESOURCE is a file path, an http(s) URL, [b]-[/b] for stdin, or literal text. The renderable is auto-detected from the file extension unless a mode flag such as [b]--markdown[/b] is given.', epilog='https://maxludden.github.io/rich-gradient-cli')
Box ¶
Bases: str, Enum
Panel border styles (keys of :data:rich_gradient_cli.options.BOXES).
SpectrumOutput ¶
TelemetryAction ¶
VerticalAlign ¶
main ¶
main(resource: Annotated[Optional[str], Argument(metavar="[PATH,TEXT,URL, or '-']", show_default=False, help="File, URL, '-' for stdin, or literal text.")] = None, print_: Annotated[bool, Option(--print, -p, help='Print console markup as gradient text.', rich_help_panel=RENDERABLE)] = False, rule: Annotated[bool, Option(--rule, -u, help='Display a horizontal rule titled RESOURCE.', rich_help_panel=RENDERABLE)] = False, json_: Annotated[bool, Option(--json, -J, help='Display as JSON.', rich_help_panel=RENDERABLE)] = False, markdown: Annotated[bool, Option(--markdown, -m, help='Display as Markdown.', rich_help_panel=RENDERABLE)] = False, rst: Annotated[bool, Option(--rst, help='Display reStructuredText.', rich_help_panel=RENDERABLE)] = False, csv_: Annotated[bool, Option(--csv, help='Display CSV/TSV as a table.', rich_help_panel=RENDERABLE)] = False, ipynb: Annotated[bool, Option(--ipynb, help='Display a Jupyter notebook.', rich_help_panel=RENDERABLE)] = False, syntax: Annotated[bool, Option(--syntax, help='Syntax highlighting.', rich_help_panel=RENDERABLE)] = False, inspect: Annotated[bool, Option(--inspect, help="Inspect a Python object, e.g. 'str' or 'rich.console.Console'. Imports/evals RESOURCE: never pass untrusted text.", rich_help_panel=RENDERABLE)] = False, tree: Annotated[bool, Option(--tree, help='Display a directory (or lines of text) as a tree.', rich_help_panel=RENDERABLE)] = False, columns: Annotated[bool, Option(--columns, help='Lay out each line as a column cell.', rich_help_panel=RENDERABLE)] = False, spectrum: Annotated[bool, Option(--spectrum, help='Show the rich-gradient color spectrum.', rich_help_panel=RENDERABLE)] = False, colors: Annotated[Optional[str], Option(--colors, envvar=GRADIENT_COLORS, metavar=COLORS, help='Comma-separated gradient colors (names, hex, CSS names).', rich_help_panel=GRADIENT)] = None, bgcolors: Annotated[Optional[str], Option(--bgcolors, metavar=COLORS, help='Comma-separated background colors.', rich_help_panel=GRADIENT)] = None, rainbow: Annotated[bool, Option(--rainbow, help='Use the rainbow spectrum (ignores --colors).', rich_help_panel=GRADIENT)] = False, hues: Annotated[int | None, Option(--hues, min=2, max=17, help='Number of spectrum hues for --rainbow / no colors (default 5), or for --spectrum (default all 17).', rich_help_panel=GRADIENT)] = None, repeat_scale: Annotated[Optional[float], Option(--repeat - scale, min=0.01, help='How many times the gradient repeats across the output.', rich_help_panel=GRADIENT)] = None, highlight_word: Annotated[Optional[list[str]], Option(--highlight - word, metavar='WORD=STYLE', help='Style a word (repeatable).', rich_help_panel=GRADIENT)] = None, highlight_regex: Annotated[Optional[list[str]], Option(--highlight - regex, metavar='PATTERN=STYLE', help='Style regex matches (repeatable).', rich_help_panel=GRADIENT)] = None, animate: Annotated[bool, Option(--animate, help='Animate the gradient (interactive terminals only).', rich_help_panel=GRADIENT)] = False, duration: Annotated[Optional[float], Option(--duration, min=0.01, help='Stop animating after this many seconds.', rich_help_panel=GRADIENT)] = None, left: Annotated[bool, Option(--left, -l, help='Align block to the left.', rich_help_panel=LAYOUT)] = False, right: Annotated[bool, Option(--right, -r, help='Align block to the right.', rich_help_panel=LAYOUT)] = False, center: Annotated[bool, Option(--center, -c, help='Align block to the center.', rich_help_panel=LAYOUT)] = False, text_left: Annotated[bool, Option(--text - left, -L, help='Justify text to the left.', rich_help_panel=LAYOUT)] = False, text_right: Annotated[bool, Option(--text - right, -R, help='Justify text to the right.', rich_help_panel=LAYOUT)] = False, text_center: Annotated[bool, Option(--text - center, -C, help='Justify text to the center.', rich_help_panel=LAYOUT)] = False, text_full: Annotated[bool, Option(--text - full, -F, help='Justify text to both edges.', rich_help_panel=LAYOUT)] = False, vertical_align: Annotated[VerticalAlign, Option(--vertical - align, help='Vertically align the block.', rich_help_panel=LAYOUT)] = VerticalAlign.MIDDLE, width: Annotated[Optional[int], Option(--width, -w, min=1, metavar=SIZE, help='Fit output to SIZE characters.', rich_help_panel=LAYOUT)] = None, max_width: Annotated[Optional[int], Option(--max - width, -W, min=1, metavar=SIZE, help='Set maximum width to SIZE characters.', rich_help_panel=LAYOUT)] = None, expand: Annotated[Optional[bool], Option(--expand / --no - expand, -e, help='Expand to full width (default: auto).', rich_help_panel=LAYOUT)] = None, padding: Annotated[Optional[str], Option(--padding, -d, metavar=(TOP, RIGHT, BOTTOM, LEFT), help='Padding around output: 1, 2 or 4 comma-separated integers.', rich_help_panel=LAYOUT)] = None, panel: Annotated[Optional[Box], Option(--panel, -a, metavar=BOX, help='Wrap the output in a gradient panel: ascii, ascii2, square, rounded, heavy, double, none.', rich_help_panel=LAYOUT)] = None, panel_style: Annotated[str, Option(--panel - style, -S, metavar=STYLE, help='Panel border style.', rich_help_panel=LAYOUT)] = '', border_only: Annotated[bool, Option(--border - only, help='With --panel, paint only the border with the gradient; the content keeps its own colors.', rich_help_panel=LAYOUT)] = False, title: Annotated[Optional[str], Option(--title, metavar=TEXT, help='Panel (or table) title.', rich_help_panel=LAYOUT)] = None, caption: Annotated[Optional[str], Option(--caption, metavar=TEXT, help='Panel (or table) caption.', rich_help_panel=LAYOUT)] = None, style: Annotated[str, Option(--style, -s, metavar=STYLE, help='Rich style applied to the text.', rich_help_panel=CONTENT)] = '', markup: Annotated[bool, Option(--markup / --no - markup, help='Parse Rich console markup in text.', rich_help_panel=CONTENT)] = True, emoji: Annotated[bool, Option(--emoji, -j, help='Enable emoji codes such as :sparkles:.', rich_help_panel=CONTENT)] = False, hyperlinks: Annotated[bool, Option(--hyperlinks, -y, help='Render hyperlinks in Markdown.', rich_help_panel=CONTENT)] = False, no_wrap: Annotated[bool, Option(--no - wrap, help="Don't word wrap text or code.", rich_help_panel=CONTENT)] = False, tab_size: Annotated[int, Option(--tab - size, min=1, help='Tab width.', rich_help_panel=CONTENT)] = 4, head: Annotated[Optional[int], Option(--head, -h, min=1, metavar=LINES, help='Display the first LINES (--syntax, --csv, --ipynb; ignored elsewhere).', rich_help_panel=CONTENT)] = None, tail: Annotated[Optional[int], Option(--tail, -t, min=1, metavar=LINES, help='Display the last LINES (--syntax, --csv, --ipynb; ignored elsewhere).', rich_help_panel=CONTENT)] = None, lexer: Annotated[Optional[str], Option(--lexer, -x, metavar=LEXER, help='Pygments lexer for --syntax.', rich_help_panel=CONTENT)] = None, theme: Annotated[str, Option(--theme, envvar=RICH_THEME, metavar=THEME, help='Pygments theme for code.', rich_help_panel=CONTENT)] = 'monokai', line_numbers: Annotated[bool, Option(--line - numbers, -n, help='Show line numbers.', rich_help_panel=CONTENT)] = False, guides: Annotated[bool, Option(--guides, -g, help='Show indentation guides.', rich_help_panel=CONTENT)] = False, rule_style: Annotated[str, Option(--rule - style, metavar=STYLE, help='Style for --rule.', rich_help_panel=CONTENT)] = '', rule_char: Annotated[Optional[str], Option(--rule - char, metavar=CHARACTER, help='Single character for --rule (overrides --thickness).', rich_help_panel=CONTENT)] = None, thickness: Annotated[int, Option(--thickness, min=0, max=3, help='Rule thickness, 0-3.', rich_help_panel=CONTENT)] = 2, delimiter: Annotated[Optional[str], Option(--delimiter, metavar=CHAR, help='CSV delimiter (default: auto-detect).', rich_help_panel=CONTENT)] = None, no_header: Annotated[bool, Option(--no - header, help='Treat the first CSV row as data.', rich_help_panel=CONTENT)] = False, max_depth: Annotated[int, Option(--max - depth, min=1, help='Directory depth for --tree.', rich_help_panel=CONTENT)] = 2, spectrum_output_: Annotated[SpectrumOutput, Option(--spectrum - output, help='Format for --spectrum.', rich_help_panel=CONTENT)] = SpectrumOutput.TABLE, export_html: Annotated[Optional[str], Option(--export - html, -o, metavar=PATH, help='Write HTML to PATH.', rich_help_panel=OUTPUT)] = None, export_svg: Annotated[Optional[str], Option(--export - svg, metavar=PATH, help='Write SVG to PATH.', rich_help_panel=OUTPUT)] = None, pager: Annotated[bool, Option(--pager, help='Display in an interactive pager.', rich_help_panel=OUTPUT)] = False, force_terminal: Annotated[bool, Option(--force - terminal, help='Keep ANSI codes when not writing to a terminal.', rich_help_panel=OUTPUT)] = False, telemetry: Annotated[Optional[TelemetryAction], Option(--telemetry, callback=_telemetry_callback, is_eager=True, metavar=[enable | disable | status], help='Manage opt-in anonymous usage telemetry, then exit.', rich_help_panel=OUTPUT)] = None, version_: Annotated[bool, Option(--version, -v, callback=_version_callback, is_eager=True, help='Print version and exit.', rich_help_panel=OUTPUT)] = False) -> None
Render text, files, URLs, or stdin with a [b]rich-gradient[/b] gradient.
RESOURCE is a file path, an http(s) URL, [b]-[/b] for stdin, or literal text. The renderable is auto-detected from the file extension unless a flag such as [b]--markdown[/b] is given.
Rendering¶
rich_gradient_cli.render ¶
Build the rich-gradient renderable for a resource.
DEFAULT_HUES
module-attribute
¶
Hues used by --rainbow / no --colors when --hues isn't given.
SPECTRUM_HUES
module-attribute
¶
Hues shown by --spectrum when --hues isn't given: the full palette.
__all__
module-attribute
¶
__all__ = ['RenderConfig', 'Runnable', 'build_inner', 'build_output', 'build_rule', 'resolve_expand', 'slice_lines', 'spectrum_output']
RenderConfig
dataclass
¶
RenderConfig(colors: str | None = None, bgcolors: str | None = None, rainbow: bool = False, hues: int | None = None, repeat_scale: float | None = None, highlight_words: list[str] = list(), highlight_regex: list[str] = list(), animate: bool = False, duration: float | None = None, align: str = 'left', text_align: str = 'default', vertical_align: str = 'middle', expand: bool | None = None, padding: str | None = None, panel: bool = False, box: str = 'rounded', panel_style: str = '', border_only: bool = False, title: str | None = None, caption: str | None = None, style: str = '', markup: bool = True, emoji: bool = False, hyperlinks: bool = False, no_wrap: bool = False, tab_size: int = 4, head: int | None = None, tail: int | None = None, lexer: str | None = None, theme: str = 'monokai', line_numbers: bool = False, guides: bool = False, rule_char: str | None = None, thickness: int = 2, delimiter: str | None = None, no_header: bool = False, max_depth: int = 2, rule_style: str = '', spectrum_output: str = 'table')
Runnable ¶
Bases: Protocol
An animated rich-gradient object that can be run to completion.
build_inner ¶
Build the plain Rich renderable for mode (before gradient styling).
build_output ¶
build_output(mode: Mode, resource: Resource, cfg: RenderConfig, console: Console, *, animated: bool) -> RenderableType | Runnable
Build the final gradient renderable (or an animation to run()).
The plain renderable for mode is padded, then wrapped in a gradient
Panel (--panel) or a plain Gradient.
build_rule ¶
Build a gradient rule whose title is the resource text.
panel_border_color ¶
Return whether --panel-style sets a foreground color for the border.
rich-gradient's Panel replaces a border foreground color with the
gradient, so a colored --panel-style needs a plain Rich panel instead.
resolve_expand ¶
Decide whether the gradient spans the full width.
Panels, Markdown and code fill the terminal; text, tables, JSON, trees and columns shrink to their content so the gradient is visible across it.
resolve_object ¶
Resolve a dotted module.attr path, falling back to eval.
Dotted paths are imported without eval; any other expression is
evaluated, exactly like rich --inspect.
Warning
Importing a dotted path executes that module, and any other expression
is passed to eval. Never pass untrusted text to --inspect.
Raises:
| Type | Description |
|---|---|
ResourceError
|
If the expression cannot be resolved. |
slice_lines ¶
Keep only the first head or last tail lines of text.
spectrum_output ¶
Render the palette (all 17 hues unless --hues is given) as a table, or as plain names/hex/csv text.
Resources¶
rich_gradient_cli.resource ¶
Locate and read the RESOURCE argument (file, URL, stdin, or literal text).
EXTENSION_MODES
module-attribute
¶
EXTENSION_MODES: dict[str, Mode] = {'.md': MARKDOWN, '.markdown': MARKDOWN, '.json': JSON, '.csv': CSV, '.tsv': CSV, '.rst': RST, '.ipynb': IPYNB}
__all__
module-attribute
¶
__all__ = ['EXTENSION_MODES', 'Mode', 'Resource', 'ResourceError', 'detect_mode', 'is_url', 'read_resource']
Mode ¶
Bases: str, Enum
The renderable used to display a resource.
Resource
dataclass
¶
Content read from a RESOURCE argument.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
The decoded content (or literal text). |
name |
str
|
A file name, URL path, or |
is_literal |
bool
|
|
path |
Path | None
|
The local path when the resource is an existing file or directory. |
ResourceError ¶
Bases: Exception
Raised when a resource cannot be read or rendered (exit code 1).
detect_mode ¶
Choose a mode from the resource's extension, defaulting to plain text.
read_resource ¶
Resolve a RESOURCE argument into content.
- (or None with piped stdin) reads stdin, http(s):// URLs are
downloaded, existing files are read as UTF-8, and anything else is treated
as literal text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource
|
str | None
|
The positional argument, or |
required |
allow_directory
|
bool
|
Return an empty :class: |
False
|
Raises:
| Type | Description |
|---|---|
ResourceError
|
On unreadable files, directories, bad UTF-8, or HTTP failures. |
Option parsing¶
rich_gradient_cli.options ¶
Parsers that turn raw command-line strings into typed values.
BOXES
module-attribute
¶
BOXES: dict[str, Box] = {'ascii': ASCII, 'ascii2': ASCII2, 'square': SQUARE, 'rounded': ROUNDED, 'heavy': HEAVY, 'double': DOUBLE}
__all__
module-attribute
¶
__all__ = ['BOXES', 'parse_colors', 'parse_mapping', 'parse_padding', 'parse_style', 'validate_colors', 'validate_highlights']
parse_colors ¶
Split a comma-separated color list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colors
|
str | None
|
Raw |
required |
Returns:
| Type | Description |
|---|---|
list[str] | None
|
The stripped, non-empty tokens, or |
parse_mapping ¶
Parse repeated KEY=VALUE options into a mapping.
Raises:
| Type | Description |
|---|---|
ValueError
|
If an item is not of the form |
parse_padding ¶
Parse CSS-style padding such as "1", "1,2" or "1,2,3,4".
Raises:
| Type | Description |
|---|---|
ValueError
|
If the string is not 1, 2 or 4 comma-separated integers. |
parse_style ¶
Parse a Rich style string, returning a null style when empty.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
validate_colors ¶
Split a comma-separated color list and check every color parses.
Raises:
| Type | Description |
|---|---|
ValueError
|
If any token is not a Rich color, hex code, or CSS name. |
validate_highlights ¶
validate_highlights(values: Sequence[str] | None, *, regex: bool = False) -> Mapping[str, str] | None
Parse KEY=STYLE highlights, checking each style (and pattern if regex).
Raises:
| Type | Description |
|---|---|
ValueError
|
On a malformed item, an invalid style, or an invalid regex. |
Export¶
rich_gradient_cli.export ¶
Export renderables to SVG or HTML files.
export_renderable ¶
export_renderable(renderable: RenderableType, *, svg: str | None = None, html: str | None = None, width: int | None = None) -> None
Render renderable to a recording console and save SVG and/or HTML.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
renderable
|
RenderableType
|
What to render. |
required |
svg
|
str | None
|
Destination SVG path, if any. |
None
|
html
|
str | None
|
Destination HTML path, if any. |
None
|
width
|
int | None
|
Console width in cells; defaults to Rich's detected width. |
None
|
Raises:
| Type | Description |
|---|---|
OSError
|
If a destination cannot be written. |
Telemetry¶
rich_gradient_cli.telemetry ¶
Opt-in, anonymous usage telemetry for the gradient CLI.
Nothing is sent unless the user runs gradient telemetry enable and an
AMPLITUDE_API_KEY is present in the environment. The only data sent is the
event name Command Run, the render mode, and a random install ID that
is generated locally on enable and never derived from the machine or user.
config_path ¶
Return the path of the telemetry config file (honors XDG_CONFIG_HOME).
run_telemetry_action ¶
Apply enable, disable or status and return the message to show.
track_command ¶
Send a Command Run event if opted in and a key is configured.
The process is short-lived, so the event is flushed and awaited before returning. Telemetry must never break the CLI: all failures are swallowed.