codewhale-tui 0.9.8

Terminal UI for open-source and open-weight coding models
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
//! Shared text helpers for TUI selection and clipboard workflows.

use ratatui::text::{Line, Span};
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

use crate::tui::history::HistoryCell;
use crate::tui::osc8;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CopyLineSeparator {
    None,
    Space,
    Newline,
}

impl CopyLineSeparator {
    #[must_use]
    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::None => "",
            Self::Space => " ",
            Self::Newline => "\n",
        }
    }
}

pub(crate) fn truncate_line_to_width(text: &str, max_width: usize) -> String {
    if max_width == 0 {
        return String::new();
    }
    if text_display_width(text) <= max_width {
        return text.to_string();
    }
    // For very small budgets, take whole graphemes until the next one would
    // exceed the display width. Never split an emoji or combining sequence.
    if max_width <= 3 {
        let mut out = String::new();
        let mut width = 0usize;
        for grapheme in text.graphemes(true) {
            let grapheme_width = grapheme_display_width(grapheme);
            if width + grapheme_width > max_width {
                break;
            }
            out.push_str(grapheme);
            width += grapheme_width;
        }
        return out;
    }

    let mut out = String::new();
    let mut width = 0usize;
    let limit = max_width.saturating_sub(3);
    for grapheme in text.graphemes(true) {
        let grapheme_width = grapheme_display_width(grapheme);
        if width + grapheme_width > limit {
            break;
        }
        out.push_str(grapheme);
        width += grapheme_width;
    }
    out.push_str("...");
    out
}

/// Truncate `text` to `max_width` display columns, preferring whole words.
pub(crate) fn semantic_truncate(text: &str, max_width: usize) -> String {
    if max_width == 0 {
        return String::new();
    }
    if text_display_width(text) <= max_width {
        return text.to_string();
    }

    const ELLIPSIS: char = '';
    let ellipsis_width = char_display_width(ELLIPSIS);
    let limit = max_width.saturating_sub(ellipsis_width);
    if limit == 0 {
        return ELLIPSIS.to_string();
    }

    let mut width = 0usize;
    let mut cut_byte = 0usize;
    let mut last_word_end = None;
    let mut in_word = false;
    for (byte_idx, grapheme) in text.grapheme_indices(true) {
        let grapheme_width = grapheme_display_width(grapheme);
        if width + grapheme_width > limit {
            break;
        }
        width += grapheme_width;
        cut_byte = byte_idx + grapheme.len();
        if grapheme.chars().all(char::is_whitespace) {
            if in_word {
                last_word_end = Some(byte_idx);
                in_word = false;
            }
        } else {
            in_word = true;
        }
    }
    if cut_byte == 0 {
        return ELLIPSIS.to_string();
    }

    let mut body = if let Some(word_end) = last_word_end {
        text[..word_end].trim_end()
    } else {
        text[..cut_byte].trim_end()
    };
    if body.is_empty() {
        body = text[..cut_byte].trim_end();
    }
    let mut out = body.to_string();
    out.push(ELLIPSIS);
    out
}

pub(crate) fn semantic_truncate_with_affixes(
    prefix: &str,
    text: &str,
    suffix: &str,
    max_width: usize,
) -> String {
    let fixed_width = text_display_width(prefix) + text_display_width(suffix);
    if fixed_width > max_width {
        return semantic_truncate(&format!("{prefix}{text}{suffix}"), max_width);
    }
    format!(
        "{prefix}{}{suffix}",
        semantic_truncate_between_affixes(prefix, text, suffix, max_width)
    )
}

pub(crate) fn semantic_truncate_between_affixes(
    prefix: &str,
    text: &str,
    suffix: &str,
    max_width: usize,
) -> String {
    let fixed_width = text_display_width(prefix) + text_display_width(suffix);
    if fixed_width > max_width {
        return String::new();
    }
    semantic_truncate(text, max_width - fixed_width)
}

pub(super) fn history_cell_to_text(cell: &HistoryCell, width: u16) -> String {
    // Error detail/copy is a diagnostic boundary, not a screenshot of the
    // live wrapping. Preserve the exact source message so narrow terminals do
    // not insert newlines into hostnames, env vars, commands, or URLs.
    if let HistoryCell::Error { message, .. } = cell {
        return message.clone();
    }
    cell.transcript_lines(width)
        .into_iter()
        .map(line_to_string)
        .collect::<Vec<_>>()
        .join("\n")
}

