Skip to main content

Module console

Module console 

Source
Expand description

Console - the central entry point for styled terminal output.

The Console handles rendering styled content to the terminal, including color detection, width calculation, and ANSI code generation.

§Examples

§Basic Printing with Markup

use rich_rust::Console;

let console = Console::new();

// Print with markup syntax
console.print("[bold red]Error:[/] Something went wrong");
console.print("[green]Success![/] Operation completed");

// Markup supports colors, attributes, and combinations
console.print("[bold italic #ff8800 on blue]Custom styling[/]");

§Console Builder

use rich_rust::console::{Console, ConsoleBuilder};
use rich_rust::color::ColorSystem;

let console = Console::builder()
    .color_system(ColorSystem::EightBit)  // Force 256 colors
    .width(80)                            // Fixed width
    .markup(true)                         // Enable markup parsing
    .build();
use rich_rust::console::{Console, PrintOptions};
use rich_rust::style::Style;
use rich_rust::text::JustifyMethod;

let console = Console::new();

let options = PrintOptions::new()
    .with_style(Style::new().bold())
    .with_justify(JustifyMethod::Center)
    .with_markup(true);

console.print_with_options("Centered bold text", &options);

§Capturing Output

use rich_rust::Console;

let mut console = Console::new();

// Start capturing
console.begin_capture();
console.print("[bold]Hello[/]");

// Get captured segments
let segments = console.end_capture();
for seg in &segments {
    println!("Text: {:?}, Style: {:?}", seg.text, seg.style);
}

§Terminal Detection

The Console automatically detects terminal capabilities:

  • Color system: TrueColor (24-bit), 256 colors, or 16 colors
  • Terminal dimensions: Width and height in character cells
  • TTY status: Whether output is to an interactive terminal

You can override these with the builder pattern or by setting explicit values.

Structs§

Console
The main Console for rendering styled output.
ConsoleBuilder
Builder for creating a Console with custom settings.
ConsoleDimensions
Console dimensions in cells.
ConsoleOptions
Options for rendering.
ExportHtmlOptions
Options for controlling HTML export.
ExportSvgOptions
Options for controlling SVG export.
LogOptions
Options for controlling log output format.
PrintOptions
Print options for controlling output.
ThemeGuard
RAII guard returned by Console::use_theme.

Enums§

LogLevel
Log level for console.log().

Constants§

CONSOLE_HTML_FORMAT
Default HTML export template (Rich 13.9.4).
CONSOLE_SVG_FORMAT
Default SVG export template (Rich 13.9.4).

Traits§

RenderHook
Hook for intercepting rendered segments before output.