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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! End-to-end tests for Syntax highlighting (requires `syntax` feature).
//!
//! Verifies syntax highlighting across multiple languages, themes, line numbers,
//! word wrap, background colors, and error handling.

#![cfg(feature = "syntax")]

mod common;

use common::init_test_logging;
use rich_rust::prelude::*;
use rich_rust::renderables::{Syntax, SyntaxError};

// =============================================================================
// Helper: render Syntax to text via Console capture
// =============================================================================

fn render_syntax_to_html(syntax: &Syntax, width: usize) -> String {
    let console = Console::builder()
        .width(width)
        .force_terminal(true)
        .color_system(ColorSystem::TrueColor)
        .markup(false)
        .build();

    console.begin_capture();
    console.print_renderable(syntax);
    console.export_html(true)
}

fn render_syntax_to_text(syntax: &Syntax) -> String {
    let console = Console::builder()
        .width(120)
        .force_terminal(true)
        .color_system(ColorSystem::TrueColor)
        .markup(false)
        .build();

    console.export_renderable_text(syntax)
}

// =============================================================================
// Multiple Languages
// =============================================================================

/// Test: Rust code highlights correctly.
#[test]
fn test_syntax_rust() {
    init_test_logging();

    let code = r#"fn main() {
    println!("Hello, world!");
}"#;
    let syntax = Syntax::new(code, "rust");
    let segments = syntax.render(Some(80)).expect("render");

    let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
    assert!(text.contains("fn"), "Should contain Rust keyword 'fn'");
    assert!(text.contains("main"), "Should contain function name");
    assert!(text.contains("println"), "Should contain macro name");
}

/// Test: Python code highlights correctly.
#[test]
fn test_syntax_python() {
    init_test_logging();

    let code = "def greet(name):\n    return f\"Hello, {name}!\"";
    let syntax = Syntax::new(code, "python");
    let segments = syntax.render(Some(80)).expect("render");

    let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
    assert!(text.contains("def"), "Should contain Python keyword");
    assert!(text.contains("greet"), "Should contain function name");
}

/// Test: JavaScript code highlights correctly.
#[test]
fn test_syntax_javascript() {
    init_test_logging();

    let code = "const add = (a, b) => a + b;\nconsole.log(add(1, 2));";
    let syntax = Syntax::new(code, "javascript");
    let segments = syntax.render(Some(80)).expect("render");

    let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
    assert!(text.contains("const"), "Should contain JS keyword");
    assert!(text.contains("add"), "Should contain variable name");
}

/// Test: HTML code highlights correctly.
#[test]
fn test_syntax_html() {
    init_test_logging();

    let code = "<html>\n<body>\n<h1>Title</h1>\n</body>\n</html>";
    let syntax = Syntax::new(code, "html");
    let segments = syntax.render(Some(80)).expect("render");

    let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
    assert!(text.contains("html"), "Should contain HTML tag");
    assert!(text.contains("Title"), "Should contain content");
}

/// Test: SQL code highlights correctly.
#[test]
fn test_syntax_sql() {
    init_test_logging();

    let code = "SELECT name, age FROM users WHERE age > 18 ORDER BY name;";
    let syntax = Syntax::new(code, "sql");
    let segments = syntax.render(Some(80)).expect("render");

    let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
    assert!(text.contains("SELECT"), "Should contain SQL keyword");
    assert!(text.contains("users"), "Should contain table name");
}

/// Test: YAML code highlights correctly.
#[test]
fn test_syntax_yaml() {
    init_test_logging();

    let code = "[package]\nname = \"test\"\nversion = \"1.0\"";
    let syntax = Syntax::new(code, "yaml");
    let segments = syntax.render(Some(80)).expect("render");

    let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
    assert!(text.contains("package"), "Should contain TOML section");
    assert!(text.contains("name"), "Should contain key");
}

/// Test: Go code highlights correctly.
#[test]
fn test_syntax_go() {
    init_test_logging();

    let code = "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"Hello\")\n}";
    let syntax = Syntax::new(code, "go");
    let segments = syntax.render(Some(80)).expect("render");

    let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
    assert!(text.contains("package"), "Should contain Go keyword");
    assert!(text.contains("func"), "Should contain func keyword");
}

// =============================================================================
// Theme Selection
// =============================================================================

/// Test: available_themes returns non-empty list.
#[test]
fn test_available_themes_non_empty() {
    init_test_logging();

    let themes = Syntax::available_themes();
    assert!(
        !themes.is_empty(),
        "Should have at least one available theme"
    );
}

/// Test: default theme "base16-ocean.dark" is available.
#[test]
fn test_default_theme_available() {
    init_test_logging();

    let themes = Syntax::available_themes();
    assert!(
        themes.iter().any(|t| t == "base16-ocean.dark"),
        "Default theme 'base16-ocean.dark' should be available"
    );
}

