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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! End-to-end tests for Panel rendering and nested renderables.
//!
//! Panels are bordered boxes that can contain various types of content
//! including text, styled content, and other renderables like tables.
//!
//! Run with: RUST_LOG=debug cargo test --test e2e_panel -- --nocapture

mod common;

use common::init_test_logging;
use rich_rust::r#box::{DOUBLE, HEAVY, MINIMAL};
use rich_rust::prelude::*;

// =============================================================================
// Scenario 1: Basic Panel
// =============================================================================

#[test]
fn e2e_basic_panel_text() {
    init_test_logging();
    tracing::info!("Starting E2E basic panel text test");

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

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Rendered panel");

    // Verify rounded corners (default)
    assert!(output.contains(''), "Missing top-left corner");
    assert!(output.contains(''), "Missing top-right corner");
    assert!(output.contains(''), "Missing bottom-left corner");
    assert!(output.contains(''), "Missing bottom-right corner");

    // Verify borders
    assert!(output.contains(''), "Missing horizontal border");
    assert!(output.contains(''), "Missing vertical border");

    // Verify content
    assert!(output.contains("Greeting"), "Missing title");
    assert!(output.contains("Hello World"), "Missing content");

    tracing::info!("E2E basic panel text test PASSED");
}

#[test]
fn e2e_panel_multiline() {
    init_test_logging();
    tracing::info!("Starting E2E panel multiline test");

    let panel = Panel::from_text("Line 1\nLine 2\nLine 3").width(20);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Multiline panel");

    assert!(output.contains("Line 1"), "Missing line 1");
    assert!(output.contains("Line 2"), "Missing line 2");
    assert!(output.contains("Line 3"), "Missing line 3");

    tracing::info!("E2E panel multiline test PASSED");
}

#[test]
fn e2e_panel_with_subtitle() {
    init_test_logging();
    tracing::info!("Starting E2E panel with subtitle test");

    let panel = Panel::from_text("Content here")
        .title("Header")
        .subtitle("Footer")
        .width(30);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Panel with title and subtitle");

    assert!(output.contains("Header"), "Missing title");
    assert!(output.contains("Footer"), "Missing subtitle");
    assert!(output.contains("Content here"), "Missing content");

    tracing::info!("E2E panel with subtitle test PASSED");
}

// =============================================================================
// Scenario 2: Panel with Styled Content
// =============================================================================

#[test]
fn e2e_panel_styled_content() {
    init_test_logging();
    tracing::info!("Starting E2E panel with styled content test");

    // Create segments with styling
    let bold_style = Style::new().bold();
    let segments = vec![vec![
        Segment::new("Important", Some(bold_style)),
        Segment::new(" message", None),
    ]];

    let panel = Panel::new(segments).width(30);
    let output = panel.render(50);
    tracing::debug!(segment_count = output.len(), "Panel with styled content");

    // Verify styled segments are present
    let has_styled = output.iter().any(|s| s.style.is_some());
    assert!(has_styled, "Should have styled segments");

    let plain: String = output.iter().map(|s| s.text.as_ref()).collect();
    assert!(plain.contains("Important"), "Missing styled text");
    assert!(plain.contains("message"), "Missing plain text");

    tracing::info!("E2E panel with styled content test PASSED");
}

#[test]
fn e2e_panel_border_style() {
    init_test_logging();
    tracing::info!("Starting E2E panel with border style test");

    let red_style = Style::new().color(Color::parse("red").unwrap());
    let panel = Panel::from_text("Warning!")
        .border_style(red_style)
        .width(20);

    let segments = panel.render(50);

    // Check that border segments have the red style
    let border_segments: Vec<_> = segments
        .iter()
        .filter(|s| s.text.contains('') || s.text.contains(''))
        .collect();

    tracing::debug!(border_count = border_segments.len(), "Border segments");
    assert!(!border_segments.is_empty(), "Should have border segments");

    tracing::info!("E2E panel with border style test PASSED");
}

// =============================================================================
// Scenario 3: Box Styles
// =============================================================================

