beady-eye 0.6.0

A tree of work in flight: bead graphs annotated with the live agents working them
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
//! One screen row fitted to the width it is given, and the cutting that fits it.
//!
//! A general widget: three blocks, one row, cut rather than wrapped, with a
//! stated yield order. It knows nothing of what it is drawing.

use std::num::NonZeroU16;

use ratatui::buffer::{Buffer, CellDiffOption};
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::text::{Line, Span};
use ratatui::widgets::{Clear, Widget};
use ratatui::Frame;

use crate::view::palette;

/// The mark left where a line ran out of width, so a cut line reads as cut
/// rather than as one that had nothing more to say.
pub(crate) const CUT: char = '';

/// The blank columns that keep two blocks from reading as one.
pub(crate) const GAP: usize = 2;

/// The escape that opens an operating-system command naming a hyperlink, and
/// the one that ends any such command. A URL between them makes what follows
/// a link; nothing between them ends it.
const OSC_8: &str = "\x1b]8;;";
const ST: &str = "\x1b\\";

/// Whether a terminal can be told that `said` points at `to`.
///
/// Both come from a tracker rather than from this program, and what ends this
/// sequence is itself a control character. One inside the sequence ends it
/// early, and the rest of that value reaches the terminal as commands of its
/// own — a row of a bead list saying whatever it likes to the terminal the
/// reader is sitting at.
///
/// Asked wherever a link is spoken about and not only where one is written:
/// a badge styled as a link the emitter then refuses is a row that says it
/// can be followed and cannot.
pub(crate) fn openable(said: &str, to: &str) -> bool {
    let holds_control = |text: &str| text.chars().any(char::is_control);
    !holds_control(said) && !holds_control(to)
}

/// `said`, wrapped so the terminal makes it a link to `to`. Nothing for a
/// pair `openable` refuses, which is this program's last word before the
/// bytes go out.
pub(crate) fn hyperlink(said: &str, to: &str) -> Option<String> {
    openable(said, to).then(|| format!("{OSC_8}{to}{ST}{said}{OSC_8}{ST}"))
}

/// What a cell says, with any escape sequence taken out of it.
///
/// A hyperlink is written into the symbol because it cannot be written into a
/// span, but it is not among the words: a reader sees where a link goes only
/// by following it.
pub(crate) fn words_of(symbol: &str) -> String {
    let mut words = String::new();
    let mut rest = symbol;
    while let Some(open) = rest.find(ESCAPE) {
        words.push_str(&rest[..open]);
        rest = match rest[open..].find(ST) {
            Some(end) => &rest[open + end + ST.len()..],
            None => "",
        };
    }
    words.push_str(rest);
    words
}

const ESCAPE: char = '\x1b';

/// A span of the title block that stands for somewhere the reader can go.
///
/// Named by its place rather than by what it draws: `Fitted` knows nothing of
/// what it is drawing, and a link is one more thing it does not have to know.
pub(crate) struct Link {
    /// Which span of the title block it is.
    pub(crate) at: usize,
    /// Where it points.
    pub(crate) to: String,
}

pub(crate) fn indent() -> String {
    " ".repeat(GAP)
}

/// One screen row, fitted to the width it is given by cutting rather than
/// wrapping. One line is one row: the selection is an index into the forest's
/// lines, so a line that wrapped would put that index and the screen row out
/// of step and the selection would leave the viewport with nothing failing.
///
/// The three blocks yield in a stated order. The `title` goes first, because a
/// reader who loses it still knows which row this is. The `state` goes next,
/// cut from its own end so its opening words survive. The `identity` yields
/// only when there is nothing else left to give.
pub struct Fitted {
    identity: Vec<Span<'static>>,
    title: Vec<Span<'static>>,
    state: Vec<Span<'static>>,
    briefly: Option<Vec<Span<'static>>>,
    links: Vec<Link>,
    whole: Style,
    title_or_nothing: bool,
    state_or_nothing: bool,
}

impl Fitted {
    pub(crate) fn new(
        identity: Vec<Span<'static>>,
        title: Vec<Span<'static>>,
        state: Vec<Span<'static>>,
    ) -> Self {
        Self {
            identity,
            title,
            state,
            briefly: None,
            links: Vec::new(),
            whole: Style::new(),
            title_or_nothing: false,
            state_or_nothing: false,
        }
    }

