gilt 2.3.1

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
//! Tests for the `Image` renderable (TDD: written before implementation).
//!
//! Run with: `cargo nextest run --all-features -E 'test(image)'`

#[cfg(test)]
mod tests {
    use crate::color::Color;
    use crate::console::Console;
    use crate::console_caps::ConsoleCapabilities;
    use crate::image::Image;

    // -----------------------------------------------------------------------
    // Helper: build a 2×2 RGBA image (red, green, blue, white pixels).
    //   Layout: [red, green] on top row, [blue, white] on bottom row
    //   RGBA bytes: R=255 G=0 B=0 A=255, R=0 G=255 B=0 A=255,
    //               R=0 G=0 B=255 A=255, R=255 G=255 B=255 A=255
    // -----------------------------------------------------------------------
    fn rgba_2x2() -> Vec<u8> {
        vec![
            255, 0, 0, 255, // top-left:  red
            0, 255, 0, 255, // top-right: green
            0, 0, 255, 255, // bot-left:  blue
            255, 255, 255, 255, // bot-right: white
        ]
    }

    // -----------------------------------------------------------------------
    // RED TEST 1 — halfblock core (no `image` crate dep required)
    //
    // A 2-wide × 2-tall pixel grid with width(2) target cells.
    // Each cell represents 1 column × 2 rows of pixels → 2 cells total.
    // halfblock: cell 0 → fg=red  bg=blue  (▀ with ESC[38;2;255;0;0m fg, ESC[48;2;0;0;255m bg)
    //            cell 1 → fg=green bg=white (▀ …)
    // Then a newline.
    // -----------------------------------------------------------------------
    #[test]
    fn halfblock_output_contains_upper_half_block_and_truecolor_escapes() {
        let img = Image::from_rgba(2, 2, rgba_2x2()).width(2);

        let mut console = Console::builder()
            .no_color(false)
            .force_terminal(true)
            .color_system("truecolor")
            .width(80)
            .build();

        // Force kitty=false AND iterm=false so halfblock is used regardless of the
        // test runner's environment (the developer may be inside ghostty/kitty/iTerm).
        console.set_capabilities(ConsoleCapabilities {
            kitty: false,
            iterm: false,
            ..console.capabilities().clone()
        });

        console.begin_capture();
        console.print(&img);
        let output = console.end_capture();

        // halfblock character must appear
        assert!(
            output.contains('\u{2580}'),
            "halfblock path must emit \u{2580}; got: {:?}",
            output
        );
        // truecolor fg SGR params for red pixel (top-left) — 38;2;255;0;0
        // The console may combine fg+bg into one SGR sequence, so we search
        // for the SGR parameter substring rather than the full standalone sequence.
        assert!(
            output.contains("38;2;255;0;0"),
            "expected fg=red truecolor params; got: {:?}",
            output
        );
        // truecolor bg SGR params for blue pixel (bottom-left) — 48;2;0;0;255
        assert!(
            output.contains("48;2;0;0;255"),
            "expected bg=blue truecolor params; got: {:?}",
            output
        );
        // truecolor fg SGR params for green pixel (top-right) — 38;2;0;255;0
        assert!(
            output.contains("38;2;0;255;0"),
            "expected fg=green truecolor params; got: {:?}",
            output
        );
        // truecolor bg SGR params for white pixel (bottom-right) — 48;2;255;255;255
        assert!(
            output.contains("48;2;255;255;255"),
            "expected bg=white truecolor params; got: {:?}",
            output
        );
        // NO Kitty APC — kitty was forced off
        assert!(
            !output.contains("\x1b_G"),
            "halfblock path must NOT emit Kitty APC"
        );
    }

    // -----------------------------------------------------------------------
    // Helper: build a single-cell (1 col × 2 rows of pixels) halfblock console.
    // One `▀` cell → fg = top pixel, bg = bottom pixel, so SGR params are easy
    // to assert. Returns the captured ANSI output.
    // -----------------------------------------------------------------------
    fn render_1x2_halfblock(top: [u8; 4], bottom: [u8; 4], background: Option<Color>) -> String {
        let mut rgba = Vec::with_capacity(8);
        rgba.extend_from_slice(&top);
        rgba.extend_from_slice(&bottom);
        let mut img = Image::from_rgba(1, 2, rgba).width(1);
        if let Some(bg) = background {
            img = img.with_background(bg);
        }

        let mut console = Console::builder()
            .no_color(false)
            .force_terminal(true)
            .color_system("truecolor")
            .width(80)
            .build();
        console.set_capabilities(ConsoleCapabilities {
            kitty: false,
            iterm: false,
            ..console.capabilities().clone()
        });

        console.begin_capture();
        console.print(&img);
        console.end_capture()
    }

