gilt 1.2.0

Fast, beautiful terminal formatting for Rust — styles, tables, trees, syntax highlighting, progress bars, markdown.
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
//! Tests extracted from markdown.rs for readability.
//! Wired in via `#[path = ...] mod tests;`.

use super::*;
use crate::cells::cell_len;

fn make_console(width: usize) -> Console {
    Console::builder()
        .width(width)
        .force_terminal(true)
        .no_color(true)
        .markup(false)
        .build()
}

fn render_markdown(console: &Console, md: &Markdown) -> String {
    let opts = console.options();
    let segments = md.gilt_console(console, &opts);
    segments.iter().map(|s| s.text.as_str()).collect()
}

fn render_segments(console: &Console, md: &Markdown) -> Vec<Segment> {
    let opts = console.options();
    md.gilt_console(console, &opts)
}

// -- Simple paragraph ---------------------------------------------------

#[test]
fn test_simple_paragraph() {
    let console = make_console(80);
    let md = Markdown::new("Hello, world!");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Hello, world!"));
}

#[test]
fn test_two_paragraphs() {
    let console = make_console(80);
    let md = Markdown::new("First paragraph.\n\nSecond paragraph.");
    let output = render_markdown(&console, &md);
    assert!(output.contains("First paragraph."));
    assert!(output.contains("Second paragraph."));
}

// -- Headings -----------------------------------------------------------

#[test]
fn test_heading_h1() {
    let console = make_console(80);
    let md = Markdown::new("# Heading 1");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Heading 1"));
}

#[test]
fn test_heading_h2() {
    let console = make_console(80);
    let md = Markdown::new("## Heading 2");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Heading 2"));
}

#[test]
fn test_heading_h3() {
    let console = make_console(80);
    let md = Markdown::new("### Heading 3");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Heading 3"));
}

#[test]
fn test_heading_h4() {
    let console = make_console(80);
    let md = Markdown::new("#### Heading 4");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Heading 4"));
}

#[test]
fn test_heading_h5() {
    let console = make_console(80);
    let md = Markdown::new("##### Heading 5");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Heading 5"));
}

#[test]
fn test_heading_h6() {
    let console = make_console(80);
    let md = Markdown::new("###### Heading 6");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Heading 6"));
}

#[test]
fn test_headings_have_appropriate_styles() {
    let console = make_console(80);

    // H1 should produce a rule underline
    let md = Markdown::new("# Title");
    let output = render_markdown(&console, &md);
    // H1 and H2 get underlines (Rule characters)
    assert!(output.contains("Title"));
    // The rule character should be present
    assert!(output.contains('\u{2501}') || output.contains('-'));
}

#[test]
fn test_h1_has_rule_underline() {
    let console = make_console(40);
    let md = Markdown::new("# Big Title");
    let segments = render_segments(&console, &md);
    let text: String = segments.iter().map(|s| s.text.as_str()).collect();
    // Should contain the heading text and a rule line
    assert!(text.contains("Big Title"));
    assert!(text.contains('\u{2501}'));
}

#[test]
fn test_h2_has_rule_underline() {
    let console = make_console(40);
    let md = Markdown::new("## Sub Title");
    let segments = render_segments(&console, &md);
    let text: String = segments.iter().map(|s| s.text.as_str()).collect();
    assert!(text.contains("Sub Title"));
    assert!(text.contains('\u{2501}'));
}

// -- Bold text ----------------------------------------------------------

#[test]
fn test_bold_text() {
    let console = make_console(80);
    let md = Markdown::new("This is **bold** text.");
    let segments = render_segments(&console, &md);
    let text: String = segments.iter().map(|s| s.text.as_str()).collect();
    assert!(text.contains("bold"));
    assert!(text.contains("This is"));
    assert!(text.contains("text."));

    // Check that "bold" segment has a style with bold attribute
    let bold_seg = segments.iter().find(|s| s.text == "bold");
    assert!(bold_seg.is_some(), "Should have a segment with text 'bold'");
    if let Some(seg) = bold_seg {
        assert!(seg.style().is_some(), "Bold segment should have a style");
    }
}

