croft-software 0.2.232

VSCode-style TUI written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
//! The quick-input card a `surfaces` theme (Croft Loch) draws for the
//! Command Palette, Go to File and the list pickers, one anatomy for all
//! three, after VS Code's quick input and Zed's command bar:
//!
//! ```text
//! ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔   accent lip, no title
//! ▏ › query|                     12 matches ▕   prompt glyph, count
//! ▏▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▕   the field's underline
//! ▎   View: Toggle Terminal     [Ctrl][J]   ▕   selected: band + bar
//! ▏   Terminal: Create New ...              ▕
//! ▏ ↑↓ navigate   ↵ run   esc close          ▕   footer on chrome
//! ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
//! ```
//!
//! The list keeps the rows the boxed themes give it (three below the card's
//! top edge), so every hit test stays where it was; the card is as tall as
//! its results, its top fixed where the tallest card's would be, so it
//! grows and shrinks downward while the user types. The query uses the
//! terminal's own blinking caret: [`paint_card`] returns where it goes.
//!
//! Boxed themes never reach this module; their pickers keep their own
//! drawing, byte for byte.

use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Clear, Widget},
};
use std::borrow::Cow;

use unicode_width::UnicodeWidthStr;

use crate::theme::Theme;
use crate::theme::Tones;
use crate::widgets::surface;

/// The fewest list rows a card shows, so a single result (or the empty
/// state's line) is not wedged between the rule and the footer.
pub const MIN_LIST_ROWS: u16 = 3;

/// Card rows that are not the list: the top edge, the input row, the rule
/// under it, the footer and the bottom edge.
const CHROME_ROWS: u16 = 5;

/// The prompt glyph's (and a file icon's) column, from the card's left edge:
/// the edge itself holds a selected row's accent bar, then one pad cell.
const GLYPH_COL: u16 = 2;

/// Where text starts, from the card's left edge: past the glyph slot and a
/// space, so a row's label lines up under the typed query.
const TEXT_COL: u16 = 4;

/// The geometry of one quick-input card.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Card {
    /// The whole card, edges included: what the widget stores as its
    /// `last_rect` for the click-outside test.
    pub rect: Rect,
    /// The result rows: three below the top edge, between the side edges.
    pub list: Rect,
    /// The footer row, when the card has room for one.
    footer: Option<u16>,
}

impl Card {
    /// Lay a card out in `area`: `width` wide, centred, never taller than
    /// `max_height`, and its top where a `max_height` card's top would sit
    /// (a quarter of the way down, or the middle with `center`, as Quick
    /// Input Position says). The list is sized to `rows` results, at least
    /// [`MIN_LIST_ROWS`], at most what the height leaves. A terminal too
    /// short for the footer drops it before it drops a result row.
    pub fn layout(area: Rect, width: u16, max_height: u16, rows: usize, center: bool) -> Card {
        let width = width.min(area.width);
        let max_h = max_height.min(area.height);
        let slack = area.height.saturating_sub(max_h);
        let y = area.y + if center { slack / 2 } else { slack / 4 };
        let footer = max_h >= CHROME_ROWS + MIN_LIST_ROWS;
        let chrome = if footer { CHROME_ROWS } else { CHROME_ROWS - 1 };
        let room = max_h.saturating_sub(chrome);
        let wanted = u16::try_from(rows).unwrap_or(u16::MAX).max(MIN_LIST_ROWS);
        let list_h = wanted.min(room);
        let height = (chrome + list_h).min(max_h);
        let rect = Rect {
            x: area.x + area.width.saturating_sub(width) / 2,
            y,
            width,
            height,
        };
        let list = Rect {
            x: rect.x + 1,
            y: rect.y + 3,
            width: rect.width.saturating_sub(2),
            height: list_h,
        };
        Card {
            rect,
            list,
            footer: footer.then_some(list.y + list_h),
        }
    }

    /// The first column past the content: one gutter cell short of the
    /// right edge, so keycaps and the count never touch the rim.
    fn content_end(&self) -> u16 {
        self.rect.right().saturating_sub(2)
    }
}

