cmdlore 0.4.0

A command library that lives in your shell
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
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
//! Picker state and key handling, kept free of any terminal so it can be tested
//! directly.

use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::path::PathBuf;

use anyhow::Result;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use crate::model::{CommandBody, Entry, Layer, ParamSpec, ShellFamily};
use crate::params;
use crate::search::{self, Candidate};
use crate::store::definitions::{self, NewEntry};
use crate::store::stats::{Score, Stats};
use crate::tui::form::{Field, Form};

/// Shown when saving is asked to go on without a command.
const NO_COMMAND: &str = "Type a command, or press up for the ones you ran";

/// Commands from the shell's history that saving can walk back through.
///
/// Deep enough to reach past a run of throwaway commands, as a shell's own up
/// arrow would.
const HISTORY_LIMIT: usize = 50;

/// What the picker hands back to the shell.
#[derive(Debug, PartialEq, Eq)]
pub enum Outcome {
    /// Command to place in the prompt. Running it stays the user's decision.
    Insert {
        command: String,
        /// Where to leave the cursor, in characters from the start of the
        /// command. `None` puts it at the end, which is where a finished
        /// command wants it.
        cursor: Option<usize>,
    },
    Cancelled,
}

impl Outcome {
    #[cfg(test)]
    fn insert(command: &str) -> Self {
        Self::Insert {
            command: command.to_string(),
            cursor: None,
        }
    }
}

pub enum Mode {
    Browse,
    Params {
        entry_id: String,
        template: String,
        form: Form,
    },
    Save(Save),
    /// Rewriting an entry in place. Everything the form does not show is
    /// carried through untouched, so editing a description cannot lose a
    /// per shell variant or a placeholder's documentation.
    Edit {
        id: String,
        cmd: CommandBody,
        params: BTreeMap<String, ParamSpec>,
        danger: bool,
        /// A builtin is rewritten as a user entry that shadows it rather than
        /// changed where it lives.
        shadowing: bool,
        form: Form,
    },
}

/// Saving a command, asked the way a shell would ask it: the command on one
/// line, then what it is for on the next. There is no form to fill in, and
/// tags are either written into the answer as `#tag` or taken from the
/// command's own words.
pub struct Save {
    pub step: SaveStep,
    pub command: String,
    pub purpose: String,
    /// The history entry the command line is showing while up and down walk
    /// through it. `None` once the line is past the newest, as in a shell.
    pub recalled: Option<usize>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SaveStep {
    Command,
    Purpose,
}

impl Save {
    fn line(&mut self) -> &mut String {
        match self.step {
            SaveStep::Command => &mut self.command,
            SaveStep::Purpose => &mut self.purpose,
        }
    }
}

pub struct App {
    entries: Vec<Entry>,
    family: ShellFamily,
    /// Entries that have a command for this shell, in load order.
    pickable: Vec<usize>,
    /// Ranked entry indices, best first.
    order: Vec<usize>,
    selected: usize,
    query: String,
    mode: Mode,
    status: Option<String>,
    stats: Stats,
    scores: HashMap<String, Score>,
    library: PathBuf,
    /// What the shell last ran, newest first, offered when saving.
    history: Vec<String>,
    /// Entry the next ctrl+x will actually remove.
    armed_to_remove: Option<String>,
    /// Set once the library file has been written, so the caller knows there
    /// is something to sync.
    changed: bool,
    now: i64,
}

impl App {
    pub fn new(
        entries: Vec<Entry>,
        family: ShellFamily,
        stats: Stats,
        library: PathBuf,
        history: Vec<String>,
        now: i64,
    ) -> Result<Self> {
        let scores = stats.scores(now)?;
        let mut app = Self {
            entries,
            family,
            pickable: Vec::new(),
            order: Vec::new(),
            selected: 0,
            query: String::new(),
            mode: Mode::Browse,
            status: None,
            stats,
            scores,
            library,
            history: prepare_history(history),
            armed_to_remove: None,
            changed: false,
            now,
        };
        app.reindex();
        Ok(app)
    }

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

    pub fn mode(&self) -> &Mode {
        &self.mode
    }

    pub fn status(&self) -> Option<&str> {
        self.status.as_deref()
    }

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

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

    /// The entries currently on offer, best first.
    pub fn rows(&self) -> impl Iterator<Item = Row<'_>> {
        self.order.iter().map(move |&index| {
            let entry = &self.entries[index];
            Row {
                entry,
                cmd: entry.cmd_for(self.family).expect("filtered on this"),
                pinned: self.scores.get(&entry.id).is_some_and(|s| s.pinned),
            }
        })
    }

