rich_rust 0.2.1

A Rust port of Python's Rich library for beautiful terminal output
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# Migration Guide: Python Rich to rich_rust

This guide helps Python Rich users migrate to rich_rust. Both libraries share similar concepts and markup syntax, but there are Rust-specific patterns you'll need to learn.

## Quick Comparison

| Aspect | Python Rich | rich_rust |
|--------|-------------|-----------|
| Language | Python | Rust |
| Install | `pip install rich` | `cargo add rich_rust` |
| Markup | `[bold red]text[/]` | `[bold red]text[/]` (same!) |
| Console | `Console()` | `Console::new()` |
| Style | `Style(bold=True)` | `Style::new().bold()` |
| Async | Native | Not built-in (sync) |

## Feature Mapping

### Core Features

| Python Rich | rich_rust | Status |
|-------------|-----------|--------|
| `Console` | `Console` | Full |
| `Text` | `Text` | Full |
| `Style` | `Style` | Full |
| `Table` | `Table` | Full |
| `Panel` | `Panel` | Full |
| `Rule` | `Rule` | Full |
| `Columns` | `Columns` | Full |
| `Tree` | `Tree` | Full |
| `Padding` | `Padding` | Full |
| `Align` | `Align` | Full |

### Optional Features (require feature flags)

| Python Rich | rich_rust | Feature Flag |
|-------------|-----------|--------------|
| `Syntax` | `Syntax` | `syntax` |
| `Markdown` | `Markdown` | `markdown` |
| `JSON` | `Json` | `json` |
| `Traceback` capture | `Traceback::capture` | `backtrace` |
| `tracing` integration | `RichTracingLayer` | `tracing` |

### Additional Systems

| Python Rich | rich_rust | Notes |
|-------------|-----------|-------|
| `Live` | `Live` | Dynamic refresh + optional process-wide stdout/stderr redirection in interactive terminals |
| `Console.status(...)` | `Status` | Spinner + message helper built on `Live` |
| `Prompt` / `Confirm` / `IntPrompt` | `Prompt` / `Confirm` / `Select` | Output-focused interactive helpers (degrade cleanly when non-interactive) |
| Logging handler (`RichHandler`) | `RichLogger` | Implements the `log` crate; prints Rich-style log lines |
| Tracebacks (`rich.traceback`) | `Traceback` | Deterministic explicit frames; optional runtime backtrace capture behind `backtrace` |

### Explicit Exclusions (Out of Scope)

- Jupyter/IPython integration
- Legacy Windows cmd.exe (use modern terminals with VT support)

## API Differences

### Console Creation

**Python:**
```python
from rich.console import Console

console = Console()
console = Console(width=80, force_terminal=True)
```

**Rust:**
```rust
use rich_rust::prelude::*;

let console = Console::new();
let console = Console::builder()
    .width(80)
    .force_terminal(true)
    .build();
```

### Printing with Markup

**Python:**
```python
console.print("[bold red]Error:[/] Something went wrong")
console.print("[green]Success![/]")
```

**Rust:**
```rust
console.print("[bold red]Error:[/] Something went wrong");
console.print("[green]Success![/]");
```

The markup syntax is identical!

### Printing without Markup

**Python:**
```python
console.print("[literal brackets]", markup=False)
```

**Rust:**
```rust
console.print_plain("[literal brackets]");
```

### Creating Styles

**Python:**
```python
from rich.style import Style

style = Style(bold=True, color="red")
style = Style.parse("bold red on white")
```

**Rust:**
```rust
use rich_rust::style::Style;
use rich_rust::color::Color;

let style = Style::new().bold().color(Color::parse("red").unwrap());
let style = Style::parse("bold red on white").unwrap();
```

### Creating Text

**Python:**
```python
from rich.text import Text

text = Text("Hello World")
text.stylize("bold", 0, 5)
```

**Rust:**
```rust
use rich_rust::text::Text;
use rich_rust::style::Style;

let mut text = Text::new("Hello World");
text.stylize(0, 5, Style::new().bold());
```

### Creating Tables

**Python:**
```python
from rich.table import Table

table = Table(title="Users")
table.add_column("Name", style="cyan")
table.add_column("Age", justify="right")
table.add_row("Alice", "30")
table.add_row("Bob", "25")
console.print(table)
```

**Rust:**
```rust
use rich_rust::prelude::*;

let mut table = Table::new()
    .title("Users")
    .with_column(Column::new("Name").style(Style::parse("cyan").unwrap()))
    .with_column(Column::new("Age").justify(JustifyMethod::Right));

table.add_row_cells(["Alice", "30"]);
table.add_row_cells(["Bob", "25"]);

for seg in table.render(80) {
    print!("{}", seg.text);
}
```

### Creating Panels

**Python:**
```python
from rich.panel import Panel

panel = Panel("Hello World", title="Greeting")
console.print(panel)
```

**Rust:**
```rust
use rich_rust::prelude::*;

let panel = Panel::from_text("Hello World")
    .title("Greeting");

for seg in panel.render(80) {
    print!("{}", seg.text);
}
```

### Creating Trees