/// What a card's input row and footer say.
pub struct Input<'a> {
    /// The accent glyph before the query (`>` for commands, `›` for files).
    pub glyph: &'a str,
    pub query: &'a str,
    /// The caret, in chars into `query`.
    pub cursor: usize,
    /// Shown, muted and italic, while the query is empty.
    pub placeholder: &'a str,
    /// Right-aligned in muted ink: how many results there are.
    pub count: &'a str,
    /// What Enter does, for the footer (`run`, `open`, `select`).
    pub verb: &'a str,
    /// What the count must never lose (a list picker's title while the user
    /// filters: what Enter will do). With it set the count keeps its place
    /// on the row and the query scrolls in what is left; where the whole
    /// count does not fit, this part alone stays, cut with `…` if need be.
    pub keep: Option<&'a str>,
}

/// Paint the card: the raised body, the edge (accent lip, rim, shadow), the
/// input row, the rule under it and the footer on the chrome tone. Returns
/// the cell for the terminal's caret, or `None` when the row has no room.
pub fn paint_card(
    buf: &mut Buffer,
    card: &Card,
    theme: Theme,
    t: Tones,
    input: &Input,
) -> Option<(u16, u16)> {
    let rect = card.rect;
    if rect.width < 2 || rect.height < 2 || rect.intersection(buf.area) != rect {
        return None;
    }
    Clear.render(rect, buf);
    buf.set_style(rect, Style::default().bg(t.raised()).fg(t.text()));
    if let Some(fy) = card.footer {
        // The footer and the bottom edge under it share the chrome tone,
        // so the footer reads as a tray the rim closes, not a stripe.
        let tray = Rect {
            y: fy,
            height: rect.bottom().saturating_sub(fy),
            ..rect
        };
        buf.set_style(tray, Style::default().bg(t.chrome()));
    }
    surface::paint_overlay_frame(buf, rect, theme);
    let end = card.content_end();
    let caret = if rect.height >= 3 {
        paint_input_row(buf, rect.x, rect.y + 1, end, t, input)
    } else {
        None
    };
    if rect.height >= 4 {
        let rule = "\u{2594}".repeat(usize::from(rect.width - 2));
        buf.set_string(
            rect.x + 1,
            rect.y + 2,
            rule,
            Style::default().fg(t.hairline()),
        );
    }
    if let Some(fy) = card.footer {
        let key = Style::default().fg(t.text());
        let word = Style::default().fg(t.muted());
        let hints = Line::from(vec![
            Span::styled("\u{2191}\u{2193}", key),
            Span::styled(" navigate   ", word),
            Span::styled("\u{21b5}", key),
            Span::styled(format!(" {}   ", input.verb), word),
            Span::styled("esc", key),
            Span::styled(" close", word),
        ]);
        let x = rect.x + GLYPH_COL;
        buf.set_line(x, fy, &hints, end.saturating_sub(x));
    }
    caret
}

/// The fewest query cells a kept count leaves beside itself.
const MIN_KEPT_QUERY: usize = 6;

/// What a kept count shows in `room` cells: the whole count, else the part
/// it must keep, else that part cut to fit with a `…`. `None` when not even
/// two cells are left for it.
fn kept_label<'a>(count: &'a str, keep: &'a str, room: usize) -> Option<Cow<'a, str>> {
    if count.width() <= room {
        return Some(Cow::Borrowed(count));
    }
    if keep.width() <= room {
        return Some(Cow::Borrowed(keep));
    }
    if room < 2 {
        return None;
    }
    let mut cut = String::new();
    let mut w = 0;
    for c in keep.chars() {
        let cw = unicode_width::UnicodeWidthChar::width(c).unwrap_or(0);
        if w + cw > room - 1 {
            break;
        }
        cut.push(c);
        w += cw;
    }
    cut.push('\u{2026}');
    Some(Cow::Owned(cut))
}