// -- Italic text --------------------------------------------------------

#[test]
fn test_italic_text() {
    let console = make_console(80);
    let md = Markdown::new("This is *italic* text.");
    let segments = render_segments(&console, &md);
    let text: String = segments.iter().map(|s| s.text.as_str()).collect();
    assert!(text.contains("italic"));

    let italic_seg = segments.iter().find(|s| s.text == "italic");
    assert!(
        italic_seg.is_some(),
        "Should have a segment with text 'italic'"
    );
    if let Some(seg) = italic_seg {
        assert!(seg.style().is_some(), "Italic segment should have a style");
    }
}

// -- Bold + italic combined ---------------------------------------------

#[test]
fn test_bold_italic_combined() {
    let console = make_console(80);
    let md = Markdown::new("This is ***bold and italic*** text.");
    let segments = render_segments(&console, &md);
    let text: String = segments.iter().map(|s| s.text.as_str()).collect();
    assert!(text.contains("bold and italic"));

    // The combined text should have a style
    let combined_seg = segments.iter().find(|s| s.text.contains("bold and italic"));
    assert!(combined_seg.is_some());
    if let Some(seg) = combined_seg {
        assert!(
            seg.style().is_some(),
            "Bold+italic segment should have a style"
        );
    }
}

// -- Inline code --------------------------------------------------------

#[test]
fn test_inline_code() {
    let console = make_console(80);
    let md = Markdown::new("Use `println!` to print.");
    let segments = render_segments(&console, &md);
    let text: String = segments.iter().map(|s| s.text.as_str()).collect();
    assert!(text.contains("println!"));

    let code_seg = segments.iter().find(|s| s.text == "println!");
    assert!(
        code_seg.is_some(),
        "Should have a segment with inline code text"
    );
    if let Some(seg) = code_seg {
        assert!(seg.style().is_some(), "Inline code should have a style");
    }
}

// -- Code blocks (fenced) -----------------------------------------------

#[test]
fn test_code_block() {
    let console = make_console(80);
    let md = Markdown::new("```\nfn main() {\n    println!(\"hello\");\n}\n```");
    let output = render_markdown(&console, &md);
    assert!(output.contains("fn main()"));
    assert!(output.contains("println!"));
}

#[test]
fn test_code_block_with_language() {
    let console = make_console(80);
    let md = Markdown::new("```rust\nlet x = 42;\n```");
    let output = render_markdown(&console, &md);
    assert!(output.contains("let x = 42;"));
}

// -- Links with URLs ----------------------------------------------------

#[test]
fn test_link_with_url() {
    let console = make_console(80);
    let md = Markdown::new("[Rust](https://www.rust-lang.org)");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Rust"));
    assert!(output.contains("https://www.rust-lang.org"));
}

#[test]
fn test_link_without_url_display() {
    let console = make_console(80);
    let md = Markdown::new("[Rust](https://www.rust-lang.org)").with_hyperlinks(false);
    let output = render_markdown(&console, &md);
    assert!(output.contains("Rust"));
    assert!(!output.contains("https://www.rust-lang.org"));
}

// -- Unordered lists (bullets) ------------------------------------------

#[test]
fn test_unordered_list() {
    let console = make_console(80);
    let md = Markdown::new("- Item 1\n- Item 2\n- Item 3");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Item 1"));
    assert!(output.contains("Item 2"));
    assert!(output.contains("Item 3"));
    // Should have bullet characters
    assert!(output.contains('\u{2022}'));
}

// -- Ordered lists (numbers) --------------------------------------------

