rs-rich-interact 0.0.13

Interactive terminal components for rs-rich: event loop, viewport, item model, headless testing
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
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
//! The building-block kit: the pieces the built-in components are made of,
//! public so your own components can use them too (0.0.14 workstream 1).
//!
//! Two kinds of piece live here.
//!
//! **Line helpers** build and measure the lines of a [`View`](crate::View):
//! [`text`] and [`plain`] make segments, [`width`] measures a line,
//! [`fit`] crops one and [`pad`] crops or pads it to an exact width,
//! [`highlight`] styles the characters a filter matched, and [`question`]
//! is the `? prompt › ` line every built-in starts with. [`slice`](fn@slice) cuts
//! cells out of a line, [`overlay`] draws one line over another and
//! [`restyle`] puts a style over a line (a backdrop); [`frame`] boxes
//! lines and [`place`] draws a box over a view, as a modal is drawn.
//! [`pasted`] and
//! [`shown`] make untrusted text safe for a one-line field or a table cell.
//!
//! **State types** hold what a component remembers between events, with the
//! editing and movement rules the built-ins share, and no rendering of
//! their own:
//!
//! - [`ListState`]: a cursor over rows, the first row shown, and a
//!   selection (what [`Select`](crate::Select) moves and marks with);
//! - [`ScrollState`]: an offset into lines (what a
//!   [`Viewport`](crate::Viewport) scrolls with);
//! - [`FilterState`]: a query, and the candidates that match it, ranked by
//!   the [fuzzy](crate::fuzzy) matcher, with the characters to highlight;
//! - [`TextBuffer`]: one line of text and a caret that moves by grapheme
//!   cluster, with the shell editing keys' operations and an undo point
//!   (what [`Input`](crate::Input) edits);
//! - [`Divider`]: a border between two panes that the mouse drags or the
//!   keyboard nudges (what [`Split`](crate::compose::Split) and
//!   `Select`'s preview resize with);
//! - [`ActionMenu`]: the Ctrl+K menu of [`Action`]s for one target.
//!
//! ```
//! use rich_interact::kit::{FilterState, ListState};
//!
//! let mut filter = FilterState::new(["main.rs", "lib.rs", "README.md"]);
//! filter.set_query("rs");
//! assert_eq!(filter.len(), 2);
//! let mut list = ListState::new();
//! list.set_len(filter.len());
//! list.step(1, 10);
//! let second = filter.index(list.cursor()).unwrap();
//! assert!(filter.candidates()[second].ends_with(".rs"));
//! ```

use std::ops::Range;

use rich::cells::{cell_len, split_graphemes};
use rich::{Segment, Style};

pub use crate::components::Theme;
use crate::event::{Event, Key, KeyCode, Mouse, MouseKind};
use crate::fuzzy::rank;
use crate::item::Action;

// ---------------------------------------------------------------------------
// Line helpers.

/// A segment of `text` in `style`.
pub fn text(text: impl Into<String>, style: &Style) -> Segment {
    Segment::new(text.into(), Some(style.clone()))
}

/// The first line of `text` as segments, unwrapped, its styles and style
/// metadata kept: an icon for a list row, a crumb or a status item. A micro
/// asset's placeholder (`rich_micro::placeholder`) stays tagged, so the
/// painter's graphics can draw the asset over its cells.
pub fn icon(text: &rich::Text) -> Vec<Segment> {
    let console = rich::Console::builder().width(4096).build();
    let options = console.options().update_width(4096);
    let mut text = text.clone();
    text.set_no_wrap(Some(true));
    console
        .render_lines(&text, &options, false)
        .into_iter()
        .next()
        .unwrap_or_default()
        .into_iter()
        .filter(|segment| !segment.text.is_empty())
        .collect()
}

/// A segment of unstyled `text`.
pub fn plain(text: impl Into<String>) -> Segment {
    Segment::new(text.into(), None)
}

/// The width of a line in cells.
pub fn width(line: &[Segment]) -> usize {
    line.iter().map(Segment::cell_length).sum()
}

/// Crop a line to `columns` cells; a shorter line is left as it is.
pub fn fit(line: Vec<Segment>, columns: usize) -> Vec<Segment> {
    if width(&line) > columns {
        Segment::adjust_line_length(&line, columns, None)
    } else {
        line
    }
}

/// Crop or pad a line to exactly `columns` cells.
pub fn pad(line: Vec<Segment>, columns: usize) -> Vec<Segment> {
    Segment::adjust_line_length(&line, columns, None)
}

/// `label` with the characters at `positions` (character indices, in
/// order) in `matched`, the rest in `base`: how a filter's matches show.
pub fn highlight(
    label: &str,
    positions: &[usize],
    base: Option<&Style>,
    matched: &Style,
) -> Vec<Segment> {
    let hit = match base {
        Some(base) => base.combine(matched),
        None => matched.clone(),
    };
    let mut segments: Vec<Segment> = Vec::new();
    let mut run = String::new();
    let mut run_hit = false;
    let mut next = positions.iter().peekable();
    for (index, c) in label.chars().enumerate() {
        let is_hit = next.peek() == Some(&&index);
        if is_hit {
            next.next();
        }
        if is_hit != run_hit && !run.is_empty() {
            let style = if run_hit {
                Some(hit.clone())
            } else {
                base.cloned()
            };
            segments.push(Segment::new(std::mem::take(&mut run), style));
        }
        run_hit = is_hit;
        run.push(c);
    }
    if !run.is_empty() {
        let style = if run_hit { Some(hit) } else { base.cloned() };
        segments.push(Segment::new(run, style));
    }
    segments
}

