tess-cli 0.33.1

A less-style terminal pager for files, pipes, and live logs — with structured-log filtering, pretty-printing (JSON/YAML/TOML/XML/HTML/CSV), ANSI passthrough, multi-file navigation, and ctags jumping. Rust, macOS + Linux.
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
use std::sync::Arc;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

/// How the renderer treats escape sequences in input bytes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AnsiMode {
    /// Pre-0.18 default. ESC renders as `^[` caret form; CSI bytes show as
    /// `^[` + literal text. Used when `--no-color` is set.
    #[default]
    Strict,
    /// Default at app level. SGR sequences update cell styles (zero columns
    /// consumed); non-SGR CSI is parsed and discarded silently; OSC 8 wraps
    /// hyperlinks.
    Interpret,
    /// `-r` / `--raw-control-chars`. Identical to Strict in the render
    /// kernel — the writer handles raw passthrough.
    Raw,
}

/// Per-source rendering state that persists across line renders. Carries the
/// SGR style register and the current OSC 8 hyperlink so that an unclosed
/// `\x1b[31m` on line N keeps line N+1 red until reset.
#[derive(Debug, Default, Clone)]
pub struct RenderState {
    pub style: crate::ansi::Style,
    pub hyperlink: Option<String>,
    pub parse: crate::ansi::ParseState,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Cell {
    Char {
        ch: char,
        width: u8,
        style: crate::ansi::Style,
        hyperlink: Option<Arc<str>>,
    },
    Continuation,
    Empty,
}

#[derive(Debug, Clone)]
pub struct RenderOpts {
    pub tab_width: u8,
    pub wrap: bool,
    pub cols: u16,
    pub mode: AnsiMode,
    /// In chop mode, when a line overflows the right edge, replace the
    /// last cell with this character to signal "more content right".
    /// `None` disables the marker. Matches less's `--rscroll=c`.
    pub rscroll_char: Option<char>,
    /// In wrap mode, break lines on whitespace boundaries instead of
    /// mid-character when possible. Falls back to mid-character break
    /// when no whitespace fits in the row. Matches less's `--wordwrap`.
    pub word_wrap: bool,
}

impl Default for RenderOpts {
    fn default() -> Self {
        Self {
            tab_width: 8, wrap: true, cols: 80,
            mode: AnsiMode::Strict, rscroll_char: None, word_wrap: false,
        }
    }
}

/// Whether the writer should pass 24-bit RGB colors through to the terminal
/// or downsample to the 256-color cube first. Resolved once at startup.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TrueColor {
    Always,
    Never,
    /// Inspect `$COLORTERM` to decide.
    #[default]
    Auto,
}

impl TrueColor {
    /// Resolve this mode to a concrete pass-through flag. `Auto` looks at
    /// the `COLORTERM` env var and treats values `truecolor` / `24bit` as
    /// supporting truecolor.
    pub fn resolve(self) -> bool {
        match self {
            TrueColor::Always => true,
            TrueColor::Never => false,
            TrueColor::Auto => matches!(
                std::env::var("COLORTERM").ok().as_deref(),
                Some("truecolor") | Some("24bit"),
            ),
        }
    }
}

/// Downsample 24-bit RGB to the xterm 256-color palette. Uses the standard
/// 6×6×6 cube plus the 24-step grayscale ramp.
pub fn rgb_to_256(r: u8, g: u8, b: u8) -> u8 {
    if r == g && g == b {
        if r < 8 { return 16; }
        if r > 248 { return 231; }
        return 232 + ((r as u16 - 8) * 24 / 240) as u8;
    }
    let q = |c: u8| -> u8 {
        if c < 48 { 0 }
        else if c < 115 { 1 }
        else { ((c as u16 - 35) / 40) as u8 }
    };
    16 + 36 * q(r) + 6 * q(g) + q(b)
}

