tui-test-rs 0.1.0-beta.2

In-process terminal automation, inspection, assertions, and recording
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
//! Crisp, full-color SVG screenshot of a terminal grid, styled after
//! `svg-term-cli`: a rounded window panel with macOS-style controls.
//!
//! Vector output renders sharply at any zoom. The viewer supplies a monospace
//! face for text, while Nerd Font icons are emitted from a bundled symbols font
//! as SVG paths. Colors, bold/italic/underline/strike, inverse, and dim are all
//! preserved. Each run of same-styled cells is forced to its exact column width
//! via `textLength`, so alignment is independent of the rendering font's
//! metrics.

use std::fmt::Write;

use super::nerd_font::NerdFont;
use crate::profile::{ColorSlot, Profile, Rgb};
use crate::terminal::cell::{truncate_to_columns, Attrs, Color, EmuCell, CONTINUATION};
use crate::terminal::emu::{CursorShape, Emulator};

pub(crate) const CELL_W: f32 = 10.0;
pub(crate) const CELL_H: f32 = 21.0;
pub(crate) const FONT_SIZE: f32 = 17.0;
pub(crate) const FONT_BASELINE: f32 = (CELL_H - FONT_SIZE) / 2.0 + FONT_SIZE * 0.78;
pub(crate) const MARGIN_X: f32 = 15.0;
pub(crate) const HEADER_H: f32 = 34.0;
pub(crate) const CONTENT_PADDING_TOP: f32 = 4.0;
const MARGIN_BOTTOM: f32 = 14.0;
pub(crate) const DOT_R: f32 = 7.0;
pub(crate) const RED_DOT_R: f32 = 2.5;
pub(crate) const RED_DOT_COLOR: Rgb = Rgb::new(105, 17, 10);
pub(crate) const WINDOW_RADIUS: f32 = 8.0;
pub(crate) const TITLE_DIVIDER_H: f32 = 1.0;
pub(crate) const TITLE_BG: Rgb = Rgb::new(217, 217, 232);
pub(crate) const TITLE_DIVIDER: Rgb = Rgb::new(0, 0, 0);
pub(crate) const CANVAS_PADDING: u32 = 24;
pub(crate) const CANVAS_BACKGROUND: Rgb = Rgb::new(104, 103, 170);
pub(crate) const SHADOW_COLOR: Rgb = Rgb::new(8, 8, 18);
pub(crate) const SHADOW_LAYERS: [(f32, f32, u8); 4] = [
    (7.0, 5.0, 18),
    (5.0, 4.0, 20),
    (3.0, 3.0, 22),
    (1.0, 2.0, 24),
];
pub(crate) const TRAFFIC_LIGHTS: [Rgb; 3] = [
    Rgb::new(236, 106, 94),
    Rgb::new(244, 191, 79),
    Rgb::new(97, 197, 84),
];
/// Title bar text, smaller than the grid font so the chrome does not compete
/// with the terminal content itself.
pub(crate) const TITLE_FONT_SIZE: f32 = 13.0;
pub(crate) const TITLE_FG: Rgb = Rgb::new(65, 65, 69);
/// Where the rightmost traffic light ends. A centred title is kept clear of
/// this on both sides, so it can never be drawn over the controls.
const DOTS_RIGHT: f32 = MARGIN_X + 5.0 + 2.0 * 20.0 + DOT_R;
const FONT_STACK: &str =
    "'Cascadia Code','JetBrains Mono','Fira Code',Menlo,Consolas,'DejaVu Sans Mono',monospace";

fn hex(c: Rgb) -> String {
    c.to_hex()
}

fn svg_dimension(value: f64) -> String {
    let mut output = format!("{value:.4}");
    while output.contains('.') && output.ends_with('0') {
        output.pop();
    }
    if output.ends_with('.') {
        output.pop();
    }
    output
}

fn dim(c: Rgb) -> Rgb {
    let s = |v: u8| (v as f32 * 0.6) as u8;
    Rgb::new(s(c.r), s(c.g), s(c.b))
}

static BLANK: EmuCell = EmuCell::blank();

fn cell_at(row: &[EmuCell], x: usize) -> &EmuCell {
    row.get(x).unwrap_or(&BLANK)
}

pub(crate) trait RenderColors {
    fn color(&self, slot: ColorSlot) -> Rgb;
    fn cursor_shape(&self) -> CursorShape;

    fn resolve(&self, color: Option<Color>, is_fg: bool) -> Rgb {
        match color {
            None => self.color(if is_fg {
                ColorSlot::Foreground
            } else {
                ColorSlot::Background
            }),
            Some(Color::Named(n)) => self.color(ColorSlot::Indexed(n.index())),
            Some(Color::Idx(i)) => self.color(ColorSlot::Indexed(i)),
            Some(Color::Rgb(r, g, b)) => Rgb::new(r, g, b),
        }
    }
}

impl<T: Emulator + ?Sized> RenderColors for T {
    fn color(&self, slot: ColorSlot) -> Rgb {
        Emulator::color(self, slot)
    }

