yuru-tui 0.2.3

Terminal user interface for Yuru
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
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
use std::collections::BTreeMap;

use unicode_segmentation::UnicodeSegmentation;
use yuru_core::KeyKind;

use crate::api::{TuiLayout, TuiStyle};
use crate::preview::PreviewRender;
use crate::render::{
    display_width, preview_geometry, render, terminal_safe_prefix, truncate_to_width_with_ellipsis,
    RenderContext, Viewport,
};
use crate::state::TuiState;
use crate::TuiAction;

use super::helpers::{force_test_color_output, scored, scored_with_id};

/// Length, final byte, and parameter bytes of the CSI sequence at the start of
/// `text`, so a rendered frame can be replayed without its escape codes.
fn csi_sequence(text: &str) -> Option<(usize, u8, &str)> {
    let bytes = text.as_bytes();
    if !bytes.starts_with(b"\x1b[") {
        return None;
    }
    bytes[2..]
        .iter()
        .position(|byte| (0x40..=0x7e).contains(byte))
        .map(|index| (index + 3, bytes[2 + index], &text[2..2 + index]))
}

/// The characters a frame prints, with its escape sequences removed — the text
/// the terminal is left holding.
fn without_escapes(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut offset = 0usize;
    while offset < text.len() {
        if let Some((len, _, _)) = csi_sequence(&text[offset..]) {
            offset += len;
            continue;
        }
        let ch = text[offset..]
            .chars()
            .next()
            .expect("valid character boundary");
        assert_ne!(ch, '\u{1b}', "unhandled escape sequence in {text:?}");
        out.push(ch);
        offset += ch.len_utf8();
    }
    out
}

/// Replays a rendered frame and returns, per one-based terminal row, the last
/// column that row actually paints. `MoveTo` starts a run at a column and every
/// other CSI sequence is skipped, since the terminal keeps composing across
/// styling: a run's width is therefore measured over the grapheme clusters of
/// its escape-free text, so a cluster written across an SGR sequence costs what
/// it prints rather than the sum of its halves.
fn painted_row_extents(rendered: &str) -> BTreeMap<usize, usize> {
    let mut runs: Vec<(usize, usize, String)> = vec![(1, 1, String::new())];
    let mut offset = 0usize;
    while offset < rendered.len() {
        if let Some((len, final_byte, params)) = csi_sequence(&rendered[offset..]) {
            if final_byte == b'H' {
                let mut parts = params.split(';');
                let row = parts.next().and_then(|part| part.parse().ok()).unwrap_or(1);
                let column = parts.next().and_then(|part| part.parse().ok()).unwrap_or(1);
                runs.push((row, column, String::new()));
            }
            offset += len;
            continue;
        }

        let ch = rendered[offset..]
            .chars()
            .next()
            .expect("valid character boundary");
        assert_ne!(ch, '\u{1b}', "unhandled escape sequence in {rendered:?}");
        runs.last_mut().expect("a run is always open").2.push(ch);
        offset += ch.len_utf8();
    }

    let mut extents = BTreeMap::new();
    for (row, column, text) in runs {
        if text.is_empty() {
            continue;
        }
        let entry = extents.entry(row).or_insert(0);
        *entry = (*entry).max(column.saturating_sub(1) + display_width(&text));
    }
    extents
}

/// SGR parameters `TuiStyle::default()` paints the selected row's background
/// with, as they appear in a rendered frame.
const SELECTED_ROW_BG: &str = "48;2;52;58;70";

/// Replays a rendered frame and returns, for every character painted on
/// one-based terminal `row`, the SGR parameters of the background colour that
/// was in effect when the terminal printed it — `None` for the default.
///
/// Only the background is tracked, because that is what `bg+` paints and what a
/// record's own SGR sequences can steal from the row's padding. `0` and `49`
/// clear it, `40`–`47` and `100`–`107` set it, and `48` sets it from the
/// extended form whose arguments follow it and are not parameters of their own.
fn row_cell_backgrounds(rendered: &str, row: usize) -> Vec<(char, Option<String>)> {
    let mut cells = Vec::new();
    let mut current_row = 1usize;
    let mut background: Option<String> = None;
    let mut offset = 0usize;
    while offset < rendered.len() {
        if let Some((len, final_byte, params)) = csi_sequence(&rendered[offset..]) {
            match final_byte {
                b'H' => {
                    current_row = params
                        .split(';')
                        .next()
                        .and_then(|part| part.parse().ok())
                        .unwrap_or(1);
                }
                b'm' => apply_background_sgr(params, &mut background),
                _ => {}
            }
            offset += len;
            continue;
        }

        let ch = rendered[offset..]
            .chars()
            .next()
            .expect("valid character boundary");
        assert_ne!(ch, '\u{1b}', "unhandled escape sequence in {rendered:?}");
        if current_row == row {
            cells.push((ch, background.clone()));
        }
        offset += ch.len_utf8();
    }
    cells
}

fn apply_background_sgr(params: &str, background: &mut Option<String>) {
    let params: Vec<&str> = params.split(';').collect();
    let mut index = 0usize;
    while index < params.len() {
        let parameter = params[index];
        // An omitted parameter is a `0`, the full reset.
        let value = if parameter.is_empty() {
            Some(0u16)
        } else {
            parameter.parse::<u16>().ok()
        };
        match value {
            Some(0) | Some(49) => *background = None,
            Some(value) if (40..=47).contains(&value) || (100..=107).contains(&value) => {
                *background = Some(parameter.to_string());
            }
            Some(48) => {
                let arguments = match params
                    .get(index + 1)
                    .and_then(|part| part.parse::<u16>().ok())
                {
                    Some(5) => 2,
                    Some(2) => 4,
                    _ => 0,
                };
                let end = (index + 1 + arguments).min(params.len());
                *background = Some(params[index..end].join(";"));
                index = end - 1;
            }
            _ => {}
        }
        index += 1;
    }
}