/// The input row: the accent glyph, the query (or the placeholder) and the
/// count at the right. A query wider than the field scrolls to keep the
/// caret in view, a `…` standing for the hidden start. Returns the caret.
fn paint_input_row(
    buf: &mut Buffer,
    left: u16,
    y: u16,
    end: u16,
    t: Tones,
    input: &Input,
) -> Option<(u16, u16)> {
    let glyph_x = left + GLYPH_COL;
    if glyph_x < end {
        buf.set_stringn(
            glyph_x,
            y,
            input.glyph,
            usize::from(end - glyph_x),
            Style::default().fg(t.accent()),
        );
    }
    let x0 = left + TEXT_COL;
    if x0 >= end {
        return None;
    }
    let field = usize::from(end - x0);
    // A kept count claims its cells (and a two-cell gap) first, leaving a
    // few cells of query beside it; the query then scrolls in the rest, so
    // typing never pushes what the count keeps off the row.
    let kept_count = input
        .keep
        .filter(|_| !input.count.is_empty())
        .and_then(|keep| kept_label(input.count, keep, field.saturating_sub(2 + MIN_KEPT_QUERY)));
    let kept = kept_count.is_some();
    let count: &str = kept_count.as_deref().unwrap_or(input.count);
    let count_w = count.width();
    let end_q = if kept {
        end - u16::try_from(count_w + 2).unwrap_or(0)
    } else {
        end
    };
    let field_q = usize::from(end_q - x0);
    let (text_w, caret_x) = if input.query.is_empty() {
        let style = Style::default()
            .fg(t.muted())
            .add_modifier(Modifier::ITALIC);
        let (after_x, _) = buf.set_stringn(x0, y, input.placeholder, field_q, style);
        (usize::from(after_x - x0), x0)
    } else {
        let cursor = input.cursor.min(input.query.chars().count());
        let (skip, lead) = crate::widgets::input_prompt::field_scroll(input.query, cursor, field_q);
        let mut x = x0;
        if lead {
            buf.set_string(x, y, "\u{2026}", Style::default().fg(t.faint()));
            x += 1;
        }
        let visible: String = input.query.chars().skip(skip).collect();
        let before: String = input.query.chars().skip(skip).take(cursor - skip).collect();
        let caret_x = x + u16::try_from(before.width()).unwrap_or(u16::MAX);
        let (after_x, _) = buf.set_stringn(
            x,
            y,
            &visible,
            usize::from(end_q.saturating_sub(x)),
            Style::default().fg(t.text()),
        );
        (
            usize::from(after_x - x0),
            caret_x.min(end_q.max(x0 + 1) - 1),
        )
    };
    // The count only where it clears the text and the caret by two cells,
    // unless it is kept, when its cells were set aside above.
    if count_w > 0 && (kept || text_w + 1 + 2 + count_w <= field) {
        let cx = end - u16::try_from(count_w).unwrap_or(0);
        buf.set_string(cx, y, count, Style::default().fg(t.muted()));
    }
    Some((caret_x, y))
}

/// One result row's content, for [`paint_row`].
pub struct Row {
    /// The row has the selection (and the list the keyboard).
    pub selected: bool,
    /// A file icon, in the prompt glyph's column.
    pub glyph: Option<Span<'static>>,
    /// The label, at the text column.
    pub label: Vec<Span<'static>>,
    /// Right-aligned against the gutter: the keycaps.
    pub trail: Vec<Span<'static>>,
}