    pub fn selected_row(&self) -> Option<Row<'_>> {
        self.rows().nth(self.selected)
    }

    /// Highlights the query inside a haystack for the rows on screen.
    pub fn highlight(&self, haystack: &str) -> Vec<u32> {
        search::highlight(haystack, &self.query)
    }

    pub fn on_key(&mut self, key: KeyEvent) -> Result<Option<Outcome>> {
        self.status = None;
        // Any other keystroke stands the confirmation down.
        let armed = self.armed_to_remove.take();

        let control = key.modifiers.contains(KeyModifiers::CONTROL);
        if control && matches!(key.code, KeyCode::Char('c')) {
            return Ok(Some(Outcome::Cancelled));
        }

        match &mut self.mode {
            Mode::Browse => self.browse_key(key, control, armed),
            Mode::Params { .. } => self.params_key(key, control),
            Mode::Save { .. } => self.save_key(key, control),
            Mode::Edit { .. } => self.edit_key(key, control),
        }
    }

    fn browse_key(
        &mut self,
        key: KeyEvent,
        control: bool,
        armed: Option<String>,
    ) -> Result<Option<Outcome>> {
        match key.code {
            KeyCode::Esc => return Ok(Some(Outcome::Cancelled)),
            KeyCode::Char('g') if control => return Ok(Some(Outcome::Cancelled)),
            KeyCode::Enter => return self.choose(),

            KeyCode::Up => self.move_by(-1),
            KeyCode::Down => self.move_by(1),
            KeyCode::Char('k') if control => self.move_by(-1),
            KeyCode::Char('j') if control => self.move_by(1),
            KeyCode::PageUp => self.move_by(-10),
            KeyCode::PageDown => self.move_by(10),

            KeyCode::Char('p') if control => self.toggle_pin()?,
            KeyCode::Char('s') if control => self.begin_save(),
            KeyCode::Char('e') if control => self.begin_edit(),
            KeyCode::Char('x') if control => self.remove(armed)?,

            KeyCode::Char('u') if control => {
                self.query.clear();
                self.reindex();
            }
            KeyCode::Backspace => {
                self.query.pop();
                self.reindex();
            }
            KeyCode::Char(character) if !control => {
                self.query.push(character);
                self.reindex();
            }
            _ => {}
        }

        Ok(None)
    }

    fn params_key(&mut self, key: KeyEvent, control: bool) -> Result<Option<Outcome>> {
        let Mode::Params { form, .. } = &mut self.mode else {
            return Ok(None);
        };

        match key.code {
            KeyCode::Esc => self.mode = Mode::Browse,
            KeyCode::Enter | KeyCode::Tab | KeyCode::Down => {
                if form.advance() {
                    return self.finish_params();
                }
            }
            KeyCode::Up | KeyCode::BackTab => form.retreat(),
            KeyCode::Char('u') if control => form.clear(),
            KeyCode::Backspace => form.backspace(),
            KeyCode::Char(character) if !control => form.insert(character),
            _ => {}
        }

        Ok(None)
    }

    fn save_key(&mut self, key: KeyEvent, control: bool) -> Result<Option<Outcome>> {
        let Mode::Save(save) = &mut self.mode else {
            return Ok(None);
        };

        match (save.step, key.code) {
            (_, KeyCode::Esc) => self.mode = Mode::Browse,
            // Submits from wherever the cursor is. The checks in finish_save
            // still apply, and send the cursor to whatever is missing.
            (_, KeyCode::Char('s')) if control => self.finish_save()?,

            (SaveStep::Command, KeyCode::Up) => self.recall(1),
            (SaveStep::Command, KeyCode::Down) => self.recall(-1),
            (SaveStep::Command, KeyCode::Enter | KeyCode::Tab) => {
                if save.command.trim().is_empty() {
                    self.status = Some(NO_COMMAND.to_string());
                } else {
                    save.step = SaveStep::Purpose;
                }
            }

            (SaveStep::Purpose, KeyCode::Enter) => self.finish_save()?,
            (SaveStep::Purpose, KeyCode::Up | KeyCode::BackTab) => {
                save.step = SaveStep::Command;
            }

            (_, KeyCode::Char('u')) if control => save.line().clear(),
            (_, KeyCode::Backspace) => {
                save.line().pop();
            }
            (_, KeyCode::Char(character)) if !control => save.line().push(character),
            _ => {}
        }

        Ok(None)
    }

    /// Walks the command line through the shell's history, `older` steps back.
    ///
    /// Past the newest entry the line is empty, the way a shell's own prompt is
    /// when down is pressed at the bottom of its history.
    fn recall(&mut self, older: isize) {
        let Mode::Save(save) = &mut self.mode else {
            return;
        };

        let next = match save.recalled {
            Some(index) => index as isize + older,
            None if older > 0 => 0,
            None => return,
        };

        if next < 0 {
            save.recalled = None;
            save.command.clear();
        } else if let Some(command) = self.history.get(next as usize) {
            save.recalled = Some(next as usize);
            save.command = command.clone();
        }
    }

    fn edit_key(&mut self, key: KeyEvent, control: bool) -> Result<Option<Outcome>> {
        let Mode::Edit { form, .. } = &mut self.mode else {
            return Ok(None);
        };

        match key.code {
            KeyCode::Esc => self.mode = Mode::Browse,
            KeyCode::Enter | KeyCode::Tab | KeyCode::Down => {
                if form.advance() {
                    self.finish_edit()?;
                }
            }
            KeyCode::Up | KeyCode::BackTab => form.retreat(),
            KeyCode::Char('s') if control => self.finish_edit()?,
            KeyCode::Char('u') if control => form.clear(),
            KeyCode::Backspace => form.backspace(),
            KeyCode::Char(character) if !control => form.insert(character),
            _ => {}
        }

        Ok(None)
    }

    /// Enter on a row: prompt for placeholders, or hand the command back.
    fn choose(&mut self) -> Result<Option<Outcome>> {
        let Some(row) = self.selected_row() else {
            return Ok(None);
        };
        let entry_id = row.entry.id.clone();
        let template = row.cmd.to_string();

        let names = params::names(&template);
        if names.is_empty() {
            self.stats.record_use(&entry_id, self.now)?;
            return Ok(Some(Outcome::Insert {
                command: template,
                cursor: None,
            }));
        }

        // One placeholder is not worth a screen. The command goes to the prompt
        // with the placeholder cut out and the cursor in the gap, so the value
        // is typed against the shell's own completion rather than into a form
        // that knows nothing about paths, branches or container names.
        if let [placeholder] = params::parse(&template).as_slice() {
            let span = placeholder.span.clone();
            let cursor = template[..span.start].chars().count();
            let mut command = template;
            command.replace_range(span, "");

            self.stats.record_use(&entry_id, self.now)?;
            return Ok(Some(Outcome::Insert {
                command,
                cursor: Some(cursor),
            }));
        }

        let remembered = self.stats.last_params(&entry_id)?;
        let defaults: BTreeMap<String, String> = params::parse(&template)
            .into_iter()
            .filter_map(|p| p.default.map(|value| (p.name, value)))
            .collect();

        let fields = names
            .iter()
            .map(|name| {
                let value = remembered
                    .get(name)
                    .or_else(|| defaults.get(name))
                    .cloned()
                    .unwrap_or_default();
                let hint = self
                    .entries
                    .iter()
                    .find(|e| e.id == entry_id)
                    .and_then(|e| e.params.get(name))
                    .and_then(|spec| spec.desc.clone());
                Field::new(name, value).with_hint(hint)
            })
            .collect();

        self.mode = Mode::Params {
            entry_id,
            template,
            form: Form::new("Fill in the placeholders", fields),
        };
        Ok(None)
    }

    fn finish_params(&mut self) -> Result<Option<Outcome>> {
        let Mode::Params {
            entry_id,
            template,
            form,
        } = &self.mode
        else {
            return Ok(None);
        };

        let mut values = BTreeMap::new();
        for field in &form.fields {
            values.insert(field.label.clone(), field.value.trim().to_string());
        }

        let command = params::render(template, &values);
        let entry_id = entry_id.clone();

        for (name, value) in &values {
            self.stats.remember_param(&entry_id, name, value)?;
        }
        self.stats.record_use(&entry_id, self.now)?;

        Ok(Some(Outcome::Insert {
            command,
            cursor: None,
        }))
    }

    /// Starts saving on the newest thing the shell has.
    ///
    /// The shell puts whatever was on the prompt line ahead of its history, so
    /// a command typed but not yet run is what gets offered first, and the
    /// last one run when the line was empty.
    fn begin_save(&mut self) {
        self.mode = Mode::Save(Save {
            step: SaveStep::Command,
            command: self.history.first().cloned().unwrap_or_default(),
            purpose: String::new(),
            recalled: (!self.history.is_empty()).then_some(0),
        });
    }

    fn finish_save(&mut self) -> Result<()> {
        let Mode::Save(save) = &mut self.mode else {
            return Ok(());
        };

        let command = save.command.trim().to_string();
        let (description, given) = definitions::split_purpose(&save.purpose);

        if command.is_empty() {
            save.step = SaveStep::Command;
            self.status = Some(NO_COMMAND.to_string());
            return Ok(());
        }
        // Without a description the entry is only findable by its own text,
        // which defeats the point of saving it in the first place.
        if description.is_empty() {
            save.step = SaveStep::Purpose;
            self.status = Some("Say what it is for, so you can find it later".to_string());
            return Ok(());
        }

        let taken: BTreeSet<String> = self.entries.iter().map(|e| e.id.clone()).collect();
        let entry = NewEntry {
            id: definitions::suggest_id(&command, &taken),
            tags: definitions::merge_tags(given, &command),
            cmd: CommandBody::Shared(command),
            desc: description,
            params: BTreeMap::new(),
            danger: false,
        };
        let id = entry.id.clone();

        definitions::append(&self.library, &entry)?;
        self.stats.record_new(&id, self.now)?;

        self.entries = definitions::load(Some(&self.library))?;
        self.scores = self.stats.scores(self.now)?;
        self.mode = Mode::Browse;
        self.query.clear();
        self.reindex();
        self.select_id(&id);
        self.status = Some(format!("Saved as {id}"));
        self.changed = true;

        Ok(())
    }

    /// Whether the library already holds exactly this command for this shell.
    pub fn is_saved(&self, command: &str) -> bool {
        let command = command.trim();
        self.entries
            .iter()
            .any(|entry| entry.cmd_for(self.family) == Some(command))
    }

    /// Tags the entry being saved would get, for showing before it is saved.
    pub fn save_tags(&self) -> Vec<String> {
        let Mode::Save(save) = &self.mode else {
            return Vec::new();
        };
        let (_, given) = definitions::split_purpose(&save.purpose);
        definitions::merge_tags(given, save.command.trim())
    }

    /// Opens the picker on a search already typed.
    pub fn search(&mut self, query: String) {
        self.query = query;
        self.reindex();
    }

    /// Shows `message` where the hints go until the first key is pressed.
    pub fn notice(&mut self, message: String) {
        self.status = Some(message);
    }

    /// Whether anything in the library was written while the picker was open.
    pub fn changed(&self) -> bool {
        self.changed
    }

    /// Opens the edit screen on the selected entry.
    ///
    /// The id is not offered: changing it would orphan everything the usage
    /// statistics have learned about the entry, and the entry can be removed
    /// and saved again if it really needs a different one.
    fn begin_edit(&mut self) {
        let Some(row) = self.selected_row() else {
            return;
        };
        let entry = row.entry;

        let form = Form::new(
            format!("Edit {}", entry.id),
            vec![
                Field::new("command", row.cmd.to_string()),
                Field::new("description", entry.desc.clone()),
                Field::new("tags", entry.tags.join(", "))
                    .with_hint(Some("comma separated".to_string())),
            ],
        );

        self.mode = Mode::Edit {
            id: entry.id.clone(),
            cmd: entry.cmd.clone(),
            params: entry.params.clone(),
            danger: entry.danger,
            shadowing: entry.layer != Layer::User,
            form,
        };
    }

    fn finish_edit(&mut self) -> Result<()> {
        let Mode::Edit {
            id,
            cmd,
            params,
            danger,
            shadowing,
            form,
        } = &self.mode
        else {
            return Ok(());
        };

        let command = form.value(0).to_string();
        let description = form.value(1).to_string();

        if command.is_empty() {
            self.status = Some("A command is required".to_string());
            return Ok(());
        }
        if description.is_empty() {
            self.status = Some("A description is required to find this later".to_string());
            if let Mode::Edit { form, .. } = &mut self.mode {
                form.focused = 1;
            }
            return Ok(());
        }

        // Only the variant for the shell being used is replaced. The others
        // were never on screen and are none of this edit's business.
        let cmd = match cmd {
            CommandBody::Shared(_) => CommandBody::Shared(command),
            CommandBody::PerShell(variants) => {
                let mut variants = variants.clone();
                variants.insert(self.family, command);
                CommandBody::PerShell(variants)
            }
        };

        let entry = NewEntry {
            id: id.clone(),
            cmd,
            desc: description,
            tags: definitions::parse_tags(form.value(2)),
            params: params.clone(),
            danger: *danger,
        };
        let id = entry.id.clone();
        let shadowing = *shadowing;

        definitions::upsert(&self.library, &entry)?;
        self.changed = true;

        self.entries = definitions::load(Some(&self.library))?;
        self.mode = Mode::Browse;
        self.reindex();
        self.select_id(&id);
        self.status = Some(if shadowing {
            format!("Saved {id} to your library, overriding the builtin")
        } else {
            format!("Updated {id}")
        });

        Ok(())
    }

    /// Takes an entry out of the picker, asking once before it does.
    ///
    /// A builtin lives inside the binary and cannot be deleted, so it is added
    /// to the user's disabled list instead. Either way it stops appearing, which
    /// is what was asked for.
    fn remove(&mut self, armed: Option<String>) -> Result<()> {
        let Some(row) = self.selected_row() else {
            return Ok(());
        };
        let id = row.entry.id.clone();
        let own = row.entry.layer == Layer::User;

        if armed.as_deref() != Some(id.as_str()) {
            self.status = Some(format!("Remove {id}? ctrl+x again to confirm"));
            self.armed_to_remove = Some(id);
            return Ok(());
        }

        if own {
            definitions::remove(&self.library, &id)?;
        } else {
            definitions::disable(&self.library, &id)?;
        }
        self.stats.forget(&id)?;

        self.changed = true;
        self.entries = definitions::load(Some(&self.library))?;
        self.scores = self.stats.scores(self.now)?;
        self.reindex();
        self.status = Some(if own {
            format!("Removed {id}")
        } else {
            format!("Hid {id}, listed under disabled in your library")
        });

        Ok(())
    }

    fn toggle_pin(&mut self) -> Result<()> {
        let Some(row) = self.selected_row() else {
            return Ok(());
        };
        let id = row.entry.id.clone();
        let pinned = !row.pinned;

        self.stats.set_pinned(&id, pinned, self.now)?;
        self.scores = self.stats.scores(self.now)?;
        self.reindex();
        self.select_id(&id);
        self.status = Some(if pinned { "Pinned" } else { "Unpinned" }.to_string());

        Ok(())
    }

    fn move_by(&mut self, delta: isize) {
        if self.order.is_empty() {
            self.selected = 0;
            return;
        }
        let last = self.order.len() - 1;
        let target = self.selected as isize + delta;
        self.selected = target.clamp(0, last as isize) as usize;
    }

    fn select_id(&mut self, id: &str) {
        if let Some(position) = self
            .order
            .iter()
            .position(|&index| self.entries[index].id == id)
        {
            self.selected = position;
        }
    }

    /// Recomputes which entries are on offer and in what order.
    fn reindex(&mut self) {
        self.pickable = (0..self.entries.len())
            .filter(|&index| self.entries[index].cmd_for(self.family).is_some())
            .collect();

        let candidates: Vec<Candidate<'_>> = self
            .pickable
            .iter()
            .map(|&index| {
                let entry = &self.entries[index];
                Candidate {
                    entry,
                    cmd: entry.cmd_for(self.family).expect("filtered on this"),
                }
            })
            .collect();

        let ranked = search::rank(&candidates, &self.scores, &self.query);
        self.order = ranked.into_iter().map(|rank| self.pickable[rank]).collect();
        self.selected = self.selected.min(self.order.len().saturating_sub(1));
    }
}