    // -----------------------------------------------------------------------
    // Alpha compositing — semi-transparent red (a=128) over a WHITE background
    // composites to a pink-ish RGB, for BOTH the upper (fg) and lower (bg) cell
    // pixels. The emitted truecolor SGR params must be the blended value, not
    // pure red and not black.
    //   out = src*a/255 + bg*(255-a)/255  →  R: 255, G/B: ~127  → (255,127,127)
    // -----------------------------------------------------------------------
    #[test]
    fn halfblock_alpha_composites_semitransparent_over_white() {
        let red_half = [255, 0, 0, 128];
        let output = render_1x2_halfblock(red_half, red_half, Some(Color::from_rgb(255, 255, 255)));

        // fg (upper pixel) blended to pink
        assert!(
            output.contains("38;2;255;127;127"),
            "expected fg blended to pink (255,127,127); got: {:?}",
            output
        );
        // bg (lower pixel) blended to pink
        assert!(
            output.contains("48;2;255;127;127"),
            "expected bg blended to pink (255,127,127); got: {:?}",
            output
        );
        // NOT pure red (alpha ignored) …
        assert!(
            !output.contains("38;2;255;0;0"),
            "must not emit pure red — alpha must be composited; got: {:?}",
            output
        );
        // … and NOT blended against black (the old behaviour).
        assert!(
            !output.contains("38;2;128;0;0"),
            "must not blend against black; background is white; got: {:?}",
            output
        );
    }

    // -----------------------------------------------------------------------
    // Regression guard — fully-opaque pixels (a=255) are unchanged by
    // compositing. fg=red, bg=blue, default (black) background.
    // -----------------------------------------------------------------------
    #[test]
    fn halfblock_opaque_pixels_unchanged_by_compositing() {
        let output = render_1x2_halfblock([255, 0, 0, 255], [0, 0, 255, 255], None);
        assert!(
            output.contains("38;2;255;0;0"),
            "opaque fg red must be unchanged; got: {:?}",
            output
        );
        assert!(
            output.contains("48;2;0;0;255"),
            "opaque bg blue must be unchanged; got: {:?}",
            output
        );
    }

    // -----------------------------------------------------------------------
    // `with_background` changes the composite result. Same a=128 red pixel:
    //   default (black) bg  → (128,0,0)
    //   white bg            → (255,127,127)
    // -----------------------------------------------------------------------
    #[test]
    fn with_background_changes_composite_result() {
        let red_half = [255, 0, 0, 128];

        // Default background is opaque black.
        let on_black = render_1x2_halfblock(red_half, red_half, None);
        assert!(
            on_black.contains("38;2;128;0;0"),
            "a=128 red over black → (128,0,0); got: {:?}",
            on_black
        );

        // White background composites to a different (pink) value.
        let on_white =
            render_1x2_halfblock(red_half, red_half, Some(Color::from_rgb(255, 255, 255)));
        assert!(
            on_white.contains("38;2;255;127;127"),
            "a=128 red over white → (255,127,127); got: {:?}",
            on_white
        );
        assert!(
            !on_white.contains("38;2;128;0;0"),
            "white-background result must differ from black-background result; got: {:?}",
            on_white
        );
    }

    // -----------------------------------------------------------------------
    // Fully-transparent pixels (a=0) yield exactly the background colour, for
    // both the upper and lower cell pixels.
    // -----------------------------------------------------------------------
    #[test]
    fn halfblock_fully_transparent_yields_background() {
        // Red source, fully transparent, over a blue background.
        let transparent_red = [255, 0, 0, 0];
        let output = render_1x2_halfblock(
            transparent_red,
            transparent_red,
            Some(Color::from_rgb(0, 0, 255)),
        );
        assert!(
            output.contains("38;2;0;0;255"),
            "fg a=0 must yield the background colour (blue); got: {:?}",
            output
        );
        assert!(
            output.contains("48;2;0;0;255"),
            "bg a=0 must yield the background colour (blue); got: {:?}",
            output
        );
        assert!(
            !output.contains("255;0;0"),
            "the transparent red source must not appear; got: {:?}",
            output
        );
    }