/// Renders one wide-text result with `query` as the live query and returns the
/// raw frame.
fn render_wide_frame(query: &str, display: &str, viewport: Viewport, ellipsis: &str) -> String {
    force_test_color_output();
    let mut output = Vec::new();
    let state = TuiState::new(query);
    let results = vec![scored(display, KeyKind::Original)];
    render(
        &mut output,
        &state,
        &results,
        RenderContext {
            candidates: &[],
            prompt: "> ",
            header: None,
            footer: None,
            viewport,
            layout: TuiLayout::Default,
            preview: None,
            style: &TuiStyle::default(),
            highlight_line: true,
            case_sensitive: false,
            multi: false,
            no_input: false,
            pointer: ">",
            marker: "*",
            ellipsis,
            ansi: false,
        },
    )
    .unwrap();
    String::from_utf8(output).unwrap()
}

#[test]
fn render_default_layout_places_prompt_at_bottom() {
    force_test_color_output();
    let mut output = Vec::new();
    let state = TuiState::new("al");
    let results = vec![scored("alpha", KeyKind::Original)];
    render(
        &mut output,
        &state,
        &results,
        RenderContext {
            candidates: &[],
            prompt: "> ",
            header: None,
            footer: None,
            viewport: Viewport { width: 40, rows: 3 },
            layout: TuiLayout::Default,
            preview: None,
            style: &TuiStyle::default(),
            highlight_line: true,
            case_sensitive: false,
            multi: false,
            no_input: false,
            pointer: ">",
            marker: "*",
            ellipsis: "..",
            ansi: false,
        },
    )
    .unwrap();

    let rendered = String::from_utf8(output).unwrap();
    assert!(rendered.contains("\u{1b}[4;1H> al"), "{rendered:?}");
    assert!(rendered.contains("\u{1b}[?25h"), "{rendered:?}");
    assert!(
        rendered.contains("\u{1b}[3;1H\u{1b}[48;2;52;58;70m> "),
        "{rendered:?}"
    );
    assert!(!rendered.contains("\u{1b}[7m"), "{rendered:?}");
}

#[test]
fn render_positions_cursor_by_display_width_and_handles_zero_width() {
    force_test_color_output();
    let state = TuiState::new("日本");

    for (width, expected_cursor) in [(40, "\u{1b}[3;7H"), (0, "\u{1b}[3;1H")] {
        let mut output = Vec::new();
        render(
            &mut output,
            &state,
            &[],
            RenderContext {
                candidates: &[],
                prompt: "> ",
                header: None,
                footer: None,
                viewport: Viewport { width, rows: 2 },
                layout: TuiLayout::Default,
                preview: None,
                style: &TuiStyle::default(),
                highlight_line: true,
                case_sensitive: false,
                multi: false,
                no_input: false,
                pointer: ">",
                marker: "*",
                ellipsis: "..",
                ansi: false,
            },
        )
        .unwrap();

        let rendered = String::from_utf8(output).unwrap();
        assert!(rendered.contains(expected_cursor), "{rendered:?}");
    }
}

#[test]
fn render_positions_cursor_after_visible_control_pictures() {
    force_test_color_output();
    let mut output = Vec::new();
    let state = TuiState::new("\tA");

    render(
        &mut output,
        &state,
        &[],
        RenderContext {
            candidates: &[],
            prompt: "\r",
            header: None,
            footer: None,
            viewport: Viewport { width: 40, rows: 2 },
            layout: TuiLayout::Default,
            preview: None,
            style: &TuiStyle::default(),
            highlight_line: true,
            case_sensitive: false,
            multi: false,
            no_input: false,
            pointer: ">",
            marker: "*",
            ellipsis: "..",
            ansi: false,
        },
    )
    .unwrap();

    let rendered = String::from_utf8(output).unwrap();
    assert!(rendered.contains("␍␉A"), "{rendered:?}");
    assert!(rendered.contains("\u{1b}[3;4H"), "{rendered:?}");
}

#[test]
fn render_default_layout_paints_results_bottom_up() {
    force_test_color_output();
    let mut output = Vec::new();
    let state = TuiState::new("");
    let results = vec![
        scored("alpha", KeyKind::Original),
        scored_with_id(1, "beta", KeyKind::Original),
        scored_with_id(2, "gamma", KeyKind::Original),
    ];
    render(
        &mut output,
        &state,
        &results,
        RenderContext {
            candidates: &[],
            prompt: "> ",
            header: None,
            footer: None,
            viewport: Viewport { width: 40, rows: 3 },
            layout: TuiLayout::Default,
            preview: None,
            style: &TuiStyle::default(),
            highlight_line: true,
            case_sensitive: false,
            multi: false,
            no_input: false,
            pointer: ">",
            marker: "*",
            ellipsis: "..",
            ansi: false,
        },
    )
    .unwrap();

    let rendered = String::from_utf8(output).unwrap();
    assert!(
        rendered.contains("\u{1b}[3;1H\u{1b}[48;2;52;58;70m> \u{1b}[39malpha"),
        "{rendered:?}"
    );
    assert!(rendered.contains("\u{1b}[2;1H  beta"), "{rendered:?}");
    assert!(rendered.contains("\u{1b}[1;1H  gamma"), "{rendered:?}");
}