#[test]
fn test_ordered_list() {
    let console = make_console(80);
    let md = Markdown::new("1. First\n2. Second\n3. Third");
    let output = render_markdown(&console, &md);
    assert!(output.contains("First"));
    assert!(output.contains("Second"));
    assert!(output.contains("Third"));
    assert!(output.contains("1."));
    assert!(output.contains("2."));
    assert!(output.contains("3."));
}

// -- Nested lists -------------------------------------------------------

#[test]
fn test_nested_list() {
    let console = make_console(80);
    let md = Markdown::new("- Outer\n  - Inner 1\n  - Inner 2\n- Outer 2");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Outer"));
    assert!(output.contains("Inner 1"));
    assert!(output.contains("Inner 2"));
    assert!(output.contains("Outer 2"));
}

// -- Block quotes -------------------------------------------------------

#[test]
fn test_block_quote() {
    let console = make_console(80);
    let md = Markdown::new("> This is a quote.");
    let output = render_markdown(&console, &md);
    assert!(output.contains("This is a quote."));
    // Should have the vertical bar character for block quotes
    assert!(output.contains('\u{2502}'));
}

// -- Horizontal rules ---------------------------------------------------

#[test]
fn test_horizontal_rule() {
    let console = make_console(40);
    let md = Markdown::new("Above\n\n---\n\nBelow");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Above"));
    assert!(output.contains("Below"));
    // Should contain rule characters
    assert!(output.contains('\u{2501}'));
}

// -- Mixed content ------------------------------------------------------

#[test]
fn test_mixed_content() {
    let console = make_console(80);
    let md = Markdown::new("# Title\n\nA paragraph.\n\n- Item 1\n- Item 2\n\n```\ncode\n```");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Title"));
    assert!(output.contains("A paragraph."));
    assert!(output.contains("Item 1"));
    assert!(output.contains("Item 2"));
    assert!(output.contains("code"));
}

// -- Empty markdown -----------------------------------------------------

#[test]
fn test_empty_markdown() {
    let console = make_console(80);
    let md = Markdown::new("");
    let output = render_markdown(&console, &md);
    assert!(output.is_empty() || output.trim().is_empty());
}

// -- Tables -------------------------------------------------------------

#[test]
fn test_table() {
    let console = make_console(80);
    let md = Markdown::new("| Name | Age |\n|------|-----|\n| Alice | 30 |\n| Bob | 25 |");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Name"));
    assert!(output.contains("Age"));
    assert!(output.contains("Alice"));
    assert!(output.contains("30"));
    assert!(output.contains("Bob"));
    assert!(output.contains("25"));
}

#[test]
fn test_table_with_alignment() {
    let console = make_console(80);
    let md = Markdown::new("| Left | Center | Right |\n|:-----|:------:|------:|\n| L | C | R |");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Left"));
    assert!(output.contains("Center"));
    assert!(output.contains("Right"));
}

// -- Renderable trait integration ---------------------------------------

#[test]
fn test_renderable_integration() {
    let console = Console::builder()
        .width(60)
        .force_terminal(true)
        .no_color(true)
        .markup(false)
        .build();

    let md = Markdown::new("Hello, **world**!");
    let opts = console.options();
    let segments = md.gilt_console(&console, &opts);

    // Should produce non-empty output
    assert!(!segments.is_empty());

    let text: String = segments.iter().map(|s| s.text.as_str()).collect();
    assert!(text.contains("Hello,"));
    assert!(text.contains("world"));
}

#[test]
fn test_renderable_through_console_render() {
    let console = Console::builder()
        .width(60)
        .force_terminal(true)
        .no_color(true)
        .markup(false)
        .build();

    let md = Markdown::new("# Title\n\nParagraph text.");
    let segments = console.render(&md, None);
    let text: String = segments.iter().map(|s| s.text.as_str()).collect();
    assert!(text.contains("Title"));
    assert!(text.contains("Paragraph text."));
}

// -- Constructor / builder pattern --------------------------------------