    /// Which spans of the title block are links, and where each one points.
    ///
    /// A link the row had to cut is dropped: an opening sequence with nothing
    /// to close it makes every cell after it on the terminal part of the link.
    #[must_use]
    pub(crate) fn linking(mut self, links: Vec<Link>) -> Self {
        self.links = links;
        self
    }

    /// A shorter form of the state, for a state holding something whose
    /// length is not this program's to choose.
    ///
    /// It changes which block gives way. Without one the state is fitted
    /// first and the title takes what is left, because a state is normally a
    /// handful of short cells this program wrote. With one, the row keeps
    /// room for the short form, gives the title the rest, and says the long
    /// form only where it costs the title nothing — so a caption that could
    /// be any length cannot eat the row it is drawn on.
    #[must_use]
    pub(crate) fn briefly(mut self, state: Vec<Span<'static>>) -> Self {
        self.briefly = Some(state);
        self
    }

    /// Give the title up whole rather than cut it.
    ///
    /// For a title that says nothing in part. An age cut to `30s a…` names no
    /// duration, so the columns it kept are spent saying that the project has
    /// been read — which its rows already said.
    #[must_use]
    pub(crate) fn title_or_nothing(mut self) -> Self {
        self.title_or_nothing = true;
        self
    }

    /// Give the state up whole rather than cut it.
    ///
    /// For a state that says nothing in part. A key row cut to `Ent…` names
    /// no key, so the columns it kept are spent saying that a key row exists
    /// — which the reader could already see.
    #[must_use]
    pub(crate) fn state_or_nothing(mut self) -> Self {
        self.state_or_nothing = true;
        self
    }

    /// The row under the cursor, drawn so the eye finds it without reading it.
    #[must_use]
    pub fn selected(mut self) -> Self {
        self.whole = self.whole.patch(palette::SELECTED);
        self
    }

    /// How live this line is, drawn under every span that did not ask for a
    /// colour of its own. A span that named one keeps it: the status glyph,
    /// the agent and the anomalies say what they say at any brightness.
    #[must_use]
    pub(crate) fn toned(mut self, tone: Style) -> Self {
        self.whole = tone.patch(self.whole);
        self
    }
}

impl Widget for Fitted {
    fn render(self, area: Rect, buf: &mut Buffer) {
        if area.width == 0 || area.height == 0 {
            return;
        }
        let area = Rect { height: 1, ..area };
        let width = area.width as usize;

        let links = self.links;
        let identity = columns(&self.identity);
        let (spans, linked) = if identity >= width {
            (cut_to(self.identity, width).0, Vec::new())
        } else {
            let room = width - identity;
            let ((title, whole), state) = match self.briefly {
                None => {
                    let (state, _) =
                        fit(self.state, room.saturating_sub(GAP), self.state_or_nothing);
                    let left = room - columns(&state) - if state.is_empty() { 0 } else { GAP };
                    (
                        fit(self.title, left.saturating_sub(GAP), self.title_or_nothing),
                        state,
                    )
                }
                Some(briefly) => {
                    // Whichever form is smaller, rather than the one named
                    // `briefly`: a pane terse enough makes the long form the
                    // short one, and room kept for a form the row will not
                    // use is room taken off the title for nothing.
                    let room_for_state = columns(&briefly).min(columns(&self.state)) + GAP;
                    let (title, whole) = fit(
                        self.title,
                        room.saturating_sub(GAP + room_for_state),
                        self.title_or_nothing,
                    );
                    let left = room - columns(&title) - if title.is_empty() { 0 } else { GAP };
                    let limit = left.saturating_sub(GAP);
                    let state = if columns(&self.state) <= limit {
                        self.state
                    } else {
                        fit(briefly, limit, self.state_or_nothing).0
                    };
                    ((title, whole), state)
                }
            };

            let linked = surviving(&links, &title, whole, identity + GAP);

            let mut spans = self.identity;
            if !title.is_empty() {
                spans.push(Span::raw(" ".repeat(GAP)));
                spans.extend(title);
            }
            if !state.is_empty() {
                let pad = width.saturating_sub(columns(&spans) + columns(&state));
                spans.push(Span::raw(" ".repeat(pad)));
                spans.extend(state);
            }
            (spans, linked)
        };

        Line::from(spans).style(self.whole).render(area, buf);
        for link in linked {
            link.told_to(area, buf);
        }
    }
}