/// Paint result row `i` of the list (0 is the top visible row). A selected
/// row gets the band with the accent bar in the card's left edge, and the
/// band runs on under the right edge too (the column [`paint_scroll_cue`]
/// lights, painted over it afterwards), so it spans the card rim to rim
/// with both ends meeting the edge alike, rather than stopping a cell
/// short on the right. `glyph` (a file icon) sits in the prompt glyph's
/// column, `label` at the text column, cut with a `…` when it runs out of
/// room, and `trail` (keycaps) right-aligned against the gutter, dropped
/// when the label leaves no room for it.
pub fn paint_row(buf: &mut Buffer, card: &Card, i: u16, t: Tones, row: Row) {
    let Row {
        selected,
        glyph,
        label,
        trail,
    } = row;
    if i >= card.list.height {
        return;
    }
    let y = card.list.y + i;
    let rect = card.rect;
    if selected {
        let row = Rect {
            x: rect.x,
            y,
            width: rect.width,
            height: 1,
        };
        surface::paint_selected_row(buf, row, t, t.raised, true);
    }
    let end = card.content_end();
    if let Some(g) = glyph {
        let gx = rect.x + GLYPH_COL;
        if gx < end {
            buf.set_span(gx, y, &g, end - gx);
        }
    }
    let x0 = rect.x + TEXT_COL;
    if x0 >= end {
        return;
    }
    let room = usize::from(end - x0);
    let label_w: usize = label.iter().map(Span::width).sum();
    let trail_w: usize = trail.iter().map(Span::width).sum();
    let label_end = if trail_w > 0 && label_w + 2 + trail_w <= room {
        let tx = end - u16::try_from(trail_w).unwrap_or(0);
        buf.set_line(tx, y, &Line::from(trail), end - tx);
        tx - 2
    } else {
        end
    };
    let line = Line::from(label);
    let fit = usize::from(label_end - x0);
    if label_w <= fit {
        buf.set_line(x0, y, &line, label_end - x0);
    } else if fit > 0 {
        let (cut, _) = buf.set_line(x0, y, &line, label_end - x0 - 1);
        buf.set_string(cut, y, "\u{2026}", Style::default().fg(t.muted()));
    }
}

/// The empty state: one muted italic line, centred in the list.
pub fn paint_empty(buf: &mut Buffer, card: &Card, t: Tones, message: &str) {
    let list = card.list;
    if list.height == 0 || list.width == 0 {
        return;
    }
    let w = u16::try_from(message.width())
        .unwrap_or(u16::MAX)
        .min(list.width);
    let x = list.x + (list.width - w) / 2;
    let y = list.y + (list.height - 1) / 2;
    buf.set_stringn(
        x,
        y,
        message,
        usize::from(w),
        Style::default()
            .fg(t.muted())
            .add_modifier(Modifier::ITALIC),
    );
}

/// Where the visible rows sit in a longer list: the card's right edge
/// brightens to muted ink along the stretch the rows cover, so an
/// overflowing list says it scrolls without a scrollbar's column. Called
/// after the rows: only the glyph and its ink change, so where the thumb
/// crosses the selected row the band [`paint_row`] laid stays under it.
pub fn paint_scroll_cue(buf: &mut Buffer, card: &Card, t: Tones, total: usize, scroll: usize) {
    let track = usize::from(card.list.height);
    if track == 0 || total <= track || card.rect.width < 2 {
        return;
    }
    let thumb = (track * track).div_ceil(total).clamp(1, track);
    let travel = track - thumb;
    let max_scroll = total - track;
    let start = (scroll.min(max_scroll) * travel + max_scroll / 2) / max_scroll;
    let x = card.rect.right() - 1;
    for row in start..start + thumb {
        let y = card.list.y + u16::try_from(row).unwrap_or(0);
        if let Some(cell) = buf.cell_mut((x, y)) {
            cell.set_symbol("\u{2595}");
            cell.set_fg(t.muted());
        }
    }
}

/// The byte offsets in `hay` of the characters `query` matched, for the
/// [`hit_style`] highlight: the first case-insensitive run of the whole
/// query when there is one (what a typed word means), else the in-order
/// subsequence the fuzzy filters accept, else nothing (a row kept for its
/// keyword or its English title shows no highlight rather than a wrong one).
pub fn match_offsets(hay: &str, query: &str) -> Vec<usize> {
    let query = query.trim();
    if query.is_empty() {
        return Vec::new();
    }
    run_offsets(hay, query)
        .or_else(|| crate::widgets::file_tree::subsequence_match(hay, query))
        .unwrap_or_default()
}

