txtview 0.1.3

A lightweight terminal text viewer with scrolling, wrapping, line numbers, and an optional interactive scrollbar
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
//! Line wrapping that keeps ANSI styling intact while neutralizing control
//! bytes and non-SGR escape sequences.

use std::borrow::Cow;

use unicode_segmentation::UnicodeSegmentation;

use super::ansi::{Esc, caret_notation, caret_width, display_width, escape_display, parse_escape};
use super::sgr::SgrState;

/// Columns between tab stops.
const TAB_WIDTH: usize = 8;

/// The terminal columns a grapheme cluster starting at column `col` occupies,
/// measured the way the wrapper lays rows out: a tab advances to its next
/// 8-column stop, a control character takes its caret notation width, and
/// anything else keeps its [`cluster_visual_width`]. Both the wrapper and
/// [`write_visible_row`](super::write_visible_row) measure rows through this
/// one function.
pub(super) fn cluster_width_at(cluster: &str, col: usize) -> usize {
    let c = cluster.chars().next().unwrap_or('\u{fffd}');
    match c {
        '\t' => TAB_WIDTH - col % TAB_WIDTH,
        c if c.is_control() => {
            if caret_width(c) {
                2
            } else {
                1
            }
        }
        _ => cluster_visual_width(cluster),
    }
}

/// Wrap `line` into visual rows of at most `width` terminal columns.
///
/// `start_col` is the width of any prefix the caller renders before the
/// line, used so tabs land on their column stops and wraps line up under
/// the prefix.
///
/// The wrapping preserves safe content verbatim and neutralizes the rest:
/// - text is measured per grapheme cluster, so combining marks, skin-tone
///   modifiers, ZWJ sequences and flag pairs never split across a wrap,
/// - SGR (`ESC [...]m`) and OSC8 hyperlinks pass through raw (OSC8 emits as
///   one atomic unit so a wrap can never split it),
/// - C0 and C1 controls, DEL, and every other escape become visible caret
///   notation,
/// - tabs stay as literal bytes in the wrapped row, measured at their
///   8-column stop, a tab whose stop lies beyond the wrap width renders as
///   spaces filling the row so the row never exceeds the width. The display
///   rows can therefore contain literal tabs, the write layer expands them
///   to spaces, so a tab byte itself never reaches the terminal
///   ([`write_visible_row`](super::write_visible_row)).
pub(crate) fn wrap_line_ansi(line: &str, width: usize, start_col: usize) -> Vec<String> {
    let width = width.max(1);
    let bytes = line.as_bytes();
    let mut chunks = Vec::new();
    let mut current_chunk = String::new();
    let mut visible_width = 0;
    let mut style = SgrState::default();

    let mut i = 0;
    while i < bytes.len() {
        let (piece, mut piece_width, is_tab, next): (Cow<'_, str>, usize, bool, usize) =
            if bytes[i] == 0x1b {
                let (end, kind) = parse_escape(bytes, i);
                let seq = &line[i..end];
                match kind {
                    Esc::Sgr => {
                        // SGR passes through raw so styling works and is folded
                        // into the live state for re-emission after a wrap
                        style.apply(seq);
                        (Cow::Borrowed(seq), 0, false, end)
                    }
                    Esc::Osc8 => {
                        // OSC8 hyperlinks pass through raw so they render as real
                        // links, emitted as one atomic unit (opener, payload and
                        // terminator together) so a wrap can never split them.
                        // OSC bytes take zero terminal columns, so no width
                        (Cow::Borrowed(seq), 0, false, end)
                    }
                    Esc::Visible => {
                        // Any other escape (OSC titles, cursor moves, clears, CSI,
                        // 2-byte) is shown as visible caret notation, like `less`.
                        // It is emitted as one atomic unit: never executed, never
                        // split across a wrap boundary
                        let display = escape_display(seq);
                        let width = display_width(&display);
                        (Cow::Owned(display), width, false, end)
                    }
                }
            } else {
                // Process a full extended grapheme cluster together so a wrap can
                // never split a base char from its combining marks, skin-tone
                // modifiers, ZWJ sequences or flag pairs
                let cluster = &line[i..].graphemes(true).next().unwrap_or_default();
                let is_tab = *cluster == "\t";
                let end = i + cluster.len();
                let c = cluster.chars().next().unwrap_or('\u{fffd}');
                // Map the character to the text placed in the chunk, neutralizing
                // control bytes so they cannot corrupt the terminal:
                // - tab keeps its literal byte in the display row (wrapped at
                //   its 8-column stop, the write layer expands it to spaces),
                // - C0/C1 controls and DEL become visible caret notation.
                let replacement: Cow<'_, str> = match c {
                    '\t' => Cow::Borrowed(cluster),
                    c if c.is_control() => Cow::Owned(caret_notation(c)),
                    _ => Cow::Borrowed(cluster),
                };
                let ch_width = cluster_width_at(cluster, start_col + visible_width);
                (replacement, ch_width, is_tab, end)
            };
        i = next;
        // Once a piece is moved to a fresh row its start column changes, so
        // when a tab is the piece that wrapped its advance has to be measured
        // again from that fresh row's first column
        if visible_width + piece_width > width && !current_chunk.is_empty() {
            let prefix = style.to_ansi();
            if prefix.is_some() {
                current_chunk.push_str("\x1b[0m");
            }
            chunks.push(current_chunk);
            current_chunk = String::new();
            visible_width = 0;
            if let Some(prefix) = prefix {
                current_chunk.push_str(&prefix);
            }
            if is_tab {
                piece_width = TAB_WIDTH - (start_col + visible_width) % TAB_WIDTH;
            }
        }
        if piece_width > width && is_tab {
            // A tab whose stop lies beyond the whole row cannot reach it, so
            // it renders as spaces filling the row and never pushes the
            // cursor past the wrap width. Grapheme clusters stay whole even
            // when wider than the width (they cannot be split), so only tabs
            // get this substitution
            current_chunk.push_str(&" ".repeat(width));
            visible_width = width;
        } else {
            current_chunk.push_str(&piece);
            visible_width += piece_width;
        }
    }

    if !current_chunk.is_empty() {
        if style.to_ansi().is_some() {
            current_chunk.push_str("\x1b[0m");
        }
        chunks.push(current_chunk);
    }

    if chunks.is_empty() {
        chunks.push(String::new());
    }

    chunks
}