/// Trims what the shell handed over, drops blanks and repeats, and caps the
/// list.
///
/// A shell history is mostly the same handful of commands run again and again,
/// and a list where nine rows in ten are `cd ..` is not worth opening.
fn prepare_history(raw: Vec<String>) -> Vec<String> {
    let mut seen = BTreeSet::new();

    raw.into_iter()
        .map(|command| command.trim().to_string())
        .filter(|command| !command.is_empty() && seen.insert(command.clone()))
        .take(HISTORY_LIMIT)
        .collect()
}

/// One line of the picker.
pub struct Row<'a> {
    pub entry: &'a Entry,
    pub cmd: &'a str,
    pub pinned: bool,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{CommandBody, Layer, ParamSpec};

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    fn ctrl(character: char) -> KeyEvent {
        KeyEvent::new(KeyCode::Char(character), KeyModifiers::CONTROL)
    }

    fn typed(app: &mut App, text: &str) {
        for character in text.chars() {
            app.on_key(key(KeyCode::Char(character))).unwrap();
        }
    }

    fn entry(id: &str, cmd: &str, desc: &str) -> Entry {
        Entry {
            id: id.to_string(),
            cmd: CommandBody::Shared(cmd.to_string()),
            desc: desc.to_string(),
            tags: Vec::new(),
            params: BTreeMap::new(),
            danger: false,
            layer: Layer::Builtin,
        }
    }

    fn app_with(entries: Vec<Entry>) -> App {
        app_with_history(entries, vec!["kics scan -p .".to_string()])
    }

    fn app_with_history(entries: Vec<Entry>, history: Vec<String>) -> App {
        let library = std::env::temp_dir().join(format!(
            "lore-app-{}-{:?}.yaml",
            std::process::id(),
            std::thread::current().id()
        ));
        let _ = std::fs::remove_file(&library);
        App::new(
            entries,
            ShellFamily::Posix,
            Stats::in_memory().unwrap(),
            library,
            history,
            0,
        )
        .unwrap()
    }

    fn history_sample() -> App {
        app_with_history(
            vec![entry("git.log", "git log --oneline", "Show history")],
            vec![
                "kics scan -p .".to_string(),
                "docker compose up -d".to_string(),
                "git log --oneline".to_string(),
            ],
        )
    }

    fn sample() -> App {
        app_with(vec![
            entry("git.log", "git log --oneline", "Show history"),
            entry("docker.ps", "docker ps -a", "List containers"),
            entry(
                "k8s.logs",
                "kubectl logs -f <pod> -n <namespace:default>",
                "Follow pod logs",
            ),
        ])
    }

    #[test]
    fn typing_filters_the_list() {
        let mut app = sample();
        assert_eq!(app.matches(), 3);
        typed(&mut app, "git");
        assert_eq!(app.matches(), 1);
        assert_eq!(app.selected_row().unwrap().entry.id, "git.log");
    }

    /// Pressing ctrl+g after typing something opens the picker on it rather
    /// than on the whole library.
    #[test]
    fn the_picker_can_open_on_a_search_already_typed() {
        let mut app = sample();
        app.search("git".to_string());

        assert_eq!(app.query(), "git");
        assert_eq!(app.matches(), 1);
        assert_eq!(app.selected_row().unwrap().entry.id, "git.log");

        // And it is an ordinary query from there on.
        app.on_key(key(KeyCode::Backspace)).unwrap();
        assert_eq!(app.query(), "gi");
        app.on_key(ctrl('u')).unwrap();
        assert_eq!(app.matches(), 3);
    }

    #[test]
    fn backspace_widens_the_list_again() {
        let mut app = sample();
        typed(&mut app, "git");
        app.on_key(key(KeyCode::Backspace)).unwrap();
        app.on_key(key(KeyCode::Backspace)).unwrap();
        app.on_key(key(KeyCode::Backspace)).unwrap();
        assert_eq!(app.query(), "");
        assert_eq!(app.matches(), 3);
    }

    #[test]
    fn selection_stays_inside_the_list() {
        let mut app = sample();
        for _ in 0..10 {
            app.on_key(key(KeyCode::Down)).unwrap();
        }
        assert_eq!(app.selected(), 2);
        for _ in 0..10 {
            app.on_key(key(KeyCode::Up)).unwrap();
        }
        assert_eq!(app.selected(), 0);
    }

    #[test]
    fn a_narrowed_list_never_leaves_the_cursor_past_the_end() {
        let mut app = sample();
        app.on_key(key(KeyCode::Down)).unwrap();
        app.on_key(key(KeyCode::Down)).unwrap();
        typed(&mut app, "git");
        assert_eq!(app.selected(), 0);
        assert!(app.selected_row().is_some());
    }

    #[test]
    fn enter_on_a_plain_command_returns_it() {
        let mut app = sample();
        typed(&mut app, "docker");
        let outcome = app.on_key(key(KeyCode::Enter)).unwrap();
        assert_eq!(outcome, Some(Outcome::insert("docker ps -a")));
    }

    #[test]
    fn escape_cancels_without_choosing() {
        let mut app = sample();
        assert_eq!(
            app.on_key(key(KeyCode::Esc)).unwrap(),
            Some(Outcome::Cancelled)
        );
    }

    #[test]
    fn the_opening_chord_also_closes_the_picker() {
        let mut app = sample();
        assert_eq!(app.on_key(ctrl('g')).unwrap(), Some(Outcome::Cancelled));
    }

    #[test]
    fn enter_on_a_parameterised_command_asks_for_values() {
        let mut app = sample();
        typed(&mut app, "kubectl");
        assert_eq!(app.on_key(key(KeyCode::Enter)).unwrap(), None);
        assert!(matches!(app.mode(), Mode::Params { .. }));
    }

    #[test]
    fn placeholder_defaults_arrive_pre_filled() {
        let mut app = sample();
        typed(&mut app, "kubectl");
        app.on_key(key(KeyCode::Enter)).unwrap();

        let Mode::Params { form, .. } = app.mode() else {
            panic!("expected the parameter form");
        };
        assert_eq!(form.fields[0].label, "pod");
        assert_eq!(form.fields[0].value, "");
        assert_eq!(form.fields[1].label, "namespace");
        assert_eq!(form.fields[1].value, "default");
    }

    #[test]
    fn filled_placeholders_produce_the_final_command() {
        let mut app = sample();
        typed(&mut app, "kubectl");
        app.on_key(key(KeyCode::Enter)).unwrap();
        typed(&mut app, "api-0");
        app.on_key(key(KeyCode::Enter)).unwrap();
        let outcome = app.on_key(key(KeyCode::Enter)).unwrap();

        assert_eq!(
            outcome,
            Some(Outcome::insert("kubectl logs -f api-0 -n default"))
        );
    }

    #[test]
    fn a_second_use_remembers_the_last_values() {
        let mut app = sample();
        typed(&mut app, "kubectl");
        app.on_key(key(KeyCode::Enter)).unwrap();
        typed(&mut app, "api-0");
        app.on_key(key(KeyCode::Enter)).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();

        // Reopening the same entry should not ask for the pod name again.
        app.mode = Mode::Browse;
        app.on_key(key(KeyCode::Enter)).unwrap();
        let Mode::Params { form, .. } = app.mode() else {
            panic!("expected the parameter form");
        };
        assert_eq!(form.fields[0].value, "api-0");
    }

    #[test]
    fn escape_leaves_the_parameter_form_without_choosing() {
        let mut app = sample();
        typed(&mut app, "kubectl");
        app.on_key(key(KeyCode::Enter)).unwrap();
        assert_eq!(app.on_key(key(KeyCode::Esc)).unwrap(), None);
        assert!(matches!(app.mode(), Mode::Browse));
    }

    #[test]
    fn parameter_descriptions_reach_the_form() {
        let mut with_desc = entry(
            "two.params",
            "scan -p <path> --format <format>",
            "Scan a directory",
        );
        with_desc.params.insert(
            "path".to_string(),
            ParamSpec {
                desc: Some("Directory to scan".to_string()),
                from: None,
            },
        );

        let mut app = app_with(vec![with_desc]);
        app.on_key(key(KeyCode::Enter)).unwrap();
        let Mode::Params { form, .. } = app.mode() else {
            panic!("expected the parameter form");
        };
        assert_eq!(form.fields[0].hint.as_deref(), Some("Directory to scan"));
    }

    /// A form is a poor place to type a path or a branch name: it knows nothing
    /// the shell's own completion knows. One placeholder goes to the prompt
    /// instead, with the cursor sitting in the gap it left.
    #[test]
    fn a_single_placeholder_lands_in_the_prompt_under_the_cursor() {
        let mut app = app_with(vec![entry(
            "one.param",
            "scan -p <path>",
            "Scan a directory",
        )]);
        let outcome = app.on_key(key(KeyCode::Enter)).unwrap();

        assert_eq!(
            outcome,
            Some(Outcome::Insert {
                command: "scan -p ".to_string(),
                cursor: Some(8),
            })
        );
    }

    /// The shortcut is about typing one value, not about skipping the form. A
    /// placeholder used twice still has to be filled in one place.
    #[test]
    fn a_placeholder_used_twice_still_opens_the_form() {
        let mut app = app_with(vec![entry(
            "twice",
            "mv <name> <name>.bak",
            "Back a file up in place",
        )]);
        assert_eq!(app.on_key(key(KeyCode::Enter)).unwrap(), None);
        assert!(matches!(app.mode(), Mode::Params { .. }));
    }

    #[test]
    fn a_command_with_no_placeholder_leaves_the_cursor_alone() {
        let mut app = sample();
        typed(&mut app, "docker");

        let Some(Outcome::Insert { cursor, .. }) = app.on_key(key(KeyCode::Enter)).unwrap() else {
            panic!("expected a command");
        };
        assert_eq!(cursor, None);
    }

    fn save_mode(app: &App) -> &Save {
        let Mode::Save(save) = app.mode() else {
            panic!("expected to be saving");
        };
        save
    }

    #[test]
    fn saving_starts_on_the_newest_command_and_asks_what_it_is_for() {
        let mut app = sample();
        app.on_key(ctrl('s')).unwrap();

        let save = save_mode(&app);
        assert_eq!(save.step, SaveStep::Command);
        assert_eq!(save.command, "kics scan -p .");

        app.on_key(key(KeyCode::Enter)).unwrap();
        assert_eq!(save_mode(&app).step, SaveStep::Purpose);

        typed(&mut app, "Scan this project");
        app.on_key(key(KeyCode::Enter)).unwrap();

        assert!(matches!(app.mode(), Mode::Browse));
        assert!(app.status().unwrap().contains("user.kics-scan"));
        assert!(app.changed());

        // Saving reloads from disk, so the entry is now in the ranked list and
        // sitting under the cursor ready to be inserted.
        let saved = app.selected_row().unwrap();
        assert_eq!(saved.entry.id, "user.kics-scan");
        assert_eq!(saved.cmd, "kics scan -p .");
        assert_eq!(saved.entry.desc, "Scan this project");
        assert_eq!(saved.entry.tags, ["kics", "scan"]);

        let _ = std::fs::remove_file(&app.library);
    }

    #[test]
    fn hashtags_in_the_answer_become_tags() {
        let mut app = sample();
        app.on_key(ctrl('s')).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();
        typed(&mut app, "Scan this project #security");

        assert_eq!(app.save_tags(), ["security", "kics", "scan"]);
        app.on_key(key(KeyCode::Enter)).unwrap();

        let saved = app.selected_row().unwrap();
        assert_eq!(saved.entry.desc, "Scan this project");
        assert_eq!(saved.entry.tags, ["security", "kics", "scan"]);

        let _ = std::fs::remove_file(&app.library);
    }

    #[test]
    fn saving_refuses_an_entry_nobody_could_find_later() {
        let mut app = sample();
        app.on_key(ctrl('s')).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();
        typed(&mut app, "#only-tags");
        app.on_key(key(KeyCode::Enter)).unwrap();

        assert_eq!(save_mode(&app).step, SaveStep::Purpose);
        assert!(app.status().unwrap().contains("what it is for"));
        assert!(!app.library.exists(), "an unfindable entry was written");
    }

    #[test]
    fn going_on_without_a_command_is_refused() {
        let mut app = app_with_history(Vec::new(), Vec::new());
        app.on_key(ctrl('s')).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();

        assert_eq!(save_mode(&app).step, SaveStep::Command);
        assert_eq!(app.status(), Some(NO_COMMAND));
    }

    /// The shell having nothing to offer is not an error. The line is still
    /// there to be typed into.
    #[test]
    fn an_empty_history_still_opens_on_an_empty_line() {
        let mut app = app_with_history(
            vec![entry("git.log", "git log", "Show history")],
            Vec::new(),
        );
        app.on_key(ctrl('s')).unwrap();

        let save = save_mode(&app);
        assert_eq!(save.command, "");
        assert_eq!(save.recalled, None);
        assert!(app.status().is_none());

        app.on_key(key(KeyCode::Up)).unwrap();
        assert_eq!(
            save_mode(&app).command,
            "",
            "up found history that is not there"
        );
    }

    /// The way a shell's own prompt behaves: up walks back, down walks
    /// forward, and down past the newest leaves an empty line.
    #[test]
    fn up_and_down_walk_the_history_like_a_shell() {
        let mut app = history_sample();
        app.on_key(ctrl('s')).unwrap();
        assert_eq!(save_mode(&app).command, "kics scan -p .");

        app.on_key(key(KeyCode::Up)).unwrap();
        assert_eq!(save_mode(&app).command, "docker compose up -d");
        app.on_key(key(KeyCode::Up)).unwrap();
        assert_eq!(save_mode(&app).command, "git log --oneline");
        app.on_key(key(KeyCode::Up)).unwrap();
        assert_eq!(
            save_mode(&app).command,
            "git log --oneline",
            "ran off the end"
        );

        app.on_key(key(KeyCode::Down)).unwrap();
        app.on_key(key(KeyCode::Down)).unwrap();
        assert_eq!(save_mode(&app).command, "kics scan -p .");

        app.on_key(key(KeyCode::Down)).unwrap();
        let save = save_mode(&app);
        assert_eq!(save.command, "");
        assert_eq!(save.recalled, None);

        app.on_key(key(KeyCode::Up)).unwrap();
        assert_eq!(save_mode(&app).command, "kics scan -p .");
    }

    #[test]
    fn a_recalled_command_can_be_edited_before_it_is_saved() {
        let mut app = history_sample();
        app.on_key(ctrl('s')).unwrap();
        app.on_key(key(KeyCode::Up)).unwrap();
        for _ in 0.."-d".len() {
            app.on_key(key(KeyCode::Backspace)).unwrap();
        }
        typed(&mut app, "--build");

        assert_eq!(save_mode(&app).command, "docker compose up --build");
    }

    #[test]
    fn a_command_already_in_the_library_is_recognised() {
        let app = history_sample();
        assert!(app.is_saved("git log --oneline"));
        assert!(app.is_saved("  git log --oneline "));
        assert!(!app.is_saved("docker compose up -d"));
    }

    /// Up on the second line goes back to the first rather than into the
    /// history, and whatever was typed as the answer is kept.
    #[test]
    fn up_from_the_question_returns_to_the_command() {
        let mut app = history_sample();
        app.on_key(ctrl('s')).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();
        typed(&mut app, "Scan");
        app.on_key(key(KeyCode::Up)).unwrap();

        let save = save_mode(&app);
        assert_eq!(save.step, SaveStep::Command);
        assert_eq!(save.command, "kics scan -p .");
        assert_eq!(save.purpose, "Scan");
    }

    #[test]
    fn escape_leaves_without_saving() {
        let mut app = history_sample();
        app.on_key(ctrl('s')).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();
        typed(&mut app, "Scan this project");
        app.on_key(key(KeyCode::Esc)).unwrap();

        assert!(matches!(app.mode(), Mode::Browse));
        assert!(!app.library.exists());
        assert!(!app.changed());
    }

    #[test]
    fn a_repeated_or_blank_history_entry_is_dropped() {
        let prepared = prepare_history(vec![
            "  git status  ".to_string(),
            "   ".to_string(),
            "cd ..".to_string(),
            "git status".to_string(),
        ]);

        assert_eq!(
            prepared,
            vec!["git status".to_string(), "cd ..".to_string()]
        );
    }

    #[test]
    fn the_history_is_capped() {
        let raw: Vec<String> = (0..HISTORY_LIMIT + 10)
            .map(|n| format!("cmd {n}"))
            .collect();
        assert_eq!(prepare_history(raw).len(), HISTORY_LIMIT);
    }

    #[test]
    fn pinning_floats_an_entry_to_the_top() {
        let mut app = sample();
        typed(&mut app, "docker");
        app.on_key(ctrl('p')).unwrap();

        app.on_key(ctrl('u')).unwrap();
        assert_eq!(app.selected_row().unwrap().entry.id, "docker.ps");
        assert!(app.selected_row().unwrap().pinned);
    }

    #[test]
    fn pinning_twice_unpins() {
        let mut app = sample();
        app.on_key(ctrl('p')).unwrap();
        assert!(app.selected_row().unwrap().pinned);
        app.on_key(ctrl('p')).unwrap();
        assert!(!app.selected_row().unwrap().pinned);
    }

    fn save_one(app: &mut App, description: &str) {
        app.on_key(ctrl('s')).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();
        typed(app, description);
        app.on_key(key(KeyCode::Enter)).unwrap();
    }

    #[test]
    fn editing_opens_on_what_the_entry_already_says() {
        let mut app = sample();
        let selected = app.selected_row().unwrap();
        let id = selected.entry.id.clone();
        let cmd = selected.cmd.to_string();
        let desc = selected.entry.desc.clone();
        app.on_key(ctrl('e')).unwrap();

        let Mode::Edit { form, .. } = app.mode() else {
            panic!("expected the edit form");
        };
        assert!(form.title.contains(&id), "the title never names the entry");
        assert_eq!(form.fields[0].value, cmd);
        assert_eq!(form.fields[1].value, desc);
    }

    #[test]
    fn editing_rewrites_the_entry_and_leaves_it_selected() {
        let mut app = app_with(vec![Entry {
            layer: Layer::User,
            ..entry("user.kics", "kics scan -p .", "Scan this project")
        }]);
        definitions::append(
            &app.library,
            &NewEntry {
                id: "user.kics".to_string(),
                cmd: CommandBody::Shared("kics scan -p .".to_string()),
                desc: "Scan this project".to_string(),
                tags: Vec::new(),
                params: BTreeMap::new(),
                danger: false,
            },
        )
        .unwrap();

        app.on_key(ctrl('e')).unwrap();
        typed(&mut app, " --report-formats json");
        app.on_key(key(KeyCode::Enter)).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();

        assert!(matches!(app.mode(), Mode::Browse));
        assert_eq!(app.status(), Some("Updated user.kics"));

        let edited = app.selected_row().unwrap();
        assert_eq!(edited.entry.id, "user.kics");
        assert_eq!(edited.cmd, "kics scan -p . --report-formats json");

        let _ = std::fs::remove_file(&app.library);
    }

    /// A builtin cannot be rewritten inside the binary, so the edit lands in the
    /// user's own library under the same id and shadows it from there.
    #[test]
    fn editing_a_builtin_writes_an_override_the_user_owns() {
        let mut app = app_with(vec![entry("git.log", "git log --oneline", "Show history")]);

        app.on_key(ctrl('e')).unwrap();
        typed(&mut app, " --graph");
        app.on_key(key(KeyCode::Enter)).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();

        assert!(app.status().unwrap().contains("overriding the builtin"));

        let text = std::fs::read_to_string(&app.library).unwrap();
        assert!(text.contains("id: git.log"), "wrote {text:?}");
        assert!(text.contains("--graph"), "wrote {text:?}");

        let _ = std::fs::remove_file(&app.library);
    }

    /// Three fields is two keystrokes of nothing on the way to the one that
    /// matters, so the chord submits from wherever the cursor happens to be.
    #[test]
    fn ctrl_s_submits_the_edit_form_from_any_field() {
        let mut app = app_with(vec![entry("git.log", "git log --oneline", "Show history")]);

        app.on_key(ctrl('e')).unwrap();
        typed(&mut app, " --graph");
        app.on_key(ctrl('s')).unwrap();

        assert!(matches!(app.mode(), Mode::Browse));
        assert_eq!(app.selected_row().unwrap().cmd, "git log --oneline --graph");

        let _ = std::fs::remove_file(&app.library);
    }

    #[test]
    fn ctrl_s_saves_from_the_answer_line() {
        let mut app = sample();

        app.on_key(ctrl('s')).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();
        typed(&mut app, "Scan this project");
        app.on_key(ctrl('s')).unwrap();

        assert!(matches!(app.mode(), Mode::Browse));
        assert!(app.status().unwrap().contains("user.kics-scan"));

        let _ = std::fs::remove_file(&app.library);
    }

    /// Submitting early must not skip the check that the entry is findable.
    #[test]
    fn the_chord_still_refuses_an_entry_with_no_description() {
        let mut app = sample();
        app.on_key(ctrl('s')).unwrap();
        app.on_key(ctrl('s')).unwrap();

        assert_eq!(save_mode(&app).step, SaveStep::Purpose);
        assert!(app.status().unwrap().contains("what it is for"));
    }

    #[test]
    fn editing_refuses_an_entry_nobody_could_find_later() {
        let mut app = sample();
        app.on_key(ctrl('e')).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();
        app.on_key(ctrl('u')).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();
        app.on_key(key(KeyCode::Enter)).unwrap();

        assert!(matches!(app.mode(), Mode::Edit { .. }));
        assert!(app.status().unwrap().contains("description"));
    }

    #[test]
    fn leaving_the_edit_form_changes_nothing() {
        let mut app = sample();
        app.on_key(ctrl('e')).unwrap();
        typed(&mut app, " --graph");
        app.on_key(key(KeyCode::Esc)).unwrap();

        assert!(matches!(app.mode(), Mode::Browse));
        assert!(
            !app.library.exists(),
            "an abandoned edit still wrote a file"
        );
    }

    #[test]
    fn removing_asks_before_it_does_anything() {
        let mut app = sample();
        save_one(&mut app, "Scan this project");

        app.on_key(ctrl('x')).unwrap();

        assert!(app.status().unwrap().contains("ctrl+x again"));
        assert!(app.rows().any(|row| row.entry.id == "user.kics-scan"));

        let _ = std::fs::remove_file(&app.library);
    }

    #[test]
    fn confirming_takes_the_entry_out_of_the_library() {
        let mut app = sample();
        save_one(&mut app, "Scan this project");

        app.on_key(ctrl('x')).unwrap();
        app.on_key(ctrl('x')).unwrap();

        assert!(!app.rows().any(|row| row.entry.id == "user.kics-scan"));
        let library = std::fs::read_to_string(&app.library).unwrap();
        assert!(!library.contains("user.kics-scan"), "left {library:?}");

        let _ = std::fs::remove_file(&app.library);
    }

    #[test]
    fn any_other_key_stands_the_confirmation_down() {
        let mut app = sample();
        save_one(&mut app, "Scan this project");

        app.on_key(ctrl('x')).unwrap();
        app.on_key(key(KeyCode::Down)).unwrap();
        app.on_key(key(KeyCode::Up)).unwrap();
        app.on_key(ctrl('x')).unwrap();

        // Back to asking rather than removing.
        assert!(app.status().unwrap().contains("ctrl+x again"));
        assert!(app.rows().any(|row| row.entry.id == "user.kics-scan"));

        let _ = std::fs::remove_file(&app.library);
    }

    /// A builtin lives inside the binary, so removing it means recording that it
    /// should stop appearing.
    #[test]
    fn removing_a_builtin_disables_it_instead() {
        let mut app = sample();
        let id = app.selected_row().unwrap().entry.id.clone();

        app.on_key(ctrl('x')).unwrap();
        app.on_key(ctrl('x')).unwrap();

        assert!(app.status().unwrap().contains("disabled"));
        let library = std::fs::read_to_string(&app.library).unwrap();
        assert!(library.contains("disabled:"), "wrote {library:?}");
        assert!(library.contains(&id), "wrote {library:?}");

        let _ = std::fs::remove_file(&app.library);
    }

    #[test]
    fn removing_forgets_what_was_remembered_about_the_entry() {
        let mut app = sample();
        save_one(&mut app, "Scan this project");
        let id = app.selected_row().unwrap().entry.id.clone();

        app.on_key(ctrl('x')).unwrap();
        app.on_key(ctrl('x')).unwrap();

        assert!(!app.stats.scores(0).unwrap().contains_key(&id));

        let _ = std::fs::remove_file(&app.library);
    }

    #[test]
    fn a_query_matching_nothing_leaves_the_picker_usable() {
        let mut app = sample();
        typed(&mut app, "zzzzz");
        assert_eq!(app.matches(), 0);
        assert!(app.selected_row().is_none());
        assert_eq!(app.on_key(key(KeyCode::Enter)).unwrap(), None);
        app.on_key(key(KeyCode::Down)).unwrap();
        assert_eq!(app.selected(), 0);
    }
}