#[test]
fn render_reverse_layout_places_prompt_at_top() {
    force_test_color_output();
    let mut output = Vec::new();
    let state = TuiState::new("al");
    let results = vec![scored("alpha", KeyKind::Original)];
    render(
        &mut output,
        &state,
        &results,
        RenderContext {
            candidates: &[],
            prompt: "> ",
            header: None,
            footer: None,
            viewport: Viewport { width: 40, rows: 3 },
            layout: TuiLayout::Reverse,
            preview: None,
            style: &TuiStyle::default(),
            highlight_line: true,
            case_sensitive: false,
            multi: false,
            no_input: false,
            pointer: ">",
            marker: "*",
            ellipsis: "..",
            ansi: false,
        },
    )
    .unwrap();

    let rendered = String::from_utf8(output).unwrap();
    assert!(rendered.contains("\u{1b}[1;1H> al"), "{rendered:?}");
}

#[test]
fn render_reverse_no_input_uses_first_row_for_results_and_preview() {
    force_test_color_output();
    let mut output = Vec::new();
    let state = TuiState::new("");
    let results = vec![scored("alpha", KeyKind::Original)];
    render(
        &mut output,
        &state,
        &results,
        RenderContext {
            candidates: &[],
            prompt: "> ",
            header: None,
            footer: None,
            viewport: Viewport { width: 80, rows: 3 },
            layout: TuiLayout::Reverse,
            preview: Some(PreviewRender::Text {
                text: "preview alpha",
                scroll: 0,
            }),
            style: &TuiStyle::default(),
            highlight_line: true,
            case_sensitive: false,
            multi: false,
            no_input: true,
            pointer: ">",
            marker: "*",
            ellipsis: "..",
            ansi: false,
        },
    )
    .unwrap();

    let rendered = String::from_utf8(output).unwrap();
    assert!(
        rendered.contains("\u{1b}[1;1H\u{1b}[48;2;52;58;70m> "),
        "{rendered:?}"
    );
    assert!(
        rendered.contains("\u{1b}[1;41Hpreview alpha"),
        "{rendered:?}"
    );
}

#[test]
fn preview_geometry_tracks_prompt_presence() {
    let viewport = Viewport { width: 80, rows: 3 };

    let with_prompt = preview_geometry(viewport, TuiLayout::Reverse, true, true).unwrap();
    assert_eq!(with_prompt.top, 1);
    assert_eq!(with_prompt.lines, 3);

    let without_prompt = preview_geometry(viewport, TuiLayout::Reverse, false, true).unwrap();
    assert_eq!(without_prompt.top, 0);
    assert_eq!(without_prompt.lines, 3);
}

#[test]
fn render_preview_pane_prints_preview_text() {
    force_test_color_output();
    let mut output = Vec::new();
    let state = TuiState::new("");
    let results = vec![scored("alpha", KeyKind::Original)];
    render(
        &mut output,
        &state,
        &results,
        RenderContext {
            candidates: &[],
            prompt: "> ",
            header: None,
            footer: None,
            viewport: Viewport { width: 80, rows: 3 },
            layout: TuiLayout::Default,
            preview: Some(PreviewRender::Text {
                text: "preview alpha\nsecond line",
                scroll: 0,
            }),
            style: &TuiStyle::default(),
            highlight_line: true,
            case_sensitive: false,
            multi: false,
            no_input: false,
            pointer: ">",
            marker: "*",
            ellipsis: "..",
            ansi: false,
        },
    )
    .unwrap();

    let rendered = String::from_utf8(output).unwrap();
    assert!(
        rendered.contains("\u{1b}[1;41Hpreview alpha"),
        "{rendered:?}"
    );
    assert!(rendered.contains("\u{1b}[2;41Hsecond line"), "{rendered:?}");
}

#[test]
fn render_preview_pane_uses_scroll_offset() {
    force_test_color_output();
    let mut output = Vec::new();
    let state = TuiState::new("");
    let results = vec![scored("alpha", KeyKind::Original)];
    render(
        &mut output,
        &state,
        &results,
        RenderContext {
            candidates: &[],
            prompt: "> ",
            header: None,
            footer: None,
            viewport: Viewport { width: 80, rows: 2 },
            layout: TuiLayout::Default,
            preview: Some(PreviewRender::Text {
                text: "first\nsecond\nthird",
                scroll: 1,
            }),
            style: &TuiStyle::default(),
            highlight_line: true,
            case_sensitive: false,
            multi: false,
            no_input: false,
            pointer: ">",
            marker: "*",
            ellipsis: "..",
            ansi: false,
        },
    )
    .unwrap();

    let rendered = String::from_utf8(output).unwrap();
    assert!(!rendered.contains("first"), "{rendered:?}");
    assert!(rendered.contains("\u{1b}[1;41Hsecond"), "{rendered:?}");
    assert!(rendered.contains("\u{1b}[2;41Hthird"), "{rendered:?}");
}

#[test]
fn render_neutralizes_terminal_controls_from_all_untrusted_text_surfaces() {
    force_test_color_output();
    let payload = "evil\0\t\r\u{7}\u{7f}\u{1b}]52;c;Y2xpcGJvYXJk\u{7}\u{1b}[999C";
    let mut output = Vec::new();
    let state = TuiState::new("");
    let results = vec![scored(payload, KeyKind::Original)];

    render(
        &mut output,
        &state,
        &results,
        RenderContext {
            candidates: &[],
            prompt: "> ",
            header: Some(payload),
            footer: None,
            viewport: Viewport {
                width: 120,
                rows: 3,
            },
            layout: TuiLayout::Reverse,
            preview: Some(PreviewRender::Text {
                text: payload,
                scroll: 0,
            }),
            style: &TuiStyle::default(),
            highlight_line: true,
            case_sensitive: false,
            multi: false,
            no_input: true,
            pointer: ">",
            marker: "*",
            ellipsis: "..",
            ansi: false,
        },
    )
    .unwrap();

    let rendered = String::from_utf8(output).unwrap();
    assert!(!rendered.contains("\u{1b}]52;"), "{rendered:?}");
    assert!(!rendered.contains('\0'), "{rendered:?}");
    assert!(!rendered.contains('\t'), "{rendered:?}");
    assert!(!rendered.contains('\r'), "{rendered:?}");
    assert!(!rendered.contains('\u{7}'), "{rendered:?}");
    assert!(!rendered.contains('\u{7f}'), "{rendered:?}");
    assert!(!rendered.contains("\u{1b}[999C"), "{rendered:?}");
    assert!(rendered.contains("␀␉␍␇␡␛]52;c;Y2xpcGJvYXJk␇␛[999C"));
}