/// Try to decode one grapheme cluster starting at `bytes[i]`.
/// Returns the cluster as &str and number of bytes consumed.
/// Returns None if `bytes[i..]` does not begin with a valid UTF-8 sequence.
fn decode_cluster(bytes: &[u8], i: usize) -> Option<(&str, usize)> {
    // Find the longest valid UTF-8 prefix starting at i (capped at 4 bytes
    // for the first codepoint, then continue while next codepoint is a
    // zero-width continuation of the same cluster).
    // Strategy: try to validate up to 4 bytes for the leading codepoint,
    // then extend as long as additional codepoints belong to the same cluster.

    // First, validate one codepoint.
    let max = (i + 4).min(bytes.len());
    let mut end = i;
    for try_end in (i + 1)..=max {
        if std::str::from_utf8(&bytes[i..try_end]).is_ok() {
            end = try_end;
            break;
        }
    }
    if end == i {
        return None;
    }

    // Now extend by additional valid codepoints that the segmenter groups
    // into the first cluster. Use unicode-segmentation for cluster boundaries.
    // We keep adding bytes (validated as UTF-8) until the cluster boundary
    // changes or we run out of bytes.
    let mut probe_end = end;
    loop {
        // Try extending by up to 4 more bytes.
        let probe_max = (probe_end + 4).min(bytes.len());
        let mut next_end = probe_end;
        for try_end in (probe_end + 1)..=probe_max {
            if std::str::from_utf8(&bytes[i..try_end]).is_ok() {
                next_end = try_end;
                break;
            }
        }
        if next_end == probe_end {
            break;
        }
        let candidate = std::str::from_utf8(&bytes[i..next_end]).unwrap();
        let cluster_count = candidate.graphemes(true).count();
        if cluster_count > 1 {
            // Adding broke into a new cluster; stop at probe_end.
            break;
        }
        probe_end = next_end;
    }

    Some((std::str::from_utf8(&bytes[i..probe_end]).unwrap(), probe_end - i))
}

/// In `AnsiMode::Interpret`, pre-filter the raw byte stream through the ANSI
/// parser and return a list of `(byte, style_at_byte, hyperlink_at_byte)` for
/// printable bytes only. ESC sequences consume bytes but produce no entries.
///
/// In `AnsiMode::Strict` / `AnsiMode::Raw`, every byte is printable (no
/// pre-filtering). Style is default and hyperlink is None for all entries.
fn prefilter(
    bytes: &[u8],
    mode: AnsiMode,
    state: Option<&mut RenderState>,
) -> Vec<(u8, crate::ansi::Style, Option<Arc<str>>)> {
    match mode {
        AnsiMode::Strict | AnsiMode::Raw => {
            // Bypass: every byte is printable with default style. Raw passthrough
            // is handled by the writer layer, not the render kernel.
            bytes
                .iter()
                .map(|&b| (b, crate::ansi::Style::default(), None))
                .collect()
        }
        AnsiMode::Interpret => {
            use crate::ansi::ParseStep;
            // Use a temporary local state when the caller passes None.
            let mut tmp;
            let st: &mut RenderState = match state {
                Some(s) => s,
                None => {
                    tmp = RenderState::default();
                    &mut tmp
                }
            };
            let mut out = Vec::with_capacity(bytes.len());
            for &b in bytes {
                let step =
                    crate::ansi::step(&mut st.parse, &mut st.style, &mut st.hyperlink, b);
                if let ParseStep::Printable(pb) = step {
                    let hl = st.hyperlink.as_deref().map(Arc::from);
                    out.push((pb, st.style, hl));
                }
            }
            out
        }
    }
}

