cctop 0.26.2

An htop-like terminal monitor for AI coding agent sessions on Linux (Claude Code, Codex, Cursor, Devin, Gemini CLI, OpenCode, Pi, Windsurf)
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
//! An assistant's reply, drawn as the markdown it was written in.
//!
//! Headings, emphasis, inline code, fenced blocks, lists, quotes, rules and
//! tables — what a coding agent actually writes — laid out to a fixed width in
//! the palette's own colours, so a sixteen-colour or colourless terminal gets
//! the same fold as the rest of the screen. Links come back with the rows and
//! columns they were drawn in, for the caller to make OSC 8 hyperlinks of once
//! it knows where on screen the text landed ([`hyperlink::link`]).
//!
//! The parser is `pulldown-cmark`; the layout is here. See the note beside the
//! dependency in `Cargo.toml` for why not `tui-markdown`.
//!
//! ponytail: fenced code is one colour, not highlighted. The pure-Rust
//! highlighter is syntect, whose bundled syntaxes and themes are several
//! megabytes of binary for a view that shows a few snippets — and whose
//! colours would be 24-bit, to be folded down to cctop's palette anyway.
//!
//! [`hyperlink::link`]: super::hyperlink::link

use super::styled::{self, Piece};
use super::theme;
use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag, TagEnd};
use ratatui::buffer::CellWidth;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use std::ops::Range;

/// A reply laid out: its rows, and where on them each link was drawn.
#[derive(Debug, Default)]
pub(super) struct Rendered {
    pub lines: Vec<Line<'static>>,
    pub links: Vec<Link>,
}

/// One row's worth of one link — a link wrapped over two rows is two of these,
/// with the same `url`.
#[derive(Debug, Clone, PartialEq)]
pub(super) struct Link {
    /// Index into [`Rendered::lines`].
    pub line: usize,
    pub columns: Range<u16>,
    pub url: String,
}

/// `text` as markdown, wrapped to `width`, with plain text in `base`.
pub(super) fn render(text: &str, width: usize, base: Style) -> Rendered {
    render_with(text, width, base, super::hyperlink::supported())
}

/// [`render`], told whether the terminal will make a link of the label. Where
/// it will not, the URL is printed after the label, because otherwise it is
/// nowhere on screen at all.
fn render_with(text: &str, width: usize, base: Style, osc8: bool) -> Rendered {
    let options =
        Options::ENABLE_STRIKETHROUGH | Options::ENABLE_TABLES | Options::ENABLE_TASKLISTS;
    let mut r = Renderer {
        width: width.max(1),
        base,
        osc8,
        out: Rendered::default(),
        blank: true,
        inline: Vec::new(),
        styles: Vec::new(),
        containers: Vec::new(),
        lists: Vec::new(),
        urls: Vec::new(),
        link: None,
        code: None,
        table: None,
    };
    for event in Parser::new_ext(text, options) {
        r.event(event);
    }
    r.flush();
    // The last block's gap is a separation from nothing.
    while r.out.lines.last().is_some_and(|l| l.width() == 0) {
        r.out.lines.pop();
    }
    r.out
}

enum Container {
    Quote,
    /// A list item, with its marker until the item's first row has shown it.
    Item {
        marker: String,
        shown: bool,
    },
}

#[derive(Default)]
struct Table {
    rows: Vec<Vec<String>>,
    /// How many of `rows` are the header, which is drawn bold and ruled off.
    head: usize,
}

struct Renderer {
    width: usize,
    base: Style,
    osc8: bool,
    out: Rendered,
    /// The last row out is a gap (or there is none yet), so a block starting
    /// now needs no gap of its own.
    blank: bool,
    /// The paragraph, heading or item text being gathered, not yet wrapped.
    inline: Vec<Piece>,
    /// Emphasis, strong, link… each patched over `base` in turn.
    styles: Vec<Style>,
    containers: Vec<Container>,
    /// Each open list's next number, `None` for a bulleted one.
    lists: Vec<Option<u64>>,
    urls: Vec<String>,
    /// The link being drawn: its index in `urls`, and where in `inline` its
    /// label starts — to tell a bare URL from a labelled one at its end.
    link: Option<(usize, usize)>,
    code: Option<String>,
    table: Option<Table>,
}

impl Renderer {
    fn style(&self) -> Style {
        self.styles.iter().fold(self.base, |s, p| s.patch(*p))
    }

