How textualize/rich works
Purpose and Entry Points
Rich is a Python 3.8+ library for styled, structured terminal output: text, tables, progress, markdown, syntax, logging, and tracebacks. Primary public entry points are rich.print / rich.inspect in rich/__init__.py, the Console API in rich/console.py, and the diagnostic demo launched by python -m rich through rich/__main__.py.
Rendering Core
Console is the orchestration boundary between application objects and terminal/Jupyter output. It accepts strings and arbitrary renderables, applies console options such as width/color policy, and writes terminal control sequences or exported output. The renderable protocol in rich/protocol.py lets user-defined and built-in objects participate without depending directly on Console; output is normalized into styled segments in rich/segment.py.
Text, Style, and Terminal Encoding
The text pipeline represents content and spans separately from presentation (rich/text.py), parses style definitions and themes (rich/style.py, rich/theme.py), and translates colors/control operations to ANSI-compatible terminal output (rich/color.py, rich/control.py, rich/ansi.py). Terminal-width calculation and wrapping account for Unicode character data (rich/cells.py, rich/_unicode_data/), which is essential for alignment and emoji-safe layout.
Layout and Built-in Renderables
Composable renderables implement measurement plus rendering against available console width. Structural/layout primitives include tables, panels, columns, trees, and layouts (rich/table.py, rich/panel.py, rich/layout.py); these may nest because table cells and labels accept renderables. Input-oriented renderers transform source representations into those primitives: markup (rich/markup.py), Markdown (rich/markdown.py), JSON (rich/json.py), pretty Python objects (rich/pretty.py), and syntax-highlighted code (rich/syntax.py).
Live and Long-Running Output
Dynamic terminal updates are coordinated by Live, which reserves/redraws a render region while preserving normal console writes (rich/live.py, rich/live_render.py). Progress bars, spinners, and status displays build on that refresh mechanism (rich/progress.py, rich/spinner.py, rich/status.py). This is a stateful control-flow boundary: task updates mutate renderable state, then live refresh emits an updated frame rather than append-only output.
Diagnostics and Integration Adapters
Rich exposes application-facing integrations rather than a server or web runtime: Python logging formatting (rich/logging.py), enhanced exception rendering/installable tracebacks (rich/traceback.py), and notebook display support (rich/jupyter.py). Syntax highlighting delegates to the external Pygments library. Platform-specific terminal handling isolates Windows console and renderer behavior in rich/_windows.py, rich/_win32_console.py, and rich/_windows_renderer.py; Linux/macOS and modern Windows terminals use ANSI-capable paths.
Packaging, Documentation, and Quality Boundaries
The distributable package is defined by pyproject.toml and setup.py; code is a pure Python library, published for pip installation rather than deployed infrastructure. Sphinx documentation lives under docs/source/, while examples under examples/ serve as executable usage patterns. CI is GitHub Actions (.github/workflows/pythonpackage.yml), and performance regressions are tracked through ASV configuration and benchmarks (asv.conf.json, benchmarks/benchmarks.py).
Main entry points: console.py Console · protocol.py Renderable protocol · segment.py Styled segments · markdown.py Input renderers
Components 21
Public API
- print / inspect
rich/__init__.py - Diagnostic demo
rich/__main__.py
Rendering Core
- Console · Applies output options and writes terminal, notebook, or exported output.
rich/console.py - Renderable protocol · Allows renderables to participate without directly depending on Console.
rich/protocol.py - Styled segments
rich/segment.py - Text and styles · Keeps text spans separate from presentation and theme definitions.
rich/text.py - ANSI terminal encoding · Converts colors and control operations into terminal-compatible sequences.
rich/ansi.py - Unicode width and wrapping · Provides emoji-safe cell measurement for alignment and wrapping.
rich/cells.py
Built-in Renderables
- Layout primitives · Tables, panels, columns, trees, and layouts measure and render nested content.
rich/table.py - Input renderers · Markup, Markdown, JSON, pretty objects, and syntax become renderables.
rich/markdown.py - Pygments
Live Output
- Live refresh controller · Reserves and redraws a render region while preserving ordinary console writes.
rich/live.py - Live frame renderer
rich/live_render.py - Progress, spinner, status · Task updates mutate display state before each refreshed frame.
rich/progress.py
Integrations & Distribution
- Logging formatter
rich/logging.py - Enhanced tracebacks · Provides renderable exception output and installable traceback handling.
rich/traceback.py - Notebook display
rich/jupyter.py - Windows terminal backend · Isolates legacy Windows console and renderer behavior.
rich/_windows.py - Python package · Pure Python library distributed through pip.
pyproject.toml - Documentation and examples · Sphinx docs and executable examples describe library usage.
docs/source/index.rst - CI and benchmarks · GitHub Actions verifies the package; ASV tracks performance regressions.
.github/workflows/pythonpackage.yml
Connections 22
- print / inspect → Console: creates / uses
- Diagnostic demo → Console: demonstrates
- Console → Renderable protocol: renders through
- Renderable protocol → Styled segments: normalizes to
- Console → Styled segments: consumes
- Text and styles → Styled segments: styles
- Unicode width and wrapping → Console: supports measurement
- Styled segments → ANSI terminal encoding: encodes as
- ANSI terminal encoding → Windows terminal backend: uses on legacy Windows
- Layout primitives → Renderable protocol: implements
- Input renderers → Renderable protocol: implements
- Input renderers → Layout primitives: composes
- Input renderers → Pygments: delegates highlighting
- Live refresh controller → Console: coordinates writes
- Live refresh controller → Live frame renderer: refreshes
- Progress, spinner, status → Live refresh controller: updates through
- Logging formatter → Console: formats with
- Enhanced tracebacks → Console: renders to
- Notebook display → Console: displays output from
- Python package → print / inspect: distributes
- Documentation and examples → print / inspect: documents
- CI and benchmarks → Python package: validates