#[test]
fn test_constructor_defaults() {
    let md = Markdown::new("test");
    assert_eq!(md.markup, "test");
    assert_eq!(md.code_theme, "monokai");
    assert!(md.inline_code_lexer.is_none());
    assert!(md.inline_code_theme.is_none());
    assert!(md.hyperlinks);
    assert!(md.justify.is_none());
}

#[test]
fn test_builder_code_theme() {
    let md = Markdown::new("test").with_code_theme("dracula");
    assert_eq!(md.code_theme, "dracula");
}

#[test]
fn test_builder_hyperlinks() {
    let md = Markdown::new("test").with_hyperlinks(false);
    assert!(!md.hyperlinks);
}

#[test]
fn test_builder_justify() {
    let md = Markdown::new("test").with_justify(JustifyMethod::Center);
    assert_eq!(md.justify, Some(JustifyMethod::Center));
}

// -- Strikethrough text -------------------------------------------------

#[test]
fn test_strikethrough() {
    let console = make_console(80);
    let md = Markdown::new("This is ~~deleted~~ text.");
    let segments = render_segments(&console, &md);
    let text: String = segments.iter().map(|s| s.text.as_str()).collect();
    assert!(text.contains("deleted"));
}

// -- Soft and hard breaks -----------------------------------------------

#[test]
fn test_soft_break() {
    let console = make_console(80);
    let md = Markdown::new("Line one\nLine two");
    let output = render_markdown(&console, &md);
    // Soft break should become a space
    assert!(output.contains("Line one"));
    assert!(output.contains("Line two"));
}

#[test]
fn test_hard_break() {
    let console = make_console(80);
    let md = Markdown::new("Line one  \nLine two");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Line one"));
    assert!(output.contains("Line two"));
}

// -- Multiple headings --------------------------------------------------

#[test]
fn test_all_heading_levels() {
    let console = make_console(80);
    let md = Markdown::new("# H1\n\n## H2\n\n### H3\n\n#### H4\n\n##### H5\n\n###### H6");
    let output = render_markdown(&console, &md);
    assert!(output.contains("H1"));
    assert!(output.contains("H2"));
    assert!(output.contains("H3"));
    assert!(output.contains("H4"));
    assert!(output.contains("H5"));
    assert!(output.contains("H6"));
}

// -- Width constraints --------------------------------------------------

#[test]
fn test_narrow_width() {
    let console = make_console(20);
    let md = Markdown::new("This is a paragraph with enough text to wrap.");
    let output = render_markdown(&console, &md);
    assert!(output.contains("This"));
    // Check that output lines are within the width constraint
    for line in output.split('\n') {
        if !line.is_empty() {
            assert!(
                cell_len(line) <= 20,
                "Line exceeds width: '{}' ({} cells)",
                line,
                cell_len(line)
            );
        }
    }
}

// -- Code block in panel ------------------------------------------------

#[test]
fn test_code_block_has_panel_border() {
    let console = make_console(40);
    let md = Markdown::new("```\nhello\n```");
    let output = render_markdown(&console, &md);
    // Panel uses HEAVY box chars
    assert!(
        output.contains('\u{2501}') || output.contains('\u{2503}') || output.contains('\u{250F}'),
        "Code block should be wrapped in a panel border"
    );
}

// -- Multiple items with inline formatting ------------------------------

#[test]
fn test_list_with_inline_formatting() {
    let console = make_console(80);
    let md = Markdown::new("- **Bold item**\n- *Italic item*\n- `Code item`");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Bold item"));
    assert!(output.contains("Italic item"));
    assert!(output.contains("Code item"));
}

// -- Edge case: only whitespace -----------------------------------------

#[test]
fn test_whitespace_only() {
    let console = make_console(80);
    let md = Markdown::new("   \n\n   ");
    let output = render_markdown(&console, &md);
    assert!(output.trim().is_empty());
}

// -- Blockquote with multiple paragraphs --------------------------------