/// The cells `from..to` of a line. A wide character cut by either end
/// becomes spaces in its style, so the result is exactly the cells asked
/// for (fewer when the line is shorter). Control segments are dropped.
pub fn slice(line: &[Segment], from: usize, to: usize) -> Vec<Segment> {
    let mut out: Vec<Segment> = Vec::new();
    let mut at = 0;
    for segment in line.iter().filter(|segment| !segment.control) {
        if at >= to {
            break;
        }
        let length = segment.cell_length();
        if at >= from && at + length <= to {
            out.push(segment.clone());
            at += length;
            continue;
        }
        let (spans, _) = split_graphemes(&segment.text);
        let mut piece = String::new();
        for (start, end, cells) in spans {
            let (left, right) = (at, at + cells);
            if left >= from && right <= to {
                piece.push_str(&segment.text[start..end]);
            } else if right > from && left < to {
                let covered = right.min(to) - left.max(from);
                piece.push_str(&" ".repeat(covered));
            }
            at = right;
        }
        if !piece.is_empty() {
            out.push(Segment::new(piece, segment.style.clone()));
        }
    }
    out
}

/// `top` drawn over `base` from cell `x`: `base`'s cells before `x` (padded
/// out to `x`), then `top`, then what of `base` is right of it.
pub fn overlay(base: &[Segment], x: usize, top: &[Segment]) -> Vec<Segment> {
    let end = x + width(top);
    let mut line = pad(slice(base, 0, x), x);
    line.extend(top.iter().cloned());
    let base_width = width(base);
    if base_width > end {
        line.extend(slice(base, end, base_width));
    }
    line
}

/// `line` with `style` over each segment's own: a backdrop's dimming.
pub fn restyle(line: &[Segment], style: &Style) -> Vec<Segment> {
    line.iter()
        .map(|segment| {
            if segment.control {
                return segment.clone();
            }
            let styled = match &segment.style {
                Some(own) => own.combine(style),
                None => style.clone(),
            };
            Segment::new(segment.text.clone(), Some(styled))
        })
        .collect()
}

/// `lines` in a rounded box `inner` cells wide inside, each padded to that
/// width, with `title` in the top border: how a modal or a popover is
/// drawn, by [`Layers`](crate::compose::Layers) and the action menu alike.
pub fn frame(
    lines: Vec<Vec<Segment>>,
    inner: usize,
    title: Option<&str>,
    style: &Style,
) -> Vec<Vec<Segment>> {
    let mut top = vec![text("â•­", style)];
    match title {
        Some(title) if inner >= 4 => {
            let title = fit(vec![text(format!(" {title} "), style)], inner - 1);
            let used = width(&title) + 1;
            top.push(text("─", style));
            top.extend(title);
            top.push(text("─".repeat(inner.saturating_sub(used)), style));
        }
        _ => top.push(text("─".repeat(inner), style)),
    }
    top.push(text("â•®", style));
    let mut boxed = vec![top];
    for line in lines {
        let mut row = vec![text("│", style)];
        row.extend(pad(line, inner));
        row.push(text("│", style));
        boxed.push(row);
    }
    boxed.push(vec![
        text("â•°", style),
        text("─".repeat(inner), style),
        text("╯", style),
    ]);
    boxed
}

/// Draw `top`'s lines over `base` from cell `x` of row `y`, adding rows
/// to `base` where `top` reaches below it; with a `backdrop`, first put
/// that style over every line of `base` (a modal's dimming).
pub fn place(
    base: &mut Vec<Vec<Segment>>,
    x: usize,
    y: usize,
    top: &[Vec<Segment>],
    backdrop: Option<&Style>,
) {
    if let Some(style) = backdrop {
        for line in base.iter_mut() {
            *line = restyle(line, style);
        }
    }
    if base.len() < y + top.len() {
        base.resize_with(y + top.len(), Vec::new);
    }
    for (row, line) in top.iter().enumerate() {
        let under = &mut base[y + row];
        *under = overlay(under, x, line);
    }
}

/// Pasted text for a one-line field: line breaks become `newline`, a tab a
/// space, and other terminal controls (C0, DEL, C1) are dropped, so a paste
/// cannot carry an escape sequence into the answer or onto the screen.
pub fn pasted(text: &str, newline: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for c in text.chars() {
        match c {
            '\n' | '\r' => out.push_str(newline),
            '\t' => out.push(' '),
            '\0'..='\u{1f}' | '\u{7f}'..='\u{9f}' => {}
            _ => out.push(c),
        }
    }
    out
}

/// `text` with its terminal controls as the characters a view paints them
/// as, one for one, so cells line up (a table's columns) before painting.
pub fn shown(text: &str) -> String {
    text.chars()
        .map(|c| crate::paint::visible(c).unwrap_or(c))
        .collect()
}

/// The question line every built-in component starts with: `? prompt › `.
pub fn question(theme: &Theme, prompt: &str) -> Vec<Segment> {
    vec![
        text(theme.question.clone(), &theme.question_style),
        plain(" "),
        text(prompt, &theme.prompt),
        plain(" "),
        text("›", &theme.hint),
        plain(" "),
    ]
}

// ---------------------------------------------------------------------------
// ListState.

/// A cursor over a list of rows, the first row shown, and a selection.
///
/// Rows are positions in what is listed (after filtering, say); the
/// selection is kept by *index*, whatever the caller's indices are (an
/// item's index before filtering), so marks survive a change of filter.
/// Movement takes the page, the rows on screen, since that can change with
/// every render.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ListState {
    len: usize,
    cursor: usize,
    offset: usize,
    selected: Vec<bool>,
}

impl ListState {
    pub fn new() -> ListState {
        ListState::default()
    }