    fn cursor_shape(&self) -> CursorShape {
        Emulator::cursor_shape(self)
    }
}

impl RenderColors for Profile {
    fn color(&self, slot: ColorSlot) -> Rgb {
        match slot {
            ColorSlot::Indexed(index) => self.colors.rgb(index),
            ColorSlot::Foreground => self.colors.foreground,
            ColorSlot::Background => self.colors.background,
            ColorSlot::Cursor => self.colors.cursor,
        }
    }

    fn cursor_shape(&self) -> CursorShape {
        CursorShape::Block
    }
}

/// The renderer-owned terminal state captured together with the grid.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct RenderState {
    indexed: [Rgb; 256],
    foreground: Rgb,
    background: Rgb,
    cursor: Rgb,
    cursor_shape: CursorShape,
}

impl RenderState {
    pub(crate) fn capture(emu: &dyn Emulator) -> Self {
        Self {
            indexed: std::array::from_fn(|index| emu.color(ColorSlot::Indexed(index as u8))),
            foreground: emu.color(ColorSlot::Foreground),
            background: emu.color(ColorSlot::Background),
            cursor: emu.color(ColorSlot::Cursor),
            cursor_shape: emu.cursor_shape(),
        }
    }
}

impl RenderColors for RenderState {
    fn color(&self, slot: ColorSlot) -> Rgb {
        match slot {
            ColorSlot::Indexed(index) => self.indexed[index as usize],
            ColorSlot::Foreground => self.foreground,
            ColorSlot::Background => self.background,
            ColorSlot::Cursor => self.cursor,
        }
    }

    fn cursor_shape(&self) -> CursorShape {
        self.cursor_shape
    }
}

/// Resolved background color for a cell (honoring inverse).
pub(crate) fn bg_of(cell: &EmuCell, colors: &dyn RenderColors) -> Rgb {
    let bg = colors.resolve(cell.bg, false);
    let fg = colors.resolve(cell.fg, true);
    if cell.has(Attrs::INVERSE) {
        fg
    } else {
        bg
    }
}

#[derive(Clone, Copy, PartialEq)]
pub(crate) struct Style {
    pub fg: Rgb,
    pub bold: bool,
    pub italic: bool,
    pub underline: bool,
    pub strike: bool,
    pub invisible: bool,
}

pub(crate) fn style_of(cell: &EmuCell, colors: &dyn RenderColors) -> Style {
    let mut fg = colors.resolve(cell.fg, true);
    let bg = colors.resolve(cell.bg, false);
    if cell.has(Attrs::INVERSE) {
        fg = bg;
    }
    if cell.has(Attrs::DIM) {
        fg = dim(fg);
    }
    Style {
        fg,
        bold: cell.has(Attrs::BOLD),
        italic: cell.has(Attrs::ITALIC),
        underline: cell.underline.is_underlined(),
        strike: cell.has(Attrs::STRIKE),
        invisible: cell.has(Attrs::INVISIBLE),
    }
}

fn escape(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            _ => out.push(c),
        }
    }
    out
}

fn run_text(row: &[EmuCell], start: usize, end: usize) -> String {
    let mut text = String::with_capacity(end - start);
    for i in start..end {
        text.push_str(&cell_at(row, i).ch);
    }
    text
}

fn write_text_run(
    out: &mut String,
    row: &[EmuCell],
    start: usize,
    end: usize,
    y: usize,
    style: Style,
    nerd_font: &NerdFont,
) {
    if style.invisible {
        return;
    }
    let fg = hex(style.fg);
    let tx = MARGIN_X + start as f32 * CELL_W;
    let baseline = HEADER_H + CONTENT_PADDING_TOP + y as f32 * CELL_H + FONT_BASELINE;
    let width = (end - start) as f32 * CELL_W;
    let original_text = run_text(row, start, end);
    let (text, run_x_adjust) = nerd_font.prepare_run(&original_text, width, CELL_W);

    // Preserve decoration for runs containing only vector glyphs.
    if !original_text.trim().is_empty() {
        let weight = if style.bold {
            r#" font-weight="bold""#
        } else {
            ""
        };
        let italic = if style.italic {
            r#" font-style="italic""#
        } else {
            ""
        };
        let deco = match (style.underline, style.strike) {
            (true, true) => r#" text-decoration="underline line-through""#,
            (true, false) => r#" text-decoration="underline""#,
            (false, true) => r#" text-decoration="line-through""#,
            (false, false) => "",
        };
        let _ = write!(
            out,
            r#"<text x="{tx:.2}" y="{baseline:.2}" fill="{fg}"{weight}{italic}{deco} textLength="{width:.2}" lengthAdjust="spacingAndGlyphs" xml:space="preserve">{esc}</text>"#,
            esc = escape(&text)
        );
    }
    for i in start..end {
        for c in cell_at(row, i).ch.chars() {
            nerd_font.write_use(
                out,
                c,
                (
                    MARGIN_X + i as f32 * CELL_W,
                    HEADER_H + CONTENT_PADDING_TOP + y as f32 * CELL_H,
                ),
                (CELL_W, CELL_H),
                run_x_adjust,
                &fg,
            );
        }
    }
}

