Console¶
The Console is your primary interface for all terminal output in Fast-Rich. It handles color detection, terminal sizing, and rendering of styled content.
Creating a Console¶
use fast_rich::prelude::*;
fn main() {
let console = Console::new();
console.print("[bold green]Ready![/]");
}
Basic Output Methods¶
print() - Styled Output¶
Prints text with markup processing, no trailing newline:
console.print("[bold]Hello[/] ");
console.print("[blue]World[/]");
// Output: Hello World (on same line)
println() - Styled Output with Newline¶
Same as print() but adds a newline:
print_renderable() - Complex Objects¶
For tables, panels, trees, and other renderables:
let mut table = Table::new();
table.add_column("Name");
table.add_row_strs(&["Fast-Rich"]);
console.print_renderable(&table);
Decorative Methods¶
rule() - Horizontal Divider¶
Creates a decorative horizontal line:
Output:
Customize with plain text or leave empty:
newline() - Blank Line¶
Adds a blank line:
Console Configuration¶
Force Color Output¶
By default, Fast-Rich detects if the terminal supports colors. Override this behavior:
// Force colors on (useful for CI/piped output)
let console = Console::new().force_color(true);
// Force colors off
let console = Console::new().force_color(false);
Terminal Width¶
Get the current terminal width:
let console = Console::new();
let width = console.get_width();
println!("Terminal is {} columns wide", width);
Capture Mode (Testing)¶
For testing, capture output instead of printing to stdout:
use fast_rich::prelude::*;
fn main() {
let console = Console::capture();
console.print("[bold]Hello[/]");
console.println(" World");
let output = console.get_captured_output();
assert!(output.contains("Hello"));
assert!(output.contains("World"));
}
This is useful for:
- Unit testing styled output
- Generating output for export
- Comparing expected vs. actual rendering
Real Terminal Output¶
Command:
Output:
Console API Demo
This is a regular print.
This is a println which adds a newline.
─────── Padding ───────
Explicit Renderable
Emoji: 🐍🦀
Best Practices¶
Reuse Console Instance
Create one Console and reuse it throughout your application:
Thread Safety
Console is not Send or Sync. For multi-threaded applications,
create a new Console in each thread or protect with appropriate synchronization.