#[test]
fn render_ansi_mode_allows_only_sgr_sequences() {
    force_test_color_output();
    let display = "\u{1b}[31mred\u{1b}[0m\u{1b}]52;c;YQ==\u{7}";
    let mut output = Vec::new();
    let state = TuiState::new("");
    let results = vec![scored(display, KeyKind::Original)];

    render(
        &mut output,
        &state,
        &results,
        RenderContext {
            candidates: &[],
            prompt: "> ",
            header: None,
            footer: None,
            viewport: Viewport { width: 80, rows: 2 },
            layout: TuiLayout::Reverse,
            preview: None,
            style: &TuiStyle::default(),
            highlight_line: false,
            case_sensitive: false,
            multi: false,
            no_input: true,
            pointer: ">",
            marker: "*",
            ellipsis: "..",
            ansi: true,
        },
    )
    .unwrap();

    let rendered = String::from_utf8(output).unwrap();
    assert!(rendered.contains("\u{1b}[31mred\u{1b}[0m"), "{rendered:?}");
    assert!(!rendered.contains("\u{1b}]52;"), "{rendered:?}");
    assert!(rendered.contains("␛]52;c;YQ==␇"), "{rendered:?}");
}

#[test]
fn painted_rows_never_exceed_the_viewport_width_for_wide_text() {
    // The first case is the audit reproduction: 13 characters, 26 columns.
    let cases = [
        "日本語検索テスト用の候補行",
        "日本語 mixed ASCII テキスト mixed",
        "🎉🎊🎈 party 🎁🎀 time 🥳",
        "한국어 검색 테스트 후보 행",
        "中文搜索测试候选行",
    ];

    for display in cases {
        // Widths below the two-column gutter are degenerate and excluded.
        for width in [4usize, 5, 8, 13, 20, 21, 40] {
            let rendered = render_wide_frame(display, display, Viewport { width, rows: 3 }, "..");
            for (row, extent) in painted_row_extents(&rendered) {
                assert!(
                    extent <= width,
                    "row {row} of a {width}-column viewport reached column {extent} \
                     for {display:?}: {rendered:?}"
                );
            }
        }
    }
}

#[test]
fn a_wide_character_at_an_odd_boundary_is_dropped_not_split() {
    let display = "日本語検索テスト用の候補行";
    // Two gutter columns leave 19 for text. The tenth wide character would need
    // columns 19 and 20, so it must be dropped whole rather than half-emitted.
    let rendered = render_wide_frame("", display, Viewport { width: 21, rows: 3 }, "..");

    assert!(rendered.contains("日本語検索テスト用"), "{rendered:?}");
    assert!(!rendered.contains(''), "{rendered:?}");
    // The selected-row background pads the one unusable column, so the row ends
    // exactly at the viewport edge instead of wrapping.
    assert_eq!(painted_row_extents(&rendered).get(&3).copied(), Some(21));
}

#[test]
fn preview_pane_truncates_a_cjk_line_by_display_columns() {
    force_test_color_output();
    let line = "日本語検索テスト用の候補行".repeat(3);
    let mut output = Vec::new();
    let state = TuiState::new("");
    let results = vec![scored("alpha", KeyKind::Original)];
    render(
        &mut output,
        &state,
        &results,
        RenderContext {
            candidates: &[],
            prompt: "> ",
            header: None,
            footer: None,
            viewport: Viewport { width: 80, rows: 3 },
            layout: TuiLayout::Default,
            preview: Some(PreviewRender::Text {
                text: &line,
                scroll: 0,
            }),
            style: &TuiStyle::default(),
            highlight_line: true,
            case_sensitive: false,
            multi: false,
            no_input: false,
            pointer: ">",
            marker: "*",
            ellipsis: "..",
            ansi: false,
        },
    )
    .unwrap();

    let rendered = String::from_utf8(output).unwrap();
    // The 40-column preview pane starts at column 41: 19 wide characters plus
    // the two-column ellipsis exactly fill it.
    assert!(
        rendered.contains("\u{1b}[1;41H日本語検索テスト用の候補行日本語検索テ.."),
        "{rendered:?}"
    );
    for (row, extent) in painted_row_extents(&rendered) {
        assert!(
            extent <= 80,
            "row {row} reached column {extent}: {rendered:?}"
        );
    }
}

#[test]
fn truncation_charges_the_ellipsis_in_display_columns() {
    let line = "日本語検索テスト";

    // ".." costs two columns, leaving nine for text, so four wide characters fit.
    assert_eq!(
        truncate_to_width_with_ellipsis(line, 11, ".."),
        "日本語検.."
    );
    // "…" costs one column, leaving ten, so a fifth character fits.
    assert_eq!(
        truncate_to_width_with_ellipsis(line, 11, ""),
        "日本語検索…"
    );
    // Sixteen columns hold all eight characters, so nothing is elided.
    assert_eq!(truncate_to_width_with_ellipsis(line, 16, ".."), line);
    // One column cannot hold a wide character, and the ellipsis is clipped too.
    assert_eq!(truncate_to_width_with_ellipsis(line, 1, ".."), ".");
}

/// Every cluster here prints in exactly two columns, even though the per-scalar
/// width sum says four for the emoji modifier sequence and one for the keycap.
const TWO_COLUMN_CLUSTERS: [&str; 3] = ["\u{301}", "👩\u{1f3fd}", "#\u{fe0f}\u{20e3}"];