    fn event(&mut self, event: Event) {
        match event {
            Event::Start(tag) => self.start(tag),
            Event::End(tag) => self.end(tag),
            Event::Text(text) => self.text(&text),
            Event::Code(code) => {
                let style = code_style();
                match &mut self.table {
                    Some(t) => cell(t).push_str(&code),
                    None => {
                        // Without colour the backticks are what still says
                        // "this is code".
                        let code = match theme::no_color() {
                            true => format!("`{code}`"),
                            false => code.to_string(),
                        };
                        self.push(code, style);
                    }
                }
            }
            Event::InlineMath(t) | Event::DisplayMath(t) => self.push(t.to_string(), code_style()),
            Event::Html(html) | Event::InlineHtml(html) => {
                for (i, part) in html.split('\n').enumerate() {
                    if i > 0 {
                        self.flush();
                    }
                    if !part.is_empty() {
                        self.push(part.to_string(), theme::dim());
                    }
                }
            }
            Event::FootnoteReference(name) => self.push(format!("[^{name}]"), theme::dim()),
            Event::SoftBreak => self.text(" "),
            Event::HardBreak => self.flush(),
            Event::Rule => {
                self.flush();
                self.gap();
                let (_, rest) = self.prefixes();
                let room = self.width.saturating_sub(width_of(&rest));
                let mut spans = rest;
                spans.push(Span::styled("─".repeat(room), theme::dim()));
                self.emit(styled::Row {
                    line: Line::from(spans),
                    links: Vec::new(),
                });
            }
            Event::TaskListMarker(done) => {
                let mark = if done { "[x] " } else { "[ ] " };
                self.push(mark.to_string(), theme::dim());
            }
        }
    }

    fn start(&mut self, tag: Tag) {
        match tag {
            Tag::Paragraph => self.gap(),
            Tag::Heading { level, .. } => {
                self.gap();
                let mut style = theme::title();
                if level <= HeadingLevel::H2 {
                    style = style.add_modifier(Modifier::UNDERLINED);
                }
                self.styles.push(style);
            }
            Tag::BlockQuote(_) => {
                self.flush();
                self.gap();
                self.containers.push(Container::Quote);
                self.styles
                    .push(Style::default().add_modifier(Modifier::ITALIC));
            }
            Tag::CodeBlock(kind) => {
                self.flush();
                self.gap();
                // The fence's language, dim above the block, is the only
                // highlighting there is; see the module note.
                if let CodeBlockKind::Fenced(lang) = kind
                    && let Some(lang) = lang.split_whitespace().next()
                {
                    self.push(lang.to_string(), theme::dim());
                    self.flush();
                }
                self.code = Some(String::new());
            }
            Tag::List(first) => {
                self.flush();
                // A list nested in an item runs straight on from the item's
                // text; only a list standing on its own is set apart.
                if !self
                    .containers
                    .iter()
                    .any(|c| matches!(c, Container::Item { .. }))
                {
                    self.gap();
                }
                self.lists.push(first);
            }
            Tag::Item => {
                self.flush();
                let marker = match self.lists.last_mut() {
                    Some(Some(n)) => {
                        let marker = format!("{n}. ");
                        *n += 1;
                        marker
                    }
                    _ => "• ".to_string(),
                };
                self.containers.push(Container::Item {
                    marker,
                    shown: false,
                });
            }
            Tag::Table(_) => {
                self.flush();
                self.gap();
                self.table = Some(Table::default());
            }
            Tag::TableHead | Tag::TableRow => {
                if let Some(t) = &mut self.table {
                    t.rows.push(Vec::new());
                }
            }
            Tag::TableCell => {
                if let Some(t) = &mut self.table {
                    if t.rows.is_empty() {
                        t.rows.push(Vec::new());
                    }
                    t.rows.last_mut().expect("a row").push(String::new());
                }
            }
            Tag::Emphasis => self
                .styles
                .push(Style::default().add_modifier(Modifier::ITALIC)),
            Tag::Strong => self
                .styles
                .push(Style::default().add_modifier(Modifier::BOLD)),
            Tag::Strikethrough => self
                .styles
                .push(Style::default().add_modifier(Modifier::CROSSED_OUT)),
            Tag::Link { dest_url, .. } | Tag::Image { dest_url, .. } => {
                self.urls.push(dest_url.to_string());
                self.link = Some((self.urls.len() - 1, self.inline.len()));
                self.styles.push(link_style());
            }
            Tag::HtmlBlock => {
                self.flush();
                self.gap();
            }
            // Footnote bodies, definition lists, front matter, super- and
            // subscript: none of them are enabled, and a parser that emitted
            // one anyway gets its text drawn plainly.
            _ => {}
        }
    }