    // -----------------------------------------------------------------------
    // RED TEST 2 — Kitty path
    //
    // Build a console whose capabilities have kitty=true and is NOT recording.
    // Rendering Image::from_rgba must produce a segment whose text contains
    // the Kitty APC introducer \x1b_G and the base64 of the raw RGBA bytes.
    // -----------------------------------------------------------------------
    #[test]
    fn kitty_path_emits_apc_and_base64_of_pixels() {
        let rgba = rgba_2x2();
        let img = Image::from_rgba(2, 2, rgba.clone()).width(2);

        // Build a console with kitty capability forced
        let mut console = Console::builder()
            .no_color(false)
            .force_terminal(true)
            .color_system("truecolor")
            .width(80)
            .build();

        // Override capabilities with kitty=true after build
        console.set_capabilities(ConsoleCapabilities {
            kitty: true,
            ..console.capabilities().clone()
        });

        console.begin_capture();
        console.print(&img);
        let output = console.end_capture();

        // Must contain Kitty APC introducer
        assert!(
            output.contains("\x1b_G"),
            "Kitty path must emit \\x1b_G APC; got: {:?}",
            output
        );
        // The full APC must survive — the introducer AND the ST terminator
        // `\x1b\\`. (Regression guard: a plain text segment gets width-cropped,
        // truncating the base64 payload and dropping the terminator.)
        assert!(
            output.contains("\x1b\\"),
            "Kitty APC must reach its ST terminator (not be width-cropped); got len {}",
            output.len()
        );
        // Must carry the display cell-box size (c=cols, r=rows) so Kitty scales
        // the image to the requested cells instead of a tiny native-pixel thumb.
        assert!(
            output.contains(",c=") && output.contains(",r="),
            "Kitty APC must specify c=/r= cell dimensions; got: {:?}",
            output
        );
        // Must NOT contain ▀ (halfblock chars)
        assert!(
            !output.contains(''),
            "Kitty path must NOT emit halfblock ▀"
        );
    }

    // -----------------------------------------------------------------------
    // RED TEST 3 — recording console always uses halfblock (for HTML/SVG export)
    // -----------------------------------------------------------------------
    #[test]
    fn recording_console_uses_halfblock_not_kitty() {
        let img = Image::from_rgba(2, 2, rgba_2x2()).width(2);

        let mut console = Console::builder()
            .no_color(false)
            .force_terminal(true)
            .color_system("truecolor")
            .width(80)
            .record(true) // recording mode → halfblock regardless
            .build();

        // Even with kitty=true, recording uses halfblock
        console.set_capabilities(ConsoleCapabilities {
            kitty: true,
            ..console.capabilities().clone()
        });

        console.begin_capture();
        console.print(&img);
        let output = console.end_capture();

        assert!(
            output.contains(''),
            "recording console must use halfblock for export; got: {:?}",
            output
        );
        assert!(
            !output.contains("\x1b_G"),
            "recording console must NOT emit Kitty APC (breaks HTML export)"
        );
    }

    // -----------------------------------------------------------------------
    // RED TEST 4 — ConsoleCapabilities detects kitty from env
    // -----------------------------------------------------------------------
    #[test]
    fn caps_kitty_flag_from_xterm_kitty() {
        let caps = ConsoleCapabilities::from_env_parts(
            Some("truecolor"),
            Some("xterm-kitty"),
            true,
            None,
            None, // kitty_window_id
            None, // term_program
            None, // image_protocol_override
        );
        assert!(caps.kitty, "TERM=xterm-kitty should set kitty=true");
    }

    #[test]
    fn caps_kitty_flag_from_kitty_window_id() {
        let caps = ConsoleCapabilities::from_env_parts(
            None,
            None,
            true,
            None,
            Some("42"), // KITTY_WINDOW_ID set
            None,
            None,
        );
        assert!(caps.kitty, "KITTY_WINDOW_ID set should set kitty=true");
    }

    #[test]
    fn caps_kitty_flag_from_wezterm() {
        let caps = ConsoleCapabilities::from_env_parts(
            None,
            None,
            true,
            None,
            None,
            Some("WezTerm"), // TERM_PROGRAM=WezTerm
            None,
        );
        assert!(caps.kitty, "TERM_PROGRAM=WezTerm should set kitty=true");
    }

    #[test]
    fn caps_kitty_flag_from_ghostty() {
        let caps = ConsoleCapabilities::from_env_parts(
            None,
            None,
            true,
            None,
            None,
            Some("ghostty"),
            None,
        );
        assert!(caps.kitty, "TERM_PROGRAM=ghostty should set kitty=true");
    }

    #[test]
    fn caps_iterm_flag_from_term_program() {
        let caps = ConsoleCapabilities::from_env_parts(
            None,
            None,
            true,
            None,
            None,
            Some("iTerm.app"),
            None,
        );
        assert!(caps.iterm, "TERM_PROGRAM=iTerm.app should set iterm=true");
    }

    #[test]
    fn caps_no_kitty_on_plain_term() {
        let caps = ConsoleCapabilities::from_env_parts(
            Some("truecolor"),
            Some("xterm-256color"),
            true,
            None,
            None,
            None,
            None,
        );
        assert!(
            !caps.kitty,
            "plain xterm-256color should NOT set kitty=true"
        );
        assert!(
            !caps.iterm,
            "plain xterm-256color should NOT set iterm=true"
        );
    }