/// The byte offsets of the first case-insensitive run of all of `query` in
/// `hay`, compared char by char (as the filters lower-case), or `None`.
pub fn run_offsets(hay: &str, query: &str) -> Option<Vec<usize>> {
    let needle: Vec<char> = query.chars().flat_map(char::to_lowercase).collect();
    let chars: Vec<(usize, char)> = hay.char_indices().collect();
    if needle.is_empty() || needle.len() > chars.len() {
        return None;
    }
    let same = |c: char, want: char| c.to_lowercase().eq([want]);
    (0..=chars.len() - needle.len()).find_map(|start| {
        let run = &chars[start..start + needle.len()];
        run.iter()
            .zip(&needle)
            .all(|(&(_, c), &w)| same(c, w))
            .then(|| run.iter().map(|&(b, _)| b).collect())
    })
}

/// Spans for `text`, which starts at byte `from` of the string `offsets`
/// index: the characters at those offsets in `hit`, the rest in `base`, a
/// run of either kept in one span.
pub fn highlighted(
    text: &str,
    from: usize,
    offsets: &[usize],
    base: Style,
    hit: Style,
) -> Vec<Span<'static>> {
    if offsets.is_empty() {
        return vec![Span::styled(text.to_string(), base)];
    }
    let mut spans = Vec::new();
    let mut run = String::new();
    let mut run_hit = false;
    for (i, c) in text.char_indices() {
        let is_hit = offsets.binary_search(&(from + i)).is_ok();
        if is_hit != run_hit && !run.is_empty() {
            let style = if run_hit { hit } else { base };
            spans.push(Span::styled(std::mem::take(&mut run), style));
        }
        run_hit = is_hit;
        run.push(c);
    }
    if !run.is_empty() {
        spans.push(Span::styled(run, if run_hit { hit } else { base }));
    }
    spans
}

/// The style a matched character takes: the strong ink, bold. Not the
/// accent: a typed word matches on every row, so an accent hit would run a
/// teal column down the whole list, louder than the selection it competes
/// with, and the accent means focus and selection alone. A step of ink and
/// weight over the row's `text` (or muted category) is enough to show what
/// matched.
pub fn hit_style(t: Tones) -> Style {
    Style::default().fg(t.strong()).add_modifier(Modifier::BOLD)
}