    fn end(&mut self, tag: TagEnd) {
        match tag {
            TagEnd::Paragraph | TagEnd::HtmlBlock => self.flush(),
            TagEnd::Heading(_) => {
                self.flush();
                self.styles.pop();
            }
            TagEnd::BlockQuote(_) => {
                self.flush();
                self.containers.pop();
                self.styles.pop();
                self.gap();
            }
            TagEnd::CodeBlock => {
                let code = self.code.take().unwrap_or_default();
                let from = self.out.lines.len();
                for line in code.trim_end_matches('\n').split('\n') {
                    self.push(line.replace('\t', "    "), code_style());
                    self.flush_with(Some(Span::styled("│ ", theme::dim())));
                }
                self.shade(from);
            }
            TagEnd::List(_) => {
                self.flush();
                self.lists.pop();
            }
            TagEnd::Item => {
                self.flush();
                self.containers.pop();
            }
            TagEnd::Table => {
                if let Some(t) = self.table.take() {
                    self.table(t);
                }
            }
            TagEnd::TableHead => {
                if let Some(t) = &mut self.table {
                    t.head = t.rows.len();
                }
            }
            TagEnd::Emphasis | TagEnd::Strong | TagEnd::Strikethrough => {
                self.styles.pop();
            }
            TagEnd::Link | TagEnd::Image => {
                self.styles.pop();
                if let Some((index, from)) = self.link.take() {
                    let url = self.urls[index].clone();
                    let label: String = self.inline[from..]
                        .iter()
                        .map(|p| p.text.as_str())
                        .collect();
                    // A bare URL is its own label and needs saying only once.
                    if !self.osc8 && label != url && !url.is_empty() {
                        match &mut self.table {
                            Some(t) => cell(t).push_str(&format!(" <{url}>")),
                            None => self.push(format!(" <{url}>"), theme::dim()),
                        }
                    }
                }
            }
            _ => {}
        }
    }

    fn text(&mut self, text: &str) {
        if let Some(code) = &mut self.code {
            code.push_str(text);
            return;
        }
        if let Some(t) = &mut self.table {
            cell(t).push_str(text);
            return;
        }
        self.push(text.to_string(), self.style());
    }

    fn push(&mut self, text: String, style: Style) {
        self.inline.push(Piece {
            text,
            style,
            link: self.link.map(|(i, _)| i),
        });
    }

    /// Set the next block apart from the last with one empty row — carrying
    /// the bars of any quote it is inside, so the quote reads as unbroken.
    fn gap(&mut self) {
        if self.blank {
            return;
        }
        let bars: Vec<Span<'static>> = self
            .containers
            .iter()
            .filter(|c| matches!(c, Container::Quote))
            .map(|_| quote_bar())
            .collect();
        self.out.lines.push(Line::from(bars));
        self.blank = true;
    }

    /// What goes in front of the next row, and of the rows its text wraps on
    /// to: a quote's bar, and an item's marker once, then the marker's width
    /// in spaces so the text lines up under itself.
    fn prefixes(&self) -> (Vec<Span<'static>>, Vec<Span<'static>>) {
        let mut first = Vec::new();
        let mut rest = Vec::new();
        for c in &self.containers {
            match c {
                Container::Quote => {
                    first.push(quote_bar());
                    rest.push(quote_bar());
                }
                Container::Item { marker, shown } => {
                    let pad = " ".repeat(marker.cell_width() as usize);
                    match shown {
                        false => first.push(Span::styled(marker.clone(), bullet_style())),
                        true => first.push(Span::raw(pad.clone())),
                    }
                    rest.push(Span::raw(pad));
                }
            }
        }
        (first, rest)
    }

    fn flush(&mut self) {
        self.flush_with(None);
    }

    /// Wrap the gathered text out as rows, `gutter` after the container
    /// prefixes — a code block's bar.
    fn flush_with(&mut self, gutter: Option<Span<'static>>) {
        if self.inline.is_empty() && gutter.is_none() {
            return;
        }
        let (mut first, mut rest) = self.prefixes();
        if let Some(g) = gutter {
            first.push(g.clone());
            rest.push(g);
        }
        let pieces = std::mem::take(&mut self.inline);
        for row in styled::wrap(&pieces, self.width, &first, &rest) {
            self.emit(row);
        }
        for c in &mut self.containers {
            if let Container::Item { shown, .. } = c {
                *shown = true;
            }
        }
    }

