This guide provides quick examples and tips for using Rich Ruby effectively, categorized by user level.
If you're new to Rich Ruby, the easiest way to start is using the Rich.print method and Markup.
require 'rich'
Rich.print("[bold red]ERROR:[/] Something went wrong!")
Rich.print("[green]SUCCESS:[/] Operation completed.")
Rich.print("[blue italic]INFO:[/] Processing data...")Use rules to organize your terminal output.
Rich.rule("Section 1")
puts "Content goes here"
Rich.rule(style: "dim")Once you're comfortable with basic printing, start using structured components like Panels and Tables.
Panels are great for highlights or error messages.
panel = Rich::Panel.new(
"Data backup complete.\nVerified 1,024 files.",
title: "Backup Status",
border_style: "cyan"
)
puts panel.render(max_width: 40)Tables automatically calculate column widths.
table = Rich::Table.new(title: "Inventory")
table.add_column("Item", header_style: "bold")
table.add_column("Stock", justify: :right)
table.add_row("Apples", "50")
table.add_row("Oranges", "12")
puts table.render(max_width: 30)For power users building complex CLI tools.
bar = Rich::ProgressBar.new(total: 100)
100.times do |i|
bar.update(i + 1)
print "\r#{bar.render}"
sleep 0.01
end
puts "" # New line after completionPerfect for dev tools or logging code snippets.
code = "def hello; puts 'world'; end"
syntax = Rich::Syntax.new(code, language: "ruby", theme: :monokai)
puts syntax.render- Pure Ruby: The entire library is implemented in Pure Ruby (with
Fiddlefor Windows API). This ensures portability but means performance may degrade with exceptionally large datasets (e.g., tables with 10,000+ rows). - Windows Integration: On Windows, we directly interface with
kernel32.dllto enable Virtual Terminal processing. This is robust but depends on modern Windows 10/11 features for the best experience. - Color Systems: While we support TrueColor, the actual appearance depends on your terminal emulator's capabilities.
| Task | Tool to Use |
|---|---|
| Quick logging | Rich.print |
| Boxing content | Rich::Panel |
| List data | Rich::Table |
| File trees | Rich::Tree |
| Source code | Rich::Syntax |
| Formatted docs | Rich::Markdown |