$ show Textualize/rich
Rich is a Python library for rendering rich text, beautiful formatting, and advanced content to the terminal. It provides a Console class as its central abstraction, which manages terminal capabilities (color systems, width, encoding) and renders a composable hierarchy of 'renderables' — objects implementing the __rich_console__ protocol that yield Segment tuples (text, style, control codes). The architecture is a monolithic library with clean internal modularity: the Console orchestrates rendering, Segment is the low-level output primitive, and higher-level renderables (Table, Tree, Markdown, Syntax, Progress, Layout, Live) compose into each other via the protocol. Key entry points are the module-level print/inspect/print_json functions (backed by a global Console singleton), the Console class for explicit control, and CLI modules (python -m rich.syntax, python -m rich.markdown, etc.). The library handles cross-platform terminal differences including Windows legacy console API calls via ctypes, Jupyter notebook HTML rendering, and Unicode width calculations across 21 Unicode versions. External dependencies are minimal: pygments for syntax highlighting and markdown-it-py for Markdown parsing. The codebase demonstrates exceptional craftsmanship in its terminal rendering pipeline, careful handling of edge cases (CJK width, emoji variants, control codes, broken file-like objects), and a well-designed protocol system that enables arbitrary composition of renderables. The Segment-based rendering architecture is both performant and elegant, allowing style application, line splitting, and cell-width calculations to be composed as transformations over segment sequences.
The LLM identified 5 files worth highlighting for reuse:
The Segment class is the fundamental output primitive of Rich's rendering pipeline. Each Segment is a NamedTuple of (text, style, control) representing an atomic unit of terminal output. The module implements sophisticated operations on segment sequences: split_lines for line-breaking, divide for column splitting, set_shape for screen filling, simplify for merging adjacent segments with same style, and apply_style for style composition. This is the core abstraction that all renderables ultimately produce, and its design as an immutable tuple with class-level transformation methods is both performant and elegant. Extractable as a standalone terminal rendering primitive.
Implements the Rich renderable protocol with two key functions: is_renderable checks if an object supports __rich__ or __rich_console__, and rich_cast recursively resolves __rich__ methods to get a terminal-renderable object, with cycle detection via a visited set and protection against objects claiming all attributes (the _GIBBERISH check). This protocol design enables arbitrary Python objects to integrate with Rich's rendering pipeline by implementing a single method, and the casting logic handles edge cases like recursive __rich__ definitions and objects with broken __getattr__. The pattern is reusable for any system needing a flexible object-to-renderable conversion pipeline.
Implements a constraint-based layout distribution algorithm using exact arithmetic (Fraction) to divide terminal space among elements with size, ratio, and minimum_size constraints. The ratio_resolve function iteratively allocates space: fixed-size edges consume their allocation first, then remaining space is distributed proportionally by ratio, with minimum_size constraints enforced by promoting edges from flexible to fixed when their proportional allocation would fall below minimum. Uses Fraction to avoid floating-point rounding errors in the distribution. This is a clean, self-contained algorithm reusable for any layout system needing ratio-based space distribution with minimum constraints.
Handles Unicode character width measurement for terminal rendering, which is critical for correct alignment of CJK characters, emoji, and combining marks. Implements cell_len for measuring display width (0 for combining marks, 2 for wide CJK/emoji, 1 for most others), set_cell_size for padding/truncating to exact display width, and chop_cells/split_graphemes for breaking text at cell boundaries. Uses pre-computed lookup tables from the wcwidth project across 21 Unicode versions, loaded lazily and cached. The CellTable class provides efficient binary-search-based width lookups. This is a production-quality, self-contained Unicode terminal width library.
Three generator functions — loop_first, loop_last, loop_first_last — that iterate over sequences while yielding boundary flags (is_first, is_last). These eliminate the common pattern of checking index == 0 or index == len-1 inside loops, producing cleaner code for rendering tasks where first/last elements need different treatment (e.g., tree guides, table borders). Simple, zero-dependency, and immediately reusable in any project that needs boundary-aware iteration.