    /// How many rows there are.
    pub fn len(&self) -> usize {
        self.len
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Set the number of rows. The cursor is left where it is: call
    /// [`reset`](Self::reset) or [`set_cursor`](Self::set_cursor) to put it
    /// on a row that exists.
    pub fn set_len(&mut self, len: usize) {
        self.len = len;
    }

    /// The row the cursor is on.
    pub fn cursor(&self) -> usize {
        self.cursor
    }

    /// Put the cursor on `row`, without scrolling (see
    /// [`follow`](Self::follow)).
    pub fn set_cursor(&mut self, row: usize) {
        self.cursor = row;
    }

    /// The first row shown.
    pub fn offset(&self) -> usize {
        self.offset
    }

    pub fn set_offset(&mut self, offset: usize) {
        self.offset = offset;
    }

    /// Put the cursor on `row` and the first row shown back at the top,
    /// then scroll just enough to show the cursor: after the rows changed.
    pub fn reset(&mut self, row: usize, page: usize) {
        self.cursor = row;
        self.offset = 0;
        self.follow(page);
    }

    /// Scroll just enough that the cursor's row is among the `page` shown.
    pub fn follow(&mut self, page: usize) {
        if self.cursor < self.offset {
            self.offset = self.cursor;
        } else if self.cursor >= self.offset + page {
            self.offset = self.cursor + 1 - page;
        }
    }

    /// Move the cursor by `delta` rows (negative: up), stopping at the
    /// ends, and scroll to follow it. Nothing happens with no rows.
    pub fn step(&mut self, delta: isize, page: usize) {
        if self.len == 0 {
            return;
        }
        let last = self.len - 1;
        self.cursor = self.cursor.saturating_add_signed(delta).min(last);
        self.follow(page);
    }

    /// Move to `row` (clamped to the last) and scroll to show it.
    pub fn select_row(&mut self, row: usize, page: usize) {
        if self.len == 0 {
            return;
        }
        self.cursor = row.min(self.len - 1);
        self.follow(page);
    }

    /// The rows shown on a page of `page` rows.
    pub fn visible(&self, page: usize) -> Range<usize> {
        let end = (self.offset + page).min(self.len);
        self.offset.min(end)..end
    }

    /// Make room to select `count` indices, all unselected.
    pub fn clear_selection(&mut self, count: usize) {
        self.selected = vec![false; count];
    }

    pub fn is_selected(&self, index: usize) -> bool {
        self.selected.get(index).copied().unwrap_or(false)
    }

    /// Select or unselect `index`; indices past the room made are grown to.
    pub fn set_selected(&mut self, index: usize, on: bool) {
        if index >= self.selected.len() {
            self.selected.resize(index + 1, false);
        }
        self.selected[index] = on;
    }

    /// Flip whether `index` is selected.
    pub fn toggle(&mut self, index: usize) {
        let on = !self.is_selected(index);
        self.set_selected(index, on);
    }

    /// The selected indices, in order.
    pub fn selected(&self) -> Vec<usize> {
        (0..self.selected.len())
            .filter(|&index| self.selected[index])
            .collect()
    }

    /// How many indices are selected.
    pub fn selected_count(&self) -> usize {
        self.selected.iter().filter(|on| **on).count()
    }
}

// ---------------------------------------------------------------------------
// ScrollState.

/// An offset into `len` lines, a page at a time: the scroll position of a
/// pager or a preview.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ScrollState {
    len: usize,
    offset: usize,
}

impl ScrollState {
    pub fn new(len: usize) -> ScrollState {
        ScrollState { len, offset: 0 }
    }

    pub fn len(&self) -> usize {
        self.len
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Set the number of lines, keeping the offset where it still fits.
    pub fn set_len(&mut self, len: usize) {
        self.len = len;
        self.offset = self.offset.min(len.saturating_sub(1));
    }

    /// The first line shown.
    pub fn offset(&self) -> usize {
        self.offset
    }

    fn last_offset(&self, page: usize) -> usize {
        self.len.saturating_sub(page)
    }

    /// Scroll by `delta` lines (negative: up), within the lines. Returns
    /// whether the offset changed.
    pub fn scroll(&mut self, delta: isize, page: usize) -> bool {
        let target = self
            .offset
            .saturating_add_signed(delta)
            .min(self.last_offset(page));
        let changed = target != self.offset;
        self.offset = target;
        changed
    }

    /// Scroll to put `line` first, or as near as the last page allows.
    pub fn scroll_to(&mut self, line: usize, page: usize) -> bool {
        let target = line.min(self.last_offset(page));
        let changed = target != self.offset;
        self.offset = target;
        changed
    }

    /// Scroll just enough that `line` is shown.
    pub fn show(&mut self, line: usize, page: usize) -> bool {
        if line < self.offset {
            self.scroll_to(line, page)
        } else if line >= self.offset + page {
            self.scroll_to(line + 1 - page, page)
        } else {
            false
        }
    }

    /// The lines shown on a page of `page` lines.
    pub fn visible(&self, page: usize) -> Range<usize> {
        let end = (self.offset + page).min(self.len);
        self.offset.min(end)..end
    }

    /// The keys [`handle`](Self::handle) scrolls with, in `context`, for a
    /// component's [`keymap`](crate::Component::keymap).
    pub fn keymap(context: &str) -> crate::keymap::Keymap {
        use crate::keymap::keys;
        crate::keymap::Keymap::new(context)
            .bind("scroll-up", keys("up k"), "scroll up")
            .bind("scroll-down", keys("down j"), "scroll down")
            .bind("page-up", keys("pageup b"), "page up")
            .bind("page-down", keys("pagedown space"), "page down")
            .bind("top", keys("home g"), "to the top")
            .bind("bottom", keys("end G"), "to the bottom")
    }

    /// Do one of [`keymap`](Self::keymap)'s actions (`scroll-up`,
    /// `page-down`, `top`, ...): how a component that looks its keys up in
    /// a keymap, and so honours rebinding, scrolls. Returns whether
    /// `action` is a scroll action (moved or not).
    pub fn act(&mut self, action: &str, page: usize) -> bool {
        let page_step = page.max(1) as isize;
        match action {
            "scroll-up" => self.scroll(-1, page),
            "scroll-down" => self.scroll(1, page),
            "page-up" => self.scroll(-page_step, page),
            "page-down" => self.scroll(page_step, page),
            "top" => self.scroll_to(0, page),
            "bottom" => self.scroll_to(usize::MAX, page),
            _ => return false,
        };
        true
    }

    /// Move with the usual keys and the mouse wheel: arrows and `j`/`k` by a
    /// line, PageUp/PageDown, `b` and Space by a page, Home/End and `g`/`G`
    /// to the ends, and the wheel by three lines. Returns whether the event
    /// was a scroll key (moved or not). The keys are fixed; see
    /// [`act`](Self::act) for keys from a keymap.
    pub fn handle(&mut self, event: &Event, page: usize) -> bool {
        let page_step = page.max(1) as isize;
        match event {
            Event::Key(key) if !key.modifiers.ctrl && !key.modifiers.alt => {
                match key.code {
                    KeyCode::Up | KeyCode::Char('k') => self.scroll(-1, page),
                    KeyCode::Down | KeyCode::Char('j') => self.scroll(1, page),
                    KeyCode::PageUp | KeyCode::Char('b') => self.scroll(-page_step, page),
                    KeyCode::PageDown | KeyCode::Char(' ') => self.scroll(page_step, page),
                    KeyCode::Home | KeyCode::Char('g') => self.scroll_to(0, page),
                    KeyCode::End | KeyCode::Char('G') => self.scroll_to(usize::MAX, page),
                    _ => return false,
                };
                true
            }
            Event::Mouse(mouse) => match mouse.kind {
                MouseKind::ScrollUp => {
                    self.scroll(-3, page);
                    true
                }
                MouseKind::ScrollDown => {
                    self.scroll(3, page);
                    true
                }
                _ => false,
            },
            _ => false,
        }
    }
}

// ---------------------------------------------------------------------------
// FilterState.

/// A query and the candidates that match it, best first.
///
/// Candidates are strings to search; a match is a candidate's index and the
/// character positions that matched, for [`highlight`]. With an empty query
/// every candidate matches, in order, except those hidden
/// ([`set_hidden`](Self::set_hidden)): a collapsed tree node's children
/// stay out of the list until something is typed.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct FilterState {
    query: String,
    candidates: Vec<String>,
    hidden: Vec<bool>,
    limits: Vec<usize>,
    matches: Vec<(usize, Vec<usize>)>,
    /// Each candidate's parent, for a tree: matches then keep their
    /// ancestors, in tree order ([`set_tree`](Self::set_tree)).
    parents: Option<Vec<Option<usize>>>,
    /// Which matches are only there as a match's ancestor.
    context: Vec<bool>,
    /// The position of the best match.
    best: Option<usize>,
}