/// Serialize one complete history cell for Copy Message.
///
/// User and assistant cells have canonical source text. Returning it directly
/// preserves authored Markdown and hard line breaks while keeping role glyphs,
/// continuation rails, and visual wrapping out of the clipboard. Selection
/// copy cannot provide that contract: it serializes the rendered live cache,
/// where Markdown has already been transformed and user-message soft wraps do
/// not carry join metadata.
///
/// Complex cells intentionally retain the full-transcript representation.
/// Tool and thinking transcript renderers include semantic headers and complete
/// output that can differ from the capped or folded live cache.
pub(super) fn history_cell_to_clipboard_text(cell: &HistoryCell, width: u16) -> String {
    match cell {
        HistoryCell::User { content } | HistoryCell::Assistant { content, .. } => content.clone(),
        _ => history_cell_to_text(cell, width),
    }
}

fn line_to_string(line: Line<'static>) -> String {
    let mut out = String::new();
    append_spans_plain(line.spans.iter(), &mut out);
    out
}

/// Convert a rendered transcript line to plain text, stripping OSC-8 link
/// escape sequences. The caller is responsible for shifting selection columns
/// to account for any visual-only rail prefix (see
/// `TranscriptViewCache::rail_prefix_width`).
pub(super) fn line_to_plain(line: &Line<'static>) -> String {
    let mut out = String::new();
    append_spans_plain(line.spans.iter(), &mut out);
    out
}

fn append_spans_plain<'a, I>(spans: I, out: &mut String)
where
    I: Iterator<Item = &'a Span<'a>>,
{
    for span in spans {
        if span.content.contains('\x1b') {
            osc8::strip_into(&span.content, out);
        } else {
            out.push_str(span.content.as_ref());
        }
    }
}

pub(crate) fn text_display_width(text: &str) -> usize {
    text.graphemes(true).map(grapheme_display_width).sum()
}

pub(super) fn slice_text(text: &str, start: usize, end: usize) -> String {
    if end <= start {
        return String::new();
    }

    let mut out = String::new();
    let mut col = 0usize;
    for grapheme in text.graphemes(true) {
        let grapheme_width = grapheme_display_width(grapheme);
        let grapheme_start = col;
        let grapheme_end = col.saturating_add(grapheme_width);
        if grapheme_end > start && grapheme_start < end {
            out.push_str(grapheme);
        }
        col = grapheme_end;
        if col >= end {
            break;
        }
    }
    out
}

pub(super) fn char_display_width(ch: char) -> usize {
    match ch {
        '\t' => 4,
        // Enclosed Alphanumerics (U+2460-U+24FF), Dingbat Circled Digits
        // (U+2776-U+2793), and Circled Numbers on Black Square (U+3248-U+324F)
        // have East Asian Width "Ambiguous" but are rendered as 2 columns in
        // CJK terminals. unicode-width's width() conservatively reports 1; we
        // match the terminal. (#4479)
        '\u{2460}'..='\u{24FF}' | '\u{2776}'..='\u{2793}' | '\u{3248}'..='\u{324F}' => 2,
        _ => {
            // `width()` returns `None` for control/unassigned chars (default them to
            // one column so layout doesn't collapse) and `Some(0)` for genuinely
            // zero-width chars — combining marks, ZWJ, zero-width spaces — which must
            // stay 0 so display-width math (truncation, slicing, overflow, copy)
            // matches what the terminal actually renders.
            UnicodeWidthChar::width(ch).unwrap_or(1)
        }
    }
}