pub fn render_line(
    bytes: &[u8],
    opts: &RenderOpts,
    state: Option<&mut RenderState>,
) -> Vec<Vec<Cell>> {
    let cols = opts.cols as usize;
    let mut rows: Vec<Vec<Cell>> = Vec::new();
    let mut current: Vec<Cell> = Vec::with_capacity(cols);

    // Pre-filter: resolve styles and strip escape sequences for Interpret mode.
    let filtered = prefilter(bytes, opts.mode, state);

    /// Returns true if the cell was dropped due to chop-mode overflow.
    /// The caller uses this to decide whether to paint the `rscroll` marker.
    fn push(current: &mut Vec<Cell>, rows: &mut Vec<Vec<Cell>>, cell: Cell, opts: &RenderOpts) -> bool {
        if current.len() >= opts.cols as usize {
            if opts.wrap {
                let mut full = std::mem::replace(current, Vec::with_capacity(opts.cols as usize));
                // `--wordwrap`: prefer a break on the last whitespace cell.
                // Anything past the break carries over to the next row as
                // its leading content. Falls back to mid-character break
                // when no whitespace is found.
                if opts.word_wrap {
                    if let Some(ws_idx) = (0..full.len()).rev().find(|&i| matches!(
                        full[i],
                        Cell::Char { ch, .. } if ch == ' ' || ch == '\t'
                    )) {
                        // Carry everything after the whitespace into the new
                        // current row (so the next word starts at column 0).
                        let carry: Vec<Cell> = full.drain((ws_idx + 1)..).collect();
                        *current = carry;
                    }
                }
                while full.len() < opts.cols as usize { full.push(Cell::Empty); }
                rows.push(full);
            } else {
                return true;
            }
        }
        current.push(cell);
        false
    }

    fn push_str(
        current: &mut Vec<Cell>,
        rows: &mut Vec<Vec<Cell>>,
        s: &str,
        style: crate::ansi::Style,
        hyperlink: Option<Arc<str>>,
        opts: &RenderOpts,
    ) -> bool {
        let mut overflowed = false;
        for c in s.chars() {
            overflowed |= push(
                current,
                rows,
                Cell::Char { ch: c, width: 1, style, hyperlink: hyperlink.clone() },
                opts,
            );
        }
        overflowed
    }

    fn push_wide(
        current: &mut Vec<Cell>,
        rows: &mut Vec<Vec<Cell>>,
        ch: char,
        width: u8,
        style: crate::ansi::Style,
        hyperlink: Option<Arc<str>>,
        opts: &RenderOpts,
    ) -> bool {
        let cols = opts.cols as usize;
        // If the wide char wouldn't fit in the remainder of this row, wrap first.
        if current.len() + width as usize > cols {
            if opts.wrap {
                let mut full = std::mem::replace(current, Vec::with_capacity(cols));
                // `--wordwrap`: prefer a break on the last whitespace. Same
                // logic as in `push`; kept duplicated rather than factored
                // out because the two helpers track `current.len()` slightly
                // differently and the inline form is easier to follow.
                if opts.word_wrap {
                    if let Some(ws_idx) = (0..full.len()).rev().find(|&i| matches!(
                        full[i],
                        Cell::Char { ch, .. } if ch == ' ' || ch == '\t'
                    )) {
                        let carry: Vec<Cell> = full.drain((ws_idx + 1)..).collect();
                        *current = carry;
                    }
                }
                while full.len() < cols { full.push(Cell::Empty); }
                rows.push(full);
            } else {
                return true; // chop overflow
            }
        }
        current.push(Cell::Char { ch, width, style, hyperlink });
        for _ in 1..width {
            current.push(Cell::Continuation);
        }
        false
    }

    // Walk filtered bytes (raw bytes for Strict, printable-only for Interpret).
    // Track chop-mode overflow so we can paint the rscroll marker afterward.
    let mut overflowed = false;
    let mut i = 0;
    while i < filtered.len() {
        let (b, style, hyperlink) = filtered[i].clone();
        if b == b'\t' {
            let stop = opts.tab_width.max(1) as usize;
            let cur_col = current.len();
            let next_stop = ((cur_col / stop) + 1) * stop;
            for _ in cur_col..next_stop {
                overflowed |= push(
                    &mut current,
                    &mut rows,
                    Cell::Char { ch: ' ', width: 1, style, hyperlink: hyperlink.clone() },
                    opts,
                );
            }
            i += 1;
        } else if b == b'\n' {
            i += 1;
        } else if b < 0x20 || b == 0x7F {
            let printable = if b == 0x7F { '?' } else { (b ^ 0x40) as char };
            overflowed |= push(
                &mut current,
                &mut rows,
                Cell::Char { ch: '^', width: 1, style, hyperlink: hyperlink.clone() },
                opts,
            );
            overflowed |= push(
                &mut current,
                &mut rows,
                Cell::Char { ch: printable, width: 1, style, hyperlink },
                opts,
            );
            i += 1;
        } else {
            // Try to decode a UTF-8 grapheme cluster. We reconstruct raw bytes
            // from the filtered stream for cluster decoding.
            let raw_bytes: Vec<u8> = filtered[i..].iter().map(|(b, _, _)| *b).collect();
            match decode_cluster(&raw_bytes, 0) {
                Some((cluster, consumed)) => {
                    let w = UnicodeWidthStr::width(cluster) as u8;
                    let base_char = cluster.chars().next().unwrap_or('\u{FFFD}');
                    if w == 0 {
                        // Lone combining mark with no base — emit replacement.
                        overflowed |= push(
                            &mut current,
                            &mut rows,
                            Cell::Char {
                                ch: '\u{FFFD}',
                                width: 1,
                                style,
                                hyperlink,
                            },
                            opts,
                        );
                    } else {
                        overflowed |= push_wide(&mut current, &mut rows, base_char, w, style, hyperlink, opts);
                    }
                    i += consumed;
                }
                None => {
                    // Invalid byte: emit <HH>, advance one byte.
                    let s = format!("<{:02X}>", b);
                    overflowed |= push_str(&mut current, &mut rows, &s, style, hyperlink, opts);
                    i += 1;
                }
            }
        }
    }

    while current.len() < cols {
        current.push(Cell::Empty);
    }

    // `--rscroll`: in chop mode, when the line overflowed the right edge,
    // replace the last cell with the marker char (styled dim) so the user
    // can see that content was truncated.
    if !opts.wrap && overflowed && cols > 0 {
        if let Some(marker) = opts.rscroll_char {
            current[cols - 1] = Cell::Char {
                ch: marker,
                width: 1,
                style: crate::ansi::Style { dim: true, ..Default::default() },
                hyperlink: None,
            };
        }
    }

    rows.push(current);
    rows
}