    /// Lay a code block's ground behind the rows from `from` on, out to the
    /// full width — a block of code reads as one when its right edge is
    /// straight, not ragged with its lines.
    ///
    /// The ground starts at the gutter rather than the row's edge, so a block
    /// inside a list or a quote keeps the marker's column and the quote's bar
    /// in the colours they have everywhere else. Without colour there is no
    /// ground to lay ([`theme::gray`] is `Reset`), and the gutter alone says
    /// "code".
    fn shade(&mut self, from: usize) {
        let ground = theme::gray(235);
        if ground == ratatui::style::Color::Reset {
            return;
        }
        let (_, rest) = self.prefixes();
        let skip = rest.len();
        for line in &mut self.out.lines[from..] {
            let used = line.width();
            for span in line.spans.iter_mut().skip(skip) {
                span.style = span.style.bg(ground);
            }
            let pad = self.width.saturating_sub(used);
            if pad > 0 {
                line.spans
                    .push(Span::styled(" ".repeat(pad), Style::default().bg(ground)));
            }
        }
    }

    fn emit(&mut self, row: styled::Row) {
        let line = self.out.lines.len();
        for (columns, index) in row.links {
            self.out.links.push(Link {
                line,
                columns,
                url: self.urls[index].clone(),
            });
        }
        self.out.lines.push(row.line);
        self.blank = false;
    }

    /// Columns padded to their widest cell, the header bold and ruled off.
    ///
    /// ponytail: a table wider than the view is wrapped like any other text,
    /// so its columns stop lining up. Squeezing columns to fit means choosing
    /// which cell's text to cut, and the transcript is there when it matters.
    fn table(&mut self, table: Table) {
        let columns = table.rows.iter().map(Vec::len).max().unwrap_or(0);
        let mut widths = vec![0usize; columns];
        for row in &table.rows {
            for (i, cell) in row.iter().enumerate() {
                widths[i] = widths[i].max(cell.trim().cell_width() as usize);
            }
        }
        let sep = || Piece::new(" │ ", theme::dim());
        for (r, row) in table.rows.iter().enumerate() {
            let style = match r < table.head {
                true => self.base.add_modifier(Modifier::BOLD),
                false => self.base,
            };
            for (i, width) in widths.iter().enumerate() {
                if i > 0 {
                    self.inline.push(sep());
                }
                let text = row.get(i).map(|c| c.trim()).unwrap_or("");
                let pad = width.saturating_sub(text.cell_width() as usize);
                self.inline
                    .push(Piece::new(format!("{text}{}", " ".repeat(pad)), style));
            }
            self.flush();
            if r + 1 == table.head {
                let rule: Vec<String> = widths.iter().map(|w| "─".repeat(*w)).collect();
                self.inline.push(Piece::new(rule.join("─┼─"), theme::dim()));
                self.flush();
            }
        }
    }
}

/// The cell text is being gathered into.
fn cell(table: &mut Table) -> &mut String {
    if table.rows.is_empty() {
        table.rows.push(Vec::new());
    }
    let row = table.rows.last_mut().expect("a row");
    if row.is_empty() {
        row.push(String::new());
    }
    row.last_mut().expect("a cell")
}

fn width_of(spans: &[Span<'static>]) -> usize {
    spans.iter().map(Span::width).sum()
}

fn quote_bar() -> Span<'static> {
    Span::styled("│ ", theme::dim())
}

fn code_style() -> Style {
    Style::default().fg(theme::colors().name_hue)
}

fn link_style() -> Style {
    Style::default()
        .fg(theme::colors().label)
        .add_modifier(Modifier::UNDERLINED)
}

