Skip to content

feat(cli): Rich-based output, prints and debug-inventory - #1902

Open
noirbizarre wants to merge 1 commit into
pyinfra-dev:3.xfrom
noirbizarre:refactor/rich
Open

feat(cli): Rich-based output, prints and debug-inventory#1902
noirbizarre wants to merge 1 commit into
pyinfra-dev:3.xfrom
noirbizarre:refactor/rich

Conversation

@noirbizarre

@noirbizarre noirbizarre commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Part 2 of 3 of the CLI modernisation, split out of #1894. Part 1 (#1901, Click → Cyclopts) has merged into 3.x; this PR is rebased on top of it.

Routes all human-facing CLI output through Rich, replacing Click as the output backend (Click is removed entirely). The core stays decoupled via the pluggable pyinfra.api.output layer, now wired to Rich implementations.

  • New pyinfra_cli.console: shared Rich stderr console (+ stdout console for --json), format_text (click.style-compatible signature) and echo.
  • pyinfra.api.output: add get_console / set_console.
  • main.py wires set_formatter(format_text) / set_echo(echo).
  • prints.py / log.py / util.py / virtualenv.py: Click echo/style → Rich.
  • debug-inventory renders a Rich table with per-host key: value data (nested values as JSON, type-coloured to match the JSON highlighter).
  • Unexpected errors render a Rich traceback (suppressing framework frames); the internal-error path still writes pyinfra-debug.log.
  • --help uses Rich formatting with a colorized usage line and a syntax-highlighted examples epilogue; the confirm prompt uses rich.prompt.
  • The core progress spinner is intentionally left unchanged here (it stays the existing self-coordinating stderr spinner); the Rich progress display is introduced in Part 3 (feat(cli): live hierarchical progress tree and informative error handling #1903) alongside the live tree, where it can be coordinated with streaming log output.
  • pyproject: remove click, add rich; keep cyclopts.

📚 Stacked PR series

This is one of three PRs that split the combined CLI refactor (originally #1894) into reviewable layers. They land in order; Part 3 stays in draft until the preceding part is merged.

Original combined PR (kept for reference/discussion): #1894

Related issue: #785 (use Rich) — referenced, not closed.


🎥 Showcase

Those are here to show what visually changed and what did not (yet).

In this PR, rich has been introduced and is responsible for rendering/styling, however there is no live+tree progress.

Help

help

Inventory

Inventory is now prettified with rich (both listing and json output). When the output is not a tty (eg. piped/redirected to something else), it is then raw json, ANSI styling do not clutter it.

inventory

Success with confirmation

success rich

Error and diff

This one show the benefit of the live+tree output coming in the next PR.

diff

Multiline output

multiline

Route all human-facing CLI output through Rich, replacing Click as the output
backend (Click is now removed entirely). The core stays decoupled via the
pluggable `pyinfra.api.output` layer, now wired to Rich implementations.

- New `pyinfra_cli.console`: shared Rich stderr console (+ stdout console for
  `--json`), `format_text` (click.style-compatible signature) and `echo`.
- `pyinfra.api.output`: add `get_console`/`set_console`.
- `main.py` wires `set_formatter(format_text)` / `set_echo(echo)`.
- `prints.py`/`log.py`/`util.py`/`virtualenv.py`: Click echo/style → Rich.
- `debug-inventory` renders a Rich table with per-host `key: value` data
  (nested values as JSON, type-coloured to match the JSON highlighter).
- Unexpected errors render a Rich traceback (suppressing framework frames);
  the internal-error path still writes `pyinfra-debug.log`.
- `--help` uses Rich formatting with a colourised usage line and a
  syntax-highlighted examples epilogue; the confirm prompt uses `rich.prompt`.
- Core spinner switches to a shared Rich progress instance.
- pyproject: remove `click`, add `rich`; keep `cyclopts`.

This is the output layer only; the live progress tree follows separately.
@noirbizarre
noirbizarre marked this pull request as ready for review August 25, 2026 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API API mode specific issues. CLI CLI mode specific issues. new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants