Skip to main content

Crate rich_rust

Crate rich_rust 

Source
Expand description

§rich_rust

A Rust port of Python’s Rich library for beautiful terminal output.

This library provides an abstraction over ANSI escape codes to render styled text, tables, panels, progress bars, trees, and more in the terminal.

§Quick Start

use rich_rust::prelude::*;

let console = Console::new();
console.print("[bold red]Hello[/] [green]World[/]!");

§Core Concepts

  • Console: The central entry point for printing styled output. Handles color detection, terminal dimensions, and ANSI code generation.
  • Style: Visual attributes including foreground/background colors, text decorations (bold, italic, underline, etc.), and hyperlinks.
  • Text: Rich text with overlapping style spans that can be justified, wrapped, and rendered to the terminal.
  • Segment: The atomic rendering unit combining text content with a style.
  • Color: Terminal colors supporting 4-bit ANSI, 8-bit (256), and 24-bit true color with automatic downgrading.

§Renderables

The library provides several high-level renderables for structured output:

§Markup Syntax

The Console supports a simple markup syntax for inline styling:

// Basic styling
console.print("[bold]Bold text[/bold]");
console.print("[italic red]Red italic[/]");  // [/] closes any open tag

// Colors
console.print("[green]Green[/] [#ff8800]Orange[/] [rgb(100,150,200)]Blue[/]");

// Combinations
console.print("[bold underline magenta on white]Styled text[/]");

// Hyperlinks
console.print("[link=https://example.com]Click here[/link]");

§Features

Optional features can be enabled in Cargo.toml:

  • syntax: Syntax highlighting for source code via syntect
  • markdown: Markdown rendering via pulldown-cmark
  • json: JSON formatting with syntax highlighting
  • tracing: Tracing integration via RichTracingLayer
[dependencies]
rich_rust = { version = "0.1", features = ["syntax", "markdown", "json"] }

§Thread Safety

All public types in rich_rust are Send + Sync. You can safely:

§Mutex Poison Recovery

rich_rust uses poison recovery for all internal mutexes. If a thread panics while holding a lock, subsequent operations will continue rather than propagate the panic. This is appropriate because:

  1. Caches: Style/cell/color caches can safely use potentially stale data
  2. Output buffers: A corrupted buffer produces garbled output, not crashes
  3. Configuration: Theme/options are read-mostly and self-healing

See the sync module for details on the poison recovery strategy.

§Caveats

  • Output may interleave when printing from multiple threads simultaneously
  • Style/color parsing caches are thread-safe but not deterministic under contention
  • For deterministic output ordering, synchronize at the application level

§Examples

§Styled Text

use rich_rust::prelude::*;

// Build text programmatically
let mut text = Text::new("Hello, ");
text.append("World", Style::new().bold().color(Color::parse("red").unwrap()));
text.append("!", Style::new().italic());

let console = Console::new();
console.print_text(&text);

§Tables

use rich_rust::prelude::*;

let table = Table::new()
    .add_column(Column::new("Name").style(Style::new().bold()))
    .add_column(Column::new("Age").justify(JustifyMethod::Right))
    .add_row(Row::new().cell("Alice").cell("30"))
    .add_row(Row::new().cell("Bob").cell("25"));

console.print_renderable(&table);

§Panels

use rich_rust::prelude::*;

let panel = Panel::new("Content inside a box")
    .title("My Panel")
    .border_style(Style::new().color(Color::parse("blue").unwrap()));

console.print_renderable(&panel);

§Trees

use rich_rust::prelude::*;

let tree = Tree::new(
    TreeNode::new("Project")
        .child(TreeNode::new("src")
            .child(TreeNode::new("main.rs"))
            .child(TreeNode::new("lib.rs")))
        .child(TreeNode::new("Cargo.toml")),
);

console.print_renderable(&tree);

Re-exports§

pub use color::Color;
pub use color::ColorSystem;
pub use color::ColorTriplet;
pub use color::ColorType;
pub use color::DEFAULT_TERMINAL_THEME;
pub use color::DIMMED_MONOKAI;
pub use color::MONOKAI;
pub use color::NIGHT_OWLISH;
pub use color::SVG_EXPORT_THEME;
pub use color::TerminalTheme;
pub use console::Console;
pub use console::CONSOLE_HTML_FORMAT;
pub use console::CONSOLE_SVG_FORMAT;
pub use console::ExportHtmlOptions;
pub use console::ExportSvgOptions;
pub use live::Live;
pub use live::LiveOptions;
pub use live::VerticalOverflowMethod;
pub use logging::RichLogger;
pub use renderables::Layout;
pub use renderables::LayoutSplitter;
pub use renderables::Region;
pub use segment::Segment;
pub use style::Attributes;
pub use style::Style;
pub use text::Span;
pub use text::Text;
pub use theme::Theme;
pub use theme::ThemeError;
pub use theme::ThemeStackError;

Modules§

ansi
ANSI decoding utilities (Python Rich rich.ansi parity).
box
Box drawing characters for tables and panels.
cells
Unicode character cell width calculations.
color
Color system for terminal rendering.
console
Console - the central entry point for styled terminal output.
emoji
Emoji support (Rich-style :name: codes).
filesize
File size formatting for human-readable output.
highlighter
Highlighters (Python Rich rich.highlighter parity).
interactive
Interactive helpers inspired by Python Rich.
live
Live display system for dynamic terminal updates.
logging
Logging integration similar to Python Rich’s RichHandler.
markup
Markup parsing for Rich-style text.
measure
Measurement protocol for determining renderable dimensions.
prelude
Re-exports for convenient usage
protocol
Protocol-style extensibility (Python Rich rich.protocol parity).
renderables
Renderable components for rich terminal output.
segment
Segment - the atomic rendering unit.
style
Style system for terminal text attributes.
sync
Synchronization Utilities
terminal
Terminal detection and manipulation.
text
Rich text with styled spans.
theme
Theme support for named styles (Python Rich parity).