#[test]
fn terminal_safe_prefix_budgets_by_grapheme_cluster() {
    for cluster in TWO_COLUMN_CLUSTERS {
        // The whole cluster fits a two-column budget: a combining mark or an
        // emoji modifier is never clipped off the base it belongs to.
        assert_eq!(
            terminal_safe_prefix(&format!("{cluster}x"), false, 2),
            (cluster.to_string(), true)
        );
        // One column cannot hold it, so it is dropped whole rather than
        // half-emitted as a bare base with its continuation scalars discarded.
        assert_eq!(
            terminal_safe_prefix(cluster, false, 1),
            (String::new(), true)
        );
    }

    // SGR escapes still consume no width budget: the two columns are spent on
    // the cluster alone, and the trailing reset — which costs nothing and is
    // the whole remainder of the text — is carried rather than counted as
    // dropped content, so the text does not report itself truncated.
    assert_eq!(
        terminal_safe_prefix("\u{1b}[31m日\u{301}\u{1b}[0m", true, 2),
        ("\u{1b}[31m日\u{301}\u{1b}[0m".to_string(), false)
    );
}

#[test]
fn display_width_stays_additive_at_every_cluster_boundary() {
    // Two flags joined by a ZWJ are two grapheme clusters, and a row may split
    // between them. Whatever the budget, the clipped prefix must cost no more
    // than the budget and must cost exactly what its pieces cost, or the
    // selected row's `bg+` padding disagrees with the truncation and overruns.
    for text in [
        "🇯🇵\u{200d}🇯🇵",
        "\u{301}\u{301}",
        "👩\u{1f3fd}👩\u{1f3fd}",
    ] {
        for width in 0..=8 {
            let (clipped, _) = terminal_safe_prefix(text, false, width);
            let clipped_width = display_width(&clipped);
            assert!(
                clipped_width <= width,
                "{clipped:?} costs {clipped_width} of a {width}-column budget"
            );
            let per_cluster: usize = clipped.graphemes(true).map(display_width).sum();
            assert_eq!(per_cluster, clipped_width, "{clipped:?}");
        }
    }
}

#[test]
fn a_grapheme_cluster_survives_truncation_or_is_dropped_whole() {
    // The audit reproduction: a four-column viewport leaves two columns for the
    // result after the gutter, exactly one cluster's worth.
    for display in TWO_COLUMN_CLUSTERS {
        let rendered = render_wide_frame("", display, Viewport { width: 4, rows: 3 }, "..");
        assert!(
            rendered.contains(display),
            "{display:?} was clipped mid-cluster: {rendered:?}"
        );

        // Three columns leave one, which no cluster fits, so every scalar of it
        // is dropped together.
        let rendered = render_wide_frame("", display, Viewport { width: 3, rows: 3 }, "..");
        for ch in display.chars() {
            assert!(
                !rendered.contains(ch),
                "{display:?} left {ch:?} behind: {rendered:?}"
            );
        }
    }

    // Two columns hold the first cluster only; the second goes with its mark.
    let rendered = render_wide_frame(
        "",
        "\u{301}\u{301}",
        Viewport { width: 4, rows: 3 },
        "..",
    );
    assert!(rendered.contains("\u{301}"), "{rendered:?}");
    assert!(!rendered.contains(''), "{rendered:?}");
}

/// Renders one `--multi` result with `marker` as the marker, marking the row
/// when `marked` is set, and returns the raw frame.
fn render_marked_frame(marked: bool, marker: &str, viewport: Viewport) -> String {
    force_test_color_output();
    let mut output = Vec::new();
    let mut state = TuiState::new("");
    let results = vec![scored("alpha", KeyKind::Original)];
    if marked {
        state.apply_with_results(TuiAction::ToggleMark, &results, false, true, None);
    }
    render(
        &mut output,
        &state,
        &results,
        RenderContext {
            candidates: &[],
            prompt: "> ",
            header: None,
            footer: None,
            viewport,
            layout: TuiLayout::Default,
            preview: None,
            style: &TuiStyle::default(),
            highlight_line: true,
            case_sensitive: false,
            multi: true,
            no_input: false,
            pointer: ">",
            marker,
            ellipsis: "..",
            ansi: false,
        },
    )
    .unwrap();
    String::from_utf8(output).unwrap()
}

#[test]
fn a_wide_custom_marker_reserves_only_the_columns_the_row_prints() {
    // `--multi --marker 界` with nothing marked prints `>` plus a one-column
    // blank, so the gutter costs two columns — not the three the two-column
    // marker would cost — and the selected row's `bg+` padding has to reach the
    // last cell of the viewport.
    let rendered = render_marked_frame(false, "", Viewport { width: 10, rows: 3 });
    assert!(!rendered.contains(''), "{rendered:?}");
    assert_eq!(painted_row_extents(&rendered).get(&3).copied(), Some(10));

    // Marking the row replaces that blank with the two-column marker, so the
    // gutter really is three columns and the row still ends at the edge.
    let rendered = render_marked_frame(true, "", Viewport { width: 10, rows: 3 });
    assert!(rendered.contains(''), "{rendered:?}");
    assert_eq!(painted_row_extents(&rendered).get(&3).copied(), Some(10));
}

#[test]
fn a_gutter_wider_than_the_viewport_is_clipped_to_it() {
    // A five-character marker of two-column characters costs ten columns, so
    // the pointer plus the marker want eleven of a ten-column row. Shrinking
    // the result width saturates at zero and stops constraining anything, so
    // the gutter has to be bounded by the list itself or the row wraps.
    let rendered = render_marked_frame(true, "界界界界界", Viewport { width: 10, rows: 3 });
    assert_eq!(painted_row_extents(&rendered).get(&3).copied(), Some(10));
    // The pointer keeps its column and the marker is clipped to the other nine,
    // which hold four of its five wide characters.
    assert!(rendered.contains(">界界界界\u{1b}"), "{rendered:?}");
    assert!(!rendered.contains("界界界界界"), "{rendered:?}");

    // A pointer alone can outgrow the row too, with nothing marked.
    let rendered = render_marked_frame(false, "*", Viewport { width: 2, rows: 3 });
    for (row, extent) in painted_row_extents(&rendered) {
        assert!(
            extent <= 2,
            "row {row} reached column {extent}: {rendered:?}"
        );
    }
}

