This document explains how Rich Ruby processes text and components to generate terminal output.
Rich Ruby follows a pipeline: Input (Markup/Object) -> Text/Segments -> Console Options -> ANSI String -> Terminal
The "brain" of the library. It holds state about the terminal's width, height, and color capabilities. It acts as the orchestrator for all rendering.
A representation of text that includes "Spans" (range of characters + a Style). This is an intermediate format before splitting into segments.
The lowest-level rendering unit. A segment is a simple string paired with a Rich::Style.
- When you render any component (Table, Panel, etc.), it ultimately breaks down into a list of
Segments. - Segments are responsible for generating the final ANSI escape codes via
Segment.render.
Encapsulates foreground color, background color, and attributes (bold, etc.). Styles are immutable. Combining styles (style1 + style2) creates a new style object.
- Measurement: Components (like
Table) calculate the required width of each cell usingRich::Cells.cell_len. - Layout: Wrapping and alignment logic is applied to fit the
max_width. - Segmentation: The component converts its visual structure into
Rich::Segmentobjects. - ANSI Generation: The
Consoletakes the segments and joins them into a single string containing escape sequences. - Output: The string is written to
$stdout(or the configured output stream).
In lib/rich/win32_console.rb, we use Fiddle to interface with kernel32.dll:
- VT Processing: We call
GetConsoleModeandSetConsoleModeto enable ANSI support. - Size Detection: We call
GetConsoleScreenBufferInfoto get accurate dimensions without relying on environment variables.
Developers can create new components by implementing a render (or to_segments) method that returns a collection of Rich::Segment objects. This ensures that any new component automatically inherits font-width calculation and color-downgrade support.