impl FilterState {
    /// Filter `candidates`, with nothing typed yet.
    pub fn new<I, S>(candidates: I) -> FilterState
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let mut filter = FilterState::default();
        filter.set_candidates(candidates);
        filter
    }

    /// Replace the candidates and filter them again.
    pub fn set_candidates<I, S>(&mut self, candidates: I)
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.candidates = candidates.into_iter().map(Into::into).collect();
        self.refilter();
    }

    pub fn candidates(&self) -> &[String] {
        &self.candidates
    }

    /// Leave these candidates out while the query is empty (`true` hides).
    /// Takes effect on the next [`refilter`](Self::refilter).
    pub fn set_hidden(&mut self, hidden: Vec<bool>) {
        self.hidden = hidden;
    }

    /// Filter as a tree (#428): `parents[i]` is candidate `i`'s parent,
    /// and candidates come in tree order, each parent before its children.
    /// While a query is typed, what matches keeps its ancestors, so a match
    /// shows where it is: the list is the matches and their ancestors in
    /// tree order, the ancestors marked as [context](Self::is_context) with
    /// nothing highlighted, and [`best`](Self::best) is where the best match
    /// is. `None` goes back to a flat ranking. Takes effect on the next
    /// [`refilter`](Self::refilter).
    pub fn set_tree(&mut self, parents: Option<Vec<Option<usize>>>) {
        self.parents = parents;
    }

    /// Highlight at most the first `limits[i]` characters of candidate `i`:
    /// for a candidate searched with more than its label (a description),
    /// so only the label highlights. Takes effect on the next
    /// [`refilter`](Self::refilter).
    pub fn set_highlight_limits(&mut self, limits: Vec<usize>) {
        self.limits = limits;
    }

    /// What is typed.
    pub fn query(&self) -> &str {
        &self.query
    }

    /// The query, to edit in place; call [`refilter`](Self::refilter)
    /// after.
    pub fn query_mut(&mut self) -> &mut String {
        &mut self.query
    }

    pub fn is_filtering(&self) -> bool {
        !self.query.is_empty()
    }

    /// Replace the query and filter again.
    pub fn set_query(&mut self, query: impl Into<String>) {
        self.query = query.into();
        self.refilter();
    }

    /// Type `c`.
    pub fn push(&mut self, c: char) {
        self.query.push(c);
        self.refilter();
    }

    /// Type `text` (a paste: see [`pasted`]).
    pub fn push_str(&mut self, text: &str) {
        self.query.push_str(text);
        self.refilter();
    }

    /// Delete the last character; `false` when there was none.
    pub fn pop(&mut self) -> bool {
        let popped = self.query.pop().is_some();
        if popped {
            self.refilter();
        }
        popped
    }

    /// Clear the query.
    pub fn clear(&mut self) {
        self.query.clear();
        self.refilter();
    }

    /// Match the candidates against the query again.
    pub fn refilter(&mut self) {
        let filtering = !self.query.is_empty();
        let hidden = &self.hidden;
        let limits = &self.limits;
        let candidates = &self.candidates;
        let ranked: Vec<(usize, Vec<usize>)> =
            rank(&self.query, candidates.iter().map(String::as_str))
                .into_iter()
                .filter(|(index, _)| filtering || !hidden.get(*index).copied().unwrap_or(false))
                .map(|(index, found)| {
                    let mut positions = found.positions;
                    if let Some(limit) = limits.get(index) {
                        positions.retain(|position| position < limit);
                    }
                    (index, positions)
                })
                .collect();
        match &self.parents {
            Some(parents) if filtering => {
                let best = ranked.first().map(|(index, _)| *index);
                let (matches, context) = keep_ancestors(ranked, parents);
                self.best = best.and_then(|best| matches.iter().position(|(i, _)| *i == best));
                self.matches = matches;
                self.context = context;
            }
            _ => {
                self.best = (!ranked.is_empty()).then_some(0);
                self.context = vec![false; ranked.len()];
                self.matches = ranked;
            }
        }
    }

    /// Whether the match at `position` is only there as an ancestor of a
    /// match ([`set_tree`](Self::set_tree)): drawn dim, not picked first.
    pub fn is_context(&self, position: usize) -> bool {
        self.context.get(position).copied().unwrap_or(false)
    }

    /// The position of the best match: the first, unless a tree keeps
    /// ancestors above it.
    pub fn best(&self) -> Option<usize> {
        self.best
    }

    /// The matches, best first: candidate indices and the positions to
    /// highlight.
    pub fn matches(&self) -> &[(usize, Vec<usize>)] {
        &self.matches
    }

    /// How many candidates match.
    pub fn len(&self) -> usize {
        self.matches.len()
    }

    pub fn is_empty(&self) -> bool {
        self.matches.is_empty()
    }

    /// The candidate at `position` in the matches.
    pub fn index(&self, position: usize) -> Option<usize> {
        self.matches.get(position).map(|(index, _)| *index)
    }

    /// Where candidate `index` is in the matches, if it matches.
    pub fn position_of(&self, index: usize) -> Option<usize> {
        self.matches.iter().position(|(i, _)| *i == index)
    }
}