/// The count at the input row's right: the whole list's size (`412
/// commands`) while nothing is typed, the number of matches after; `capped`
/// says the list stops at its limit, so there may be more (`40+ matches`).
pub fn count_label(n: usize, filtering: bool, capped: bool, one: &str, many: &str) -> String {
    if filtering {
        let plus = if capped { "+" } else { "" };
        let noun = if n == 1 && !capped {
            "match"
        } else {
            "matches"
        };
        format!("{n}{plus} {noun}")
    } else {
        format!("{n} {}", if n == 1 { one } else { many })
    }
}

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

    fn loch() -> Theme {
        Theme::from_id("loch")
    }

    fn input<'a>(query: &'a str) -> Input<'a> {
        Input {
            glyph: ">",
            query,
            cursor: query.chars().count(),
            placeholder: "Type a command",
            count: "3 matches",
            verb: "run",
            keep: None,
        }
    }

    #[test]
    fn a_kept_count_falls_back_to_what_it_keeps_then_cuts_it() {
        assert_eq!(
            kept_label("Pop Stash \u{b7} 3 matches", "Pop Stash", 30).as_deref(),
            Some("Pop Stash \u{b7} 3 matches")
        );
        assert_eq!(
            kept_label("Pop Stash \u{b7} 100 matches", "Pop Stash", 12).as_deref(),
            Some("Pop Stash")
        );
        assert_eq!(
            kept_label("Pop Stash \u{b7} 100 matches", "Pop Stash", 5).as_deref(),
            Some("Pop \u{2026}")
        );
        assert_eq!(kept_label("x \u{b7} 1 match", "Pop Stash", 1), None);
    }

    fn row(buf: &Buffer, y: u16, rect: Rect) -> String {
        (rect.x..rect.right())
            .map(|x| buf[(x, y)].symbol().to_string())
            .collect()
    }

    #[test]
    fn the_card_hugs_its_results_and_keeps_its_top() {
        let area = Rect::new(0, 0, 100, 40);
        let few = Card::layout(area, 70, 24, 2, false);
        let many = Card::layout(area, 70, 24, 200, false);
        assert_eq!(few.rect.y, many.rect.y, "the top never moves");
        assert_eq!(few.list.height, MIN_LIST_ROWS, "one result is not cramped");
        assert_eq!(few.rect.height, MIN_LIST_ROWS + CHROME_ROWS);
        assert_eq!(many.rect.height, 24);
        assert_eq!(
            many.list.y,
            many.rect.y + 3,
            "the list stays three rows down"
        );
        assert_eq!(few.footer, Some(few.list.bottom()));
    }

    #[test]
    fn a_short_terminal_drops_the_footer_before_a_row() {
        let card = Card::layout(Rect::new(0, 0, 80, 6), 60, 6, 9, false);
        assert_eq!(card.footer, None);
        assert_eq!(card.list.height, 2);
        assert_eq!(card.rect.bottom(), 6);
        for h in 0..9 {
            let area = Rect::new(0, 0, 30, h);
            let card = Card::layout(area, 40, 10, 5, false);
            let mut buf = Buffer::empty(area);
            paint_card(
                &mut buf,
                &card,
                loch(),
                crate::theme::tones(loch()).unwrap(),
                &input("x"),
            );
            assert!(card.rect.bottom() <= h);
        }
    }

    #[test]
    fn the_input_row_has_the_glyph_the_query_the_count_and_the_caret() {
        let t = crate::theme::tones(loch()).unwrap();
        let area = Rect::new(0, 0, 60, 20);
        let card = Card::layout(area, 50, 12, 3, false);
        let mut buf = Buffer::empty(area);
        let caret = paint_card(&mut buf, &card, loch(), t, &input("term")).unwrap();
        let r = card.rect;
        let line = row(&buf, r.y + 1, r);
        assert!(line.contains("> term"), "{line:?}");
        assert!(
            line.trim_end_matches('\u{2595}').ends_with("3 matches "),
            "{line:?}"
        );
        assert_eq!(
            buf[(r.x + 2, r.y + 1)].fg,
            t.accent(),
            "the glyph is the accent"
        );
        assert_eq!(buf[(r.x + 4, r.y + 1)].fg, t.text());
        assert!(!buf[(r.x + 4, r.y + 1)].modifier.contains(Modifier::BOLD));
        assert_eq!(caret, (r.x + 8, r.y + 1), "the caret sits after the query");
        let rule = row(&buf, r.y + 2, r);
        assert_eq!(
            rule,
            format!("\u{258f}{}\u{2595}", "\u{2594}".repeat(48)),
            "an eighth-block rule under the field"
        );
        assert_eq!(buf[(r.x + 5, r.y + 2)].fg, t.hairline());
        assert!(!rule.contains('\u{2500}'), "no box-drawing line");
    }

    #[test]
    fn an_empty_query_shows_the_placeholder_muted_and_italic() {
        let t = crate::theme::tones(loch()).unwrap();
        let area = Rect::new(0, 0, 60, 20);
        let card = Card::layout(area, 50, 12, 3, false);
        let mut buf = Buffer::empty(area);
        let caret = paint_card(&mut buf, &card, loch(), t, &input("")).unwrap();
        let r = card.rect;
        assert!(row(&buf, r.y + 1, r).contains("Type a command"));
        let cell = &buf[(r.x + 4, r.y + 1)];
        assert_eq!(cell.fg, t.muted());
        assert!(cell.modifier.contains(Modifier::ITALIC));
        assert_eq!(caret, (r.x + 4, r.y + 1), "the caret waits at the start");
    }

    #[test]
    fn the_footer_sits_on_the_chrome_tone_with_the_keys() {
        let t = crate::theme::tones(loch()).unwrap();
        let area = Rect::new(0, 0, 60, 20);
        let card = Card::layout(area, 50, 12, 3, false);
        let mut buf = Buffer::empty(area);
        paint_card(&mut buf, &card, loch(), t, &input(""));
        let fy = card.footer.unwrap();
        assert_eq!(fy, card.rect.bottom() - 2);
        let line = row(&buf, fy, card.rect);
        assert!(line.contains("navigate"), "{line:?}");
        assert!(line.contains("\u{21b5} run"), "{line:?}");
        assert!(line.contains("esc close"), "{line:?}");
        assert_eq!(buf[(card.rect.x + 6, fy)].bg, t.chrome());
        assert_eq!(buf[(card.rect.x + 6, fy + 1)].bg, t.chrome());
        assert_eq!(
            buf[(card.rect.x + 2, fy)].fg,
            t.text(),
            "the keys in text ink"
        );
        assert_eq!(buf[(card.rect.x + 6, fy)].fg, t.muted(), "the words muted");
    }

    #[test]
    fn a_selected_row_puts_the_bar_on_the_cards_edge_and_keycaps_clear_the_rim() {
        let t = crate::theme::tones(loch()).unwrap();
        let area = Rect::new(0, 0, 60, 20);
        let card = Card::layout(area, 50, 12, 3, false);
        let mut buf = Buffer::empty(area);
        paint_card(&mut buf, &card, loch(), t, &input(""));
        let caps = surface::keycap_spans("Ctrl+J", t, t.band(), true);
        let label = vec![Span::raw("View: Toggle Terminal")];
        paint_row(
            &mut buf,
            &card,
            0,
            t,
            Row {
                selected: true,
                glyph: None,
                label,
                trail: caps,
            },
        );
        let (r, y) = (card.rect, card.list.y);
        assert_eq!(buf[(r.x, y)].symbol(), surface::ROW_BAR);
        assert_eq!(buf[(r.x, y)].fg, t.accent());
        assert_eq!(buf[(r.x + 1, y)].bg, t.band(), "the band spans the card");
        assert_eq!(buf[(r.right() - 2, y)].bg, t.band());
        assert_eq!(
            buf[(r.right() - 1, y)].bg,
            t.band(),
            "the band meets the right rim as it meets the left"
        );
        assert_eq!(buf[(r.right() - 1, y)].symbol(), "\u{2595}", "the rim");
        assert_ne!(buf[(r.right(), y)].bg, t.band(), "not past the card");
        assert_eq!(
            buf[(r.right() - 2, y)].symbol(),
            " ",
            "a gutter before the rim"
        );
        let line = row(&buf, y, r);
        assert!(
            line.starts_with("\u{258e}   View: Toggle Terminal"),
            "{line:?}"
        );
        assert!(line.contains(" Ctrl + J  \u{2595}"), "{line:?}");
        assert!(!line.contains('>'), "no ASCII marker");
    }

    #[test]
    fn a_label_too_long_for_the_row_ends_in_an_ellipsis_and_drops_its_keys() {
        let t = crate::theme::tones(loch()).unwrap();
        let area = Rect::new(0, 0, 30, 12);
        let card = Card::layout(area, 30, 10, 1, false);
        let mut buf = Buffer::empty(area);
        paint_card(&mut buf, &card, loch(), t, &input(""));
        let caps = surface::keycap_spans("Ctrl+J", t, t.raised(), false);
        let label = vec![Span::raw("a".repeat(40))];
        paint_row(
            &mut buf,
            &card,
            0,
            t,
            Row {
                selected: false,
                glyph: None,
                label,
                trail: caps,
            },
        );
        let line = row(&buf, card.list.y, card.rect);
        assert!(line.contains("a\u{2026} \u{2595}"), "{line:?}");
        assert!(!line.contains("Ctrl"), "{line:?}");
    }

    #[test]
    fn the_empty_state_is_a_centred_muted_italic_line() {
        let t = crate::theme::tones(loch()).unwrap();
        let area = Rect::new(0, 0, 60, 20);
        let card = Card::layout(area, 50, 12, 0, false);
        let mut buf = Buffer::empty(area);
        paint_card(&mut buf, &card, loch(), t, &input("zzz"));
        paint_empty(&mut buf, &card, t, "No matching commands");
        let y = card.list.y + 1;
        let x = card.list.x + (card.list.width - 20) / 2;
        assert_eq!(buf[(x, y)].symbol(), "N");
        let (left, right) = (x - card.list.x, card.list.right() - (x + 20));
        assert!(left.abs_diff(right) <= 1, "centred: {left} / {right}");
        assert_eq!(buf[(x, y)].fg, t.muted());
        assert!(buf[(x, y)].modifier.contains(Modifier::ITALIC));
    }

    #[test]
    fn an_overflowing_list_lights_its_stretch_of_the_right_edge() {
        let t = crate::theme::tones(loch()).unwrap();
        let area = Rect::new(0, 0, 60, 20);
        let card = Card::layout(area, 50, 10, 40, false);
        let mut buf = Buffer::empty(area);
        paint_card(&mut buf, &card, loch(), t, &input(""));
        paint_scroll_cue(&mut buf, &card, t, 40, 0);
        let x = card.rect.right() - 1;
        assert_eq!(buf[(x, card.list.y)].fg, t.muted(), "the thumb at the top");
        assert_eq!(buf[(x, card.list.bottom() - 1)].fg, t.raised(), "the track");
    }

    /// Croft Loch: where the scroll thumb crosses the selected row it is
    /// painted on the band, which runs on under it to the rim, so the
    /// band's right end is as square as its left.
    #[test]
    fn the_scroll_thumb_sits_on_the_selected_rows_band() {
        let t = crate::theme::tones(loch()).unwrap();
        let area = Rect::new(0, 0, 60, 20);
        let card = Card::layout(area, 50, 10, 40, false);
        let mut buf = Buffer::empty(area);
        paint_card(&mut buf, &card, loch(), t, &input(""));
        for i in 0..card.list.height {
            paint_row(
                &mut buf,
                &card,
                i,
                t,
                Row {
                    selected: i == 0,
                    glyph: None,
                    label: vec![Span::raw("row")],
                    trail: Vec::new(),
                },
            );
        }
        paint_scroll_cue(&mut buf, &card, t, 40, 0);
        let (x, y) = (card.rect.right() - 1, card.list.y);
        assert_eq!(buf[(x, y)].symbol(), "\u{2595}");
        assert_eq!(buf[(x, y)].fg, t.muted(), "the thumb");
        assert_eq!(buf[(x, y)].bg, t.band(), "on the band");
        assert_eq!(buf[(card.rect.x, y)].bg, t.band(), "both ends alike");
        assert_eq!(buf[(x, y + 1)].bg, t.raised(), "an idle row is the card");
    }

    #[test]
    fn matches_prefer_a_contiguous_run_then_fall_back_to_a_subsequence() {
        assert_eq!(
            match_offsets("View: Toggle Terminal", "term"),
            [13, 14, 15, 16]
        );
        assert_eq!(match_offsets("src/foo/bar.rs", "fbr"), [4, 8, 10]);
        assert_eq!(match_offsets("abc", "xyz"), Vec::<usize>::new());
        assert_eq!(match_offsets("abc", "  "), Vec::<usize>::new());
        let t = crate::theme::tones(loch()).unwrap();
        let spans = highlighted(
            "Toggle",
            6,
            &[6, 7],
            Style::default().fg(t.text()),
            hit_style(t),
        );
        let text: Vec<&str> = spans.iter().map(|s| s.content.as_ref()).collect();
        assert_eq!(text, ["To", "ggle"]);
        assert_eq!(spans[0].style, hit_style(t));
    }

    /// Croft Loch: a matched character is the strong ink in bold, a step
    /// over the body text, never the accent, which a whole-word query would
    /// otherwise run down every row as a teal column.
    #[test]
    fn a_match_is_strong_bold_ink_not_the_accent() {
        let t = crate::theme::tones(loch()).unwrap();
        let hit = hit_style(t);
        assert_eq!(hit.fg, Some(t.strong()));
        assert_ne!(hit.fg, Some(t.accent()));
        assert!(hit.add_modifier.contains(Modifier::BOLD));
        assert_eq!(hit.bg, None, "the row's fill shows through");
    }

    #[test]
    fn the_count_names_the_list_then_the_matches() {
        assert_eq!(
            count_label(412, false, false, "command", "commands"),
            "412 commands"
        );
        assert_eq!(count_label(1, true, false, "file", "files"), "1 match");
        assert_eq!(count_label(40, true, true, "file", "files"), "40+ matches");
    }
}