#[test]
fn e2e_panel_rounded_box() {
    init_test_logging();
    tracing::info!("Starting E2E panel rounded box test");

    let panel = Panel::from_text("Rounded").rounded().width(20);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Rounded box");

    assert!(output.contains(''), "Missing rounded top-left");
    assert!(output.contains(''), "Missing rounded top-right");
    assert!(output.contains(''), "Missing rounded bottom-left");
    assert!(output.contains(''), "Missing rounded bottom-right");

    tracing::info!("E2E panel rounded box test PASSED");
}

#[test]
fn e2e_panel_square_box() {
    init_test_logging();
    tracing::info!("Starting E2E panel square box test");

    let panel = Panel::from_text("Square").square().width(20);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Square box");

    assert!(output.contains(''), "Missing square top-left");
    assert!(output.contains(''), "Missing square top-right");
    assert!(output.contains(''), "Missing square bottom-left");
    assert!(output.contains(''), "Missing square bottom-right");

    tracing::info!("E2E panel square box test PASSED");
}

#[test]
fn e2e_panel_ascii_box() {
    init_test_logging();
    tracing::info!("Starting E2E panel ASCII box test");

    let panel = Panel::from_text("ASCII").ascii().width(20);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "ASCII box");

    assert!(output.contains('+'), "Missing ASCII corner '+'");
    assert!(output.contains('-'), "Missing ASCII horizontal '-'");
    assert!(output.contains('|'), "Missing ASCII vertical '|'");

    // Should NOT contain Unicode box chars
    assert!(!output.contains(''), "Should not have rounded corners");
    assert!(!output.contains(''), "Should not have square corners");

    tracing::info!("E2E panel ASCII box test PASSED");
}

#[test]
fn e2e_panel_double_box() {
    init_test_logging();
    tracing::info!("Starting E2E panel double box test");

    let panel = Panel::from_text("Double").box_style(&DOUBLE).width(20);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Double box");

    assert!(output.contains(''), "Missing double top-left");
    assert!(output.contains(''), "Missing double top-right");
    assert!(output.contains(''), "Missing double bottom-left");
    assert!(output.contains(''), "Missing double bottom-right");
    assert!(output.contains(''), "Missing double horizontal");
    assert!(output.contains(''), "Missing double vertical");

    tracing::info!("E2E panel double box test PASSED");
}

#[test]
fn e2e_panel_heavy_box() {
    init_test_logging();
    tracing::info!("Starting E2E panel heavy box test");

    let panel = Panel::from_text("Heavy").box_style(&HEAVY).width(20);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Heavy box");

    assert!(output.contains(''), "Missing heavy top-left");
    assert!(output.contains(''), "Missing heavy top-right");
    assert!(output.contains(''), "Missing heavy bottom-left");
    assert!(output.contains(''), "Missing heavy bottom-right");
    assert!(output.contains(''), "Missing heavy horizontal");
    assert!(output.contains(''), "Missing heavy vertical");

    tracing::info!("E2E panel heavy box test PASSED");
}

#[test]
fn e2e_panel_minimal_box() {
    init_test_logging();
    tracing::info!("Starting E2E panel minimal box test");

    let panel = Panel::from_text("Minimal").box_style(&MINIMAL).width(20);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Minimal box");

    // Minimal uses spaces for corners
    assert!(output.contains("Minimal"), "Missing content");

    tracing::info!("E2E panel minimal box test PASSED");
}

// =============================================================================
// Scenario 4: Title Alignment
// =============================================================================

#[test]
fn e2e_panel_title_left() {
    init_test_logging();
    tracing::info!("Starting E2E panel title left aligned test");

    let panel = Panel::from_text("Content")
        .title("Left")
        .title_align(JustifyMethod::Left)
        .width(30);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Left-aligned title");

    assert!(output.contains("Left"), "Missing title");

    tracing::info!("E2E panel title left aligned test PASSED");
}