/// `index`'s ancestors in a tree given by `parents`, nearest first.
///
/// ```
/// use rich_interact::kit::ancestors;
///
/// // 0 ─ 1 ─ 2, and 3 under 0.
/// let parents = [None, Some(0), Some(1), Some(0)];
/// assert_eq!(ancestors(&parents, 2).collect::<Vec<_>>(), [1, 0]);
/// ```
pub fn ancestors(parents: &[Option<usize>], index: usize) -> impl Iterator<Item = usize> + '_ {
    let mut at = parents.get(index).copied().flatten();
    // A malformed parent list (a cycle) stops after every node once.
    let mut left = parents.len();
    std::iter::from_fn(move || {
        let current = at.filter(|_| left > 0)?;
        left -= 1;
        at = parents.get(current).copied().flatten();
        Some(current)
    })
}

/// Tree filtering that keeps ancestors (#428): `matches` (candidate
/// indices with the positions to highlight, in any order) plus every
/// ancestor of each, in tree order (index order, parents before
/// children). Returns the list and, for each entry, whether it is only
/// there as an ancestor (context), with nothing highlighted. A parent
/// index past the list is not a node: the walk up stops there.
///
/// ```
/// use rich_interact::kit::keep_ancestors;
///
/// //  0 config
/// //  1 ├── server
/// //  2 │   └── port
/// //  3 └── debug
/// let parents = [None, Some(0), Some(1), Some(0)];
/// let (list, context) = keep_ancestors(vec![(2, vec![0, 1])], &parents);
/// assert_eq!(list, [(0, vec![]), (1, vec![]), (2, vec![0, 1])]);
/// assert_eq!(context, [true, true, false]);
/// ```
pub fn keep_ancestors(
    matches: Vec<(usize, Vec<usize>)>,
    parents: &[Option<usize>],
) -> (Vec<(usize, Vec<usize>)>, Vec<bool>) {
    let count = parents
        .len()
        .max(matches.iter().map(|(i, _)| i + 1).max().unwrap_or(0));
    let mut found: Vec<Option<Vec<usize>>> = vec![None; count];
    let mut kept = vec![false; count];
    for (index, positions) in matches {
        // A parent past the list (a malformed parent list) ends the walk.
        for ancestor in ancestors(parents, index) {
            if kept.get(ancestor).is_none_or(|&kept| kept) {
                break;
            }
            kept[ancestor] = true;
        }
        kept[index] = true;
        found[index] = Some(positions);
    }
    let mut list = Vec::new();
    let mut context = Vec::new();
    for (index, positions) in found.into_iter().enumerate() {
        if !kept[index] {
            continue;
        }
        context.push(positions.is_none());
        list.push((index, positions.unwrap_or_default()));
    }
    (list, context)
}

// ---------------------------------------------------------------------------
// TextBuffer.

/// One line of text and a caret, for a text field.
///
/// The caret is a character index and moves by grapheme cluster, so an
/// accent is never split from its letter. The operations are the ones a
/// shell line has (and [`Input`](crate::Input) binds): insert, Backspace,
/// Delete, Ctrl+U (delete to the start), Ctrl+W (delete a word), and moving
/// by grapheme or to the ends. [`checkpoint`](Self::checkpoint) keeps an
/// undo point and [`undo`](Self::undo) goes back to the last one.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TextBuffer {
    value: String,
    caret: usize,
    undo: Vec<(String, usize)>,
}

impl TextBuffer {
    pub fn new() -> TextBuffer {
        TextBuffer::default()
    }

    /// A buffer holding `text`, the caret at its end.
    pub fn with_text(text: impl Into<String>) -> TextBuffer {
        let mut buffer = TextBuffer::new();
        buffer.set_text(text);
        buffer
    }

    pub fn text(&self) -> &str {
        &self.value
    }

    pub fn is_empty(&self) -> bool {
        self.value.is_empty()
    }

    /// The length in characters.
    pub fn len(&self) -> usize {
        self.value.chars().count()
    }

    /// Replace the text, the caret at its end.
    pub fn set_text(&mut self, text: impl Into<String>) {
        self.value = text.into();
        self.caret = self.len();
    }

    /// The caret, in characters.
    pub fn caret(&self) -> usize {
        self.caret
    }

    /// Put the caret at character `caret` (clamped to the end), or at the
    /// start of the grapheme `caret` falls inside.
    pub fn set_caret(&mut self, caret: usize) {
        self.caret = caret.min(self.len());
        self.snap(false);
    }

    /// Move the caret onto a grapheme boundary: on to the end of the
    /// grapheme it is inside (`forward`) or back to its start. Typing a
    /// base character in front of a combining mark or U+FE0F, or deleting
    /// what kept two clusters apart, can leave it inside one.
    fn snap(&mut self, forward: bool) {
        let stops = self.stops();
        self.caret = if forward {
            stops.iter().copied().find(|&stop| stop >= self.caret)
        } else {
            stops.iter().rev().copied().find(|&stop| stop <= self.caret)
        }
        .unwrap_or(self.caret);
    }