/// Test: Rendering with different themes produces different styled output.
#[test]
fn test_different_themes_produce_output() {
    init_test_logging();

    let code = "fn test() { 42 }";
    let themes = Syntax::available_themes();

    // Try rendering with multiple themes
    for theme_name in themes.iter().take(3) {
        let syntax = Syntax::new(code, "rust").theme(theme_name);
        let segments = syntax.render(Some(80)).expect("render");
        assert!(
            !segments.is_empty(),
            "Theme '{}' should produce segments",
            theme_name
        );
    }
}

/// Test: unknown theme returns error.
#[test]
fn test_unknown_theme_error() {
    init_test_logging();

    let syntax = Syntax::new("code", "rust").theme("nonexistent-theme-xyz");
    let result = syntax.render(Some(80));
    assert!(matches!(
        result,
        Err(SyntaxError::UnknownTheme(name)) if name == "nonexistent-theme-xyz"
    ));
}

/// Test: "InspiredGitHub" theme works.
#[test]
fn test_inspired_github_theme() {
    init_test_logging();

    let syntax = Syntax::new("fn main() {}", "rust").theme("InspiredGitHub");
    let segments = syntax.render(Some(80)).expect("render");
    assert!(!segments.is_empty());
}

// =============================================================================
// Line Numbers
// =============================================================================

/// Test: line numbers appear in rendered segments.
#[test]
fn test_line_numbers_enabled() {
    init_test_logging();

    let code = "x = 1\ny = 2\nz = 3";
    let syntax = Syntax::new(code, "python").line_numbers(true);
    let text = render_syntax_to_text(&syntax);
    let mut lines = text.lines();

    // Rich-style gutter: two spaces, line number (right-aligned), trailing space.
    assert!(
        lines.next().expect("first line").starts_with("  1 "),
        "First line should start with line number gutter"
    );
    assert!(
        lines.next().expect("second line").starts_with("  2 "),
        "Second line should start with line number gutter"
    );
    assert!(
        lines.next().expect("third line").starts_with("  3 "),
        "Third line should start with line number gutter"
    );
}

/// Test: line numbers disabled produces fewer segments.
#[test]
fn test_line_numbers_disabled() {
    init_test_logging();

    let code = "x = 1";
    let syntax_no_nums = Syntax::new(code, "python").line_numbers(false);
    let syntax_with_nums = Syntax::new(code, "python").line_numbers(true);

    let text_no = render_syntax_to_text(&syntax_no_nums);
    let text_with = render_syntax_to_text(&syntax_with_nums);

    assert!(
        text_with
            .lines()
            .next()
            .expect("first line")
            .starts_with("  1 "),
        "With line numbers, first line should start with the gutter"
    );
    assert!(
        !text_no
            .lines()
            .next()
            .expect("first line")
            .starts_with("  1 "),
        "Without line numbers, first line should not start with the gutter"
    );
}

/// Test: custom start_line offsets the line numbering.
#[test]
fn test_start_line_offset() {
    init_test_logging();

    let code = "a = 1\nb = 2\nc = 3";
    let syntax = Syntax::new(code, "python")
        .line_numbers(true)
        .start_line(10);
    let segments = syntax.render(Some(80)).expect("render");

    let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
    assert!(text.contains("10"), "Should start numbering at 10");
    assert!(text.contains("11"), "Second line should be 11");
    assert!(text.contains("12"), "Third line should be 12");
}

/// Test: start_line minimum clamps to 1.
#[test]
fn test_start_line_minimum_clamp() {
    init_test_logging();

    let syntax = Syntax::new("x = 1", "python")
        .line_numbers(true)
        .start_line(0);
    let segments = syntax.render(Some(80)).expect("render");

    let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
    // Should clamp to 1
    assert!(text.contains('1'), "Start line 0 should clamp to 1");
}

// =============================================================================
// Line Highlighting (via styled output)
// =============================================================================

/// Test: syntax highlighting produces styled segments (not all plain).
#[test]
fn test_highlighting_produces_styled_segments() {
    init_test_logging();

    let code = "fn main() {\n    let x = 42;\n    println!(\"{}\", x);\n}";
    let syntax = Syntax::new(code, "rust");
    let segments = syntax.render(Some(80)).expect("render");

    // At least some segments should have styles (syntax highlighting)
    let styled_count = segments.iter().filter(|s| s.style.is_some()).count();
    assert!(
        styled_count > 0,
        "Syntax highlighting should produce styled segments, got {styled_count} styled out of {}",
        segments.len()
    );
}