/// Renders one result whose display text carries its own SGR sequences, with
/// `--ansi` handling enabled, and returns the raw frame.
fn render_ansi_frame(display: &str, viewport: Viewport) -> String {
    force_test_color_output();
    let mut output = Vec::new();
    let state = TuiState::new("");
    let results = vec![scored(display, KeyKind::Original)];
    render(
        &mut output,
        &state,
        &results,
        RenderContext {
            candidates: &[],
            prompt: "> ",
            header: None,
            footer: None,
            viewport,
            layout: TuiLayout::Default,
            preview: None,
            style: &TuiStyle::default(),
            highlight_line: true,
            case_sensitive: false,
            multi: false,
            no_input: false,
            pointer: ">",
            marker: "*",
            ellipsis: "..",
            ansi: true,
        },
    )
    .unwrap();
    String::from_utf8(output).unwrap()
}

#[test]
fn a_combining_mark_reaches_its_base_across_an_sgr_sequence() {
    // A control byte ends a grapheme cluster, so `日` and the combining acute
    // after the reset are two clusters even though the terminal draws them as
    // one. Budgeting cluster by cluster is therefore not enough on its own: the
    // scan has to keep going past a full budget through everything that costs
    // no columns, or the accent is clipped off the character it belongs to.
    let text = "\u{1b}[31m日\u{1b}[0m\u{301}";
    assert_eq!(
        terminal_safe_prefix(text, true, 2),
        (text.to_string(), false)
    );

    // The same at the render path: a four-column viewport leaves the result two
    // columns, and the row keeps its colour, its reset, and its accent.
    let rendered = render_ansi_frame(text, Viewport { width: 4, rows: 3 });
    assert!(rendered.contains(text), "{rendered:?}");
    for (row, extent) in painted_row_extents(&rendered) {
        assert!(
            extent <= 4,
            "row {row} reached column {extent}: {rendered:?}"
        );
    }

    // SGR bytes still buy no columns: one column cannot hold the base, so the
    // cluster is dropped whole and the accent goes with it.
    let (clipped, truncated) = terminal_safe_prefix(text, true, 1);
    assert!(!clipped.contains(''), "{clipped:?}");
    assert!(!clipped.contains('\u{301}'), "{clipped:?}");
    assert!(truncated);
}

/// Grapheme clusters an `--ansi` record can split with an SGR sequence: the
/// cluster written plainly, and the same cluster with a styling sequence
/// between its base and its continuation scalars. The terminal composes each
/// pair identically — the escape buys no columns and breaks nothing apart — so
/// whatever the plain form costs, the styled one costs too.
const SGR_SPLIT_CLUSTERS: [(&str, &str); 5] = [
    // Keycap: the sum of the parts is one column, the composed cluster two.
    ("#\u{fe0f}\u{20e3}", "#\u{1b}[31m\u{fe0f}\u{20e3}"),
    // Emoji modifier: the sum of the parts is four columns, the cluster two.
    ("👩\u{1f3fb}", "👩\u{1b}[0m\u{1f3fb}"),
    // ZWJ sequence.
    ("👩\u{200d}💻", "👩\u{1b}[1m\u{200d}💻"),
    // Regional indicator pair.
    ("🇯🇵", "🇯\u{1b}[32m🇵"),
    // Variation selector.
    ("\u{fe0f}", "\u{1b}[33m\u{fe0f}"),
];

#[test]
fn an_sgr_sequence_inside_a_cluster_changes_nothing_but_the_bytes() {
    for (plain, styled) in SGR_SPLIT_CLUSTERS {
        for width in 0..=6 {
            let (plain_clipped, plain_truncated) = terminal_safe_prefix(plain, false, width);
            let (styled_clipped, styled_truncated) = terminal_safe_prefix(styled, true, width);

            // The escape is carried through, and what it separated is charged
            // and kept — or dropped — exactly as if it had never been written.
            assert_eq!(
                without_escapes(&styled_clipped),
                plain_clipped,
                "{styled:?} at {width} columns"
            );
            assert_eq!(
                styled_truncated, plain_truncated,
                "{styled:?} at {width} columns"
            );
            let cost = display_width(&without_escapes(&styled_clipped));
            assert!(cost <= width, "{styled_clipped:?} costs {cost} of {width}");
        }
    }
}

#[test]
fn a_cluster_split_by_an_sgr_sequence_survives_the_row_or_is_dropped_whole() {
    for (plain, styled) in SGR_SPLIT_CLUSTERS {
        // The gutter — pointer plus a one-column blank — leaves the result
        // exactly the columns the composed cluster prints in.
        let fits = 2 + display_width(plain);
        let rendered = render_ansi_frame(
            styled,
            Viewport {
                width: fits,
                rows: 3,
            },
        );
        assert!(
            rendered.contains(styled),
            "{styled:?} lost part of its cluster: {rendered:?}"
        );
        for (row, extent) in painted_row_extents(&rendered) {
            assert!(
                extent <= fits,
                "row {row} reached column {extent} of a {fits}-column viewport: {rendered:?}"
            );
        }

        // One column short, the whole cluster goes: painting the base alone
        // would drop a modifier that costs nothing, and painting it with a
        // continuation the terminal composes into a wider glyph would overrun
        // the row.
        let rendered = render_ansi_frame(
            styled,
            Viewport {
                width: fits - 1,
                rows: 3,
            },
        );
        for ch in plain.chars() {
            assert!(
                !rendered.contains(ch),
                "{styled:?} left {ch:?} behind: {rendered:?}"
            );
        }
        for (row, extent) in painted_row_extents(&rendered) {
            assert!(
                extent < fits,
                "row {row} reached column {extent} of a {}-column viewport: {rendered:?}",
                fits - 1
            );
        }
    }
}