/// Draw the window title centred in the title bar.
///
/// The title is chrome rather than grid content, so unlike a cell run it is
/// not forced to a `textLength`: stretching a proportional string to a
/// computed width would distort it. It is instead truncated to what fits, and
/// kept clear of the traffic lights by reserving the same margin on both
/// sides, which also keeps it centred on the space that remains.
pub(crate) fn title_advance() -> f32 {
    TITLE_FONT_SIZE * (CELL_W / FONT_SIZE)
}

pub(crate) fn media_title(title: Option<&str>, cols: u16, rows: usize, fits: usize) -> String {
    let base = title
        .map(str::trim)
        .filter(|title| !title.is_empty())
        .unwrap_or("tui-test capture");
    let suffix = format!(" - {cols}x{rows}");
    let full = format!("{base}{suffix}");
    if crate::terminal::cell::display_width(&full) <= fits {
        return full;
    }

    let suffix_width = crate::terminal::cell::display_width(&suffix);
    if suffix_width >= fits {
        return truncate_to_columns(&full, fits);
    }
    format!("{}{suffix}", truncate_to_columns(base, fits - suffix_width))
}

pub(crate) fn visible_title(
    title: Option<&str>,
    cols: u16,
    rows: usize,
    width: f32,
) -> Option<String> {
    const GAP: f32 = 8.0;
    let available = width - 2.0 * (DOTS_RIGHT + GAP);
    let fits = (available / title_advance()).floor().max(0.0) as usize;
    if fits == 0 {
        return None;
    }
    Some(media_title(title, cols, rows, fits))
}

fn write_title(out: &mut String, title: Option<&str>, cols: u16, rows: usize, width: f32) {
    let Some(shown) = visible_title(title, cols, rows, width) else {
        return;
    };
    let _ = write!(
        out,
        r#"<text x="{cx:.2}" y="{baseline:.2}" fill="{fill}" font-size="{TITLE_FONT_SIZE}px" font-weight="bold" text-anchor="middle" xml:space="preserve">{esc}</text>"#,
        cx = width / 2.0,
        baseline = HEADER_H / 2.0 + TITLE_FONT_SIZE * 0.35,
        fill = hex(TITLE_FG),
        esc = escape(&shown),
    );
}

/// How much of a cell the thin cursor shapes cover.
const CURSOR_THICKNESS: f32 = 2.0;

/// Draw the cursor over the cell it sits on.
///
/// A block is filled and the character redrawn in the cell's background color,
/// which is how a terminal keeps the character under a block cursor readable.
/// It is drawn after the text pass so the block covers the first, normally
/// colored draw of that character.
fn write_cursor(
    out: &mut String,
    rows: &[Vec<EmuCell>],
    (cx, cy): (u16, usize),
    colors: &dyn RenderColors,
    nerd_font: &NerdFont,
) {
    let Some(row) = rows.get(cy) else {
        return;
    };
    let cell = cell_at(row, cx as usize);
    // A double-width character stores its second half as a continuation cell,
    // so the cursor has to cover both or it clips the glyph down the middle.
    let span = if row
        .get(cx as usize + 1)
        .is_some_and(|next| next.ch == CONTINUATION)
    {
        2.0
    } else {
        1.0
    };
    let w = span * CELL_W;
    let x = MARGIN_X + cx as f32 * CELL_W;
    let y = HEADER_H + CONTENT_PADDING_TOP + cy as f32 * CELL_H;
    let fill = hex(colors.color(ColorSlot::Cursor));

    let (rx, ry, rw, rh) = match colors.cursor_shape() {
        CursorShape::Block => (x, y, w, CELL_H),
        CursorShape::Underline => (x, y + CELL_H - CURSOR_THICKNESS, w, CURSOR_THICKNESS),
        CursorShape::Bar => (x, y, CURSOR_THICKNESS, CELL_H),
    };
    let _ = write!(
        out,
        r#"<rect x="{rx:.2}" y="{ry:.2}" width="{rw:.2}" height="{rh:.2}" fill="{fill}"/>"#
    );

    if colors.cursor_shape() != CursorShape::Block {
        return;
    }
    let mut style = style_of(cell, colors);
    style.fg = bg_of(cell, colors);
    write_text_run(
        out,
        row,
        cx as usize,
        cx as usize + span as usize,
        cy,
        style,
        nerd_font,
    );
}

/// Render the grid. `cursor` is where to draw the cursor *within `rows`*, so a
/// caller passing scrollback has already offset it, and `None` means the
/// terminal is not showing one. `title` is the window title a program set,
/// drawn in the title bar, and `None` leaves the bar bare.
///
/// Its row is a `usize` because it indexes `rows`, which for a full-history
/// render is as long as the scrollback and so is not bounded by the screen.
#[cfg(test)]
pub(crate) fn render_svg(
    rows: &[Vec<EmuCell>],
    cols: u16,
    colors: &dyn RenderColors,
    cursor: Option<(u16, usize)>,
    title: Option<&str>,
) -> String {
    render_svg_with_zoom(rows, cols, colors, cursor, title, 1.0)
}