    // -----------------------------------------------------------------------
    // iTerm2 path (OSC 1337). Requires `inline-images` for PNG encoding.
    //
    // With iterm=true, kitty=false, and not recording, Image must emit an
    // `ESC ]1337;File=inline=1;…:<base64 PNG> BEL` sequence — not halfblock,
    // not Kitty APC.
    // -----------------------------------------------------------------------
    #[cfg(feature = "inline-images")]
    #[test]
    fn iterm_path_emits_osc1337_with_png_base64() {
        let img = Image::from_rgba(2, 2, rgba_2x2()).width(2);

        let mut console = Console::builder()
            .no_color(false)
            .force_terminal(true)
            .color_system("truecolor")
            .width(80)
            .build();

        console.set_capabilities(ConsoleCapabilities {
            kitty: false,
            iterm: true,
            ..console.capabilities().clone()
        });

        console.begin_capture();
        console.print(&img);
        let output = console.end_capture();

        // OSC 1337 inline-image introducer + args.
        assert!(
            output.contains("\x1b]1337;File=inline=1"),
            "iTerm2 path must emit OSC 1337 File=inline=1; got: {:?}",
            output
        );
        // The base64 of any PNG begins with "iVBORw0KGgo" (base64 of the PNG
        // magic \x89PNG\r\n\x1a\n) — proves the payload is a real PNG.
        assert!(
            output.contains("iVBORw0KGgo"),
            "iTerm2 payload must be a base64-encoded PNG; got: {:?}",
            output
        );
        // BEL terminator for the OSC sequence.
        assert!(
            output.contains('\u{0007}'),
            "iTerm2 OSC 1337 must be BEL-terminated; got: {:?}",
            output
        );
        // Must NOT fall back to halfblock or use Kitty.
        assert!(
            !output.contains(''),
            "iTerm2 path must NOT emit halfblock ▀"
        );
        assert!(
            !output.contains("\x1b_G"),
            "iTerm2 path must NOT emit Kitty APC"
        );
    }

    // Recording mode wins over iTerm2: export must stay halfblock styled text.
    #[cfg(feature = "inline-images")]
    #[test]
    fn recording_console_uses_halfblock_not_iterm() {
        let img = Image::from_rgba(2, 2, rgba_2x2()).width(2);

        let mut console = Console::builder()
            .no_color(false)
            .force_terminal(true)
            .color_system("truecolor")
            .width(80)
            .record(true)
            .build();

        console.set_capabilities(ConsoleCapabilities {
            iterm: true,
            kitty: false,
            ..console.capabilities().clone()
        });

        console.begin_capture();
        console.print(&img);
        let output = console.end_capture();

        assert!(
            output.contains(''),
            "recording console must use halfblock for export; got: {:?}",
            output
        );
        assert!(
            !output.contains("\x1b]1337"),
            "recording console must NOT emit iTerm2 OSC 1337 (breaks HTML export)"
        );
    }

    // -----------------------------------------------------------------------
    // Sixel path (DCS). Dep-free — no `inline-images` feature needed.
    //
    // With sixel=true, kitty/iterm=false, and not recording, Image must emit a
    // `ESC P … q … ESC \\` sixel stream with colour registers — not halfblock,
    // not Kitty, not iTerm2.
    // -----------------------------------------------------------------------
    #[test]
    fn sixel_path_emits_dcs_stream() {
        let img = Image::from_rgba(2, 2, rgba_2x2()).width(2);

        let mut console = Console::builder()
            .no_color(false)
            .force_terminal(true)
            .color_system("truecolor")
            .width(80)
            .build();

        console.set_capabilities(ConsoleCapabilities {
            kitty: false,
            iterm: false,
            sixel: true,
            ..console.capabilities().clone()
        });

        console.begin_capture();
        console.print(&img);
        let output = console.end_capture();

        // DCS sixel introducer (ESC P ... q) and raster attributes.
        assert!(
            output.contains("\x1bP") && output.contains('q'),
            "Sixel path must emit a DCS `\\x1bP…q` introducer; got: {:?}",
            output
        );
        assert!(
            output.contains("\"1;1;"),
            "Sixel stream must carry raster attributes; got: {:?}",
            output
        );
        // At least one colour register definition (#n;2;r;g;b).
        assert!(
            output.contains(";2;"),
            "Sixel stream must define RGB colour registers; got: {:?}",
            output
        );
        // ST terminator.
        assert!(
            output.contains("\x1b\\"),
            "Sixel stream must be ST-terminated; got: {:?}",
            output
        );
        // Not any other protocol / fallback.
        assert!(
            !output.contains(''),
            "Sixel path must NOT emit halfblock ▀"
        );
        assert!(
            !output.contains("\x1b_G"),
            "Sixel path must NOT emit Kitty APC"
        );
        assert!(
            !output.contains("\x1b]1337"),
            "Sixel path must NOT emit iTerm2 OSC 1337"
        );
    }
}