fn bullet_style() -> Style {
    Style::default().fg(theme::colors().accent)
}

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

    fn draw(text: &str, width: usize) -> Rendered {
        render_with(text, width, Style::default(), true)
    }

    fn rows(r: &Rendered) -> Vec<String> {
        r.lines
            .iter()
            .map(|l| l.spans.iter().map(|s| s.content.as_ref()).collect())
            .collect()
    }

    /// The span carrying `needle`, for asserting on how it was styled.
    fn span_of<'a>(r: &'a Rendered, needle: &str) -> &'a Span<'static> {
        r.lines
            .iter()
            .flat_map(|l| l.spans.iter())
            .find(|s| s.content.contains(needle))
            .unwrap_or_else(|| panic!("{needle:?} was not drawn"))
    }

    /// The markers are gone and what they meant is in the style instead.
    #[test]
    fn headings_and_emphasis_become_style() {
        let r = draw("# Plan\n\nThis is **bold**, *soft* and `code`.", 60);
        assert_eq!(rows(&r), ["Plan", "", "This is bold, soft and code."]);
        let heading = span_of(&r, "Plan").style;
        assert!(
            heading
                .add_modifier
                .contains(Modifier::BOLD | Modifier::UNDERLINED)
        );
        assert!(
            span_of(&r, "bold")
                .style
                .add_modifier
                .contains(Modifier::BOLD)
        );
        assert!(
            span_of(&r, "soft")
                .style
                .add_modifier
                .contains(Modifier::ITALIC)
        );
        assert_eq!(span_of(&r, "code").style, code_style());
    }

    /// A fenced block keeps its lines and indentation as written, behind a
    /// gutter, with the fence's language named above it.
    #[test]
    fn a_fenced_block_keeps_its_lines() {
        let r = draw("```rust\nfn main() {\n    run();\n}\n```", 40);
        assert_eq!(
            trimmed(&r),
            ["rust", "│ fn main() {", "│     run();", "│ }"]
        );
    }

    fn trimmed(r: &Rendered) -> Vec<String> {
        rows(r).iter().map(|l| l.trim_end().to_string()).collect()
    }

    /// Every row of a block, the language label aside, carries the code ground
    /// out to the full width — so the block's right edge is straight — and the
    /// label and the prose around it carry none.
    #[test]
    fn a_fenced_block_sits_on_a_ground_to_the_full_width() {
        let r = draw(
            "Before.\n\n```sh\nls\ncargo build --release\n```\n\nAfter.",
            30,
        );
        let ground = theme::gray(235);
        let grounds: Vec<bool> = r
            .lines
            .iter()
            .map(|l| l.spans.iter().any(|s| s.style.bg == Some(ground)))
            .collect();
        assert_eq!(
            trimmed(&r),
            [
                "Before.",
                "",
                "sh",
                "│ ls",
                "│ cargo build --release",
                "",
                "After."
            ]
        );
        assert_eq!(grounds, [false, false, false, true, true, false, false]);
        for i in [3, 4] {
            assert_eq!(r.lines[i].width(), 30, "row {i} stops short");
        }
    }

    /// A code line longer than the row wraps behind the gutter rather than
    /// running off the edge, and keeps its indentation on the first row.
    #[test]
    fn a_long_code_line_wraps_behind_its_gutter() {
        let r = draw("```\n    let total = alpha + beta + gamma;\n```", 20);
        let rows = trimmed(&r);
        assert!(rows.iter().all(|l| l.starts_with("│ ")), "{rows:?}");
        assert!(rows[0].starts_with("│     let"), "{rows:?}");
        assert!(rows.len() > 1, "{rows:?}");
        assert!(r.lines.iter().all(|l| l.width() <= 20));
    }

    /// A code block inside a list item stays under the item's text, with the
    /// marker in front of nothing but the item's own first row. (The blank line
    /// makes it a loose list, so its items are set apart.)
    #[test]
    fn a_code_block_in_a_list_item_hangs_under_the_text() {
        let r = draw("1. Run it:\n\n   ```\n   make\n   ```\n2. Done", 30);
        assert_eq!(trimmed(&r), ["1. Run it:", "", "   │ make", "", "2. Done"]);
    }

    /// Emphasis inside emphasis carries both styles, and each ends where its
    /// own marker does rather than where the outer one does.
    #[test]
    fn nested_emphasis_carries_both_styles() {
        let r = draw("**bold *both* bold** and ***all*** then plain", 80);
        assert_eq!(rows(&r), ["bold both bold and all then plain"]);
        let both = span_of(&r, "both").style.add_modifier;
        assert!(both.contains(Modifier::BOLD | Modifier::ITALIC), "{both:?}");
        let all = span_of(&r, "all").style.add_modifier;
        assert!(all.contains(Modifier::BOLD | Modifier::ITALIC), "{all:?}");
        let plain = span_of(&r, " then plain").style.add_modifier;
        assert!(!plain.intersects(Modifier::BOLD | Modifier::ITALIC));
    }

    /// Markers that never close are what they look like — characters — and do
    /// not bleed a style over the rest of the reply.
    #[test]
    fn an_unclosed_marker_stays_a_character() {
        let r = draw("a **dangling star and `half code", 80);
        assert_eq!(rows(&r), ["a **dangling star and `half code"]);
        assert!(
            !span_of(&r, "dangling")
                .style
                .add_modifier
                .contains(Modifier::BOLD)
        );
    }

    /// Bold text wrapped over a row boundary is bold on both rows, and the
    /// break falls at a space, never inside a word.
    #[test]
    fn emphasis_survives_a_wrap() {
        let r = draw("x **one two three four** y", 10);
        assert_eq!(rows(&r), ["x one two", "three four", "y"]);
        for word in ["one", "three", "four"] {
            assert!(
                span_of(&r, word)
                    .style
                    .add_modifier
                    .contains(Modifier::BOLD),
                "{word} lost its weight"
            );
        }
    }

    /// A word longer than the row is cut, since there is no space to break it
    /// at — and no row comes out wider than it was asked for.
    #[test]
    fn a_word_longer_than_the_row_is_cut() {
        let r = draw("see abcdefghijklmnopqrstuvwxyz", 10);
        assert_eq!(rows(&r), ["see", "abcdefghij", "klmnopqrst", "uvwxyz"]);
    }

    /// An ordered list keeps the number it started at, and a deeper list keeps
    /// its own count.
    #[test]
    fn a_numbered_list_counts_from_where_it_starts() {
        let r = draw("3. three\n4. four\n   1. sub\n   2. sub two", 30);
        assert_eq!(
            rows(&r),
            ["3. three", "4. four", "   1. sub", "   2. sub two"]
        );
    }

    /// Bullets and numbers are drawn, and a wrapped item's continuation lines
    /// up under its text rather than under its marker.
    #[test]
    fn lists_hang_their_continuation_under_the_text() {
        let r = draw("- one two three four\n- five\n\n1. first\n2. second", 12);
        assert_eq!(
            rows(&r),
            [
                "• one two",
                "  three four",
                "• five",
                "",
                "1. first",
                "2. second"
            ]
        );
    }

    /// A nested list runs on from its parent item, indented under its text.
    #[test]
    fn a_nested_list_indents_under_its_parent() {
        let r = draw("- outer\n  - inner", 30);
        assert_eq!(rows(&r), ["• outer", "  • inner"]);
    }

    /// A link is reported at the row and columns its label was drawn in —
    /// on both rows when it wraps — so OSC 8 can be laid over exactly them.
    #[test]
    fn a_link_is_reported_where_it_was_drawn() {
        let r = draw("See [the guide](https://example.com/g) now.", 12);
        assert_eq!(rows(&r), ["See the", "guide now."]);
        assert_eq!(
            r.links,
            vec![
                Link {
                    line: 0,
                    columns: 4..7,
                    url: "https://example.com/g".into()
                },
                Link {
                    line: 1,
                    columns: 0..5,
                    url: "https://example.com/g".into()
                },
            ]
        );
        assert!(
            span_of(&r, "guide")
                .style
                .add_modifier
                .contains(Modifier::UNDERLINED)
        );
    }

    /// Where the terminal cannot make a link, the URL is printed after its
    /// label — but a bare URL is not printed twice.
    #[test]
    fn without_osc8_the_url_is_spelled_out() {
        let r = render_with(
            "[docs](https://a.io) and <https://b.io>",
            80,
            Style::default(),
            false,
        );
        assert_eq!(rows(&r), ["docs <https://a.io> and https://b.io"]);
    }

    /// Quotes carry their bar on every row, gaps included.
    #[test]
    fn a_quote_keeps_its_bar() {
        let r = draw("> one\n>\n> two", 20);
        assert_eq!(rows(&r), ["│ one", "│ ", "│ two"]);
    }

    /// A table's columns line up, the header ruled off from the body.
    #[test]
    fn a_table_lines_up() {
        let r = draw("| a | bee |\n|---|---|\n| cc | d |", 40);
        assert_eq!(rows(&r), ["a  │ bee", "───┼────", "cc │ d  "]);
    }

    /// A rule spans the width; task markers stay legible.
    #[test]
    fn rules_and_task_lists() {
        let r = draw("- [x] done\n- [ ] todo\n\n---", 12);
        assert_eq!(rows(&r), ["• [x] done", "• [ ] todo", "", &"─".repeat(12)]);
    }
}