/// Test: HTML export of syntax contains color CSS.
#[test]
fn test_syntax_html_export_has_colors() {
    init_test_logging();

    let code = "fn main() { println!(\"Hello\"); }";
    let syntax = Syntax::new(code, "rust");
    let html = render_syntax_to_html(&syntax, 80);

    // Syntax highlighting should produce CSS color properties
    assert!(
        html.contains("color: #"),
        "HTML export should contain color styles from syntax highlighting"
    );
}

// =============================================================================
// Word Wrap
// =============================================================================

/// Test: word_wrap limits line width.
#[test]
fn test_word_wrap_limits_width() {
    init_test_logging();

    let long_line = "fn very_long_function_name_that_is_quite_extensive_and_should_probably_be_refactored() -> Result<(), Box<dyn std::error::Error>> { Ok(()) }";
    let syntax = Syntax::new(long_line, "rust").word_wrap(Some(40));
    let text = render_syntax_to_text(&syntax);

    // With word wrap, output should have newlines
    let line_count = text.lines().count();
    assert!(
        line_count >= 1,
        "Word wrapped long line should produce output"
    );
}

/// Test: word_wrap None allows full-width rendering.
#[test]
fn test_word_wrap_none() {
    init_test_logging();

    let code = "short";
    let syntax = Syntax::new(code, "python").word_wrap(None);
    let segments = syntax.render(Some(120)).expect("render");

    let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
    assert!(text.contains("short"));
}

// =============================================================================
// Background Colors
// =============================================================================

/// Test: custom background color is applied to segments.
#[test]
fn test_background_color_override() {
    init_test_logging();

    let syntax = Syntax::new("x = 1", "python").background_color(Color::parse("blue").unwrap());
    let segments = syntax.render(Some(80)).expect("render");

    // At least some segments should have a background color style
    let has_bg = segments.iter().any(|s| {
        s.style
            .as_ref()
            .map(|style| style.bgcolor.is_some())
            .unwrap_or(false)
    });
    assert!(has_bg, "Background color override should apply to segments");
}

/// Test: default syntax (no background override) still renders.
#[test]
fn test_default_background() {
    init_test_logging();

    let syntax = Syntax::new("fn main() {}", "rust");
    let segments = syntax.render(Some(80)).expect("render");
    assert!(!segments.is_empty());
}

// =============================================================================
// Unknown/Invalid Languages
// =============================================================================

/// Test: unknown language returns error.
#[test]
fn test_unknown_language_error() {
    init_test_logging();

    let syntax = Syntax::new("code", "nonexistent-language-xyz");
    let result = syntax.render(Some(80));
    assert!(matches!(
        result,
        Err(SyntaxError::UnknownLanguage(lang)) if lang == "nonexistent-language-xyz"
    ));
}

/// Test: python language works as a reliable language for plain content.
#[test]
fn test_reliable_language_renders_plain_content() {
    init_test_logging();

    let syntax = Syntax::new("plain text content", "python");
    let segments = syntax.render(Some(80)).expect("render");

    let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
    assert!(text.contains("plain text content"));
}

/// Test: available_languages returns non-empty list.
#[test]
fn test_available_languages_non_empty() {
    init_test_logging();

    let languages = Syntax::available_languages();
    assert!(!languages.is_empty(), "Should have available languages");
}

// =============================================================================
// Large File Rendering
// =============================================================================

/// Test: large code block renders without error.
#[test]
fn test_large_code_block() {
    init_test_logging();

    let mut code = String::new();
    for i in 0..100 {
        code.push_str(&format!("fn func_{i}() -> i32 {{ {i} }}\n"));
    }

    let syntax = Syntax::new(&code, "rust").line_numbers(true);
    let segments = syntax.render(Some(80)).expect("render");
    assert!(!segments.is_empty(), "100-line code block should render");

    let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
    assert!(text.contains("func_0"), "Should contain first function");
    assert!(text.contains("func_99"), "Should contain last function");
    assert!(text.contains("100"), "Should show line 100");
}

