# textualize/rich architecture

> Architecture diagram of the GitHub repository textualize/rich, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/textualize/rich
- Repository: https://github.com/textualize/rich

## Overview

#### 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: [`rich/console.py`](https://github.com/textualize/rich/blob/main/rich/console.py) (Console), [`rich/protocol.py`](https://github.com/textualize/rich/blob/main/rich/protocol.py) (Renderable protocol), [`rich/segment.py`](https://github.com/textualize/rich/blob/main/rich/segment.py) (Styled segments), [`rich/markdown.py`](https://github.com/textualize/rich/blob/main/rich/markdown.py) (Input renderers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Public API"]
  node_public_helpers["print / inspect<br/>public helpers<br/>[__init__.py]"]
  node_module_demo["Diagnostic demo<br/>module entry point<br/>[__main__.py]"]
end

subgraph group_core["Rendering Core"]
  node_console["Console<br/>render orchestrator<br/>[console.py]"]
  node_protocol["Renderable protocol<br/>extension contract<br/>[protocol.py]"]
  node_segments["Styled segments<br/>normalized output<br/>[segment.py]"]
  node_text_style["Text and styles<br/>content model<br/>[text.py]"]
  node_terminal_encoding["ANSI terminal encoding<br/>terminal backend<br/>[ansi.py]"]
  node_unicode_layout["Unicode width and wrapping<br/>layout support<br/>[cells.py]"]
end

subgraph group_renderables["Built-in Renderables"]
  node_layout_primitives["Layout primitives<br/>composable renderables<br/>[table.py]"]
  node_input_renderers["Input renderers<br/>content renderables<br/>[markdown.py]"]
  node_pygments["Pygments<br/>external highlighter"]
end

subgraph group_live["Live Output"]
  node_live_controller["Live refresh controller<br/>stateful renderer<br/>[live.py]"]
  node_live_render["Live frame renderer<br/>frame layout<br/>[live_render.py]"]
  node_progress_displays["Progress, spinner, status<br/>live renderables<br/>[progress.py]"]
end

subgraph group_integration["Integrations &amp; Distribution"]
  node_logging["Logging formatter<br/>logging adapter<br/>[logging.py]"]
  node_tracebacks["Enhanced tracebacks<br/>exception adapter<br/>[traceback.py]"]
  node_jupyter["Notebook display<br/>notebook adapter<br/>[jupyter.py]"]
  node_windows_backend["Windows terminal backend<br/>platform adapter<br/>[_windows.py]"]
  node_package["Python package<br/>distribution<br/>[pyproject.toml]"]
  node_documentation["Documentation and examples<br/>usage material<br/>[index.rst]"]
  node_quality["CI and benchmarks<br/>quality automation<br/>[pythonpackage.yml]"]
end

node_public_helpers -->|"creates / uses"| node_console
node_module_demo -->|"demonstrates"| node_console
node_console -->|"renders through"| node_protocol
node_protocol -->|"normalizes to"| node_segments
node_console -->|"consumes"| node_segments
node_text_style -->|"styles"| node_segments
node_unicode_layout -->|"supports measurement"| node_console
node_segments -->|"encodes as"| node_terminal_encoding
node_terminal_encoding -.->|"uses on legacy Windows"| node_windows_backend
node_layout_primitives -->|"implements"| node_protocol
node_input_renderers -->|"implements"| node_protocol
node_input_renderers -->|"composes"| node_layout_primitives
node_input_renderers -.->|"delegates highlighting"| node_pygments
node_live_controller -->|"coordinates writes"| node_console
node_live_controller -->|"refreshes"| node_live_render
node_progress_displays -->|"updates through"| node_live_controller
node_logging -->|"formats with"| node_console
node_tracebacks -->|"renders to"| node_console
node_jupyter -->|"displays output from"| node_console
node_package -.->|"distributes"| node_public_helpers
node_documentation -.->|"documents"| node_public_helpers
node_quality -.->|"validates"| node_package