pub fn count_rows(
    bytes: &[u8],
    opts: &RenderOpts,
    state: Option<&mut RenderState>,
) -> usize {
    if !opts.wrap {
        return 1;
    }
    let cols = opts.cols.max(1) as usize;
    let mut col = 0usize;
    let mut rows = 1usize;

    let bump = |w: usize, col: &mut usize, rows: &mut usize| {
        if *col + w > cols {
            *rows += 1;
            *col = 0;
        }
        *col += w;
    };

    // Pre-filter: only printable bytes contribute to column count.
    let filtered = prefilter(bytes, opts.mode, state);

    let mut i = 0;
    while i < filtered.len() {
        let (b, _, _) = filtered[i];
        if b == b'\t' {
            let stop = opts.tab_width.max(1) as usize;
            let next_stop = ((col / stop) + 1) * stop;
            let advance = next_stop - col;
            // Tabs may overflow into multiple wraps if cols < tab_width.
            for _ in 0..advance {
                bump(1, &mut col, &mut rows);
            }
            i += 1;
        } else if b == b'\n' {
            i += 1;
        } else if b < 0x20 || b == 0x7F {
            bump(1, &mut col, &mut rows); // ^
            bump(1, &mut col, &mut rows); // X
            i += 1;
        } else {
            let raw_bytes: Vec<u8> = filtered[i..].iter().map(|(b, _, _)| *b).collect();
            match decode_cluster(&raw_bytes, 0) {
                Some((cluster, consumed)) => {
                    let w = UnicodeWidthStr::width(cluster);
                    let w = if w == 0 { 1 } else { w };
                    bump(w, &mut col, &mut rows);
                    i += consumed;
                }
                None => {
                    // <HH> = 4 cells
                    for _ in 0..4 { bump(1, &mut col, &mut rows); }
                    i += 1;
                }
            }
        }
    }
    rows
}

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

    #[test]
    fn rgb_to_256_pure_corners_map_to_palette_extremes() {
        assert_eq!(rgb_to_256(0, 0, 0), 16);
        assert_eq!(rgb_to_256(255, 255, 255), 231);
    }

    #[test]
    fn rgb_to_256_mid_gray_lands_in_grayscale_ramp() {
        let n = rgb_to_256(128, 128, 128);
        assert!((232..=255).contains(&n), "expected grayscale slot 232..=255, got {n}");
    }

    #[test]
    fn rgb_to_256_pure_rgb_lands_in_cube_extremes() {
        assert_eq!(rgb_to_256(255, 0, 0), 196);
        assert_eq!(rgb_to_256(0, 255, 0), 46);
        assert_eq!(rgb_to_256(0, 0, 255), 21);
    }

    #[test]
    fn rgb_to_256_low_channel_quantizes_to_zero() {
        // 256-cube index = 16 + 36*r6 + 6*g6 + b6, here r6=0 g6=4 b6=0 -> 40.
        assert_eq!(rgb_to_256(40, 200, 0), 40);
    }

    #[test]
    fn rgb_to_256_near_black_gray_is_palette_black() {
        assert_eq!(rgb_to_256(5, 5, 5), 16);
    }

    #[test]
    fn rgb_to_256_near_white_gray_is_palette_white() {
        assert_eq!(rgb_to_256(250, 250, 250), 231);
    }

    #[test]
    fn truecolor_always_resolves_true_regardless_of_env() {
        assert!(TrueColor::Always.resolve());
    }

    #[test]
    fn truecolor_never_resolves_false_regardless_of_env() {
        assert!(!TrueColor::Never.resolve());
    }

    #[test]
    fn rscroll_marker_appears_on_chopped_row() {
        let mut o = opts(5, false); // 5 cols, chop mode
        o.rscroll_char = Some('>');
        let rows = render_line(b"abcdefgh", &o, None);
        assert_eq!(rows.len(), 1);
        match &rows[0][4] {
            Cell::Char { ch, .. } => assert_eq!(*ch, '>'),
            other => panic!("expected `>` marker, got {other:?}"),
        }
    }

    #[test]
    fn rscroll_marker_absent_on_fitting_row() {
        let mut o = opts(10, false);
        o.rscroll_char = Some('>');
        let rows = render_line(b"abc", &o, None);
        match &rows[0][2] {
            Cell::Char { ch, .. } => assert_eq!(*ch, 'c'),
            other => panic!("expected content `c`, got {other:?}"),
        }
    }

    #[test]
    fn rscroll_marker_disabled_emits_normal_chop() {
        let mut o = opts(5, false);
        o.rscroll_char = None;
        let rows = render_line(b"abcdefgh", &o, None);
        match &rows[0][4] {
            Cell::Char { ch, .. } => assert_eq!(*ch, 'e'),
            other => panic!("expected last fitting char, got {other:?}"),
        }
    }

    #[test]
    fn word_wrap_breaks_on_whitespace() {
        let mut o = opts(8, true);
        o.word_wrap = true;
        let rows = render_line(b"the quick brown fox", &o, None);
        // First row should break at the last whitespace before col 8.
        let r0: String = rows[0].iter().filter_map(|c| match c {
            Cell::Char { ch, .. } => Some(*ch),
            _ => None,
        }).collect();
        assert_eq!(r0.trim_end(), "the");
    }

    #[test]
    fn word_wrap_falls_back_when_no_whitespace_fits() {
        let mut o = opts(5, true);
        o.word_wrap = true;
        let rows = render_line(b"antidisestablishment", &o, None);
        let r0: String = rows[0].iter().filter_map(|c| match c {
            Cell::Char { ch, .. } => Some(*ch),
            _ => None,
        }).collect();
        // No whitespace anywhere → mid-character break preserved.
        assert_eq!(r0.trim_end(), "antid");
    }

    #[test]
    fn word_wrap_off_breaks_mid_word() {
        let mut o = opts(8, true);
        o.word_wrap = false;
        let rows = render_line(b"the quick brown fox", &o, None);
        let r0: String = rows[0].iter().filter_map(|c| match c {
            Cell::Char { ch, .. } => Some(*ch),
            _ => None,
        }).collect();
        // First 8 chars verbatim: "the quic"
        assert_eq!(r0.trim_end(), "the quic");
    }

    #[test]
    fn rscroll_marker_absent_in_wrap_mode() {
        let mut o = opts(5, true);
        o.rscroll_char = Some('>');
        let rows = render_line(b"abcdefgh", &o, None);
        // Wrap mode produces multiple rows; rscroll only fires in chop.
        assert!(rows.len() > 1);
        for row in &rows {
            for cell in row {
                if let Cell::Char { ch, .. } = cell {
                    assert_ne!(*ch, '>', "rscroll marker leaked into wrap mode");
                }
            }
        }
    }

    fn opts(cols: u16, wrap: bool) -> RenderOpts {
        RenderOpts { tab_width: 8, wrap, cols, mode: AnsiMode::Strict, rscroll_char: None, word_wrap: false }
    }

    fn ch(c: char) -> Cell {
        Cell::Char { ch: c, width: 1, style: crate::ansi::Style::default(), hyperlink: None }
    }

    #[test]
    fn ascii_short_line_pads_to_cols() {
        let rows = render_line(b"hi", &opts(5, true), None);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0], vec![ch('h'), ch('i'), Cell::Empty, Cell::Empty, Cell::Empty]);
    }

    #[test]
    fn ascii_exact_width() {
        let rows = render_line(b"hello", &opts(5, true), None);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0], vec![ch('h'), ch('e'), ch('l'), ch('l'), ch('o')]);
    }

    #[test]
    fn empty_input_yields_one_empty_row() {
        let rows = render_line(b"", &opts(3, true), None);
        assert_eq!(rows, vec![vec![Cell::Empty, Cell::Empty, Cell::Empty]]);
    }

    #[test]
    fn tab_at_col_zero_expands_to_eight() {
        let rows = render_line(b"\tx", &opts(20, true), None);
        // Eight spaces, then 'x', then padding.
        for (i, cell) in rows[0].iter().take(8).enumerate() {
            assert_eq!(*cell, ch(' '), "col {i} should be space");
        }
        assert_eq!(rows[0][8], ch('x'));
    }

    #[test]
    fn tab_at_col_three_advances_to_next_stop() {
        // "abc\tx" → cols 0,1,2 = a,b,c; tab fills to col 8 with spaces; col 8 = x
        let rows = render_line(b"abc\tx", &opts(20, true), None);
        assert_eq!(rows[0][0], ch('a'));
        assert_eq!(rows[0][2], ch('c'));
        for cell in rows[0].iter().skip(3).take(5) {
            assert_eq!(*cell, ch(' '));
        }
        assert_eq!(rows[0][8], ch('x'));
    }

    #[test]
    fn tab_at_col_eight_advances_to_sixteen() {
        let mut input = vec![b'a'; 8];
        input.push(b'\t');
        input.push(b'x');
        let rows = render_line(&input, &opts(20, true), None);
        for cell in rows[0].iter().skip(8).take(8) {
            assert_eq!(*cell, ch(' '));
        }
        assert_eq!(rows[0][16], ch('x'));
    }

    #[test]
    fn null_renders_as_caret_at() {
        let rows = render_line(b"\0", &opts(5, true), None);
        assert_eq!(rows[0][0], ch('^'));
        assert_eq!(rows[0][1], ch('@'));
    }

    #[test]
    fn esc_renders_as_caret_lbracket() {
        let rows = render_line(b"\x1b", &opts(5, true), None);
        assert_eq!(rows[0][0], ch('^'));
        assert_eq!(rows[0][1], ch('['));
    }

    #[test]
    fn del_renders_as_caret_question() {
        let rows = render_line(b"\x7f", &opts(5, true), None);
        assert_eq!(rows[0][0], ch('^'));
        assert_eq!(rows[0][1], ch('?'));
    }

    #[test]
    fn invalid_utf8_byte_renders_as_angle_hex() {
        let rows = render_line(&[0xFF], &opts(8, true), None);
        assert_eq!(rows[0][0], ch('<'));
        assert_eq!(rows[0][1], ch('F'));
        assert_eq!(rows[0][2], ch('F'));
        assert_eq!(rows[0][3], ch('>'));
    }

    #[test]
    fn partial_multibyte_each_byte_renders_separately() {
        // 0xC3 starts a 2-byte sequence; alone it's invalid → <C3>
        let rows = render_line(&[0xC3], &opts(8, true), None);
        assert_eq!(rows[0][0], ch('<'));
        assert_eq!(rows[0][1], ch('C'));
        assert_eq!(rows[0][2], ch('3'));
        assert_eq!(rows[0][3], ch('>'));
    }

    #[test]
    fn single_byte_utf8_e_acute() {
        let rows = render_line("é".as_bytes(), &opts(5, true), None);
        assert_eq!(
            rows[0][0],
            Cell::Char { ch: 'é', width: 1, style: crate::ansi::Style::default(), hyperlink: None }
        );
    }

    #[test]
    fn cjk_char_takes_two_columns() {
        // 日 is width 2.
        let rows = render_line("".as_bytes(), &opts(5, true), None);
        assert_eq!(
            rows[0][0],
            Cell::Char { ch: '', width: 2, style: crate::ansi::Style::default(), hyperlink: None }
        );
        assert_eq!(rows[0][1], Cell::Continuation);
        assert_eq!(rows[0][2], Cell::Empty);
    }

    #[test]
    fn emoji_takes_two_columns() {
        let rows = render_line("🦀".as_bytes(), &opts(5, true), None);
        // Width depends on unicode-width; crab emoji is width 2.
        assert!(matches!(rows[0][0], Cell::Char { width: 2, .. }));
        assert_eq!(rows[0][1], Cell::Continuation);
    }

    #[test]
    fn combining_mark_folds_into_prior_cell() {
        // "e\u{0301}" is one grapheme cluster (e with combining acute).
        let rows = render_line("e\u{0301}".as_bytes(), &opts(5, true), None);
        // Cluster renders as a single cell carrying base char.
        assert!(matches!(rows[0][0], Cell::Char { width: 1, .. }));
        assert_eq!(rows[0][1], Cell::Empty);
    }

    #[test]
    fn wrap_long_line_into_multiple_rows() {
        let rows = render_line(b"abcdefghij", &opts(4, true), None);
        assert_eq!(rows.len(), 3);
        assert_eq!(rows[0], vec![ch('a'), ch('b'), ch('c'), ch('d')]);
        assert_eq!(rows[1], vec![ch('e'), ch('f'), ch('g'), ch('h')]);
        assert_eq!(rows[2], vec![ch('i'), ch('j'), Cell::Empty, Cell::Empty]);
    }

    #[test]
    fn chop_long_line_truncates() {
        let rows = render_line(b"abcdefghij", &opts(4, false), None);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0], vec![ch('a'), ch('b'), ch('c'), ch('d')]);
    }

    #[test]
    fn wide_char_at_boundary_pushed_to_next_row() {
        // cols=3, content "ab日" — 日 is width 2, doesn't fit at col 2,
        // so row 0 = a, b, Empty; row 1 = 日(continuation), Empty.
        let rows = render_line("ab日".as_bytes(), &opts(3, true), None);
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0], vec![ch('a'), ch('b'), Cell::Empty]);
        assert_eq!(
            rows[1][0],
            Cell::Char { ch: '', width: 2, style: crate::ansi::Style::default(), hyperlink: None }
        );
        assert_eq!(rows[1][1], Cell::Continuation);
        assert_eq!(rows[1][2], Cell::Empty);
    }

    #[test]
    fn count_rows_matches_render_line_for_short() {
        let o = opts(80, true);
        let bytes = b"hello world";
        assert_eq!(count_rows(bytes, &o, None), render_line(bytes, &o, None).len());
    }

    #[test]
    fn count_rows_matches_render_line_for_long_wrap() {
        let o = opts(4, true);
        let bytes = b"abcdefghij";
        assert_eq!(count_rows(bytes, &o, None), render_line(bytes, &o, None).len());
    }

    #[test]
    fn count_rows_chop_is_one() {
        let o = opts(4, false);
        let bytes = b"abcdefghij";
        assert_eq!(count_rows(bytes, &o, None), 1);
    }

    #[test]
    fn count_rows_handles_wide_char() {
        let o = opts(3, true);
        let bytes = "ab日".as_bytes();
        assert_eq!(count_rows(bytes, &o, None), render_line(bytes, &o, None).len());
    }

    // ---- Interpret-mode tests ----

    fn interpret_opts() -> RenderOpts {
        RenderOpts { mode: AnsiMode::Interpret, ..Default::default() }
    }

    #[test]
    fn interpret_red_text() {
        let mut state = RenderState::default();
        let rows = render_line(b"\x1b[31mhi", &interpret_opts(), Some(&mut state));
        let cells: Vec<&Cell> =
            rows.iter().flatten().filter(|c| matches!(c, Cell::Char { .. })).collect();
        assert_eq!(cells.len(), 2);
        for c in cells {
            if let Cell::Char { style, .. } = c {
                assert_eq!(style.fg, Some(crate::ansi::Color::Ansi(1)));
            }
        }
    }

    #[test]
    fn interpret_truecolor() {
        let mut state = RenderState::default();
        let rows =
            render_line(b"\x1b[38;2;255;0;0mfoo", &interpret_opts(), Some(&mut state));
        let cells: Vec<&Cell> =
            rows.iter().flatten().filter(|c| matches!(c, Cell::Char { .. })).collect();
        for c in cells {
            if let Cell::Char { style, .. } = c {
                assert_eq!(style.fg, Some(crate::ansi::Color::Rgb(255, 0, 0)));
            }
        }
    }

    #[test]
    fn interpret_wide_char_carries_color() {
        let mut state = RenderState::default();
        let rows =
            render_line("\x1b[31m日".as_bytes(), &interpret_opts(), Some(&mut state));
        let jp_cell = rows.iter().flatten().find_map(|c| match c {
            Cell::Char { ch: '', style, width, .. } => Some((style, *width)),
            _ => None,
        });
        let (style, width) = jp_cell.expect("expected 日 cell");
        assert_eq!(style.fg, Some(crate::ansi::Color::Ansi(1)));
        assert_eq!(width, 2);
    }

    #[test]
    fn interpret_state_persists_across_calls() {
        let mut state = RenderState::default();
        let _ = render_line(b"\x1b[31mline1", &interpret_opts(), Some(&mut state));
        let rows = render_line(b"line2", &interpret_opts(), Some(&mut state));
        let l_cell = rows.iter().flatten().find_map(|c| match c {
            Cell::Char { ch: 'l', style, .. } => Some(style),
            _ => None,
        });
        assert_eq!(
            l_cell.expect("expected l cell").fg,
            Some(crate::ansi::Color::Ansi(1))
        );
    }

    #[test]
    fn interpret_reset_clears_state() {
        let mut state = RenderState::default();
        let _ =
            render_line(b"\x1b[31mline1\x1b[0m", &interpret_opts(), Some(&mut state));
        let rows = render_line(b"line2", &interpret_opts(), Some(&mut state));
        let l_cell = rows.iter().flatten().find_map(|c| match c {
            Cell::Char { ch: 'l', style, .. } => Some(style),
            _ => None,
        });
        assert_eq!(l_cell.expect("expected l cell"), &crate::ansi::Style::default());
    }

    #[test]
    fn interpret_non_sgr_csi_is_zero_width() {
        let mut state = RenderState::default();
        let rows = render_line(b"\x1b[2Jdata", &interpret_opts(), Some(&mut state));
        let chars: String = rows
            .iter()
            .flatten()
            .filter_map(|c| match c {
                Cell::Char { ch, .. } => Some(*ch),
                _ => None,
            })
            .collect();
        assert_eq!(chars, "data");
    }

    #[test]
    fn strict_mode_esc_still_renders_as_caret_lbracket() {
        // LOCKDOWN: pre-0.18 behavior must survive.
        let rows = render_line(b"\x1b[31mhi", &RenderOpts::default(), None);
        let chars: String = rows
            .iter()
            .flatten()
            .filter_map(|c| match c {
                Cell::Char { ch, .. } => Some(*ch),
                _ => None,
            })
            .collect();
        assert!(chars.starts_with("^["), "got: {chars:?}");
    }

    #[test]
    fn osc8_hyperlink_attached_to_cells() {
        let mut state = RenderState::default();
        let rows = render_line(
            b"\x1b]8;;https://example.com\x07click\x1b]8;;\x07",
            &interpret_opts(),
            Some(&mut state),
        );
        let click_cell = rows.iter().flatten().find_map(|c| match c {
            Cell::Char { ch: 'c', hyperlink, .. } => Some(hyperlink.clone()),
            _ => None,
        });
        let link = click_cell.expect("expected c cell").expect("expected hyperlink");
        assert_eq!(link.as_ref(), "https://example.com");
    }
}