pub(crate) fn render_svg_with_zoom(
    rows: &[Vec<EmuCell>],
    cols: u16,
    colors: &dyn RenderColors,
    cursor: Option<(u16, usize)>,
    title: Option<&str>,
    zoom: f64,
) -> String {
    render_svg_with_font(rows, cols, colors, cursor, title, FONT_STACK, zoom)
}

pub(crate) fn render_svg_with_font(
    rows: &[Vec<EmuCell>],
    cols: u16,
    colors: &dyn RenderColors,
    cursor: Option<(u16, usize)>,
    title: Option<&str>,
    font_family: &str,
    zoom: f64,
) -> String {
    let nerd_font = NerdFont::new(rows, FONT_SIZE);
    let cols = cols as usize;
    let x0 = MARGIN_X;
    let y0 = HEADER_H + CONTENT_PADDING_TOP;
    let panel_width = MARGIN_X * 2.0 + cols as f32 * CELL_W;
    let panel_height =
        HEADER_H + CONTENT_PADDING_TOP + MARGIN_BOTTOM + rows.len().max(1) as f32 * CELL_H;
    let padding = CANVAS_PADDING as f32;
    let width = panel_width + padding * 2.0;
    let height = panel_height + padding * 2.0;
    let output_width = svg_dimension(f64::from(width) * zoom);
    let output_height = svg_dimension(f64::from(height) * zoom);

    let mut out = String::new();
    let _ = write!(
        out,
        r#"<svg xmlns="http://www.w3.org/2000/svg" width="{output_width}" height="{output_height}" viewBox="0 0 {width:.0} {height:.0}" font-family="{font_family}" font-size="{FONT_SIZE}px">"#
    );
    nerd_font.write_defs(&mut out);
    let _ = write!(
        out,
        r#"<rect width="{width:.0}" height="{height:.0}" fill="{}"/>"#,
        hex(CANVAS_BACKGROUND)
    );
    for (spread, offset_y, alpha) in SHADOW_LAYERS {
        let shadow_x = padding - spread;
        let shadow_y = padding - spread + offset_y;
        let shadow_width = panel_width + spread * 2.0;
        let shadow_height = panel_height + spread * 2.0;
        let shadow_radius = WINDOW_RADIUS + spread;
        let opacity = f32::from(alpha) / 255.0;
        let _ = write!(
            out,
            r#"<rect x="{shadow_x:.1}" y="{shadow_y:.1}" width="{shadow_width:.1}" height="{shadow_height:.1}" rx="{shadow_radius:.1}" fill="{}" fill-opacity="{opacity:.6}"/>"#,
            hex(SHADOW_COLOR)
        );
    }
    let _ = write!(
        out,
        r#"<g transform="translate({padding:.0} {padding:.0})"><rect width="{panel_width:.0}" height="{panel_height:.0}" rx="{WINDOW_RADIUS:.0}" fill="{}"/>"#,
        hex(colors.resolve(None, false))
    );
    let title_bottom = HEADER_H - TITLE_DIVIDER_H;
    let right_curve = panel_width - WINDOW_RADIUS;
    let _ = write!(
        out,
        r#"<path d="M0 {WINDOW_RADIUS:.1} Q0 0 {WINDOW_RADIUS:.1} 0 H{right_curve:.1} Q{panel_width:.1} 0 {panel_width:.1} {WINDOW_RADIUS:.1} V{title_bottom:.1} H0 Z" fill="{}"/>"#,
        hex(TITLE_BG)
    );
    let _ = write!(
        out,
        r#"<rect y="{title_bottom:.1}" width="{panel_width:.0}" height="{TITLE_DIVIDER_H:.1}" fill="{}"/>"#,
        hex(TITLE_DIVIDER)
    );
    for (i, dot) in TRAFFIC_LIGHTS.iter().copied().enumerate() {
        let cx = MARGIN_X + 5.0 + i as f32 * 20.0;
        let _ = write!(
            out,
            r#"<circle cx="{cx:.1}" cy="{cy:.1}" r="{DOT_R:.1}" fill="{}"/>"#,
            hex(dot),
            cy = HEADER_H / 2.0,
        );
    }
    let _ = write!(
        out,
        r#"<circle cx="{cx:.1}" cy="{cy:.1}" r="{RED_DOT_R:.1}" fill="{}"/>"#,
        hex(RED_DOT_COLOR),
        cx = MARGIN_X + 5.0,
        cy = HEADER_H / 2.0,
    );
    write_title(&mut out, title, cols as u16, rows.len().max(1), panel_width);

    for (y, row) in rows.iter().enumerate() {
        let mut x = 0;
        while x < cols {
            let bg = bg_of(cell_at(row, x), colors);
            let mut run = 1;
            while x + run < cols && bg_of(cell_at(row, x + run), colors) == bg {
                run += 1;
            }
            if bg != colors.resolve(None, false) {
                let rx = x0 + x as f32 * CELL_W;
                let ry = y0 + y as f32 * CELL_H;
                let rw = run as f32 * CELL_W;
                let _ = write!(
                    out,
                    r#"<rect x="{rx:.2}" y="{ry:.2}" width="{rw:.2}" height="{CELL_H:.2}" fill="{}"/>"#,
                    hex(bg)
                );
            }
            x += run;
        }
    }

    for (y, row) in rows.iter().enumerate() {
        let mut x = 0;
        while x < cols {
            let style = style_of(cell_at(row, x), colors);
            let mut run = 1;
            while x + run < cols && style_of(cell_at(row, x + run), colors) == style {
                run += 1;
            }
            write_text_run(&mut out, row, x, x + run, y, style, &nerd_font);
            x += run;
        }
    }

    if let Some(at) = cursor {
        write_cursor(&mut out, rows, at, colors, &nerd_font);
    }

    out.push_str("</g></svg>");
    out
}