click node_public_helpers "https://github.com/textualize/rich/blob/main/rich/__init__.py"
click node_module_demo "https://github.com/textualize/rich/blob/main/rich/__main__.py"
click node_console "https://github.com/textualize/rich/blob/main/rich/console.py"
click node_protocol "https://github.com/textualize/rich/blob/main/rich/protocol.py"
click node_segments "https://github.com/textualize/rich/blob/main/rich/segment.py"
click node_text_style "https://github.com/textualize/rich/blob/main/rich/text.py"
click node_terminal_encoding "https://github.com/textualize/rich/blob/main/rich/ansi.py"
click node_unicode_layout "https://github.com/textualize/rich/blob/main/rich/cells.py"
click node_layout_primitives "https://github.com/textualize/rich/blob/main/rich/table.py"
click node_input_renderers "https://github.com/textualize/rich/blob/main/rich/markdown.py"
click node_live_controller "https://github.com/textualize/rich/blob/main/rich/live.py"
click node_live_render "https://github.com/textualize/rich/blob/main/rich/live_render.py"
click node_progress_displays "https://github.com/textualize/rich/blob/main/rich/progress.py"
click node_logging "https://github.com/textualize/rich/blob/main/rich/logging.py"
click node_tracebacks "https://github.com/textualize/rich/blob/main/rich/traceback.py"
click node_jupyter "https://github.com/textualize/rich/blob/main/rich/jupyter.py"
click node_windows_backend "https://github.com/textualize/rich/blob/main/rich/_windows.py"
click node_package "https://github.com/textualize/rich/blob/main/pyproject.toml"
click node_documentation "https://github.com/textualize/rich/blob/main/docs/source/index.rst"
click node_quality "https://github.com/textualize/rich/blob/main/.github/workflows/pythonpackage.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_public_helpers,node_module_demo toneBlue
class node_console,node_protocol,node_segments,node_text_style,node_terminal_encoding,node_unicode_layout toneAmber
class node_layout_primitives,node_input_renderers,node_pygments toneMint
class node_live_controller,node_live_render,node_progress_displays toneRose
class node_logging,node_tracebacks,node_jupyter,node_windows_backend,node_package,node_documentation,node_quality toneIndigo
```

## Components

### Public API

- [print / inspect](https://github.com/textualize/rich/blob/main/rich/__init__.py): public helpers · `rich/__init__.py`
- [Diagnostic demo](https://github.com/textualize/rich/blob/main/rich/__main__.py): module entry point · `rich/__main__.py`

### Rendering Core

- [Console](https://github.com/textualize/rich/blob/main/rich/console.py): render orchestrator · Applies output options and writes terminal, notebook, or exported output. · `rich/console.py`
- [Renderable protocol](https://github.com/textualize/rich/blob/main/rich/protocol.py): extension contract · Allows renderables to participate without directly depending on Console. · `rich/protocol.py`
- [Styled segments](https://github.com/textualize/rich/blob/main/rich/segment.py): normalized output · `rich/segment.py`
- [Text and styles](https://github.com/textualize/rich/blob/main/rich/text.py): content model · Keeps text spans separate from presentation and theme definitions. · `rich/text.py`
- [ANSI terminal encoding](https://github.com/textualize/rich/blob/main/rich/ansi.py): terminal backend · Converts colors and control operations into terminal-compatible sequences. · `rich/ansi.py`
- [Unicode width and wrapping](https://github.com/textualize/rich/blob/main/rich/cells.py): layout support · Provides emoji-safe cell measurement for alignment and wrapping. · `rich/cells.py`

### Built-in Renderables

- [Layout primitives](https://github.com/textualize/rich/blob/main/rich/table.py): composable renderables · Tables, panels, columns, trees, and layouts measure and render nested content. · `rich/table.py`
- [Input renderers](https://github.com/textualize/rich/blob/main/rich/markdown.py): content renderables · Markup, Markdown, JSON, pretty objects, and syntax become renderables. · `rich/markdown.py`
- **Pygments**: external highlighter

### Live Output

- [Live refresh controller](https://github.com/textualize/rich/blob/main/rich/live.py): stateful renderer · Reserves and redraws a render region while preserving ordinary console writes. · `rich/live.py`
- [Live frame renderer](https://github.com/textualize/rich/blob/main/rich/live_render.py): frame layout · `rich/live_render.py`
- [Progress, spinner, status](https://github.com/textualize/rich/blob/main/rich/progress.py): live renderables · Task updates mutate display state before each refreshed frame. · `rich/progress.py`

### Integrations & Distribution

- [Logging formatter](https://github.com/textualize/rich/blob/main/rich/logging.py): logging adapter · `rich/logging.py`
- [Enhanced tracebacks](https://github.com/textualize/rich/blob/main/rich/traceback.py): exception adapter · Provides renderable exception output and installable traceback handling. · `rich/traceback.py`
- [Notebook display](https://github.com/textualize/rich/blob/main/rich/jupyter.py): notebook adapter · `rich/jupyter.py`
- [Windows terminal backend](https://github.com/textualize/rich/blob/main/rich/_windows.py): platform adapter · Isolates legacy Windows console and renderer behavior. · `rich/_windows.py`
- [Python package](https://github.com/textualize/rich/blob/main/pyproject.toml): distribution · Pure Python library distributed through pip. · `pyproject.toml`
- [Documentation and examples](https://github.com/textualize/rich/blob/main/docs/source/index.rst): usage material · Sphinx docs and executable examples describe library usage. · `docs/source/index.rst`
- [CI and benchmarks](https://github.com/textualize/rich/blob/main/.github/workflows/pythonpackage.yml): quality automation · GitHub Actions verifies the package; ASV tracks performance regressions. · `.github/workflows/pythonpackage.yml`

## Connections

- 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