    /// The byte offset of character `caret`.
    pub fn byte(&self, caret: usize) -> usize {
        self.value
            .char_indices()
            .nth(caret)
            .map_or(self.value.len(), |(index, _)| index)
    }

    /// The text before the caret.
    pub fn before_caret(&self) -> &str {
        &self.value[..self.byte(self.caret)]
    }

    /// The character indices where graphemes start, and the end: where
    /// the caret may stop.
    pub fn stops(&self) -> Vec<usize> {
        let (spans, _) = split_graphemes(&self.value);
        let mut stops = Vec::with_capacity(spans.len() + 1);
        let (mut chars, mut byte) = (0, 0);
        for (start, _, _) in spans {
            chars += self.value[byte..start].chars().count();
            byte = start;
            stops.push(chars);
        }
        stops.push(self.value.chars().count());
        stops
    }

    /// The caret one grapheme back.
    pub fn previous_stop(&self) -> usize {
        self.stops()
            .iter()
            .rev()
            .copied()
            .find(|&stop| stop < self.caret)
            .unwrap_or(0)
    }

    /// The caret one grapheme on.
    pub fn next_stop(&self) -> usize {
        self.stops()
            .iter()
            .copied()
            .find(|&stop| stop > self.caret)
            .unwrap_or(self.caret)
    }

    /// Insert `text` at the caret, and move past it.
    pub fn insert(&mut self, text: &str) {
        let at = self.byte(self.caret);
        self.value.insert_str(at, text);
        self.caret += text.chars().count();
        self.snap(true);
    }

    /// Move the caret one grapheme left.
    pub fn left(&mut self) {
        self.caret = self.previous_stop();
    }

    /// Move the caret one grapheme right.
    pub fn right(&mut self) {
        self.caret = self.next_stop().min(self.len());
    }

    pub fn home(&mut self) {
        self.caret = 0;
    }

    pub fn end(&mut self) {
        self.caret = self.len();
    }

    /// Delete the grapheme before the caret; `false` at the start.
    pub fn backspace(&mut self) -> bool {
        if self.caret == 0 {
            return false;
        }
        let previous = self.previous_stop();
        let (from, to) = (self.byte(previous), self.byte(self.caret));
        self.value.replace_range(from..to, "");
        self.caret = previous;
        self.snap(false);
        true
    }

    /// Delete the grapheme after the caret; `false` at the end.
    pub fn delete(&mut self) -> bool {
        if self.caret >= self.len() {
            return false;
        }
        let (from, to) = (self.byte(self.caret), self.byte(self.next_stop()));
        self.value.replace_range(from..to, "");
        self.snap(false);
        true
    }

    /// Delete everything before the caret (Ctrl+U).
    pub fn delete_to_start(&mut self) {
        let at = self.byte(self.caret);
        self.value.replace_range(..at, "");
        self.caret = 0;
    }

    /// Delete the word before the caret, and the spaces after it (Ctrl+W).
    pub fn delete_word(&mut self) {
        let chars: Vec<char> = self.value.chars().collect();
        let mut start = self.caret;
        while start > 0 && chars[start - 1] == ' ' {
            start -= 1;
        }
        while start > 0 && chars[start - 1] != ' ' {
            start -= 1;
        }
        let (from, to) = (self.byte(start), self.byte(self.caret));
        self.value.replace_range(from..to, "");
        self.caret = start;
        self.snap(false);
    }

    /// Keep the text and caret as an undo point.
    pub fn checkpoint(&mut self) {
        let point = (self.value.clone(), self.caret);
        if self.undo.last() != Some(&point) {
            self.undo.push(point);
        }
    }

    /// Go back to the last undo point; `false` when there is none.
    pub fn undo(&mut self) -> bool {
        match self.undo.pop() {
            Some((value, caret)) => {
                self.value = value;
                self.caret = caret;
                true
            }
            None => false,
        }
    }

    /// `text` as shown: every character as `mask` when there is one.
    pub fn masked(text: &str, mask: Option<char>) -> String {
        match mask {
            Some(mask) => std::iter::repeat_n(mask, text.chars().count()).collect(),
            None => text.to_string(),
        }
    }

    /// The text as shown (masked or not), cut to `available` cells so the
    /// caret stays in view: a line longer than the space scrolls. Returns
    /// what to show and the caret's column in it.
    pub fn window(&self, available: usize, mask: Option<char>) -> (String, usize) {
        let shown = Self::masked(&self.value, mask);
        // Masking keeps one character for each, so the caret is the same
        // character index in what is shown.
        let caret = shown
            .char_indices()
            .nth(self.caret)
            .map_or(shown.len(), |(index, _)| index);
        // The cells before the caret and the cells dropped are both counted
        // grapheme by grapheme: `cell_len` of the whole prefix can differ
        // from that (a leading U+200D joins what follows it).
        let (spans, _) = split_graphemes(&shown);
        let before: usize = spans
            .iter()
            .filter(|(start, _, _)| *start < caret)
            .map(|(_, _, cells)| cells)
            .sum();
        // One cell for the caret after the last character.
        let skip = (before + 1).saturating_sub(available.max(1));
        if skip == 0 {
            return (shown, before);
        }
        let mut dropped = 0;
        let mut from = shown.len();
        for (start, _, cells) in spans {
            if dropped >= skip || start >= caret {
                from = start;
                break;
            }
            dropped += cells;
        }
        (shown[from..].to_string(), before.saturating_sub(dropped))
    }
}

// ---------------------------------------------------------------------------
// Divider.

/// A border between two panes along one axis, where the mouse can grab and
/// drag it and the keyboard can nudge it.
///
/// Positions are cells along the axis: the first pane's size, then
/// `thickness` cells of border, then the second pane. Both panes keep at
/// least their minimum while the total has room for that.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[must_use = "builder methods return a new value; it does nothing until it is rendered or used"]
pub struct Divider {
    position: Option<usize>,
    dragging: bool,
    /// The fewest cells the first pane takes.
    pub min_before: usize,
    /// The fewest cells the second pane takes.
    pub min_after: usize,
    /// The cells the border takes.
    pub thickness: usize,
}