/// Test: multiline code with various Rust constructs.
#[test]
fn test_complex_rust_code() {
    init_test_logging();

    let code = r#"use std::collections::HashMap;

/// A documentation comment
pub struct Config {
    settings: HashMap<String, String>,
}

impl Config {
    pub fn new() -> Self {
        Self {
            settings: HashMap::new(),
        }
    }

    pub fn get(&self, key: &str) -> Option<&String> {
        self.settings.get(key)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_new() {
        let config = Config::new();
        assert!(config.get("missing").is_none());
    }
}
"#;

    let syntax = Syntax::new(code, "rust")
        .line_numbers(true)
        .theme("base16-ocean.dark");
    let segments = syntax.render(Some(100)).expect("render");

    let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
    assert!(text.contains("Config"), "Should contain struct name");
    assert!(text.contains("HashMap"), "Should contain used types");
    assert!(text.contains("pub"), "Should contain visibility modifier");
}

// =============================================================================
// SyntaxError Display
// =============================================================================

/// Test: SyntaxError display messages.
#[test]
fn test_syntax_error_display() {
    init_test_logging();

    let err = SyntaxError::UnknownLanguage("foobar".into());
    assert_eq!(err.to_string(), "Unknown language: foobar");

    let err = SyntaxError::UnknownTheme("badtheme".into());
    assert_eq!(err.to_string(), "Unknown theme: badtheme");

    let err = SyntaxError::IoError("file not found".into());
    assert_eq!(err.to_string(), "IO error: file not found");
}

/// Test: SyntaxError implements std::error::Error.
#[test]
fn test_syntax_error_is_std_error() {
    init_test_logging();

    let err: Box<dyn std::error::Error> = Box::new(SyntaxError::UnknownLanguage("test".into()));
    assert!(err.source().is_none());
    assert!(!err.to_string().is_empty());
}

// =============================================================================
// Builder Pattern
// =============================================================================

/// Test: full builder chain works.
#[test]
fn test_full_builder_chain() {
    init_test_logging();

    let syntax = Syntax::new("let x = 1;", "rust")
        .line_numbers(true)
        .start_line(5)
        .theme("base16-ocean.dark")
        .indent_guides(true)
        .tab_size(2)
        .word_wrap(Some(60))
        .padding(1, 2);

    let segments = syntax.render(Some(80)).expect("render");
    assert!(!segments.is_empty());
}

/// Test: plain_text extraction.
#[test]
fn test_plain_text_extraction() {
    init_test_logging();

    let code = "fn hello() {}";
    let syntax = Syntax::new(code, "rust");
    let plain = syntax.plain_text();
    assert!(
        plain.contains("fn hello()"),
        "Plain text should contain the code"
    );
}

// =============================================================================
// Console Integration: Renderable Trait
// =============================================================================

/// Test: Syntax renders through Console.print_renderable.
#[test]
fn test_syntax_console_integration() {
    init_test_logging();

    let console = Console::builder()
        .width(80)
        .force_terminal(true)
        .color_system(ColorSystem::TrueColor)
        .build();

    let syntax = Syntax::new("fn main() {}", "rust");

    console.begin_capture();
    console.print_renderable(&syntax);
    let html = console.export_html(true);

    assert!(
        html.contains("main"),
        "Console render should include code content"
    );
}

/// Test: Syntax export to SVG works.
#[test]
fn test_syntax_svg_export() {
    init_test_logging();

    let console = Console::builder()
        .width(80)
        .force_terminal(true)
        .color_system(ColorSystem::TrueColor)
        .build();

    let syntax = Syntax::new("print('hello')", "python");

    console.begin_capture();
    console.print_renderable(&syntax);
    let svg = console.export_svg(true);

    assert!(svg.contains("<svg"), "Should produce SVG output");
    assert!(svg.contains("hello"), "SVG should contain code content");
}

// =============================================================================
// Tab Size
// =============================================================================

/// Test: tab_size minimum clamps to 1.
#[test]
fn test_tab_size_minimum() {
    init_test_logging();

    let syntax = Syntax::new("\tindented", "python").tab_size(0);
    let segments = syntax.render(Some(80)).expect("render");
    assert!(!segments.is_empty());
}

/// Test: different tab sizes produce different output widths.
#[test]
fn test_tab_size_affects_width() {
    init_test_logging();

    let code = "\ttab here";
    let syntax_small = Syntax::new(code, "python").tab_size(2);
    let syntax_large = Syntax::new(code, "python").tab_size(8);

    let text_small = render_syntax_to_text(&syntax_small);
    let text_large = render_syntax_to_text(&syntax_large);

    // Larger tab size should produce more spaces
    assert!(
        text_large.len() >= text_small.len(),
        "Larger tab size should produce wider output"
    );
}

// =============================================================================
// CRLF Handling
// =============================================================================

/// Test: Windows line endings (CRLF) are handled correctly.
#[test]
fn test_crlf_handling() {
    init_test_logging();

    let code = "x = 1\r\ny = 2\r\nz = 3";
    let syntax = Syntax::new(code, "python").line_numbers(true);
    let segments = syntax.render(Some(80)).expect("render");

    let text: String = segments.iter().map(|s| s.text.as_ref()).collect();
    // Should render all three lines, stripping \r
    assert!(!text.contains('\r'), "CRLF should be stripped");
    assert!(text.contains("1"), "Should contain line 1 content");
    assert!(text.contains("2"), "Should contain line 2 content");
    assert!(text.contains("3"), "Should contain line 3 content");
}