**Python:**
```python
from rich.tree import Tree

tree = Tree("Root")
tree.add("Child 1")
branch = tree.add("Child 2")
branch.add("Grandchild")
console.print(tree)
```

**Rust:**
```rust
use rich_rust::prelude::*;

let mut root = TreeNode::new("Root");
root.add_child(TreeNode::new("Child 1"));
let mut child2 = TreeNode::new("Child 2");
child2.add_child(TreeNode::new("Grandchild"));
root.add_child(child2);

let tree = Tree::new(root);
for seg in tree.render(80) {
    print!("{}", seg.text);
}
```

### Horizontal Rules

**Python:**
```python
from rich.rule import Rule

console.rule("Section Title")
console.print(Rule(style="cyan"))
```

**Rust:**
```rust
use rich_rust::prelude::*;

console.rule(Some("Section Title"));

// Or with custom style:
let rule = Rule::with_title("Section")
    .style(Style::parse("cyan").unwrap());
```

## Markup Syntax Reference

The markup syntax is identical between Python Rich and rich_rust:

| Markup | Effect |
|--------|--------|
| `[bold]text[/]` | Bold |
| `[italic]text[/]` | Italic |
| `[underline]text[/]` | Underline |
| `[strike]text[/]` | Strikethrough |
| `[red]text[/]` | Red foreground |
| `[on blue]text[/]` | Blue background |
| `[bold red on white]text[/]` | Combined |
| `[#ff0000]text[/]` | Hex color |
| `[rgb(255,0,0)]text[/]` | RGB color |
| `[color(196)]text[/]` | 256-color palette |
| `[link=https://...]text[/]` | Hyperlink |

### Escaping Brackets

**Python:**
```python
console.print(r"\[not markup\]")
```

**Rust:**
```rust
console.print(r"\[not markup\]");
```

## Feature Flags

Enable optional features in your `Cargo.toml`:

```toml
[dependencies]
rich_rust = { version = "0.1", features = ["syntax", "markdown", "json"] }

# Or enable all:
rich_rust = { version = "0.1", features = ["full"] }
```

### Syntax Highlighting

**Python:**
```python
from rich.syntax import Syntax

syntax = Syntax(code, "python", line_numbers=True)
console.print(syntax)
```

**Rust (requires `syntax` feature):**
```rust
use rich_rust::prelude::*;

let syntax = Syntax::new(code, "python")
    .line_numbers(true);

for seg in syntax.render(80) {
    print!("{}", seg.text);
}
```

### Markdown

**Python:**
```python
from rich.markdown import Markdown

md = Markdown("# Hello\n\nWorld")
console.print(md)
```

**Rust (requires `markdown` feature):**
```rust
use rich_rust::prelude::*;

let md = Markdown::new("# Hello\n\nWorld");

for seg in md.render(80) {
    print!("{}", seg.text);
}
```

## Key Differences Summary

1. **Builder Pattern**: Rust uses builder methods (`Style::new().bold()`) instead of keyword arguments
2. **Explicit Rendering**: Call `.render(width)` to get segments, then iterate
3. **Error Handling**: Methods that can fail return `Result`, use `.unwrap()` or proper error handling
4. **Ownership**: Rust's ownership model means some methods take `&self`, others `&mut self`
5. **Interactive helpers**: rich_rust includes Live/Status/Prompt helpers, but it is not a full TUI widget framework
6. **Feature Flags**: Optional features (syntax, markdown, json, tracing, backtrace) require explicit Cargo.toml flags

## Common Migration Patterns

### Python idiom: Style chaining
```python
style = Style(bold=True) + Style(color="red")
```

**Rust equivalent:**
```rust
let style = Style::new().bold() + Style::new().color(Color::parse("red").unwrap());
// Or:
let style = Style::new().bold().color(Color::parse("red").unwrap());
```

### Python idiom: Console recording
```python
console = Console(record=True)
console.print("Hello")
html = console.export_html()
```

**Rust equivalent:**
```rust
let console = Console::new();
console.begin_capture();
console.print("Hello");
let html = console.export_html(false);
let svg = console.export_svg(true);
```

### Python idiom: Render to string
```python
from io import StringIO
console = Console(file=StringIO())
console.print("Hello")
output = console.file.getvalue()
```

**Rust equivalent:**
```rust
use std::io::Write;

let buffer = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));

// Create a wrapper that implements Write + Send
struct SharedBuffer(std::sync::Arc<std::sync::Mutex<Vec<u8>>>);
impl Write for SharedBuffer {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.0.lock().unwrap().write(buf)
    }
    fn flush(&mut self) -> std::io::Result<()> {
        self.0.lock().unwrap().flush()
    }
}

let console = Console::builder()
    .file(Box::new(SharedBuffer(buffer.clone())))
    .build();

console.print("Hello");
let output = String::from_utf8_lossy(&buffer.lock().unwrap()).to_string();
```

## Getting Help

- [rich_rust Documentation]https://docs.rs/rich_rust
- [RICH_SPEC.md]../RICH_SPEC.md - Detailed behavioral specification
- [Examples]../examples/ - Working code examples
- [GitHub Issues]https://github.com/Dicklesworthstone/rich_rust/issues