/// Visual width of a grapheme cluster. Emoji-presentation sequences carry the
/// U+FE0F variation selector (e.g. ❤️, ©️) or a U+20E3 keycap (e.g. #️⃣, 1️⃣)
/// for rendering at double width in modern terminals; `unicode-width` keeps
/// the base char's text width, so bump those clusters to two columns.
pub(super) fn cluster_visual_width(cluster: &str) -> usize {
    let width = display_width(cluster);
    if cluster.contains('\u{fe0f}') || cluster.contains('\u{20e3}') {
        width.max(2)
    } else {
        width
    }
}

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

    #[test]
    fn csi_ending_in_multibyte_keeps_the_char() {
        assert_eq!(wrap_line_ansi("\x1b[中", 20, 0), vec!["^[[中"]);
        assert_eq!(wrap_line_ansi("\x1b[31中x", 20, 0), vec!["^[[31中x"]);
    }

    #[test]
    fn adversarial_inputs_do_not_panic() {
        let inputs = [
            "\x1b[🎉",
            "\x1b[中x",
            "a\x1b[🎉b",
            "🎉\x1b[🎉",
            "\x1b\x1b[中",
            "\x1b[3;🎉",
            "\x1b[;中",
            "\x1b[中\x1b[0m",
        ];
        for input in inputs {
            let _ = wrap_line_ansi(input, 4, 0);
        }
    }

    #[test]
    fn grapheme_clusters_are_not_split() {
        assert_eq!(
            wrap_line_ansi("🎉🏽👍🇺🇸abc", 4, 0),
            vec!["🎉🏽", "👍🇺🇸", "abc"]
        );
        assert_eq!(wrap_line_ansi("e\u{301}x", 1, 0), vec!["e\u{301}", "x"]);
        assert_eq!(
            wrap_line_ansi("x👨\u{200d}👩\u{200d}👧y", 4, 0),
            vec!["x", "👨\u{200d}👩\u{200d}👧", "y"]
        );
    }

    #[test]
    fn keycap_sequence_counts_two_columns() {
        let chunks = wrap_line_ansi("#\u{fe0f}\u{20e3}x", 2, 0);
        assert_eq!(chunks, vec!["#\u{fe0f}\u{20e3}", "x"]);
    }

    #[test]
    fn vs16_emoji_counts_two_columns() {
        let chunks = wrap_line_ansi("❤\u{fe0f}x", 2, 0);
        assert_eq!(chunks, vec!["❤\u{fe0f}", "x"]);
    }

    #[test]
    fn vs15_emoji_keeps_text_width() {
        let chunks = wrap_line_ansi("❤\u{fe0e}x", 2, 0);
        assert_eq!(chunks, vec!["❤\u{fe0e}x"]);
    }

    #[test]
    fn ansi_single_style_fits_one_row() {
        let line = "\x1b[31mhello\x1b[0m";
        let chunks = wrap_line_ansi(line, 10, 0);
        assert_eq!(chunks, vec!["\x1b[31mhello\x1b[0m"]);
    }

    #[test]
    fn ansi_wraps_without_splitting_codes() {
        let line = "\x1b[31m12345\x1b[0m";
        let chunks = wrap_line_ansi(line, 3, 0);
        assert_eq!(chunks.len(), 2);
        assert!(chunks[0].contains("\x1b[31m"));
        assert!(chunks[0].contains("123"));
        assert!(chunks[0].ends_with("\x1b[0m"));
        assert!(chunks[1].contains("\x1b[31m"));
        assert!(chunks[1].contains("45"));
        assert!(chunks[1].ends_with("\x1b[0m"));
    }

    #[test]
    fn ansi_state_cleared_by_reset() {
        let line = "\x1b[31mabc\x1b[0mdefghi";
        let chunks = wrap_line_ansi(line, 3, 0);
        assert_eq!(chunks.len(), 3);
        assert_eq!(chunks[0], "\x1b[31mabc\x1b[0m");
        assert_eq!(chunks[1], "def");
        assert_eq!(chunks[2], "ghi");
    }

    #[test]
    fn empty_sgr_reset_clears_tracked_state() {
        let chunks = wrap_line_ansi("\x1b[31mab\x1b[mcd\x1b[0m", 3, 0);
        assert_eq!(chunks, vec!["\x1b[31mab\x1b[mc", "d\x1b[0m"]);
    }

    #[test]
    fn attribute_off_removes_attribute_from_replay() {
        let chunks = wrap_line_ansi("\x1b[1mbold\x1b[22mnormal", 4, 0);
        assert_eq!(chunks, vec!["\x1b[1mbold\x1b[22m", "norm", "al"]);
    }

    #[test]
    fn replayed_state_is_canonical_and_compact() {
        let chunks = wrap_line_ansi("\x1b[1m\x1b[31mabcdef\x1b[0m", 3, 0);
        assert_eq!(
            chunks,
            vec!["\x1b[1m\x1b[31mabc\x1b[0m", "\x1b[1;31mdef\x1b[0m"]
        );
    }

    #[test]
    fn extended_color_survives_replay() {
        let chunks = wrap_line_ansi("\x1b[38;5;123mabcdef\x1b[0m", 3, 0);
        assert_eq!(
            chunks,
            vec!["\x1b[38;5;123mabc\x1b[0m", "\x1b[38;5;123mdef\x1b[0m"]
        );
    }

    #[test]
    fn newer_foreground_replaces_older_in_replay() {
        let chunks = wrap_line_ansi("\x1b[31m\x1b[32mabcdef\x1b[0m", 3, 0);
        assert_eq!(
            chunks,
            vec!["\x1b[31m\x1b[32mabc\x1b[0m", "\x1b[32mdef\x1b[0m"]
        );
    }

    #[test]
    fn plain_text_unchanged() {
        let line = "hello world";
        let chunks = wrap_line_ansi(line, 5, 0);
        assert_eq!(chunks, vec!["hello", " worl", "d"]);
    }

    #[test]
    fn tab_advances_to_next_stop() {
        let chunks = wrap_line_ansi("a\tb", 8, 0);
        assert_eq!(chunks, vec!["a\t", "b"]);
    }

    #[test]
    fn tab_overshoots_own_row_when_it_does_not_fit() {
        let chunks = wrap_line_ansi("a\tb", 5, 0);
        assert_eq!(chunks, vec!["a", "     ", "b"]);
    }

    #[test]
    fn tab_at_column_zero_advances_full_width() {
        let chunks = wrap_line_ansi("\t\tx", 8, 0);
        assert_eq!(chunks, vec!["\t", "\t", "x"]);
    }

    #[test]
    fn tab_advance_accounts_for_prefix_column() {
        assert_eq!(wrap_line_ansi("\tX", 5, 0), vec!["     ", "X"]);
        assert_eq!(wrap_line_ansi("\tX", 5, 5), vec!["\tX"]);
    }

    #[test]
    fn mid_tab_stop_wraps_tab_followed_by_text() {
        let chunks = wrap_line_ansi("abcdefgh\tx", 12, 0);
        assert_eq!(chunks, vec!["abcdefgh", "\tx"]);
    }

    #[test]
    fn tab_mid_line_wraps_after_the_stop() {
        assert_eq!(wrap_line_ansi("abc\tdef", 8, 0), vec!["abc\t", "def"]);
        assert_eq!(wrap_line_ansi("ab\tcde", 8, 0), vec!["ab\t", "cde"]);
    }

    #[test]
    fn tab_at_line_end_and_following_wrap() {
        assert_eq!(wrap_line_ansi("ab\tcd", 4, 0), vec!["ab", "    ", "cd"]);
    }

    #[test]
    fn styled_tab_on_narrow_viewport_never_exceeds_the_width() {
        let chunks = wrap_line_ansi("\x1b[31ma\tb\x1b[0m", 5, 0);
        assert_eq!(
            chunks,
            vec![
                "\x1b[31ma\x1b[0m",
                "\x1b[31m     \x1b[0m",
                "\x1b[31mb\x1b[0m"
            ]
        );
    }

    #[test]
    fn emoji_wider_than_the_viewport_stays_whole() {
        assert_eq!(
            wrap_line_ansi("🎉", 1, 0),
            vec!["🎉"],
            "a grapheme wider than the viewport is never split or dropped"
        );
        assert_eq!(wrap_line_ansi("🎉\t🎉", 1, 0), vec!["🎉", " ", "🎉"]);
    }

    #[test]
    fn tab_survives_ansi_prefix_replay_on_next_row() {
        let chunks = wrap_line_ansi("\x1b[31mabcd\tef\x1b[0m", 8, 0);
        assert_eq!(chunks, vec!["\x1b[31mabcd\t\x1b[0m", "\x1b[31mef\x1b[0m"]);
    }

    #[test]
    fn tab_after_wide_char_counts_remaining_stop() {
        assert_eq!(wrap_line_ansi("中\tx", 10, 0), vec!["中\tx"]);
        assert_eq!(wrap_line_ansi("中\tx", 8, 0), vec!["中\t", "x"]);
    }

    #[test]
    fn control_chars_become_caret_notation() {
        let chunks = wrap_line_ansi("a\x07b\x08c\x7fd", 20, 0);
        assert_eq!(chunks, vec!["a^Gb^Hc^?d"]);
    }

    #[test]
    fn c1_controls_become_caret_notation() {
        // U+009B is the 8-bit CSI and U+0085 the 8-bit NEL; fed to a terminal
        // raw they execute, so they must become visible caret notation.
        assert_eq!(wrap_line_ansi("a\u{9b}2Jb\u{85}", 20, 0), vec!["a^[2Jb^E"]);
        assert_eq!(
            wrap_line_ansi("\u{9b}2J", 3, 0),
            vec!["^[2", "J"],
            "8-bit CSI caret notation must count two columns"
        );
        assert_eq!(
            wrap_line_ansi("\u{80}\u{9f}", 20, 0),
            vec!["^@^_"],
            "C1 maps to the same caret range as its C0 equivalent"
        );
    }

    #[test]
    fn caret_notation_counts_two_columns() {
        assert_eq!(wrap_line_ansi("ab\x07cd", 4, 0), vec!["ab^G", "cd"]);
    }

    #[test]
    fn caret_notation_carries_ansi_state_across_wrap() {
        let chunks = wrap_line_ansi("\x1b[31mab\x07cd\x1b[0m", 4, 0);
        assert_eq!(chunks, vec!["\x1b[31mab^G\x1b[0m", "\x1b[31mcd\x1b[0m"]);
    }

    #[test]
    fn sgr_passes_through_lone_control_becomes_caret() {
        let chunks = wrap_line_ansi("\x1b[1mhi\x07\x1b[0m", 20, 0);
        assert_eq!(chunks, vec!["\x1b[1mhi^G\x1b[0m"]);
    }

    #[test]
    fn non_sgr_csi_is_visible_not_executed() {
        assert_eq!(wrap_line_ansi("\x1b[2Aab", 20, 0), vec!["^[[2Aab"]);
        assert_eq!(wrap_line_ansi("\x1b[2Jx", 20, 0), vec!["^[[2Jx"]);
    }

    #[test]
    fn csi_with_non_alpha_final_byte_is_visible() {
        assert_eq!(wrap_line_ansi("\x1b[2~ab", 20, 0), vec!["^[[2~ab"]);
    }

    #[test]
    fn osc8_hyperlink_passes_through_whole() {
        let chunks = wrap_line_ansi("\x1b]8;;https://x.dev\x07here", 8, 0);
        assert_eq!(chunks, vec!["\x1b]8;;https://x.dev\x07here"]);
    }

    #[test]
    fn osc8_never_split_across_wrap() {
        let chunks = wrap_line_ansi("abcde\x1b]8;;u\x07fghij", 5, 0);
        assert_eq!(chunks, vec!["abcde\x1b]8;;u\x07", "fghij"]);
    }

    #[test]
    fn osc8_kept_with_styling_across_wrap() {
        let chunks = wrap_line_ansi("\x1b[31mabc\x1b]8;;u\x07defgh", 5, 0);
        assert_eq!(
            chunks,
            vec!["\x1b[31mabc\x1b]8;;u\x07de\x1b[0m", "\x1b[31mfgh\x1b[0m"]
        );
    }

    #[test]
    fn osc8_hyperlink_reset_link_passes_through() {
        let chunks = wrap_line_ansi("\x1b]8;;u\x07go\x1b]8;;\x07", 40, 0);
        assert_eq!(chunks, vec!["\x1b]8;;u\x07go\x1b]8;;\x07"]);
    }

    #[test]
    fn unterminated_osc8_is_visible_not_raw() {
        let chunks = wrap_line_ansi("\x1b]8;;https://x.dev", 40, 0);
        assert_eq!(chunks, vec!["^[]8;;https://x.dev"]);
    }

    #[test]
    fn non_osc8_osc_stays_visible() {
        let chunks = wrap_line_ansi("\x1b]0;longtitle\x07body", 8, 0);
        assert_eq!(chunks, vec!["^[]0;longtitle^G", "body"]);
    }

    #[test]
    fn osc_terminated_by_st_is_visible() {
        assert_eq!(
            wrap_line_ansi("\x1b]0;hi\x1b\\x", 20, 0),
            vec!["^[]0;hi^[\\x"]
        );
    }

    #[test]
    fn two_byte_and_lone_escapes_are_visible() {
        assert_eq!(wrap_line_ansi("\x1b7abc", 20, 0), vec!["^[7abc"]);
        assert_eq!(wrap_line_ansi("\x1bXab", 20, 0), vec!["^[Xab"]);
        assert_eq!(wrap_line_ansi("\x1b", 20, 0), vec!["^["]);
    }

    #[test]
    fn empty_line_returns_empty_string() {
        let chunks = wrap_line_ansi("", 10, 0);
        assert_eq!(chunks, vec![""]);
    }

    #[test]
    fn cjk_chars_wrap_by_visual_width() {
        let chunks = wrap_line_ansi("一二三四五六七八九十", 10, 0);
        assert_eq!(chunks.len(), 2);
        assert_eq!(chunks[0], "一二三四五");
        assert_eq!(chunks[1], "六七八九十");
    }

    #[test]
    fn japanese_chars_wrap_by_visual_width() {
        let chunks = wrap_line_ansi("ひらがなカタカナ", 8, 0);
        assert_eq!(chunks.len(), 2);
        assert_eq!(chunks[0], "ひらがな");
        assert_eq!(chunks[1], "カタカナ");
    }

    #[test]
    fn korean_chars_wrap_by_visual_width() {
        let chunks = wrap_line_ansi("한국어테스트", 8, 0);
        assert_eq!(chunks.len(), 2);
        assert_eq!(chunks[0], "한국어테");
        assert_eq!(chunks[1], "스트");
    }

    #[test]
    fn mixed_ascii_cjk_wrap_correctly() {
        let chunks = wrap_line_ansi("A中B日C", 5, 0);
        assert_eq!(chunks.len(), 2);
        assert_eq!(chunks[0], "A中B");
        assert_eq!(chunks[1], "日C");
    }

    #[test]
    fn mixed_ascii_japanese_wrap_correctly() {
        let chunks = wrap_line_ansi("A日本語B", 4, 0);
        assert_eq!(chunks.len(), 3);
        assert_eq!(chunks[0], "A日");
        assert_eq!(chunks[1], "本語");
        assert_eq!(chunks[2], "B");
    }
}