impl Default for Divider {
    fn default() -> Self {
        Divider::new(1)
    }
}

impl Divider {
    /// A border `thickness` cells thick, with no minimum pane size.
    pub fn new(thickness: usize) -> Divider {
        Divider {
            position: None,
            dragging: false,
            min_before: 0,
            min_after: 0,
            thickness,
        }
    }

    /// Both panes at least `cells`.
    pub fn min(mut self, cells: usize) -> Divider {
        self.min_before = cells;
        self.min_after = cells;
        self
    }

    /// Put the border at `position` (the first pane's size), until dragged.
    pub fn at(mut self, position: usize) -> Divider {
        self.position = Some(position);
        self
    }

    /// Where the border was put or dragged to, before clamping.
    pub fn position(&self) -> Option<usize> {
        self.position
    }

    pub fn set_position(&mut self, position: Option<usize>) {
        self.position = position;
    }

    /// Whether a drag is under way.
    pub fn dragging(&self) -> bool {
        self.dragging
    }

    /// The first pane's size for a border at `position` in `total` cells:
    /// both panes at least their minimum when there is room for that.
    pub fn clamp(&self, position: usize, total: usize) -> usize {
        let most = total
            .saturating_sub(self.min_after + self.thickness)
            .max(self.min_before.min(total));
        position.clamp(self.min_before.min(most), most)
    }

    /// The first pane's size in `total` cells: where the border was put or
    /// dragged, clamped, or `default` when it has not been.
    pub fn resolve(&self, total: usize, default: usize) -> usize {
        match self.position {
            Some(position) => self.clamp(position, total),
            None => default,
        }
    }

    /// Whether `at` (a cell along the axis) is on the border when the first
    /// pane is `first` cells.
    pub fn hit(&self, at: usize, first: usize) -> bool {
        (first..first + self.thickness.max(1)).contains(&at)
    }

    /// A press at `at`: grab the border if it is there. Returns whether it
    /// was grabbed.
    pub fn press(&mut self, at: usize, first: usize) -> bool {
        self.dragging = self.hit(at, first);
        self.dragging
    }

    /// A drag to `at` in `total` cells: move the border there if grabbed.
    /// Returns whether it moved.
    pub fn drag(&mut self, at: usize, total: usize) -> bool {
        if !self.dragging {
            return false;
        }
        self.position = Some(self.clamp(at, total));
        true
    }

    /// Let go of the border.
    pub fn release(&mut self) {
        self.dragging = false;
    }

    /// Press, drag and release from one mouse event along the axis, `at`
    /// being the event's cell along it. Returns whether the divider used
    /// the event.
    pub fn handle_mouse(&mut self, mouse: &Mouse, at: usize, first: usize, total: usize) -> bool {
        match mouse.kind {
            MouseKind::Down(crate::event::Button::Left) => self.press(at, first),
            MouseKind::Drag(crate::event::Button::Left) => self.drag(at, total),
            MouseKind::Up(_) if self.dragging => {
                self.release();
                true
            }
            _ => false,
        }
    }

    /// Move the border by `delta` cells from `first`, within `total`.
    pub fn nudge(&mut self, delta: isize, first: usize, total: usize) {
        self.position = Some(self.clamp(first.saturating_add_signed(delta), total));
    }
}

// ---------------------------------------------------------------------------
// ActionMenu.

/// The hints under an [`ActionMenu`]'s rows.
const MENU_HINTS: &str = "  ↑↓ move · enter run · esc close";

/// What an [`ActionMenu`] did with an event.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MenuReply {
    /// Nothing to do (moved, or not its event).
    Stay,
    /// Closed without running anything.
    Close,
    /// Run this action.
    Run(Action),
}

/// A menu of [`Action`]s for one target, as Ctrl+K opens in
/// [`Select`](crate::Select): the arrows move, Enter runs, an action's own
/// key runs it, Escape or the key that opened it closes it, and a click
/// runs the action under it or closes the menu.
#[derive(Clone, Debug)]
pub struct ActionMenu {
    pub actions: Vec<Action>,
    pub focus: usize,
    /// The key that opened it, which closes it again.
    pub key: Key,
}

impl ActionMenu {
    pub fn new(actions: Vec<Action>, key: Key) -> ActionMenu {
        ActionMenu {
            actions,
            focus: 0,
            key,
        }
    }

    /// Handle `event`, the menu's first row being `first` in the view the
    /// mouse's rows count from.
    pub fn handle(&mut self, event: &Event, first: usize) -> MenuReply {
        let count = self.actions.len();
        if let Some(mouse) = event.mouse() {
            if mouse.is_click() {
                let row = mouse.row as usize;
                if row >= first && row < first + count {
                    return MenuReply::Run(self.actions[row - first].clone());
                }
                return MenuReply::Close;
            }
            return MenuReply::Stay;
        }
        let Some(key) = event.key() else {
            return MenuReply::Stay;
        };
        match key.code {
            KeyCode::Escape => return MenuReply::Close,
            _ if key.matches(&self.key) => return MenuReply::Close,
            KeyCode::Up => self.focus = (self.focus + count - 1) % count.max(1),
            KeyCode::Down | KeyCode::Tab => self.focus = (self.focus + 1) % count.max(1),
            KeyCode::Enter => return MenuReply::Run(self.actions[self.focus].clone()),
            _ => {
                if let Some(at) = key.pick(self.actions.iter().map(|a| a.key.as_slice())) {
                    return MenuReply::Run(self.actions[at].clone());
                }
            }
        }
        MenuReply::Stay
    }