#[cfg(feature = "recording-raster")]
pub(crate) fn pixel_size(cols: u16, rows: usize) -> (u32, u32) {
    let width = (MARGIN_X * 2.0 + f32::from(cols) * CELL_W).ceil() as u32;
    let height = (HEADER_H + CONTENT_PADDING_TOP + MARGIN_BOTTOM + rows.max(1) as f32 * CELL_H)
        .ceil() as u32;
    (width + width % 2, height + height % 2)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::profile::{ColorSlot, Profile};
    use crate::terminal::alacritty::AlacrittyEmu;
    use crate::terminal::cell::{Color, UnderlineStyle};

    /// A real emulator: the renderer resolves through the same path a session
    /// uses, so a stand-in could not drift from it.
    fn colors() -> AlacrittyEmu {
        AlacrittyEmu::new(10, 2, &Profile::default())
    }

    fn cell(ch: &str, fg: Option<Color>, bg: Option<Color>) -> EmuCell {
        EmuCell {
            ch: ch.into(),
            fg,
            bg,
            ..EmuCell::blank()
        }
    }

    /// Each shape draws something recognisably different.
    ///
    /// A block covers the cell, an underline sits on the bottom edge, and a
    /// bar on the left, so all three are checked by the rectangle they emit
    /// rather than by merely appearing.
    #[test]
    fn each_cursor_shape_draws_its_own_rectangle() {
        use crate::terminal::emu::Emulator;
        let rows = vec![vec![cell("x", None, None)]];
        let cursor_fill = hex(Profile::default().colors.cursor);

        let mut emu = colors();
        let block = render_svg(&rows, 1, &emu, Some((0, 0)), None);
        assert!(
            block.contains(&format!(
                r#"width="10.00" height="21.00" fill="{cursor_fill}""#
            )),
            "a block covers the whole cell: {block}"
        );

        emu.process(b"\x1b[4 q");
        let underline = render_svg(&rows, 1, &emu, Some((0, 0)), None);
        assert!(
            underline.contains(&format!(
                r#"width="10.00" height="2.00" fill="{cursor_fill}""#
            )),
            "an underline is a thin full-width bar: {underline}"
        );

        emu.process(b"\x1b[6 q");
        let bar = render_svg(&rows, 1, &emu, Some((0, 0)), None);
        assert!(
            bar.contains(&format!(
                r#"width="2.00" height="21.00" fill="{cursor_fill}""#
            )),
            "a bar is a thin full-height stripe: {bar}"
        );
    }

    /// The character under a block cursor is redrawn in the cell background,
    /// which is how a terminal keeps it readable rather than hiding it behind
    /// the block.
    #[test]
    fn a_block_cursor_keeps_its_character_readable() {
        let rows = vec![vec![cell("Z", None, None)]];
        let svg = render_svg(&rows, 1, &colors(), Some((0, 0)), None);
        let background = hex(Profile::default().colors.background);
        assert!(
            svg.contains(&format!(r#"fill="{background}""#)) && svg.matches(">Z<").count() == 2,
            "the character is drawn again, in the background color: {svg}"
        );
    }

    #[test]
    fn a_block_cursor_does_not_reveal_invisible_text() {
        let mut hidden = cell("X", None, None);
        hidden.attrs = Attrs::INVISIBLE;
        let svg = render_svg(&[vec![hidden]], 1, &colors(), Some((0, 0)), None);
        assert!(
            !svg.contains('X'),
            "the cursor must not redraw text hidden with SGR 8: {svg}"
        );
    }

    #[test]
    fn a_block_cursor_preserves_the_character_style() {
        let mut styled = cell("S", None, None);
        styled.attrs = Attrs::BOLD | Attrs::ITALIC | Attrs::STRIKE;
        styled.underline = UnderlineStyle::Single;
        let svg = render_svg(&[vec![styled]], 1, &colors(), Some((0, 0)), None);

        for attribute in [
            r#"font-weight="bold""#,
            r#"font-style="italic""#,
            r#"text-decoration="underline line-through""#,
        ] {
            assert_eq!(
                svg.matches(attribute).count(),
                2,
                "the normal draw and cursor redraw both preserve {attribute}: {svg}"
            );
        }
    }

    /// A double-width character keeps both of its halves.
    ///
    /// The second half lives in a continuation cell, so a cursor sized to one
    /// cell would cover half the glyph and redraw it squashed into that half.
    #[test]
    fn a_block_cursor_covers_a_double_width_character() {
        let rows = vec![vec![
            cell("", None, None),
            cell(CONTINUATION, None, None),
            cell("a", None, None),
        ]];
        let svg = render_svg(&rows, 3, &colors(), Some((0, 0)), None);
        let cursor_fill = hex(Profile::default().colors.cursor);
        assert!(
            svg.contains(&format!(
                r#"width="20.00" height="21.00" fill="{cursor_fill}""#
            )),
            "the block spans both halves: {svg}"
        );
        assert!(
            svg.contains(
                r#"textLength="20.00" lengthAdjust="spacingAndGlyphs" xml:space="preserve">日<"#
            ),
            "the redraw is given both halves too, so it is not squashed: {svg}"
        );
    }

    /// A vector glyph under a block cursor comes back as a glyph.
    ///
    /// Nerd font characters are drawn as `<use>` references and masked out of
    /// the text run, so redrawing one as text would emit a character the text
    /// font has no glyph for and the block would simply swallow it.
    #[test]
    fn a_block_cursor_redraws_a_vector_glyph() {
        let rows = vec![vec![cell("\u{f115}", None, None)]];
        let background = hex(Profile::default().colors.background);
        let svg = render_svg(&rows, 1, &colors(), Some((0, 0)), None);
        assert_eq!(
            svg.matches("<use href=\"#nf-f115\"").count(),
            2,
            "the glyph is drawn once normally and once over the block: {svg}"
        );
        let in_background = svg
            .match_indices(r##"<use href="#nf-f115""##)
            .filter(|(start, _)| {
                svg[*start..]
                    .split("/>")
                    .next()
                    .is_some_and(|glyph| glyph.contains(&format!(r#"fill="{background}""#)))
            })
            .count();
        assert_eq!(
            in_background, 1,
            "exactly the redrawn glyph is in the cell background color: {svg}"
        );
    }

    /// A row past what a `u16` holds is still drawn on its own line.
    ///
    /// A profile can set a scrollback deeper than 65535 rows, and a full
    /// render is as long as the scrollback. Counting the offset in a `u16`
    /// wrapped it, which put the cursor on a line that looks plausible and is
    /// tens of thousands of rows from where the terminal left it.
    #[test]
    fn a_cursor_below_the_u16_mark_keeps_its_row() {
        let row = 70_000usize;
        let rows = vec![vec![cell("x", None, None)]; row + 1];
        let svg = render_svg(&rows, 1, &colors(), Some((0, row)), None);
        let expected = HEADER_H + CONTENT_PADDING_TOP + row as f32 * CELL_H;
        assert!(
            svg.contains(&format!(r#"<rect x="15.00" y="{expected:.2}""#)),
            "the cursor sits on row {row}, not on a wrapped one"
        );
    }

    /// No cursor is drawn when the caller says the terminal is not showing
    /// one, and an out-of-range position is ignored rather than panicking.
    #[test]
    fn a_hidden_or_out_of_range_cursor_draws_nothing() {
        use crate::terminal::emu::Emulator;
        let rows = vec![vec![cell("x", None, None)]];
        // A color nothing else in the image uses, so finding it can only mean
        // the cursor was drawn. The default cursor color is the foreground,
        // which the text itself is painted with.
        let mut emu = colors();
        emu.process(b"\x1b]12;#ff00ff\x07");

        assert!(
            render_svg(&rows, 1, &emu, Some((0, 0)), None).contains("#ff00ff"),
            "the cursor is drawn when there is one to draw"
        );
        assert!(
            !render_svg(&rows, 1, &emu, None, None).contains("#ff00ff"),
            "a terminal not showing a cursor gets none"
        );
        // A row past the end of the grid: reachable if a caller miscounts the
        // scrollback offset, and not worth a panic.
        assert!(
            !render_svg(&rows, 1, &emu, Some((0, 9)), None).contains("#ff00ff"),
            "an out-of-range position is ignored"
        );
    }

    /// The cursor is painted in the color `OSC 12` sets, like every other
    /// color the terminal shows.
    #[test]
    fn the_cursor_follows_a_color_a_program_set() {
        use crate::terminal::emu::Emulator;
        let rows = vec![vec![cell("x", None, None)]];
        let mut emu = colors();
        emu.process(b"\x1b]12;#ff00ff\x07");
        assert!(render_svg(&rows, 1, &emu, Some((0, 0)), None).contains("#ff00ff"));
    }

    /// A program that repaints the terminal repaints the screenshot.
    ///
    /// The renderer draws what the terminal is currently showing, not what it
    /// was configured with, so a background set with `OSC 11` is the one that
    /// gets painted. Nothing else covers the path from an escape sequence to
    /// a rendered pixel.
    #[test]
    fn a_screenshot_follows_colors_a_program_set() {
        use crate::terminal::emu::Emulator;
        let mut emu = colors();
        let rows = vec![vec![cell("x", Some(Color::from_index(1)), None)]];

        let before = render_svg(&rows, 1, &emu, None, None);
        assert!(before.contains(&hex(Profile::default().colors.red)));
        assert!(before.contains(&hex(Profile::default().colors.background)));

        // The program picks its own background and recolors palette slot 1.
        emu.process(b"\x1b]11;#3b0764\x07\x1b]4;1;#22c55e\x07");

        let after = render_svg(&rows, 1, &emu, None, None);
        assert!(
            after.contains("#3b0764"),
            "the window is painted with the background the program set"
        );
        assert!(
            after.contains("#22c55e"),
            "a cell follows the slot the program recolored"
        );
        assert!(
            !after.contains(&hex(Profile::default().colors.red)),
            "the configured red is no longer what slot 1 shows"
        );
    }

    /// And a reset puts the configured colors back on screen.
    #[test]
    fn a_screenshot_returns_to_the_profile_after_a_reset() {
        use crate::terminal::emu::Emulator;
        let mut emu = colors();
        let rows = vec![vec![cell("x", Some(Color::from_index(1)), None)]];

        emu.process(b"\x1b]11;#3b0764\x07\x1b]4;1;#22c55e\x07");
        emu.process(b"\x1b]111\x07\x1b]104;1\x07");

        let after = render_svg(&rows, 1, &emu, None, None);
        assert!(after.contains(&hex(Profile::default().colors.background)));
        assert!(after.contains(&hex(Profile::default().colors.red)));
    }

    #[test]
    fn emits_valid_svg_with_text_and_color() {
        let rows = vec![vec![
            cell("h", Some(Color::from_index(1)), None),
            cell("i", Some(Color::from_index(1)), None),
        ]];
        let svg = render_svg(&rows, 2, &colors(), None, None);
        assert!(svg.starts_with("<svg"));
        assert!(svg.ends_with("</svg>"));
        assert!(svg.contains("textLength"));
        assert!(
            svg.contains(&hex(Emulator::color(&colors(), ColorSlot::Indexed(1)))),
            "slot 1 is painted with the profile color"
        );
        assert!(svg.contains(">hi</text>"));
        assert!(!svg.contains("<defs>"));
        assert!(!svg.contains("<use"));
    }

    #[test]
    fn zoom_changes_output_size_without_changing_the_view_box() {
        let rows = vec![vec![cell("x", None, None)]];
        let svg = render_svg_with_zoom(&rows, 1, &colors(), None, None, 0.5);
        assert!(svg.contains(r#"width="44" height="60.5" viewBox="0 0 88 121""#));
    }

    #[test]
    fn emits_window_chrome() {
        let svg = render_svg(&[vec![cell(" ", None, None)]], 1, &colors(), None, None);
        assert!(svg.contains("<circle"));
        assert!(svg.contains(r##"<rect width="88" height="121" fill="#6867aa"/>"##));
        assert!(svg.contains(r#"<g transform="translate(24 24)">"#));
        assert!(svg.contains(r##"fill="#080812" fill-opacity="0.070588""##));
        assert!(svg.contains(&hex(Profile::default().colors.background)));
        assert!(svg.contains("#d9d9e8"));
        assert!(svg.contains("#000000"));
        assert!(svg.contains("#ec6a5e"));
        assert!(svg.contains("#f4bf4f"));
        assert!(svg.contains("#61c554"));
        assert!(svg.contains("#69110a"));
        assert!(svg.contains(r#"r="2.5""#));
    }

    #[test]
    fn centers_the_text_font_box_in_each_cell() {
        let svg = render_svg(&[vec![cell("x", None, None)]], 1, &colors(), None, None);
        let expected_baseline = HEADER_H + CONTENT_PADDING_TOP + FONT_BASELINE;
        assert!(svg.contains(&format!(r#"y="{expected_baseline:.2}""#)));
    }

    #[test]
    fn escapes_markup_characters() {
        let rows = vec![vec![cell("<", None, None)]];
        let svg = render_svg(&rows, 1, &colors(), None, None);
        assert!(svg.contains("&lt;"));
        assert!(!svg.contains("><</text>"));
    }

    #[test]
    fn background_run_emitted_for_non_default_bg() {
        let rows = vec![vec![cell(" ", None, Some(Color::from_index(4)))]];
        let svg = render_svg(&rows, 1, &colors(), None, None);
        assert!(
            svg.contains(&hex(Emulator::color(&colors(), ColorSlot::Indexed(4)))),
            "slot 4 is painted with the profile color"
        );
    }

    #[test]
    fn embeds_nerd_font_glyphs_as_vector_paths() {
        let glyph = "\u{f115}";
        let rows = vec![vec![
            cell("a", None, None),
            cell(glyph, None, None),
            cell("b", None, None),
        ]];
        let svg = render_svg(&rows, 3, &colors(), None, None);

        assert!(svg.contains(r#"<path id="nf-f115" d=""#));
        assert!(svg.contains(r##"<use href="#nf-f115""##));
        assert!(svg.contains(">a b</text>"));
        assert!(!svg.contains(glyph));
        assert!(svg.contains(&format!(r#"font-family="{FONT_STACK}" font-size="#)));
    }

    #[test]
    fn defines_repeated_nerd_font_glyph_once() {
        let glyph = "\u{f115}";
        let svg = render_svg(
            &[vec![cell(glyph, None, None), cell(glyph, None, None)]],
            2,
            &colors(),
            None,
            None,
        );

        assert_eq!(svg.matches(r#"<path id="nf-f115""#).count(), 1);
        assert_eq!(svg.matches(r##"<use href="#nf-f115""##).count(), 2);
    }

    #[test]
    fn leaves_unknown_private_use_glyphs_as_text() {
        let glyph = "\u{10fffd}";
        let svg = render_svg(&[vec![cell(glyph, None, None)]], 1, &colors(), None, None);

        assert!(svg.contains(glyph));
        assert!(!svg.contains("<defs>"));
        assert!(!svg.contains(r#"<use href="#));
    }

    /// A window title is drawn centred with the rendered cell dimensions, and
    /// media without a program title gets a useful default.
    #[test]
    fn draws_the_window_title_centred_in_the_bar() {
        let rows = vec![vec![cell("x", None, None); 40]];
        let bare = render_svg(&rows, 40, &colors(), None, None);
        let titled = render_svg(&rows, 40, &colors(), None, Some("vim: notes.md"));

        assert!(
            bare.contains(">tui-test capture - 40x1</text>"),
            "untitled media gets the capture default: {bare}"
        );
        assert!(
            titled.contains(">vim: notes.md - 40x1</text>")
                && titled.contains("text-anchor=\"middle\""),
            "the title is drawn, centred: {titled}"
        );
        assert!(
            titled.contains(r##"fill="#414145" font-size="13px" font-weight="bold""##),
            "the title uses the dark title-bar foreground: {titled}"
        );
        // The panel is 2*15 margin + 40 cells of 10, so its middle is 215.
        assert!(
            titled.contains(r#"<text x="215.00""#),
            "centred on the panel, not on the grid origin: {titled}"
        );
    }

    /// A title too long for the bar is truncated rather than drawn over the
    /// window controls or past the panel edge.
    #[test]
    fn truncates_a_title_that_does_not_fit() {
        let rows = vec![vec![cell("x", None, None); 20]];
        let long = "a-very-long-window-title-that-cannot-possibly-fit";
        let svg = render_svg(&rows, 20, &colors(), None, Some(long));

        assert!(!svg.contains(long), "the full title cannot have been drawn");
        let drawn = svg
            .split("text-anchor=\"middle\" xml:space=\"preserve\">")
            .nth(1)
            .and_then(|rest| rest.split("</text>").next())
            .expect("a title element");
        assert!(drawn.contains(''), "truncation is marked: {drawn}");
        assert!(
            drawn.ends_with(" - 20x1"),
            "the dimensions survive truncation: {drawn}"
        );
        assert_fits_clear_of_the_controls(drawn, 20.0);
    }

    /// A wide-glyph title is budgeted by the columns it really occupies.
    ///
    /// The bar inherits the monospace stack, so a CJK glyph takes two
    /// advances. Sized by character count it would be drawn twice as wide as
    /// measured and, being centred, would spill over the controls at one end
    /// and past the panel at the other.
    #[test]
    fn budgets_a_wide_glyph_title_by_column() {
        let rows = vec![vec![cell("x", None, None); 24]];
        let svg = render_svg(&rows, 24, &colors(), None, Some(&"".repeat(40)));
        let drawn = svg
            .split("text-anchor=\"middle\" xml:space=\"preserve\">")
            .nth(1)
            .and_then(|rest| rest.split("</text>").next())
            .expect("a title element");
        assert_fits_clear_of_the_controls(drawn, 24.0);
    }

    /// The drawn title must sit inside the space between the traffic lights
    /// and the mirrored margin on the right.
    fn assert_fits_clear_of_the_controls(drawn: &str, cols: f32) {
        let panel = MARGIN_X * 2.0 + cols * CELL_W;
        let drawn_width = crate::terminal::cell::display_width(drawn) as f32 * title_advance();
        assert!(
            drawn_width <= panel - 2.0 * DOTS_RIGHT,
            "title {drawn:?} is {drawn_width} wide, past the {} available",
            panel - 2.0 * DOTS_RIGHT
        );
    }

    /// A title is markup-escaped like any other text. It comes from whatever
    /// the program chose to send, so an unescaped one would let that program
    /// inject elements into the image.
    #[test]
    fn escapes_markup_in_the_title() {
        let rows = vec![vec![cell("x", None, None); 40]];
        let svg = render_svg(
            &rows,
            40,
            &colors(),
            None,
            Some("</text><script>x</script>"),
        );

        assert!(!svg.contains("<script>"), "no injected element: {svg}");
        assert!(
            svg.contains("&lt;script&gt;"),
            "it is escaped instead: {svg}"
        );
    }
}