#[test]
fn test_blockquote_multiple_paragraphs() {
    let console = make_console(80);
    let md = Markdown::new("> First quote.\n>\n> Second quote.");
    let output = render_markdown(&console, &md);
    assert!(output.contains("First quote."));
    assert!(output.contains("Second quote."));
}

// -- Image as link-like -------------------------------------------------

#[test]
fn test_image() {
    let console = make_console(80);
    let md = Markdown::new("![Alt text](https://example.com/image.png)");
    let output = render_markdown(&console, &md);
    assert!(output.contains("Alt text"));
    assert!(output.contains("https://example.com/image.png"));
}

// -- Verify output ends with newline ------------------------------------

#[test]
fn test_output_has_trailing_content() {
    let console = make_console(80);
    let md = Markdown::new("Hello");
    let segments = render_segments(&console, &md);
    assert!(!segments.is_empty());
}

#[test]
fn test_display_trait() {
    let md = Markdown::new("# Hello\n\nWorld");
    let s = format!("{}", md);
    assert!(!s.is_empty());
}

// -- Inline code in table cells (Rich v15.0.0 regression guard) ----------

#[test]
fn inline_code_in_table_cell_is_rendered() {
    // Regression guard: inline code inside a table cell must not be dropped.
    let console = make_console(80);
    let md = Markdown::new("| Col | Code |\n|---|---|\n| a | `foo` |");
    let output = render_markdown(&console, &md);
    assert!(
        output.contains("foo"),
        "Expected 'foo' to appear in table output, got: {:?}",
        output
    );
}

#[test]
fn inline_code_in_table_cell_keeps_style() {
    // The inline-code segment inside a table cell must carry the
    // markdown.code style (rendered as a styled segment, not plain text).
    let console = make_console(80);
    let md = Markdown::new("| Col | Code |\n|---|---|\n| a | `foo` |");
    let segments = render_segments(&console, &md);

    let text: String = segments.iter().map(|s| s.text.as_str()).collect();
    assert!(text.contains("foo"), "Expected 'foo' in output");

    let code_seg = segments.iter().find(|s| s.text.contains("foo"));
    assert!(code_seg.is_some(), "Expected a segment containing 'foo'");
    assert!(
        code_seg.unwrap().style.is_some(),
        "Inline code in table cell must carry a style"
    );
}

#[test]
fn inline_code_outside_table_still_works() {
    // Regression guard: inline code outside tables must remain unaffected.
    let console = make_console(80);
    let md = Markdown::new("Use `println!` to print.");
    let segments = render_segments(&console, &md);
    let text: String = segments.iter().map(|s| s.text.as_str()).collect();
    assert!(text.contains("println!"));

    let code_seg = segments.iter().find(|s| s.text == "println!");
    assert!(
        code_seg.is_some(),
        "Should have a segment with text 'println!'"
    );
    assert!(
        code_seg.unwrap().style.is_some(),
        "Inline code outside table should still have a style"
    );
}

#[test]
fn inline_code_between_text_in_table_cell_preserves_order() {
    // Ordering guard: inline code sandwiched between plain text in a cell
    // must appear at its correct position, not appended after surrounding text.
    let console = make_console(80);
    let md = Markdown::new("| x |\n|---|\n| pre `mid` post |");
    let output = render_markdown(&console, &md);
    let pre_pos = output.find("pre");
    let mid_pos = output.find("mid");
    let post_pos = output.find("post");
    assert!(
        pre_pos.is_some() && mid_pos.is_some() && post_pos.is_some(),
        "Expected 'pre', 'mid', and 'post' in output, got: {:?}",
        output
    );
    assert!(
        pre_pos.unwrap() < mid_pos.unwrap() && mid_pos.unwrap() < post_pos.unwrap(),
        "Expected order pre < mid < post, got positions: pre={:?} mid={:?} post={:?}",
        pre_pos,
        mid_pos,
        post_pos
    );
}