#[test]
fn a_keycap_split_by_an_sgr_sequence_does_not_overflow_a_three_column_row() {
    // The audit reproduction for the over-charging direction. The gutter leaves
    // one text column; `#` alone costs one, so charging it and its post-escape
    // tail separately let the row through — and the terminal then composed the
    // pieces into a two-column keycap and painted four columns into three.
    let rendered = render_ansi_frame(
        "#\u{1b}[31m\u{fe0f}\u{20e3}",
        Viewport { width: 3, rows: 3 },
    );
    // The result row is the gutter and the `bg+` padding that replaces the
    // keycap, and it ends at the last column rather than past it.
    assert_eq!(painted_row_extents(&rendered).get(&3).copied(), Some(3));
    assert!(!rendered.contains('#'), "{rendered:?}");
}

#[test]
fn an_emoji_modifier_split_by_an_sgr_sequence_is_not_dropped_from_a_row_it_fits() {
    // The audit reproduction for the under-charging direction. `👩` is charged
    // two columns and the lone skin-tone modifier was then measured as another
    // two-column cluster and dropped, so the row painted a bare `👩` where the
    // whole styled grapheme takes the two columns available.
    let text = "👩\u{1b}[0m\u{1f3fb}";
    assert_eq!(
        terminal_safe_prefix(text, true, 2),
        (text.to_string(), false)
    );

    let rendered = render_ansi_frame(text, Viewport { width: 4, rows: 3 });
    assert!(rendered.contains(text), "{rendered:?}");
}

/// SGR sequences that only ever clear styling. A sequence sitting exactly at
/// the clip point is the last thing the row emits, so one of these belongs to
/// the text that was retained and has to survive.
const CLEARING_BOUNDARY_SGRS: [&str; 18] = [
    // A full reset, written out and written as the empty default.
    "\u{1b}[0m",
    "\u{1b}[m",
    // Leading zeros and an omitted parameter are both a `0`.
    "\u{1b}[00m",
    "\u{1b}[0;m",
    // Partial resets: each turns one attribute back off.
    "\u{1b}[22m",
    "\u{1b}[23m",
    "\u{1b}[24m",
    "\u{1b}[25m",
    "\u{1b}[27m",
    "\u{1b}[28m",
    "\u{1b}[29m",
    "\u{1b}[39m",
    "\u{1b}[49m",
    // A sequence carrying several parameters, every one of them a reset.
    "\u{1b}[0;39;49m",
    // Judged by final state: reverse video set and then cleared ends with
    // nothing active. (Codex review of the first classifier.)
    "\u{1b}[7;27m",
    // Overline-off and framed-off, missing from the first whitelist.
    "\u{1b}[55m",
    "\u{1b}[54m",
    // The colon-form underline-off.
    "\u{1b}[4:0m",
];

/// SGR sequences that leave some styling set, so one at the clip point belongs
/// to the character that was just discarded and must go with it.
const SETTING_BOUNDARY_SGRS: [&str; 13] = [
    // The issue reproduction: a background opener.
    "\u{1b}[41m",
    "\u{1b}[31m",
    "\u{1b}[1m",
    // A reset followed by a set still ends with red active.
    "\u{1b}[0;31m",
    "\u{1b}[39;41m",
    // Extended colour. Its embedded numbers spell partial resets — `49`, `39`,
    // `22`, `29` — and reading them as parameters in their own right would keep
    // a sequence that paints a colour.
    "\u{1b}[38;5;49m",
    "\u{1b}[48;2;39;49;22m",
    // Overline on; its off-opcode 55 is in the clearing set above.
    "\u{1b}[53m",
    // Colon-form underline styles other than 0 select a style.
    "\u{1b}[4:3m",
    // Clearing reverse and then setting it again ends set.
    "\u{1b}[27;7m",
    // A malformed extended colour: whatever follows 38 is unknowable.
    "\u{1b}[38m",
    // The colon-delimited forms of the same thing.
    "\u{1b}[38:5:29m",
    "\u{1b}[48:2:39:49:22m",
];

#[test]
fn a_boundary_sgr_sequence_is_kept_only_when_it_clears_styling() {
    // `a` costs one of the two columns, `界` needs two and one remains, so `界`
    // is dropped whole — and the sequence written immediately before it sits
    // exactly at the clip point.
    for sequence in CLEARING_BOUNDARY_SGRS {
        assert_eq!(
            terminal_safe_prefix(&format!("a{sequence}\u{754c}"), true, 2),
            (format!("a{sequence}"), true),
            "{sequence:?} clears styling and belongs to the retained text"
        );
    }

    for sequence in SETTING_BOUNDARY_SGRS {
        assert_eq!(
            terminal_safe_prefix(&format!("a{sequence}\u{754c}"), true, 2),
            ("a".to_string(), true),
            "{sequence:?} sets styling and belongs to the discarded character"
        );
    }
}

#[test]
fn a_setting_sgr_sequence_inside_the_retained_text_is_untouched() {
    // Only the clip point distinguishes the two. Every sequence the retained
    // text was written around stays exactly where the record put it, opener or
    // not, or a row loses the styling it asked for.
    for sequence in SETTING_BOUNDARY_SGRS {
        let text = format!("{sequence}ab\u{754c}");
        assert_eq!(
            terminal_safe_prefix(&text, true, 2),
            (format!("{sequence}ab"), true),
            "{sequence:?} is inside the retained text"
        );
    }
}