    /// The width and rows [`render`](Self::render) needs: the widest row,
    /// and a row per action and one for the hints.
    pub fn size(&self) -> (usize, usize) {
        let label_width = self
            .actions
            .iter()
            .map(|action| cell_len(&action.label))
            .max()
            .unwrap_or(0);
        let key_width = self
            .actions
            .iter()
            .filter_map(|action| action.key.map(|key| cell_len(&key.to_string()) + 2))
            .max()
            .unwrap_or(0);
        let hints = cell_len(MENU_HINTS);
        (
            (4 + label_width + key_width).max(hints) + 2,
            self.actions.len() + 1,
        )
    }

    /// The menu's rows at `width`, and a line of hints under them.
    pub fn render(&self, theme: &Theme, width: usize) -> Vec<Vec<Segment>> {
        let label_width = self
            .actions
            .iter()
            .map(|action| cell_len(&action.label))
            .max()
            .unwrap_or(0);
        let mut lines = Vec::new();
        for (index, action) in self.actions.iter().enumerate() {
            let focused = index == self.focus;
            let mut line = if focused {
                vec![text(format!("  {} ", theme.pointer), &theme.pointer_style)]
            } else {
                vec![plain("    ")]
            };
            let padding = label_width - cell_len(&action.label);
            line.push(if focused {
                text(action.label.clone(), &theme.focused)
            } else {
                plain(action.label.clone())
            });
            if let Some(key) = action.key {
                line.push(text(format!("{}  {key}", " ".repeat(padding)), &theme.hint));
            }
            lines.push(fit(line, width));
        }
        lines.push(fit(vec![text(MENU_HINTS.to_string(), &theme.hint)], width));
        lines
    }
}

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

    #[test]
    fn highlights_matched_characters() {
        let matched = Style::parse("bold").unwrap();
        let segments = highlight("main.rs", &[0, 1, 5], None, &matched);
        let texts: Vec<(&str, bool)> = segments
            .iter()
            .map(|s| (s.text.as_str(), s.style.is_some()))
            .collect();
        assert_eq!(
            texts,
            [("ma", true), ("in.", false), ("r", true), ("s", false)]
        );
    }

    #[test]
    fn pastes_drop_terminal_controls() {
        assert_eq!(
            pasted("X\x1bcY\u{9b}2JZ\x07\x08W\tV\r\nU", " "),
            "XcY2JZW V  U"
        );
        assert_eq!(pasted("a\nb", ""), "ab");
    }

    #[test]
    fn fits_and_pads_lines() {
        let line = vec![plain("hello "), plain("world")];
        assert_eq!(width(&fit(line.clone(), 7)), 7);
        assert_eq!(width(&fit(line.clone(), 20)), 11);
        assert_eq!(width(&pad(line, 20)), 20);
    }

    #[test]
    fn slices_and_overlays_by_cell() {
        let base = vec![plain("ab"), plain("日本"), plain("cd")];
        let cut: String = slice(&base, 3, 7).iter().map(|s| s.text.as_str()).collect();
        assert_eq!(cut, " 本c", "half a wide character is a space");
        let over = overlay(&base, 1, &[plain("XY")]);
        let text: String = over.iter().map(|s| s.text.as_str()).collect();
        assert_eq!(text, "aXY 本cd");
        let past = overlay(&[plain("ab")], 4, &[plain("Z")]);
        assert_eq!(width(&past), 5);
    }

    #[test]
    fn a_list_follows_its_cursor() {
        let mut list = ListState::new();
        list.set_len(20);
        list.step(7, 5);
        assert_eq!((list.cursor(), list.offset()), (7, 3));
        assert_eq!(list.visible(5), 3..8);
        list.step(-100, 5);
        assert_eq!((list.cursor(), list.offset()), (0, 0));
        list.step(isize::MAX / 2, 5);
        assert_eq!(list.cursor(), 19);
        list.toggle(4);
        list.toggle(2);
        assert_eq!(list.selected(), [2, 4]);
        list.toggle(4);
        assert_eq!(list.selected_count(), 1);
    }

    #[test]
    fn a_filter_ranks_and_hides() {
        let mut filter = FilterState::new(["alpha", "beta", "gamma"]);
        filter.set_hidden(vec![false, true, false]);
        filter.refilter();
        assert_eq!(filter.len(), 2, "hidden while nothing is typed");
        filter.set_query("ta");
        assert_eq!(filter.index(0), Some(1), "hidden ones still match a query");
        assert!(filter.pop());
        filter.clear();
        assert!(!filter.is_filtering());
        filter.set_highlight_limits(vec![2, 2, 2]);
        filter.set_query("ma");
        assert!(filter.matches()[0].1.iter().all(|p| *p < 2));
    }

    #[test]
    fn a_text_buffer_edits_by_grapheme() {
        let mut buffer = TextBuffer::with_text("cafe\u{301} au lait");
        buffer.checkpoint();
        buffer.delete_word();
        assert_eq!(buffer.text(), "cafe\u{301} au ");
        buffer.home();
        buffer.right();
        buffer.right();
        buffer.right();
        buffer.right();
        assert_eq!(buffer.caret(), 5, "the accent moves with its letter");
        assert!(buffer.backspace());
        assert_eq!(buffer.text(), "caf au ");
        buffer.delete_to_start();
        assert_eq!(buffer.text(), " au ");
        assert!(buffer.undo());
        assert_eq!(buffer.text(), "cafe\u{301} au lait");
        let (shown, column) = TextBuffer::with_text("abcdefgh").window(4, Some('*'));
        assert_eq!((shown.as_str(), column), ("***", 3));
    }

    #[test]
    fn a_divider_drags_within_its_minimums() {
        let mut divider = Divider::new(3).min(12);
        assert_eq!(divider.clamp(5, 80), 12);
        assert_eq!(divider.clamp(79, 80), 80 - 12 - 3);
        assert_eq!(divider.resolve(80, 36), 36);
        assert!(!divider.press(10, 36));
        assert!(divider.press(37, 36));
        assert!(divider.drag(50, 80));
        divider.release();
        assert!(!divider.drag(60, 80));
        assert_eq!(divider.resolve(80, 36), 50);
        divider.nudge(-100, 50, 80);
        assert_eq!(divider.resolve(80, 36), 12);
    }
}