#[test]
fn e2e_panel_title_center() {
    init_test_logging();
    tracing::info!("Starting E2E panel title center aligned test");

    let panel = Panel::from_text("Content")
        .title("Center")
        .title_align(JustifyMethod::Center)
        .width(40);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Center-aligned title");

    assert!(output.contains("Center"), "Missing title");

    tracing::info!("E2E panel title center aligned test PASSED");
}

#[test]
fn e2e_panel_title_right() {
    init_test_logging();
    tracing::info!("Starting E2E panel title right aligned test");

    let panel = Panel::from_text("Content")
        .title("Right")
        .title_align(JustifyMethod::Right)
        .width(30);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Right-aligned title");

    assert!(output.contains("Right"), "Missing title");

    tracing::info!("E2E panel title right aligned test PASSED");
}

// =============================================================================
// Scenario 5: Width and Padding
// =============================================================================

#[test]
fn e2e_panel_fixed_width() {
    init_test_logging();
    tracing::info!("Starting E2E panel fixed width test");

    let panel = Panel::from_text("Short").width(40).expand(true);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Fixed width panel");

    // First line should be exactly 40 characters (including box chars)
    let first_line = output.lines().next().unwrap_or("");
    tracing::debug!(
        first_line_len = first_line.chars().count(),
        "First line length"
    );

    assert!(
        first_line.chars().count() >= 20,
        "Panel should have reasonable width"
    );

    tracing::info!("E2E panel fixed width test PASSED");
}

#[test]
fn e2e_panel_expand_false() {
    init_test_logging();
    tracing::info!("Starting E2E panel expand=false test");

    let panel = Panel::from_text("Hi").expand(false);

    let output = panel.render_plain(80);
    tracing::debug!(output = %output, "Non-expanded panel");

    // Panel should fit content, not expand to 80
    let first_line = output.lines().next().unwrap_or("");
    let line_width = first_line.chars().count();
    tracing::debug!(line_width = line_width, "First line width");

    assert!(line_width < 40, "Non-expanded panel should be narrow");

    tracing::info!("E2E panel expand=false test PASSED");
}

#[test]
fn e2e_panel_with_padding() {
    init_test_logging();
    tracing::info!("Starting E2E panel with padding test");

    let panel = Panel::from_text("Content")
        .padding((2, 1)) // top/bottom, left/right
        .width(30);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Panel with padding");

    // Count lines to verify padding
    let lines: Vec<_> = output.lines().collect();
    tracing::debug!(line_count = lines.len(), "Total lines");

    // Should have: top border + 2 padding + content + 2 padding + bottom border = 7 lines
    assert!(lines.len() >= 5, "Should have padding lines");

    tracing::info!("E2E panel with padding test PASSED");
}

// =============================================================================
// Scenario 6: Wide Characters
// =============================================================================

#[test]
fn e2e_panel_cjk_content() {
    init_test_logging();
    tracing::info!("Starting E2E panel with CJK content test");

    let panel = Panel::from_text("你好世界").title("中文").width(25);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "CJK panel");

    assert!(output.contains("你好世界"), "Missing CJK content");
    assert!(output.contains("中文"), "Missing CJK title");

    tracing::info!("E2E panel with CJK content test PASSED");
}

#[test]
fn e2e_panel_emoji_content() {
    init_test_logging();
    tracing::info!("Starting E2E panel with emoji content test");

    let panel = Panel::from_text("Status: ✓ OK").title("Check").width(25);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Emoji panel");

    assert!(output.contains(""), "Missing checkmark");
    assert!(output.contains("OK"), "Missing text");

    tracing::info!("E2E panel with emoji content test PASSED");
}

// =============================================================================
// Scenario 7: Table in Panel
// =============================================================================

#[test]
fn e2e_panel_containing_table_content() {
    init_test_logging();
    tracing::info!("Starting E2E panel containing table content test");

    // Build a table
    let mut table = Table::new()
        .with_column(Column::new("Key"))
        .with_column(Column::new("Value"));

    table.add_row_cells(["Name", "Alice"]);
    table.add_row_cells(["Age", "30"]);

    // Get table output
    let table_output = table.render_plain(40);

    // Put table in panel
    let panel = Panel::from_text(&table_output).title("User Data").width(45);

    let output = panel.render_plain(60);
    tracing::debug!(output = %output, "Panel with table");

    // Verify both panel and table elements
    assert!(output.contains("User Data"), "Missing panel title");
    assert!(output.contains("Key"), "Missing table header");
    assert!(output.contains("Alice"), "Missing table data");

    tracing::info!("E2E panel containing table content test PASSED");
}