#[test]
fn a_boundary_sgr_opener_does_not_recolour_the_selected_row_padding() {
    // The issue reproduction at the render path. The gutter — pointer plus a
    // one-column blank — takes two of the four columns, leaving the result two:
    // `a` costs one, `界` needs two, and the column `界` vacates is filled with
    // `bg+` padding. Keeping the red opener painted that padding red.
    let rendered = render_ansi_frame("a\u{1b}[41m\u{754c}", Viewport { width: 4, rows: 3 });
    assert!(
        !rendered.contains("\u{1b}[41m"),
        "the discarded character's opener reached the terminal: {rendered:?}"
    );
    assert_eq!(
        row_cell_backgrounds(&rendered, 3),
        vec![
            ('>', Some(SELECTED_ROW_BG.to_string())),
            (' ', Some(SELECTED_ROW_BG.to_string())),
            ('a', Some(SELECTED_ROW_BG.to_string())),
            (' ', Some(SELECTED_ROW_BG.to_string())),
        ],
        "{rendered:?}"
    );

    // Unstyled text pads the same row the same way, so the assertion above is
    // pinning the padding's colour rather than the absence of styling.
    let rendered = render_ansi_frame("a\u{754c}", Viewport { width: 4, rows: 3 });
    assert_eq!(
        row_cell_backgrounds(&rendered, 3),
        vec![
            ('>', Some(SELECTED_ROW_BG.to_string())),
            (' ', Some(SELECTED_ROW_BG.to_string())),
            ('a', Some(SELECTED_ROW_BG.to_string())),
            (' ', Some(SELECTED_ROW_BG.to_string())),
        ],
        "{rendered:?}"
    );
}

#[test]
fn a_boundary_reset_still_reaches_the_terminal() {
    // The other half of the rule, end to end: the reset belongs to the `a` that
    // was kept, and dropping it would let styling opened earlier in the record
    // run on into the rest of the interface.
    let rendered = render_ansi_frame(
        "\u{1b}[31ma\u{1b}[0m\u{754c}",
        Viewport { width: 4, rows: 3 },
    );
    assert!(rendered.contains("\u{1b}[31ma\u{1b}[0m"), "{rendered:?}");
    assert!(!rendered.contains('\u{754c}'), "{rendered:?}");
}

#[test]
fn free_content_is_followed_no_further_than_the_byte_cap() {
    // Nothing in a record made only of SGR sequences and zero-width clusters
    // costs a column, so the budget can never stop the scan and the byte cap
    // has to — including through the widening window that lets a cluster reach
    // its continuation, which must widen to the cap and stop rather than loop.
    for (text, allow_sgr) in [
        ("\u{200b}".repeat(100_000), false),
        ("\u{1b}[31m".repeat(100_000), true),
    ] {
        let (clipped, truncated) = terminal_safe_prefix(&text, allow_sgr, 4);
        assert!(truncated);
        assert!(clipped.len() <= 4 * 64, "{} bytes scanned", clipped.len());
    }

    // The cap bounds where a cluster may begin, not where its bytes may end:
    // `a` starts one byte inside a 64-byte cap and its combining acute lies
    // past it, and a cap that cut there would clip the base off its mark — the
    // same defect from the other side. It costs the one column it prints.
    let text = format!("{}a\u{301}b", "\u{200b}".repeat(21));
    let (clipped, truncated) = terminal_safe_prefix(&text, false, 1);
    assert!(clipped.ends_with("a\u{301}"), "{clipped:?}");
    assert!(truncated);
}

/// Renders a frame with `prompt` as the prompt and returns it.
fn render_prompt_frame(prompt: &str, viewport: Viewport) -> String {
    force_test_color_output();
    let mut output = Vec::new();
    let state = TuiState::new("");
    let results = vec![scored("alpha", KeyKind::Original)];
    render(
        &mut output,
        &state,
        &results,
        RenderContext {
            candidates: &[],
            prompt,
            header: None,
            footer: None,
            viewport,
            layout: TuiLayout::Default,
            preview: None,
            style: &TuiStyle::default(),
            highlight_line: true,
            case_sensitive: false,
            multi: false,
            no_input: false,
            pointer: ">",
            marker: "*",
            ellipsis: "..",
            ansi: false,
        },
    )
    .unwrap();
    String::from_utf8(output).unwrap()
}

#[test]
fn a_zero_width_tail_is_not_a_truncation() {
    // U+200B is a grapheme cluster of its own and costs no columns, so this
    // text occupies exactly two of them and nothing has to be dropped. Ending
    // the scan the moment the budget filled left the tail unconsumed and
    // reported the text as truncated, which spent the entire two-column budget
    // on an ellipsis for content that already fit.
    assert_eq!(
        terminal_safe_prefix("ab\u{200b}", false, 2),
        ("ab\u{200b}".to_string(), false)
    );
    assert_eq!(
        truncate_to_width_with_ellipsis("ab\u{200b}", 2, ".."),
        "ab\u{200b}"
    );
    // Anything past the tail that does cost a column still truncates.
    assert_eq!(
        truncate_to_width_with_ellipsis("ab\u{200b}c", 2, ".."),
        ".."
    );

    // At the render path, through the prompt: two columns of prompt in a
    // two-column viewport print as themselves, not as "..".
    let rendered = render_prompt_frame("ab\u{200b}", Viewport { width: 2, rows: 3 });
    assert!(rendered.contains("ab\u{200b}"), "{rendered:?}");
    assert!(!rendered.contains(".."), "{rendered:?}");
    for (row, extent) in painted_row_extents(&rendered) {
        assert!(
            extent <= 2,
            "row {row} reached column {extent}: {rendered:?}"
        );
    }
}