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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Golden file (snapshot) tests for visual regression detection.
//!
//! These tests capture the expected rendering output of various components
//! and detect visual regressions by comparing actual output against stored expectations.
//!
//! ## Running Tests
//!
//! ```bash
//! # Run all golden tests
//! cargo test --test golden_test
//!
//! # Update snapshots when intentional changes are made
//! cargo insta test --accept
//!
//! # Review pending snapshots interactively
//! cargo insta review
//! ```
//!
//! ## Environment Variables
//!
//! - `INSTA_UPDATE=always` - Auto-accept new snapshots
//! - `INSTA_UPDATE=unseen` - Only update new snapshots, fail on changed
//! - `INSTA_UPDATE=no` - Never update, fail on any difference

mod common;

use common::init_test_logging;
use rich_rust::prelude::*;

/// Strip ANSI escape codes for text-only comparison.
fn strip_ansi(s: &str) -> String {
    let ansi_regex = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();
    ansi_regex.replace_all(s, "").to_string()
}

/// Collect segments into a single string.
fn segments_to_string(segments: Vec<Segment>) -> String {
    segments.into_iter().map(|s| s.text).collect()
}

// =============================================================================
// Rule Tests
// =============================================================================

#[test]
fn golden_rule_simple() {
    init_test_logging();
    let rule = Rule::new();
    let output = segments_to_string(rule.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("rule_simple", plain);
}

#[test]
fn golden_rule_with_title() {
    init_test_logging();
    let rule = Rule::with_title("Section Title");
    let output = segments_to_string(rule.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("rule_with_title", plain);
}

#[test]
fn golden_rule_left_aligned() {
    init_test_logging();
    let rule = Rule::with_title("Left").align_left();
    let output = segments_to_string(rule.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("rule_left_aligned", plain);
}

#[test]
fn golden_rule_right_aligned() {
    init_test_logging();
    let rule = Rule::with_title("Right").align_right();
    let output = segments_to_string(rule.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("rule_right_aligned", plain);
}

// =============================================================================
// Panel Tests
// =============================================================================

#[test]
fn golden_panel_simple() {
    init_test_logging();
    let panel = Panel::from_text("Hello, Panel!").width(30);
    let output = segments_to_string(panel.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("panel_simple", plain);
}

#[test]
fn golden_panel_with_title() {
    init_test_logging();
    let panel = Panel::from_text("Content here").title("My Panel").width(30);
    let output = segments_to_string(panel.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("panel_with_title", plain);
}

#[test]
fn golden_panel_with_title_and_subtitle() {
    init_test_logging();
    let panel = Panel::from_text("Multi-line\nContent\nHere")
        .title("Info Panel")
        .subtitle("v1.0")
        .width(30);
    let output = segments_to_string(panel.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("panel_with_title_subtitle", plain);
}

#[test]
fn golden_panel_square() {
    init_test_logging();
    let panel = Panel::from_text("Square corners")
        .square()
        .title("Square")
        .width(30);
    let output = segments_to_string(panel.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("panel_square", plain);
}

#[test]
fn golden_panel_ascii() {
    init_test_logging();
    let panel = Panel::from_text("ASCII safe!")
        .ascii()
        .title("Legacy")
        .width(25);
    let output = segments_to_string(panel.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("panel_ascii", plain);
}

// =============================================================================
// Table Tests
// =============================================================================

#[test]
fn golden_table_basic() {
    init_test_logging();
    let mut table = Table::new()
        .with_column(Column::new("Name"))
        .with_column(Column::new("Value"));

    table.add_row_cells(["Key1", "Value1"]);
    table.add_row_cells(["Key2", "Value2"]);
    table.add_row_cells(["Key3", "Value3"]);

    let output = segments_to_string(table.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("table_basic", plain);
}

#[test]
fn golden_table_with_title() {
    init_test_logging();
    let mut table = Table::new()
        .title("Data Table")
        .with_column(Column::new("ID").width(5))
        .with_column(Column::new("Name").min_width(10))
        .with_column(Column::new("Score").justify(JustifyMethod::Right));

    table.add_row_cells(["1", "Alice", "100"]);
    table.add_row_cells(["2", "Bob", "95"]);
    table.add_row_cells(["3", "Charlie", "87"]);

    let output = segments_to_string(table.render(50));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("table_with_title", plain);
}

#[test]
fn golden_table_ascii() {
    init_test_logging();
    let mut table = Table::new()
        .ascii()
        .with_column(Column::new("Key"))
        .with_column(Column::new("Value"));

    table.add_row_cells(["version", "1.0.0"]);
    table.add_row_cells(["author", "Test"]);

    let output = segments_to_string(table.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("table_ascii", plain);
}

#[test]
fn golden_table_no_header() {
    init_test_logging();
    let mut table = Table::new()
        .show_header(false)
        .with_column(Column::new("A"))
        .with_column(Column::new("B"));

    table.add_row_cells(["Data 1", "Data 2"]);
    table.add_row_cells(["Data 3", "Data 4"]);

    let output = segments_to_string(table.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("table_no_header", plain);
}

#[test]
fn golden_table_wide() {
    init_test_logging();
    let mut table = Table::new()
        .with_column(Column::new("Name").min_width(15))
        .with_column(Column::new("Department").min_width(12))
        .with_column(Column::new("Email").min_width(20));

    table.add_row_cells(["John Smith", "Engineering", "john@example.com"]);
    table.add_row_cells(["Jane Doe", "Marketing", "jane@example.com"]);

    let output = segments_to_string(table.render(60));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("table_wide", plain);
}

// =============================================================================
// Tree Tests
// =============================================================================

#[test]
fn golden_tree_simple() {
    init_test_logging();
    let tree = Tree::new(TreeNode::new("Root"))
        .child(TreeNode::new("Child 1"))
        .child(TreeNode::new("Child 2"))
        .child(TreeNode::new("Child 3"));

    let output = segments_to_string(tree.render());
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("tree_simple", plain);
}

#[test]
fn golden_tree_nested() {
    init_test_logging();
    let tree = Tree::new(TreeNode::new("Project"))
        .child(
            TreeNode::new("src")
                .child(TreeNode::new("main.rs"))
                .child(TreeNode::new("lib.rs")),
        )
        .child(
            TreeNode::new("tests")
                .child(TreeNode::new("unit.rs"))
                .child(TreeNode::new("integration.rs")),
        )
        .child(TreeNode::new("Cargo.toml"));

    let output = segments_to_string(tree.render());
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("tree_nested", plain);
}

#[test]
fn golden_tree_ascii() {
    init_test_logging();
    let tree = Tree::new(TreeNode::new("Root"))
        .guides(TreeGuides::Ascii)
        .child(TreeNode::new("Branch A").child(TreeNode::new("Leaf 1")))
        .child(TreeNode::new("Branch B").child(TreeNode::new("Leaf 2")));

    let output = segments_to_string(tree.render());
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("tree_ascii", plain);
}

// =============================================================================
// Columns Tests
// =============================================================================

#[test]
fn golden_columns_basic() {
    init_test_logging();
    let columns = Columns::from_strings(&["Column 1", "Column 2", "Column 3"]).equal_width(true);

    let output = segments_to_string(columns.render_flat(60));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("columns_basic", plain);
}

#[test]
fn golden_columns_multiline() {
    init_test_logging();
    let columns = Columns::from_strings(&[
        "First column\nwith multiple\nlines",
        "Second column\nalso multiline",
        "Third",
    ])
    .equal_width(true);

    let output = segments_to_string(columns.render_flat(60));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("columns_multiline", plain);
}

// =============================================================================
// Progress Bar Tests
// =============================================================================

#[test]
fn golden_progress_bar_empty() {
    init_test_logging();
    let mut bar = ProgressBar::new().width(30);
    bar.set_progress(0.0);
    let output = segments_to_string(bar.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("progress_bar_empty", plain);
}

#[test]
fn golden_progress_bar_half() {
    init_test_logging();
    let mut bar = ProgressBar::new().width(30);
    bar.set_progress(0.5);
    let output = segments_to_string(bar.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("progress_bar_half", plain);
}

#[test]
fn golden_progress_bar_full() {
    init_test_logging();
    let mut bar = ProgressBar::new().width(30);
    bar.set_progress(1.0);
    let output = segments_to_string(bar.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("progress_bar_full", plain);
}

#[test]
fn golden_progress_bar_ascii() {
    init_test_logging();
    let mut bar = ProgressBar::new().width(30).bar_style(BarStyle::Ascii);
    bar.set_progress(0.75);
    let output = segments_to_string(bar.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("progress_bar_ascii", plain);
}

// =============================================================================
// Text Wrapping Tests
// =============================================================================

#[test]
fn golden_text_wrapped() {
    init_test_logging();
    // Use Panel to demonstrate truncation to width (Panel does not wrap content).
    let long_text = "This is a very long piece of text that should be wrapped \
        across multiple lines when rendered at a narrow width.";

    let panel = Panel::from_text(long_text).width(35);
    let output = segments_to_string(panel.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("text_wrapped", plain);
}

// =============================================================================
// Combined/Complex Tests
// =============================================================================

#[test]
fn golden_combined_rules_and_panels() {
    init_test_logging();
    let mut output = String::new();

    // Add a rule
    let rule = Rule::with_title("Section 1");
    output += &segments_to_string(rule.render(40));

    // Add a panel
    let panel = Panel::from_text("Content in section 1").width(38);
    output += &segments_to_string(panel.render(40));

    // Add another rule
    let rule2 = Rule::with_title("Section 2");
    output += &segments_to_string(rule2.render(40));

    let plain = strip_ansi(&output);
    insta::assert_snapshot!("combined_rules_panels", plain);
}

// =============================================================================
// Edge Case Tests
// =============================================================================

#[test]
fn golden_empty_table() {
    init_test_logging();
    let table = Table::new()
        .with_column(Column::new("A"))
        .with_column(Column::new("B"));

    let output = segments_to_string(table.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("table_empty", plain);
}

#[test]
fn golden_single_cell_table() {
    init_test_logging();
    let mut table = Table::new().with_column(Column::new("Only"));
    table.add_row_cells(["Value"]);

    let output = segments_to_string(table.render(40));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("table_single_cell", plain);
}

#[test]
fn golden_panel_empty() {
    init_test_logging();
    let panel = Panel::from_text("").width(20);
    let output = segments_to_string(panel.render(30));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("panel_empty", plain);
}

#[test]
fn golden_rule_very_short() {
    init_test_logging();
    let rule = Rule::with_title("X");
    let output = segments_to_string(rule.render(10));
    let plain = strip_ansi(&output);
    insta::assert_snapshot!("rule_very_short", plain);
}