/// Measure one extended grapheme using the same string-level Unicode rules as
/// Ratatui. String width intentionally differs from the sum of codepoint widths
/// for keycaps, ZWJ emoji, modifiers, and other terminal ligatures.
///
/// Keycap sequences (such as 1\u{fe0f}\u{20e3}) that lack an FE0F variation
/// selector still render as 2 columns in terminals, but unicode-width's
/// `grapheme.width()` only reports 1. We force 2 when U+20E3 is present in a
/// multi-codepoint grapheme. (#4479)
pub(super) fn grapheme_display_width(grapheme: &str) -> usize {
    if grapheme == "\t" {
        return 4;
    }
    if let Some(ch) = grapheme.chars().next()
        && ch.len_utf8() == grapheme.len()
    {
        return char_display_width(ch);
    }
    // Keycap sequences always render as 2 columns. unicode-width's
    // `width()` undercounts the non-FE0F variant to 1.
    if grapheme.contains('\u{20e3}') {
        return 2;
    }
    UnicodeWidthStr::width(grapheme)
}

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

    #[test]
    fn line_to_plain_strips_osc_8_wrapper() {
        let wrapped = format!(
            "\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\",
            "https://example.com", "https://example.com"
        );
        let line = Line::from(vec![
            Span::raw("see "),
            Span::raw(wrapped),
            Span::raw(" for details"),
        ]);
        let text = line_to_plain(&line);
        assert_eq!(text, "see https://example.com for details");
    }

    #[test]
    fn line_to_plain_passes_through_plain_spans() {
        let line = Line::from(vec![Span::raw("plain "), Span::raw("text")]);
        let text = line_to_plain(&line);
        assert_eq!(text, "plain text");
    }

    #[test]
    fn line_to_plain_includes_all_spans() {
        // Visual-only rail spans are stripped by the caller using
        // TranscriptViewCache::rail_prefix_width — line_to_plain itself
        // is a faithful span-to-string pass-through.
        let line = Line::from(vec![Span::raw("\u{2502} "), Span::raw("tool output")]);
        let text = line_to_plain(&line);
        assert_eq!(text, "\u{2502} tool output");
    }

    #[test]
    fn slice_text_respects_column_bounds() {
        let text = "hello world";
        assert_eq!(slice_text(text, 0, 5), "hello");
        assert_eq!(slice_text(text, 6, 11), "world");
        assert_eq!(slice_text(text, 0, 0), "");
        assert_eq!(slice_text(text, 0, 100), text);
    }

    #[test]
    fn slice_text_handles_multibyte_characters() {
        let text = "a─b"; // U+2500 is 1 display column on supported terminals
        assert_eq!(slice_text(text, 1, 2), "");
        assert_eq!(slice_text(text, 0, 3), text);
    }

    #[test]
    fn slice_text_truncates_at_end() {
        let text = "ab";
        assert_eq!(slice_text(text, 1, 5), "b");
    }

    // --- Unicode / CJK / terminal-width QA (issue #3488) -------------------
    // These exercise the production width helpers directly so the assertions
    // track the same code path the renderer uses.

    #[test]
    fn text_display_width_counts_cjk_as_two_columns() {
        assert_eq!(text_display_width("中文"), 4); // two wide glyphs
        assert_eq!(text_display_width("Hello世界"), 9); // 5 ASCII + 2×2
        // Full-width (ambiguous→wide) punctuation is two columns each.
        assert_eq!(text_display_width(",。!?"), 8);
    }

    #[test]
    fn text_display_width_treats_zero_width_marks_as_zero() {
        // A combining mark adds no column: "e" + U+0301 renders as one cell.
        // (Regression guard: the old `.max(1)` counted it as 1, over-reporting
        // width and causing premature truncation / border drift on text with
        // combining marks or ZWJ emoji sequences.)
        assert_eq!(text_display_width("e\u{0301}"), 1);
        assert_eq!(text_display_width("cafe\u{0301}"), 4);
        // The complete ZWJ emoji is one two-column grapheme, matching Ratatui.
        assert_eq!(text_display_width("\u{1F469}\u{200D}\u{1F4BB}"), 2);
    }

    #[test]
    fn text_display_width_keeps_control_and_tab_widths() {
        // Control chars still occupy a column (avoid layout collapse); tab = 4.
        assert_eq!(text_display_width("a\u{0007}b"), 3);
        assert_eq!(text_display_width("\t"), 4);
        assert_eq!(text_display_width("\ta"), 5);
    }

    #[test]
    fn truncate_line_to_width_respects_display_width_not_byte_len() {
        // No truncation when the string already fits by display width.
        assert_eq!(truncate_line_to_width("中文", 10), "中文");
        // Oversized: reserve 3 cols for the ellipsis, fill the rest by width.
        let out = truncate_line_to_width("中文测试", 7);
        assert_eq!(out, "中文...");
        assert_eq!(text_display_width(&out), 7);
        // Never split a wide glyph across the boundary, and never emit U+FFFD.
        let clipped = truncate_line_to_width("界界界界界", 5);
        assert!(text_display_width(&clipped) <= 5);
        assert!(!clipped.contains('\u{FFFD}'));
    }

    #[test]
    fn semantic_truncate_prefers_word_boundaries() {
        let out = semantic_truncate("hello world foo bar", 14);
        assert_eq!(out, "hello world…");
        assert!(text_display_width(&out) <= 14);
    }

    #[test]
    fn semantic_truncate_falls_back_with_long_words_and_wide_glyphs() {
        let long_word = semantic_truncate("supercalifragilistic", 8);
        assert_eq!(long_word, "superca…");
        assert!(text_display_width(&long_word) <= 8);

        let cjk = semantic_truncate("中文测试文本", 7);
        assert_eq!(cjk, "中文测…");
        assert!(text_display_width(&cjk) <= 7);
    }

    #[test]
    fn semantic_truncate_handles_empty_and_tiny_budgets() {
        assert_eq!(semantic_truncate("", 10), "");
        assert_eq!(semantic_truncate("hello", 0), "");
        assert_eq!(semantic_truncate("hello", 1), "");
    }

    #[test]
    fn semantic_truncate_between_affixes_reserves_fixed_columns() {
        let hint = semantic_truncate_between_affixes(
            " > [ ] Prefix stability  (",
            "whether system/tools stayed cacheable",
            ")",
            49,
        );
        let row = format!(" > [ ] Prefix stability  ({hint})");
        assert_eq!(hint, "whether system/tools…");
        assert!(text_display_width(&row) <= 49);
    }

    #[test]
    fn slice_text_slices_cjk_by_display_column() {
        // Columns:  中=[0,2) 文=[2,4) a=[4,5) b=[5,6)
        let text = "中文ab";
        assert_eq!(slice_text(text, 0, 2), "");
        assert_eq!(slice_text(text, 2, 4), "");
        assert_eq!(slice_text(text, 4, 6), "ab");
    }

    // --- New #3488 fixtures: CJK/wide-glyph truncation on selector-style rows.
    // truncate_line_to_width is the production helper behind sidebar (file_tree),
    // statusline (footer_ui), hotbar, and picker (mouse_ui) row rendering, so
    // these exercise the same truncation path those surfaces use.

    #[test]
    fn truncate_line_to_width_full_width_cjk_lands_on_glyph_boundary() {
        // Each Han glyph is two columns. With an odd budget the truncation must
        // land on a whole-glyph boundary (reserving three columns for the
        // ellipsis), never leaving a half-rendered wide cell or emitting U+FFFD.
        let title = "项目报告结果"; // 6 glyphs, 12 columns
        let out = truncate_line_to_width(title, 7);
        // Budget 7 -> limit 4 columns -> two glyphs fit, then the ellipsis.
        assert_eq!(out, "项目...");
        assert_eq!(text_display_width(&out), 7);
        // The kept prefix is composed only of whole wide glyphs (each 2 cols),
        // proving the boundary glyph was dropped whole, not split.
        let prefix = out.strip_suffix("...").expect("ellipsis present");
        assert!(prefix.chars().all(|c| char_display_width(c) == 2));
        assert!(!out.contains('\u{FFFD}'));
    }

    #[test]
    fn truncate_line_to_width_mixed_ascii_cjk_row_keeps_ellipsis_within_budget() {
        // A sidebar/selector row mixing an ASCII label with a CJK title, wider
        // than the column budget, must truncate with a trailing ellipsis that
        // still fits by display width and must not split a wide glyph.
        let row = "Task: 数据库迁移任务 done"; // ASCII label + 7 Han glyphs
        let budget = 12;
        let out = truncate_line_to_width(row, budget);
        assert!(out.ends_with("..."), "expected ellipsis, got {out:?}");
        // Ellipsis-and-content fit within the budget by *display* width.
        assert!(text_display_width(&out) <= budget);
        // The non-ellipsis prefix stays within budget-minus-ellipsis, so the
        // wide glyph on the boundary was dropped whole rather than half-drawn.
        let prefix = out.strip_suffix("...").expect("ellipsis present");
        assert!(text_display_width(prefix) <= budget - 3);
        assert!(!out.contains('\u{FFFD}'));
        // The semantic ASCII prefix survives truncation.
        assert!(out.starts_with("Task:"));
    }

    #[test]
    fn truncate_line_to_width_dense_cjk_selector_row_survives_narrow_widths() {
        // Picker/selector rows degrade through truncate_line_to_width when the
        // terminal is narrow. A dense row with a leading marker glyph and CJK
        // content must stay within budget at tiny widths, without panicking or
        // emitting a replacement char from a mid-glyph byte split.
        let row = "▸ 中文项目 · main"; // marker + CJK + separator + branch
        for width in [1usize, 2, 3, 4, 6, 8] {
            let out = truncate_line_to_width(row, width);
            assert!(
                text_display_width(&out) <= width,
                "width={width}: {out:?} exceeds budget"
            );
            assert!(
                !out.contains('\u{FFFD}'),
                "width={width}: truncation split a wide glyph"
            );
        }
    }

    // --- keycap / grapheme regression guard (#4479) ---------------------------
    // Fully qualified keycap sequences render as two columns. Codepoint sums
    // report one; the canonical string/grapheme contract reports two.

    #[test]
    fn text_display_width_treats_keycap_sequence_as_two_columns() {
        for keycap in [
            "1\u{fe0f}\u{20e3}",
            "9\u{fe0f}\u{20e3}",
            "#\u{fe0f}\u{20e3}",
        ] {
            assert_eq!(text_display_width(keycap), 2);
            assert_eq!(text_display_width(keycap), UnicodeWidthStr::width(keycap));
        }
        // A digit directly followed by U+20E3 (without FE0F variation selector)
        // still renders as a 2-column keycap in terminals. We force this in
        // grapheme_display_width when the grapheme contains U+20E3.
        // A standalone U+20E3 is a zero-width combining mark.
        assert_eq!(text_display_width("1\u{20e3}"), 2);
        assert_eq!(text_display_width("\u{20e3}"), 0);
    }

    #[test]
    fn circled_digit_display_width() {
        assert_eq!(char_display_width('\u{2460}'), 2);
        assert_eq!(char_display_width('\u{2461}'), 2);
        assert_eq!(char_display_width('\u{24ea}'), 2);
        assert_eq!(char_display_width('\u{2776}'), 2);
        assert_eq!(text_display_width("\u{2460}\u{2461}\u{2462}"), 6);
        assert_eq!(text_display_width("Step \u{2460}: init"), 13);
        assert_eq!(text_display_width("A\u{24d0}B"), 4);
    }

    #[test]
    fn unicode_width_reports_circled_digits_as_two_columns() {
        // Regression guard for the unicode-width patch (#4479): Ratatui
        // renders text through UnicodeWidthChar::width(), so the patch must
        // make even the raw crate API report 2 columns for ambiguous-width
        // characters — otherwise Ratatui places them in 1 cell while the
        // terminal paints 2, shifting every downstream column.
        assert_eq!(UnicodeWidthChar::width('\u{2460}'), Some(2));
        assert_eq!(UnicodeWidthChar::width('\u{24ea}'), Some(2));
        assert_eq!(UnicodeWidthStr::width("\u{2460}\u{2461}\u{2462}"), 6);
    }

    #[test]
    fn slice_text_does_not_split_keycap_sequence() {
        let row = "step 1\u{fe0f}\u{20e3} done";
        // The keycap occupies columns [5, 7). Any overlapping selection keeps
        // the complete grapheme; no isolated FE0F/U+20E3 mark may escape.
        for (start, end) in [(0, 7), (5, 6), (6, 7)] {
            let sliced = slice_text(row, start, end);
            assert!(
                sliced.contains("1\u{fe0f}\u{20e3}"),
                "range=({start}, {end}) split keycap: {sliced:?}"
            );
        }
    }

    #[test]
    fn truncate_line_to_width_always_stays_within_budget_with_keycap() {
        // Budgets from zero through wide, with and without surrounding text.
        let cases = [
            "1\u{fe0f}\u{20e3}",
            "A 1\u{fe0f}\u{20e3} B",
            "step 2\u{fe0f}\u{20e3} and 3\u{fe0f}\u{20e3} continue",
        ];
        for text in &cases {
            for budget in 0..=text_display_width(text) + 4 {
                let out = truncate_line_to_width(text, budget);
                let width = text_display_width(&out);
                assert!(
                    width <= budget,
                    "budget={budget} text={text:?} -> {out:?} (width={width})"
                );
                assert!(!out.ends_with('\u{fe0f}'));
                assert!(!out.starts_with('\u{20e3}'));
            }
        }
    }

    #[test]
    fn clipboard_text_for_assistant_uses_source_without_visual_rails() {
        let content = "A long assistant response that will wrap at a narrow width.";
        let cell = HistoryCell::Assistant {
            content: content.to_string(),
            streaming: false,
        };

        let rendered = history_cell_to_text(&cell, 12);
        assert_ne!(rendered, content, "test setup must exercise rendering");
        assert!(
            rendered.contains('\n'),
            "test setup must exercise visual wrapping: {rendered:?}"
        );
        assert_eq!(history_cell_to_clipboard_text(&cell, 12), content);
    }

    #[test]
    fn clipboard_text_preserves_authored_rail_glyphs() {
        let content = "● literal role glyph\n▏ literal rail glyph";
        let cell = HistoryCell::Assistant {
            content: content.to_string(),
            streaming: false,
        };

        assert_eq!(history_cell_to_clipboard_text(&cell, 10), content);
    }

    #[test]
    fn clipboard_text_preserves_markdown_source_and_hard_breaks() {
        let content = r#"Heading

```rust
fn main() {
    println!("hello");
}
```

After code."#;
        let cell = HistoryCell::Assistant {
            content: content.to_string(),
            streaming: false,
        };

        assert_eq!(history_cell_to_clipboard_text(&cell, 16), content);
    }

    #[test]
    fn clipboard_text_for_user_uses_source_text() {
        let content = "user text that wraps visually";
        let cell = HistoryCell::User {
            content: content.to_string(),
        };

        let rendered = history_cell_to_text(&cell, 8);
        assert_ne!(rendered, content, "test setup must exercise rendering");
        assert!(
            rendered.contains('\n'),
            "test setup must exercise visual wrapping: {rendered:?}"
        );
        assert_eq!(history_cell_to_clipboard_text(&cell, 8), content);
    }

    #[test]
    fn clipboard_text_for_thinking_keeps_full_transcript_semantics() {
        let content = "First paragraph lede.\n\
            Second sentence of the first paragraph.\n\n\
            Second paragraph: deeper analysis follows.\n\
            More detail in paragraph two.\n\n\
            Third paragraph: even more reasoning.\n\
            With another line.\n\n\
            Fourth paragraph: the conclusion.\n\
            And one more line for good measure.\n\n\
            Fifth paragraph: final verification.\n\
            One last supporting detail.";
        let cell = HistoryCell::Thinking {
            content: content.to_string(),
            streaming: false,
            duration_secs: Some(3.2),
        };

        let live = cell
            .lines_with_options(
                80,
                crate::tui::history::TranscriptRenderOptions {
                    low_motion: true,
                    ..crate::tui::history::TranscriptRenderOptions::default()
                },
            )
            .into_iter()
            .map(line_to_string)
            .collect::<Vec<_>>()
            .join("\n");
        let transcript = history_cell_to_text(&cell, 80);
        let copied = history_cell_to_clipboard_text(&cell, 80);

        assert!(!live.contains("Fifth paragraph"), "{live:?}");
        assert!(transcript.contains("Fifth paragraph"), "{transcript:?}");
        assert_eq!(copied, transcript);
    }

    #[test]
    fn clipboard_text_for_tool_keeps_full_transcript_semantics() {
        use crate::tui::history::{GenericToolCell, ToolCell, ToolStatus};

        let cell = HistoryCell::Tool(ToolCell::Generic(GenericToolCell {
            name: "exec_shell".to_string(),
            status: ToolStatus::Success,
            input_summary: Some("cargo test".to_string()),
            output: Some("complete tool output".to_string()),
            prompts: None,
            spillover_path: None,
            output_summary: None,
            is_diff: false,
        }));

        let transcript = history_cell_to_text(&cell, 80);
        assert!(
            transcript.contains("complete tool output"),
            "{transcript:?}"
        );
        assert_eq!(history_cell_to_clipboard_text(&cell, 80), transcript);
    }
}