/// A link the row kept whole, at the column it starts on.
struct Kept {
    at: usize,
    width: usize,
    said: String,
    to: String,
}

impl Kept {
    /// The link, told to the terminal in the cell it starts on.
    ///
    /// Everything the link says goes in that one cell, wrapped in the escape
    /// sequences, and the cell reports the width the words alone take. The
    /// diff then skips the columns behind it, so the opening sequence and its
    /// closer are one thing to send or to leave: a redraw carries both or
    /// neither, whatever else on the row changed.
    fn told_to(self, area: Rect, buf: &mut Buffer) {
        let (Ok(at), Some(width)) = (
            u16::try_from(self.at),
            u16::try_from(self.width).ok().and_then(NonZeroU16::new),
        ) else {
            return;
        };
        let Some(said) = hyperlink(&self.said, &self.to) else {
            return;
        };
        if let Some(cell) = buf.cell_mut((area.x + at, area.y)) {
            cell.set_symbol(&said)
                .set_diff_option(CellDiffOption::ForcedWidth(width));
        }
    }
}

/// The links whose span the title block kept whole, each at the column it
/// starts on. A link whose span was cut or dropped is not among them.
fn surviving(links: &[Link], title: &[Span<'static>], whole: usize, starts: usize) -> Vec<Kept> {
    links
        .iter()
        .filter(|link| link.at < whole)
        .map(|link| Kept {
            at: starts + columns(&title[..link.at]),
            width: title[link.at].width(),
            said: title[link.at].content.to_string(),
            to: link.to.clone(),
        })
        .collect()
}

/// Blank the ground a window is about to stand on.
///
/// `Clear` alone is not enough over a row carrying a link. The whole link sits
/// in the cell it starts on, reporting the width of its words, and the diff
/// skips the columns behind that cell whatever they now hold — so a link
/// starting outside the window and reaching under it swallows the window's own
/// left edge, and the border never reaches the terminal. Such a link gives the
/// columns back and stops being a link, and its words are written again into
/// the columns it still has. A badge under a window is a badge the reader
/// cannot see anyway.
pub(crate) fn cover(frame: &mut Frame, window: Rect) {
    frame.render_widget(Clear, window);

    let buf = frame.buffer_mut();
    for y in window.top()..window.bottom() {
        for x in buf.area.left()..window.left() {
            let Some(cell) = buf.cell((x, y)) else {
                continue;
            };
            let CellDiffOption::ForcedWidth(width) = cell.diff_option else {
                continue;
            };
            if x.saturating_add(width.get()) <= window.left() {
                continue;
            }
            let words = words_of(cell.symbol());
            let style = cell.style();
            let room = (window.left() - x) as usize;
            if let Some(cell) = buf.cell_mut((x, y)) {
                cell.reset();
            }
            buf.set_stringn(x, y, words, room, style);
        }
    }
}

/// What a run of spans takes up on screen, in columns rather than in bytes:
/// every glyph in this vocabulary is several bytes long, and a width counted
/// in bytes would put a cut inside one.
pub(crate) fn columns(spans: &[Span<'static>]) -> usize {
    spans.iter().map(Span::width).sum()
}

/// `spans` fitted into `limit` columns: cut with the cut marked, or, for a
/// block that says nothing in part, given up whole.
///
/// Alongside the spans, how many of them the block kept whole — the leading
/// run that says everything it was written to say. Anything after that run was
/// cut short or dropped, and what a caller holds against a span of it no
/// longer holds.
fn fit(spans: Vec<Span<'static>>, limit: usize, or_nothing: bool) -> (Vec<Span<'static>>, usize) {
    if or_nothing && columns(&spans) > limit {
        return (Vec::new(), 0);
    }
    cut_to(spans, limit)
}

/// `spans`, cut down to `limit` columns with the cut marked, and how many of
/// them survived whole.
fn cut_to(spans: Vec<Span<'static>>, limit: usize) -> (Vec<Span<'static>>, usize) {
    if columns(&spans) <= limit {
        let whole = spans.len();
        return (spans, whole);
    }
    if limit == 0 {
        return (Vec::new(), 0);
    }

    let room = limit - columns(&[Span::raw(CUT.to_string())]);
    let mut kept: Vec<Span<'static>> = Vec::new();
    let mut whole = 0;
    let mut used = 0;
    for span in spans {
        let width = span.width();
        if used + width <= room {
            used += width;
            whole += 1;
            kept.push(span);
            continue;
        }
        let head = head_of(&span.content, room - used);
        if !head.is_empty() {
            kept.push(Span::styled(head, span.style));
        }
        break;
    }
    kept.push(Span::raw(CUT.to_string()));
    (kept, whole)
}

/// As much of `text` as fits in `limit` columns, never splitting a glyph.
fn head_of(text: &str, limit: usize) -> String {
    let mut head = String::new();
    let mut used = 0;
    for glyph in text.chars() {
        let width = Span::raw(String::from(glyph)).width();
        if used + width > limit {
            break;
        }
        used += width;
        head.push(glyph);
    }
    head
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    use ratatui::backend::TestBackend;
    use ratatui::widgets::Block;
    use ratatui::Terminal;

    use crate::view::painted::Painted;

    /// A row with something in all three blocks, so any painting at all shows.
    fn a_row() -> Fitted {
        Fitted::new(
            vec![Span::raw("orb-7")],
            vec![Span::raw("a title")],
            vec![Span::raw("open")],
        )
    }

    fn blank(width: usize, height: usize) -> Vec<String> {
        vec![" ".repeat(width); height]
    }

    /// One row of what a widget puts on screen.
    fn drawn(row: Fitted, width: u16) -> String {
        Painted::of(row, width, 1).rows().remove(0)
    }

    /// A title cut in half can still be worth its columns — half a bead's
    /// title names the bead. A title that cannot is drawn whole or not at
    /// all, and the columns go to the blocks that can use them.
    #[test]
    fn a_title_that_says_nothing_in_part_is_given_up_whole() {
        let row = || {
            Fitted::new(
                vec![Span::raw("orb-7")],
                vec![Span::raw("collected 10:22:14")],
                vec![Span::raw("open")],
            )
            .title_or_nothing()
        };

        assert_eq!(drawn(row(), 31), "orb-7  collected 10:22:14  open");
        assert_eq!(drawn(row(), 30), "orb-7                     open");
    }

    /// Only where it is asked for. Every other row keeps the cut it had.
    #[test]
    fn a_title_is_cut_like_any_other_block_unless_it_asks_not_to_be() {
        let row = Fitted::new(
            vec![Span::raw("orb-7")],
            vec![Span::raw("collected 10:22:14")],
            vec![Span::raw("open")],
        );

        assert_eq!(drawn(row, 30), "orb-7  collected 10:22:…  open");
    }

    /// The foot's key row is the same case on the state block: half a key
    /// name presses nothing, so the row is drawn whole or not at all, and the
    /// columns it would have taken go to the title instead.
    #[test]
    fn a_state_that_says_nothing_in_part_is_given_up_whole() {
        let row = || {
            Fitted::new(
                vec![Span::raw("orb-7")],
                vec![Span::raw("a title")],
                vec![Span::raw("Enter focus   q quit")],
            )
            .state_or_nothing()
        };

        assert_eq!(drawn(row(), 27), "orb-7  Enter focus   q quit");
        assert_eq!(drawn(row(), 26), "orb-7  a title            ");
    }

    /// Only where it is asked for, as with the title.
    #[test]
    fn a_state_is_cut_like_any_other_block_unless_it_asks_not_to_be() {
        let row = Fitted::new(
            vec![Span::raw("orb-7")],
            vec![Span::raw("a title")],
            vec![Span::raw("Enter focus   q quit")],
        );

        assert_eq!(drawn(row, 26), "orb-7  Enter focus   q qu…");
    }

    /// A band of no rows is a band that was not asked for. Nothing on screen
    /// shows this going wrong: the row a zero-height band lands on is one the
    /// buffer is happy to be written to, so the drawing simply covers whatever
    /// was beneath it.
    #[test]
    fn a_band_of_no_rows_is_left_alone() {
        let mut buf = Buffer::empty(Rect::new(0, 0, 20, 3));

        a_row().render(
            Rect {
                x: 0,
                y: 1,
                width: 20,
                height: 0,
            },
            &mut buf,
        );

        assert_eq!(Painted::read(&buf).rows(), blank(20, 3));
    }

    /// A band of no columns likewise.
    #[test]
    fn a_band_of_no_columns_is_left_alone() {
        let mut buf = Buffer::empty(Rect::new(0, 0, 20, 3));

        a_row().render(
            Rect {
                x: 0,
                y: 1,
                width: 0,
                height: 1,
            },
            &mut buf,
        );

        assert_eq!(Painted::read(&buf).rows(), blank(20, 3));
    }

    /// The URL a reader clicks, in the vocabulary the fixtures use.
    const SOMEWHERE: &str = "https://forge.invalid/orbital/atlas/pull/12";

    /// A row whose third title span is a link, so the link is neither the
    /// first thing on the line nor the last.
    fn a_linked_row() -> Fitted {
        a_row_linking("⇢ #12")
    }

    /// The same row, for a badge whose text the caller chooses.
    fn a_row_linking(badge: &str) -> Fitted {
        Fitted::new(
            vec![Span::raw("orb-7")],
            vec![
                Span::raw("a title"),
                Span::raw(" "),
                Span::raw(badge.to_string()),
                Span::raw(" done"),
            ],
            Vec::new(),
        )
        .linking(vec![Link {
            at: 2,
            to: SOMEWHERE.to_string(),
        }])
    }

    /// Every symbol of a rendered row, run together.
    fn symbols(buf: &Buffer) -> String {
        (buf.area.left()..buf.area.right())
            .map(|x| buf[(x, 0)].symbol())
            .collect()
    }

    fn rendered(row: Fitted, width: u16) -> Buffer {
        let area = Rect::new(0, 0, width, 1);
        let mut buf = Buffer::empty(area);
        row.render(area, &mut buf);
        buf
    }

    /// The escape bytes cannot go in a span, because a span is measured by
    /// what it shows. They go in the cell the link starts on instead, around
    /// everything the link says.
    #[test]
    fn a_linked_span_is_wrapped_in_a_hyperlink_where_it_starts() {
        let buf = rendered(a_linked_row(), 40);

        assert!(
            symbols(&buf).contains(
                &hyperlink("⇢ #12", SOMEWHERE).expect("this vocabulary holds no control character")
            ),
            "the link is not on the row: {:?}",
            symbols(&buf)
        );
    }

    /// The cell says the width the link really takes, so the diff skips the
    /// columns behind it rather than counting the escape bytes as glyphs.
    #[test]
    fn a_linked_cell_reports_the_width_the_link_takes_on_screen() {
        let buf = rendered(a_linked_row(), 40);
        let at = opened_at(&buf);

        assert_eq!(
            buf[(at, 0)].diff_option,
            CellDiffOption::ForcedWidth(NonZeroU16::new(5).expect("⇢ #12 is five columns"))
        );
    }

    /// What a link needs is its own span whole, rather than the block it sits
    /// in. Here the row is cut after the link, and the link is still a link.
    #[test]
    fn a_link_the_cut_stopped_short_of_is_opened_as_it_always_was() {
        let said = symbols(&rendered(a_linked_row(), 21));

        assert!(
            said.contains(&CUT.to_string()),
            "the row was not cut at all, so it says nothing about a link \
             before the cut: {said:?}"
        );
        assert!(
            said.contains(
                &hyperlink("⇢ #12", SOMEWHERE).expect("this vocabulary holds no control character")
            ),
            "a link the cut stopped short of was dropped: {said:?}"
        );
    }

    /// A URL is a tracker's to write, and what ends the sequence carrying one
    /// is a control character. A link is worth less than a terminal a row can
    /// say anything it likes to.
    #[test]
    fn a_link_carrying_a_control_character_is_not_opened() {
        for to in [
            format!("https://forge.invalid{ST}\x1b]52;c;cGF5bG9hZA=={ST}"),
            "https://forge.invalid/\nfoo".to_string(),
            "https://forge.invalid/\rfoo".to_string(),
        ] {
            let row = Fitted::new(
                vec![Span::raw("orb-7")],
                vec![Span::raw("a title"), Span::raw(" "), Span::raw("⇢ #12")],
                Vec::new(),
            )
            .linking(vec![Link {
                at: 2,
                to: to.clone(),
            }]);

            let said = symbols(&rendered(row, 40));

            assert!(
                !said.contains(ESCAPE),
                "a link naming {to:?} reached the terminal: {said:?}"
            );
            assert!(
                said.contains("⇢ #12"),
                "the badge stopped drawing as well as stopped linking: {said:?}"
            );
        }
    }

    /// A window standing on a row whose link starts outside it. The link is
    /// holding columns the window needs, and the diff cannot reach past it, so
    /// without the hand-back the window's own left edge is never sent and the
    /// badge prints over it.
    ///
    /// Both a badge whose first glyph takes one column and one whose first
    /// glyph takes two, because the columns the link keeps are counted rather
    /// than assumed.
    #[test]
    fn a_window_over_a_link_that_started_outside_it_still_draws_its_own_edge() {
        // A badge starts at column 15, so a window opening at 17 stands on the
        // middle of one.
        const STARTS: u16 = 15;
        let window = Rect::new(17, 0, 10, 3);

        for (badge, glyph) in [("⇢ #12", ""), ("🔗 #12", "🔗")] {
            let mut terminal = Terminal::new(TestBackend::new(40, 3)).expect("a test backend");
            let mut draw_row_and = |window: Option<Rect>| {
                terminal
                    .draw(|frame| {
                        a_row_linking(badge).render(Rect::new(0, 0, 40, 1), frame.buffer_mut());
                        if let Some(window) = window {
                            cover(frame, window);
                            frame.render_widget(Block::bordered(), window);
                        }
                    })
                    .expect("a draw into memory");
            };
            draw_row_and(None);
            draw_row_and(Some(window));

            let screen = terminal.backend().buffer().clone();
            assert_eq!(
                screen[(window.left(), 0)].symbol(),
                "",
                "the window's left edge never reached the terminal, over {badge:?}"
            );
            assert_eq!(
                screen[(STARTS, 0)].symbol(),
                glyph,
                "the columns the link handed back say nothing the reader can see"
            );
        }
    }

    /// A link cut for width loses the link rather than its closing sequence.
    /// An opening sequence with nothing to close it makes every cell after it
    /// on the terminal part of the link.
    #[test]
    fn a_link_cut_for_width_is_not_opened_at_all() {
        let said = symbols(&rendered(a_linked_row(), 20));

        assert!(
            !said.contains(OSC_8),
            "a cut link opened a hyperlink: {said:?}"
        );
    }

    /// The opening and the closing sequence are one cell, so a diff carries
    /// both or neither. Here the link's first glyph changes and its last does
    /// not, which is the shape that would send an opening sequence alone.
    #[test]
    fn a_partial_redraw_cannot_send_an_opening_sequence_without_its_closer() {
        let moved = || {
            Fitted::new(
                vec![Span::raw("orb-7")],
                vec![Span::raw("a title"), Span::raw(" "), Span::raw("→ #12")],
                Vec::new(),
            )
            .linking(vec![Link {
                at: 2,
                to: SOMEWHERE.to_string(),
            }])
        };

        let before = rendered(a_linked_row(), 40);
        let after = rendered(moved(), 40);
        let sent: Vec<&str> = before
            .diff(&after)
            .into_iter()
            .map(|(_, _, cell)| cell.symbol())
            .collect();
        let opened: Vec<&&str> = sent.iter().filter(|said| said.contains(OSC_8)).collect();

        assert!(!opened.is_empty(), "the moved link was not sent: {sent:?}");
        for said in opened {
            assert!(
                said.ends_with(&format!("{OSC_8}{ST}")),
                "an opening sequence went without its closer: {said:?}"
            );
        }
    }

    /// Where the row's one hyperlink starts.
    fn opened_at(buf: &Buffer) -> u16 {
        (buf.area.left()..buf.area.right())
            .find(|&x| buf[(x, 0)].symbol().starts_with(OSC_8))
            .expect("a cell opening a hyperlink")
    }

    /// One line is one row. The selection is an index into the forest's lines,
    /// so a band that spilled past its first row would put that index and the
    /// screen out of step.
    #[test]
    fn a_band_of_several_rows_is_given_one() {
        let mut buf = Buffer::empty(Rect::new(0, 0, 20, 3));

        a_row().render(Rect::new(0, 0, 20, 3), &mut buf);

        assert_eq!(Painted::read(&buf).rows()[1..], blank(20, 2));
    }
}