// =============================================================================
// Scenario 8: Edge Cases
// =============================================================================

#[test]
fn e2e_panel_empty_content() {
    init_test_logging();
    tracing::info!("Starting E2E empty panel test");

    let panel = Panel::new(vec![]).width(20);
    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Empty panel");

    // Should still render borders
    assert!(output.contains(''), "Should have top-left corner");
    assert!(output.contains(''), "Should have bottom-right corner");

    tracing::info!("E2E empty panel test PASSED");
}

#[test]
fn e2e_panel_very_long_title() {
    init_test_logging();
    tracing::info!("Starting E2E panel with long title test");

    let panel = Panel::from_text("Short")
        .title("This is a very long title that exceeds the panel width")
        .width(25);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Panel with long title");

    // Should still render without panic
    assert!(output.contains("Short"), "Missing content");

    tracing::info!("E2E panel with long title test PASSED");
}

#[test]
fn e2e_panel_narrow_width() {
    init_test_logging();
    tracing::info!("Starting E2E narrow panel test");

    let panel = Panel::from_text("Content").width(10);

    let output = panel.render_plain(10);
    tracing::debug!(output = %output, "Narrow panel");

    // Should include content even at narrow widths
    assert!(output.contains("Content"), "Missing content");

    tracing::info!("E2E narrow panel test PASSED");
}

#[test]
fn e2e_panel_single_char_content() {
    init_test_logging();
    tracing::info!("Starting E2E single character panel test");

    let panel = Panel::from_text("X").expand(false);

    let output = panel.render_plain(50);
    tracing::debug!(output = %output, "Single char panel");

    assert!(output.contains('X'), "Missing content");

    tracing::info!("E2E single character panel test PASSED");
}

// =============================================================================
// Snapshot Tests
// =============================================================================

#[test]
fn e2e_snapshot_basic_panel() {
    init_test_logging();

    let panel = Panel::from_text("Hello, World!")
        .title("Greeting")
        .width(30);

    let output = panel.render_plain(50);
    insta::assert_snapshot!("e2e_basic_panel", output);
}

#[test]
fn e2e_snapshot_panel_with_subtitle() {
    init_test_logging();

    let panel = Panel::from_text("Main content here")
        .title("Header")
        .subtitle("Footer")
        .width(35);

    let output = panel.render_plain(50);
    insta::assert_snapshot!("e2e_panel_with_subtitle", output);
}

#[test]
fn e2e_snapshot_ascii_panel() {
    init_test_logging();

    let panel = Panel::from_text("ASCII safe!")
        .title("Legacy")
        .ascii()
        .width(25);

    let output = panel.render_plain(50);
    insta::assert_snapshot!("e2e_ascii_panel", output);
}

#[test]
fn e2e_snapshot_double_panel() {
    init_test_logging();

    let panel = Panel::from_text("Important!")
        .title("Alert")
        .box_style(&DOUBLE)
        .width(25);

    let output = panel.render_plain(50);
    insta::assert_snapshot!("e2e_double_panel", output);
}

#[test]
fn e2e_snapshot_heavy_panel() {
    init_test_logging();

    let panel = Panel::from_text("Heavy borders")
        .box_style(&HEAVY)
        .width(25);

    let output = panel.render_plain(50);
    insta::assert_snapshot!("e2e_heavy_panel", output);
}

#[test]
fn e2e_snapshot_multiline_panel() {
    init_test_logging();

    let panel = Panel::from_text("Line 1\nLine 2\nLine 3\nLine 4")
        .title("List")
        .width(20);

    let output = panel.render_plain(50);
    insta::assert_snapshot!("e2e_multiline_panel", output);
}