bath 0.4.1

A TUI tool to manage and export environment variable profiles
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
use crate::config::{EnvProfile, VarKind};
use crate::db;
use crate::profile_editor::{confirm_dialog, edit_profile_name_dialog};
use crate::tui::state::{AppState, Holding, InputMode, PlacePartOutcome, RenameOutcome};
use crate::tui::view::View;
use crate::tui::{commands, dialogs, editor, select};
use anyhow::Result;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::backend::Backend;
use ratatui::Terminal;

pub fn handle_key_event<B: Backend>(
    terminal: &mut Terminal<B>,
    app: &mut AppState,
    key: KeyEvent,
) -> Result<bool> {
    // Ctrl+C quits like q, regardless of input mode.
    if key.modifiers.contains(KeyModifiers::CONTROL)
        && matches!(key.code, KeyCode::Char('c') | KeyCode::Char('C'))
    {
        return Ok(true);
    }

    match app.input_mode {
        InputMode::Normal => handle_normal_key(terminal, app, key.code),
        InputMode::Command => handle_command_key(terminal, app, key),
        InputMode::Search => handle_search_key(app, key),
    }
}

fn cycle_view(app: &mut AppState) {
    app.active_view = match app.active_view {
        View::Profiles => View::Vars,
        View::Vars => View::Parts,
        View::Parts => View::Items,
        View::Items => View::Defs,
        View::Defs => View::Preview,
        View::Preview => View::Export,
        View::Export => View::Help,
        View::Help => View::Profiles,
    };
}

fn handle_normal_key<B: Backend>(
    terminal: &mut Terminal<B>,
    app: &mut AppState,
    code: KeyCode,
) -> Result<bool> {
    match code {
        // Quit
        KeyCode::Char('q') => return Ok(true),

        // Vim-ish movement keys
        KeyCode::Char('j') => move_selection(app, 1),
        KeyCode::Char('k') => move_selection(app, -1),
        KeyCode::Char('G') | KeyCode::Home => jump_to_top(app),
        KeyCode::Char('g') | KeyCode::End => jump_to_bottom(app),
        KeyCode::PageUp => move_selection(app, -10),
        KeyCode::PageDown => move_selection(app, 10),

        KeyCode::Esc => {
            // Global cancel for an in-progress pick. Holds are deferred (the
            // part never left its origin), so there is nothing to restore.
            match app.holding.take() {
                Some(Holding::Part { .. }) => app.status = "cancelled move".to_string(),
                Some(Holding::Item(_)) => app.status = "cancelled pick".to_string(),
                None => app.status.clear(),
            }
        }

        KeyCode::Tab => cycle_view(app),

        KeyCode::Char('?') => app.active_view = View::Help,

        KeyCode::Char(':') => {
            app.input_mode = InputMode::Command;
            app.command_input.clear();
            app.command_selected = 0;
            commands::refresh_command_suggestions(app);
        }

        KeyCode::Char('/') => {
            if app.active_view.is_filterable() {
                app.input_mode = InputMode::Search;
                app.search_target = app.active_view;
                app.command_input.clear();
            }
        }

        KeyCode::Up => move_selection(app, -1),
        KeyCode::Down => move_selection(app, 1),

        KeyCode::Enter => activate_selection(app),

        // Profiles view actions
        KeyCode::Char('A') if app.active_view == View::Profiles => {
            if let Some(new_name) = edit_profile_name_dialog(terminal, None)? {
                let new_profile = EnvProfile::new(&new_name);
                if app.add_profile(new_profile)? {
                    app.active_profile_index = app.profiles.len().saturating_sub(1);
                    app.status = format!("added profile: {new_name}");
                } else {
                    app.status = format!("profile already exists: {new_name}");
                }
            }
        }
        KeyCode::Char('E') if app.active_view == View::Profiles => {
            if let Some(i) = select::selected_profile_index(app) {
                let current_name = app.profiles[i].name.clone();
                if let Some(new_name) = edit_profile_name_dialog(terminal, Some(&current_name))? {
                    match app.update_profile(i, new_name.clone()) {
                        Ok(RenameOutcome::Renamed) => {
                            app.status = format!("renamed profile: {current_name} -> {new_name}");
                        }
                        Ok(RenameOutcome::Unchanged) => {
                            app.status = "name unchanged".to_string();
                        }
                        Ok(RenameOutcome::Rejected) => {
                            app.status = format!("profile already exists: {new_name}");
                        }
                        Err(e) => {
                            app.status = format!("rename failed: {e}");
                        }
                    }
                }
            }
        }
        KeyCode::Char('D') if app.active_view == View::Profiles => {
            if let Some(i) = select::selected_profile_index(app) {
                if confirm_dialog(terminal, "Delete profile?")? {
                    let name = app.profiles[i].name.clone();
                    if app.delete_profile(i)? {
                        app.status = format!("deleted profile: {name}");
                    } else {
                        app.status = "cannot delete the last profile".to_string();
                    }
                }
            }
        }

        // Defs view actions
        KeyCode::Char('C') if app.active_view == View::Defs => {
            if let Some(def) = dialogs::create_custom_var_dialog(terminal)? {
                db::save_custom_var_def(&app.conn, &def)?;
                app.refresh_var_options()?;
                app.status = format!("saved var def: {}", def.name);
            }
        }

        // Items view actions
        KeyCode::Char('a') if app.active_view == View::Items => {
            if let Some(mut item) = dialogs::create_or_edit_item_dialog(terminal, None)? {
                db::save_item(&app.conn, &mut item)?;
                app.refresh_items()?;
                app.status = format!("saved item: {}", item.value);
            }
        }
        KeyCode::Char('e') if app.active_view == View::Items => {
            if let Some(i) = select::selected_item_index(app) {
                if let Some(initial) = app.items.get(i).cloned() {
                    if let Some(mut edited) =
                        dialogs::create_or_edit_item_dialog(terminal, Some(&initial))?
                    {
                        db::save_item(&app.conn, &mut edited)?;
                        app.refresh_items()?;
                        app.status = format!("updated item: {}", edited.value);
                    }
                }
            }
        }
        KeyCode::Char('d') if app.active_view == View::Items => {
            if let Some(i) = select::selected_item_index(app) {
                if let Some(id) = app.items.get(i).and_then(|it| it.id) {
                    if confirm_dialog(terminal, "Delete item?")? {
                        db::delete_item(&app.conn, id)?;
                        app.refresh_items()?;
                        app.status = "deleted item".to_string();
                    }
                }
            }
        }
        KeyCode::Char('y') if app.active_view == View::Items => {
            if let Some(i) = select::selected_item_index(app) {
                if let Some(orig) = app.items.get(i).cloned() {
                    let mut dup = orig.clone();
                    dup.id = None;
                    db::save_item(&app.conn, &mut dup)?;
                    app.refresh_items()?;
                    app.status = "duplicated item".to_string();
                }
            }
        }
        KeyCode::Char('m') if app.active_view == View::Items => {
            if let Some(i) = select::selected_item_index(app) {
                if let Some(it) = app.items.get(i).cloned() {
                    app.holding = Some(Holding::Item(it));
                    app.status = "picked item".to_string();
                }
            }
        }
        KeyCode::Char('p') if app.active_view == View::Items => {
            // Drop selected item into current var context.
            let var = app
                .selected_var_name()
                .unwrap_or_else(|| "PATH".to_string());
            let opt = select::var_option_for(app, &var);
            if opt.kind != VarKind::List {
                app.status = "cannot drop into scalar var".to_string();
            } else if let Some(i) = select::selected_item_index(app) {
                if let Some(it) = app.items.get(i).cloned() {
                    if let Some(e) = select::make_part_entry(app, &var, it.value) {
                        app.add_env_var(e)?;
                        app.status = format!("dropped into {var}");
                    }
                }
            }
        }

        // Vars view actions
        KeyCode::Char('p') if app.active_view == View::Vars => {
            // Drop held item or part into selected var (append).
            if let Some(holding) = app.holding.clone() {
                let rows = select::compute_var_rows(app);
                if let Some(i) = app.vars_list_state.selected() {
                    if let Some(row) = rows.get(i) {
                        if row.kind != VarKind::List {
                            app.status = "cannot drop into scalar var".to_string();
                        } else {
                            match holding {
                                Holding::Item(it) => {
                                    if let Some(e) =
                                        select::make_part_entry(app, &row.name, it.value.clone())
                                    {
                                        app.add_env_var(e)?;
                                        app.holding = None;
                                        app.status = format!("dropped into {}", row.name);
                                    }
                                }
                                Holding::Part { .. } => {
                                    let insert_at = select::current_var_parts(app, &row.name).len();
                                    match app.place_held_part(&row.name, insert_at)? {
                                        PlacePartOutcome::Moved => {
                                            app.status = format!("moved part into {}", row.name);
                                        }
                                        PlacePartOutcome::OriginGone => {
                                            app.status =
                                                "held part no longer exists; cancelled move"
                                                    .to_string();
                                        }
                                        PlacePartOutcome::CannotConvert => {
                                            app.status = "cannot drop into target var".to_string();
                                        }
                                        PlacePartOutcome::NotHolding => {}
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // Parts view actions
        KeyCode::Char('a') if app.active_view == View::Parts => {
            let var = app
                .selected_var_name()
                .unwrap_or_else(|| "PATH".to_string());
            let opt = select::var_option_for(app, &var);
            if let Some(new_entry) =
                editor::edit_env_var_dialog(terminal, std::slice::from_ref(&opt), None)?
            {
                if opt.kind == VarKind::Scalar {
                    app.replace_var_parts(&var, vec![new_entry])?;
                } else {
                    app.add_env_var(new_entry)?;
                }
                app.status = format!("added part to {var}");
            }
        }
        KeyCode::Char('e') if app.active_view == View::Parts => {
            let var = app
                .selected_var_name()
                .unwrap_or_else(|| "PATH".to_string());
            let opt = select::var_option_for(app, &var);
            let mut parts = select::current_var_parts(app, &var);
            let visible = select::visible_part_indices(app, &parts);
            if let Some(sel) = app.parts_list_state.selected() {
                if let Some(part_i) = visible.get(sel).copied() {
                    if let Some(initial) = parts.get(part_i).cloned() {
                        if let Some(new_entry) = editor::edit_env_var_dialog(
                            terminal,
                            std::slice::from_ref(&opt),
                            Some(&initial),
                        )? {
                            parts[part_i] = new_entry;
                            app.replace_var_parts(&var, parts)?;
                            app.status = format!("edited part in {var}");
                        }
                    }
                }
            }
        }
        KeyCode::Char('d') if app.active_view == View::Parts => {
            let var = app
                .selected_var_name()
                .unwrap_or_else(|| "PATH".to_string());
            let mut parts = select::current_var_parts(app, &var);
            let visible = select::visible_part_indices(app, &parts);
            if let Some(sel) = app.parts_list_state.selected() {
                if let Some(part_i) = visible.get(sel).copied() {
                    parts.remove(part_i);
                    app.replace_var_parts(&var, parts)?;
                    app.status = format!("deleted part from {var}");
                }
            }
        }
        KeyCode::Char('y') if app.active_view == View::Parts => {
            let var = app
                .selected_var_name()
                .unwrap_or_else(|| "PATH".to_string());
            let opt = select::var_option_for(app, &var);
            if opt.kind != VarKind::List {
                // A scalar var must never gain a second entry.
                app.status = "cannot duplicate part in scalar var".to_string();
            } else {
                let mut parts = select::current_var_parts(app, &var);
                let visible = select::visible_part_indices(app, &parts);
                if let Some(sel) = app.parts_list_state.selected() {
                    if let Some(part_i) = visible.get(sel).copied() {
                        let dup = parts[part_i].clone();
                        parts.insert(part_i + 1, dup);
                        app.replace_var_parts(&var, parts)?;
                        app.status = format!("duplicated part in {var}");
                    }
                }
            }
        }
        KeyCode::Char('K') if app.active_view == View::Parts => {
            // With a filter active, the adjacent full index can be a hidden
            // part; swapping with it would silently scramble the real order.
            if !app.parts_filter.is_empty() {
                app.status = "clear filter to reorder".to_string();
            } else {
                let var = app
                    .selected_var_name()
                    .unwrap_or_else(|| "PATH".to_string());
                let mut parts = select::current_var_parts(app, &var);
                if let Some(sel) = app.parts_list_state.selected() {
                    if sel > 0 && sel < parts.len() {
                        parts.swap(sel - 1, sel);
                        app.replace_var_parts(&var, parts)?;
                        app.parts_list_state.select(Some(sel - 1));
                        app.status = format!("moved part up in {var}");
                    }
                }
            }
        }
        KeyCode::Char('J') if app.active_view == View::Parts => {
            // See K above: reordering is only meaningful on the full list.
            if !app.parts_filter.is_empty() {
                app.status = "clear filter to reorder".to_string();
            } else {
                let var = app
                    .selected_var_name()
                    .unwrap_or_else(|| "PATH".to_string());
                let mut parts = select::current_var_parts(app, &var);
                if let Some(sel) = app.parts_list_state.selected() {
                    if sel + 1 < parts.len() {
                        parts.swap(sel, sel + 1);
                        app.replace_var_parts(&var, parts)?;
                        app.parts_list_state.select(Some(sel + 1));
                        app.status = format!("moved part down in {var}");
                    }
                }
            }
        }
        KeyCode::Char('m') if app.active_view == View::Parts => {
            let var = app
                .selected_var_name()
                .unwrap_or_else(|| "PATH".to_string());
            let parts = select::current_var_parts(app, &var);
            let visible = select::visible_part_indices(app, &parts);
            if let Some(sel) = app.parts_list_state.selected() {
                if let Some(part_i) = visible.get(sel).copied() {
                    // Deferred move: the part stays in place (memory and DB)
                    // until it is dropped, so quitting or cancelling while
                    // holding loses nothing.
                    if app.hold_part(&var, part_i) {
                        app.status = "moving part (navigate, p:drop, Esc:cancel)".to_string();
                    }
                }
            }
        }
        KeyCode::Char('p') if app.active_view == View::Parts => {
            // Drop held item/part into parts list at cursor.
            let var = app
                .selected_var_name()
                .unwrap_or_else(|| "PATH".to_string());
            let opt = select::var_option_for(app, &var);
            if opt.kind != VarKind::List {
                app.status = "cannot drop into scalar var".to_string();
            } else if let Some(holding) = app.holding.clone() {
                let mut parts = select::current_var_parts(app, &var);
                let visible = select::visible_part_indices(app, &parts);
                let insert_at = app
                    .parts_list_state
                    .selected()
                    .and_then(|sel| visible.get(sel).copied())
                    .unwrap_or(parts.len());

                match holding {
                    Holding::Item(it) => {
                        if let Some(e) = select::make_part_entry(app, &var, it.value.clone()) {
                            parts.insert(insert_at, e);
                            app.replace_var_parts(&var, parts)?;
                            app.holding = None;
                            app.status = format!("dropped into {var}");
                        }
                    }
                    Holding::Part { .. } => {
                        // Remove from origin and reinsert (verbatim within the
                        // same var) as one atomic operation.
                        match app.place_held_part(&var, insert_at)? {
                            PlacePartOutcome::Moved => {
                                app.status = format!("moved part into {var}");
                            }
                            PlacePartOutcome::OriginGone => {
                                app.status =
                                    "held part no longer exists; cancelled move".to_string();
                            }
                            PlacePartOutcome::CannotConvert => {
                                app.status = "cannot drop into target var".to_string();
                            }
                            PlacePartOutcome::NotHolding => {}
                        }
                    }
                }
            }
        }

        _ => {}
    }

    Ok(false)
}

fn move_selection(app: &mut AppState, delta: isize) {
    // Clamp selection to visible list bounds at input-time. Otherwise the selection index can grow
    // unbounded (e.g. holding Down at end), making it take many Up presses to get back in range.
    let len = {
        let a: &AppState = &*app;
        match a.active_view {
            View::Profiles => select::visible_profile_indices(a).len(),
            View::Vars => select::compute_var_rows(a).len(),
            View::Parts => {
                let var = a.selected_var_name().unwrap_or_else(|| "PATH".to_string());
                let parts = select::current_var_parts(a, &var);
                select::visible_part_indices(a, &parts).len()
            }
            View::Items => select::visible_item_indices(a).len(),
            View::Defs => {
                let mut defs = a.var_options.clone();
                defs.sort_by(|x, y| x.name.cmp(&y.name));
                if !a.defs_filter.is_empty() {
                    let q = a.defs_filter.to_lowercase();
                    defs.retain(|d| d.name.to_lowercase().contains(&q));
                }
                defs.len()
            }
            View::Preview | View::Export | View::Help => 0,
        }
    };

    let state = match app.active_view {
        View::Profiles => Some(&mut app.profile_list_state),
        View::Vars => Some(&mut app.vars_list_state),
        View::Defs => Some(&mut app.defs_list_state),
        View::Parts => Some(&mut app.parts_list_state),
        View::Items => Some(&mut app.items_list_state),
        View::Preview | View::Export | View::Help => None,
    };

    let Some(state) = state else {
        return;
    };

    if len == 0 {
        state.select(None);
        return;
    }

    let cur = state.selected().unwrap_or(0).min(len - 1) as isize;
    let next = (cur + delta).clamp(0, (len - 1) as isize) as usize;
    state.select(Some(next));

    // The var context needs no explicit resync here: it is derived from the
    // highlighted Vars row (AppState::selected_var_name), so it can never go
    // stale, no matter how the highlight moved.
}

fn activate_selection(app: &mut AppState) {
    match app.active_view {
        View::Profiles => {
            if let Some(i) = select::selected_profile_index(app) {
                app.active_profile_index = i;
                app.status = format!("profile: {}", app.profiles[i].name);
            }
        }
        View::Vars => {
            let rows = select::compute_var_rows(app);
            if let Some(i) = app.vars_list_state.selected() {
                if let Some(row) = rows.get(i) {
                    app.active_view = View::Parts;
                    app.status = format!("selected var: {}", row.name);
                }
            }
        }
        _ => {}
    }
}

fn handle_command_key<B: Backend>(
    terminal: &mut Terminal<B>,
    app: &mut AppState,
    key: KeyEvent,
) -> Result<bool> {
    match key.code {
        KeyCode::Esc => {
            app.input_mode = InputMode::Normal;
        }
        KeyCode::Enter => {
            let exec = commands::pick_command_to_execute(app);
            let quit = commands::execute_command(terminal, app, &exec)?;
            app.input_mode = InputMode::Normal;
            if quit {
                return Ok(true);
            }
        }
        KeyCode::Tab => {
            let idx =
                commands::clamp_selection(app.command_selected, app.command_suggestions.len());
            if let Some(s) = app.command_suggestions.get(idx).cloned() {
                app.command_input = s;
                commands::refresh_command_suggestions(app);
            }
        }
        KeyCode::Up => {
            app.command_selected = commands::clamp_selection(
                app.command_selected.saturating_sub(1),
                app.command_suggestions.len(),
            );
        }
        KeyCode::Down => {
            app.command_selected =
                commands::clamp_selection(app.command_selected + 1, app.command_suggestions.len());
        }
        KeyCode::Backspace => {
            app.command_input.pop();
            commands::refresh_command_suggestions(app);
        }
        KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
            app.command_input.push(c);
            commands::refresh_command_suggestions(app);
        }
        _ => {}
    }
    Ok(false)
}

fn handle_search_key(app: &mut AppState, key: KeyEvent) -> Result<bool> {
    match key.code {
        KeyCode::Esc => {
            app.command_input.clear();
            apply_live_filter(app, "");
            app.input_mode = InputMode::Normal;
        }
        KeyCode::Enter => {
            app.command_input.clear();
            app.input_mode = InputMode::Normal;
        }
        KeyCode::Backspace => {
            app.command_input.pop();
            let q = app.command_input.clone();
            apply_live_filter(app, &q);
        }
        KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
            app.command_input.push(c);
            let q = app.command_input.clone();
            apply_live_filter(app, &q);
        }
        _ => {}
    }
    Ok(false)
}

fn apply_live_filter(app: &mut AppState, q: &str) {
    let q = q.to_string();
    match app.search_target {
        View::Profiles => app.profiles_filter = q,
        View::Vars => app.vars_filter = q,
        View::Defs => app.defs_filter = q,
        View::Parts => app.parts_filter = q,
        View::Items => app.items_filter = q,
        View::Preview | View::Export | View::Help => {}
    }
}

fn jump_to_top(app: &mut AppState) {
    let state = match app.active_view {
        View::Profiles => Some(&mut app.profile_list_state),
        View::Vars => Some(&mut app.vars_list_state),
        View::Defs => Some(&mut app.defs_list_state),
        View::Parts => Some(&mut app.parts_list_state),
        View::Items => Some(&mut app.items_list_state),
        View::Preview | View::Export | View::Help => None,
    };
    if let Some(state) = state {
        state.select(Some(0));
    }
}

fn jump_to_bottom(app: &mut AppState) {
    let (len, state) = match app.active_view {
        View::Profiles => {
            let len = select::visible_profile_indices(app).len();
            (len, Some(&mut app.profile_list_state))
        }
        View::Vars => {
            let len = select::compute_var_rows(app).len();
            (len, Some(&mut app.vars_list_state))
        }
        View::Parts => {
            let var = app
                .selected_var_name()
                .unwrap_or_else(|| "PATH".to_string());
            let parts = select::current_var_parts(app, &var);
            let len = select::visible_part_indices(app, &parts).len();
            (len, Some(&mut app.parts_list_state))
        }
        View::Items => {
            let len = select::visible_item_indices(app).len();
            (len, Some(&mut app.items_list_state))
        }
        View::Defs => {
            let mut defs = app.var_options.clone();
            defs.sort_by(|a, b| a.name.cmp(&b.name));
            if !app.defs_filter.is_empty() {
                let q = app.defs_filter.to_lowercase();
                defs.retain(|d| d.name.to_lowercase().contains(&q));
            }
            (defs.len(), Some(&mut app.defs_list_state))
        }
        View::Preview | View::Export | View::Help => (0, None),
    };

    if let Some(state) = state {
        if len > 0 {
            state.select(Some(len - 1));
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::EnvProfile;
    use crate::tui::state::{builtin_var_options, InputMode};
    use crate::tui::theme::BathConfig;
    use crate::tui::view::View;
    use crossterm::event::KeyModifiers;
    use ratatui::backend::TestBackend;
    use ratatui::widgets::ListState;
    use rusqlite::Connection;

    fn test_app() -> AppState {
        let conn = Connection::open_in_memory().unwrap();
        db::initialize_db(&conn).unwrap();
        let default = EnvProfile::new("default");
        db::save_profile(&conn, &default).unwrap();

        let mut app = AppState {
            conn,
            profiles: vec![default],
            active_profile_index: 0,
            profile_list_state: ListState::default(),
            custom_var_defs: Vec::new(),
            var_options: builtin_var_options(),
            active_view: View::Vars,
            input_mode: InputMode::Normal,
            theme_preset: crate::tui::theme::default_preset().to_string(),
            theme: crate::tui::theme::resolve_theme(crate::tui::theme::default_preset(), None)
                .unwrap(),
            config: BathConfig::default(),
            vars_list_state: ListState::default(),
            defs_list_state: ListState::default(),
            parts_list_state: ListState::default(),
            items_list_state: ListState::default(),
            profiles_filter: String::new(),
            vars_filter: String::new(),
            defs_filter: String::new(),
            parts_filter: String::new(),
            items_filter: String::new(),
            command_input: String::new(),
            command_suggestions: Vec::new(),
            command_selected: 0,
            search_target: View::Vars,
            status: String::new(),
            holding: None,
            items: Vec::new(),
        };
        select_var_row(&mut app, "PATH");
        app
    }

    /// Highlight `name`'s row in the Vars list; the var context is derived
    /// from this highlight.
    fn select_var_row(app: &mut AppState, name: &str) {
        let row = select::compute_var_rows(app)
            .iter()
            .position(|r| r.name == name)
            .unwrap();
        app.vars_list_state.select(Some(row));
    }

    fn test_terminal() -> Terminal<TestBackend> {
        Terminal::new(TestBackend::new(80, 24)).unwrap()
    }

    #[test]
    fn ctrl_c_quits_like_q() {
        let mut terminal = test_terminal();
        let mut app = test_app();

        let key = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL);
        let quit = handle_key_event(&mut terminal, &mut app, key).unwrap();
        assert!(quit, "Ctrl+C must quit like q does");
    }

    #[test]
    fn ctrl_modified_char_is_not_inserted_in_search_input() {
        let mut terminal = test_terminal();
        let mut app = test_app();
        app.input_mode = InputMode::Search;
        app.search_target = View::Vars;

        let key = KeyEvent::new(KeyCode::Char('f'), KeyModifiers::CONTROL);
        handle_key_event(&mut terminal, &mut app, key).unwrap();
        assert_eq!(
            app.command_input, "",
            "Ctrl-modified keys must not land in the search field as plain chars"
        );
        assert_eq!(app.vars_filter, "");
    }

    #[test]
    fn ctrl_modified_char_is_not_inserted_in_command_input() {
        let mut terminal = test_terminal();
        let mut app = test_app();
        app.input_mode = InputMode::Command;

        let key = KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL);
        handle_key_event(&mut terminal, &mut app, key).unwrap();
        assert_eq!(
            app.command_input, "",
            "Ctrl-modified keys must not land in the command palette as plain chars"
        );
    }

    #[test]
    fn plain_and_shifted_chars_are_still_inserted_in_search_input() {
        let mut terminal = test_terminal();
        let mut app = test_app();
        app.input_mode = InputMode::Search;
        app.search_target = View::Vars;

        let plain = KeyEvent::new(KeyCode::Char('p'), KeyModifiers::NONE);
        handle_key_event(&mut terminal, &mut app, plain).unwrap();
        let shifted = KeyEvent::new(KeyCode::Char('A'), KeyModifiers::SHIFT);
        handle_key_event(&mut terminal, &mut app, shifted).unwrap();
        assert_eq!(app.command_input, "pA");
    }

    use crate::config::{Entry, PathEntry};

    fn path_entry(path: &str, program: &str, version: &str) -> Entry {
        Entry::Path(PathEntry {
            path: path.to_string(),
            program: program.to_string(),
            version: version.to_string(),
        })
    }

    fn press(terminal: &mut Terminal<TestBackend>, app: &mut AppState, c: char) -> bool {
        let key = KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE);
        handle_key_event(terminal, app, key).unwrap()
    }

    fn press_key(terminal: &mut Terminal<TestBackend>, app: &mut AppState, code: KeyCode) -> bool {
        let key = KeyEvent::new(code, KeyModifiers::NONE);
        handle_key_event(terminal, app, key).unwrap()
    }

    fn buffer_lines(terminal: &Terminal<TestBackend>) -> Vec<String> {
        let buf = terminal.backend().buffer();
        (0..buf.area.height)
            .map(|y| {
                (0..buf.area.width)
                    .map(|x| buf.get(x, y).symbol.clone())
                    .collect::<String>()
            })
            .collect()
    }

    fn draw(terminal: &mut Terminal<TestBackend>, app: &mut AppState) {
        terminal
            .draw(|f| crate::tui::ui::draw_main_ui(f, app))
            .unwrap();
    }

    #[test]
    fn palette_enter_executes_the_visibly_highlighted_suggestion() {
        let mut terminal = test_terminal();
        let mut app = test_app();

        press(&mut terminal, &mut app, ':');
        let total = app.command_suggestions.len();
        assert!(
            total > 8,
            "test needs more suggestions than fit in the palette window, got {total}"
        );
        // Move well past the visible window (and past the end of the list).
        for _ in 0..total + 5 {
            press_key(&mut terminal, &mut app, KeyCode::Down);
        }

        let exec = commands::pick_command_to_execute(&app);
        draw(&mut terminal, &mut app);
        let lines = buffer_lines(&terminal);
        let highlighted: Vec<&String> = lines.iter().filter(|l| l.contains("» ")).collect();
        assert_eq!(
            highlighted.len(),
            1,
            "exactly one suggestion row must carry the highlight symbol, got {highlighted:?}"
        );
        assert!(
            highlighted[0].contains(&exec),
            "Enter must execute the visibly highlighted suggestion; highlighted row {:?} but Enter would run {:?}",
            highlighted[0],
            exec
        );
    }

    #[test]
    fn palette_prompt_renders_typed_command_text() {
        let mut terminal = test_terminal();
        let mut app = test_app();

        press(&mut terminal, &mut app, ':');
        for c in "xyz".chars() {
            press(&mut terminal, &mut app, c);
        }

        draw(&mut terminal, &mut app);
        let lines = buffer_lines(&terminal);
        assert!(
            lines.iter().any(|l| l.contains(":xyz")),
            "the typed palette input must be visible in the prompt, got {lines:?}"
        );
    }

    #[test]
    fn header_command_hints_row_is_rendered() {
        let mut terminal = test_terminal();
        let mut app = test_app();

        draw(&mut terminal, &mut app);
        let lines = buffer_lines(&terminal);
        assert!(
            lines.iter().any(|l| l.contains("Commands: :profiles")),
            "the command-hints header row must be visible, got {lines:?}"
        );
    }

    #[test]
    fn picking_part_with_m_defers_removal_so_quit_loses_nothing() {
        let mut terminal = test_terminal();
        let mut app = test_app();
        app.add_env_var(path_entry("/opt/gcc/bin", "gcc", "13"))
            .unwrap();
        app.active_view = View::Parts;
        app.parts_list_state.select(Some(0));

        press(&mut terminal, &mut app, 'm');

        assert!(
            matches!(app.holding, Some(Holding::Part { .. })),
            "m must pick the part"
        );
        assert_eq!(
            app.profiles[0].entries.len(),
            1,
            "picking with m must not remove the part from the profile"
        );
        let loaded = db::load_profile(&app.conn, "default").unwrap();
        assert_eq!(
            loaded.entries.len(),
            1,
            "picking with m must not persist a removal; quitting while holding would lose the part"
        );

        // Quitting while holding must leave the DB untouched.
        let quit = press(&mut terminal, &mut app, 'q');
        assert!(quit);
        let loaded = db::load_profile(&app.conn, "default").unwrap();
        assert_eq!(loaded.entries.len(), 1);
    }

    #[test]
    fn moving_path_part_within_var_preserves_program_and_version() {
        let mut terminal = test_terminal();
        let mut app = test_app();
        app.add_env_var(path_entry("/opt/gcc/bin", "gcc", "13"))
            .unwrap();
        app.add_env_var(path_entry("/a", "", "")).unwrap();
        app.add_env_var(path_entry("/b", "", "")).unwrap();
        app.active_view = View::Parts;

        app.parts_list_state.select(Some(0));
        press(&mut terminal, &mut app, 'm');
        app.parts_list_state.select(Some(2));
        press(&mut terminal, &mut app, 'p');

        let parts = select::current_var_parts(&app, "PATH");
        assert_eq!(parts.len(), 3);
        let gcc = parts
            .iter()
            .find_map(|e| match e {
                Entry::Path(pe) if pe.path == "/opt/gcc/bin" => Some(pe.clone()),
                _ => None,
            })
            .expect("moved part must still exist");
        assert_eq!(
            (gcc.program.as_str(), gcc.version.as_str()),
            ("gcc", "13"),
            "moving a part must not wipe PathEntry program/version metadata"
        );
        // The held part is dropped before the cursor position.
        let paths: Vec<String> = parts
            .iter()
            .map(|e| match e {
                Entry::Path(pe) => pe.path.clone(),
                _ => unreachable!(),
            })
            .collect();
        assert_eq!(paths, vec!["/a", "/opt/gcc/bin", "/b"]);
        assert!(app.holding.is_none());

        // Metadata must also survive in the DB.
        let loaded = db::load_profile(&app.conn, "default").unwrap();
        assert!(loaded
            .entries
            .iter()
            .any(|e| matches!(e, Entry::Path(pe) if pe.program == "gcc" && pe.version == "13")));
    }

    #[test]
    fn held_marker_follows_the_held_entry_after_reorder() {
        let mut terminal = test_terminal();
        let mut app = test_app();
        app.add_env_var(path_entry("/held", "gcc", "13")).unwrap();
        app.add_env_var(path_entry("/a", "", "")).unwrap();
        app.add_env_var(path_entry("/b", "", "")).unwrap();
        app.active_view = View::Parts;
        app.parts_list_state.select(Some(0));
        press(&mut terminal, &mut app, 'm');

        // The list is reordered while holding: the held entry drifts to the
        // end, but the dimmed [held] marker must follow it, not its old index.
        app.replace_var_parts(
            "PATH",
            vec![
                path_entry("/a", "", ""),
                path_entry("/b", "", ""),
                path_entry("/held", "gcc", "13"),
            ],
        )
        .unwrap();

        draw(&mut terminal, &mut app);
        let lines = buffer_lines(&terminal);
        let held_rows: Vec<&String> = lines.iter().filter(|l| l.contains("[held]")).collect();
        assert_eq!(
            held_rows.len(),
            1,
            "exactly one row must carry the [held] marker, got {held_rows:?}"
        );
        assert!(
            held_rows[0].contains("/held"),
            "the [held] marker must follow the held entry after a reorder, got {held_rows:?}"
        );
    }

    #[test]
    fn held_marker_vanishes_when_the_held_entry_is_gone() {
        let mut terminal = test_terminal();
        let mut app = test_app();
        app.add_env_var(path_entry("/held", "gcc", "13")).unwrap();
        app.add_env_var(path_entry("/a", "", "")).unwrap();
        app.active_view = View::Parts;
        app.parts_list_state.select(Some(0));
        press(&mut terminal, &mut app, 'm');

        // The held part is deleted while holding: no row may be dimmed as
        // held, least of all the unrelated part now at the pick-time index.
        app.replace_var_parts("PATH", vec![path_entry("/a", "", "")])
            .unwrap();

        draw(&mut terminal, &mut app);
        let lines = buffer_lines(&terminal);
        assert!(
            !lines.iter().any(|l| l.contains("[held]")),
            "no row may be marked held once the held entry is gone, got {lines:?}"
        );
    }

    #[test]
    fn esc_after_profile_switch_does_not_move_part_across_profiles() {
        let mut terminal = test_terminal();
        let mut app = test_app();
        app.add_profile(EnvProfile::new("other")).unwrap();
        app.add_env_var(path_entry("/opt/gcc/bin", "gcc", "13"))
            .unwrap();
        app.active_view = View::Parts;
        app.parts_list_state.select(Some(0));

        press(&mut terminal, &mut app, 'm');
        // Simulate `:use other`.
        app.active_profile_index = 1;
        let key = KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE);
        handle_key_event(&mut terminal, &mut app, key).unwrap();

        assert!(app.holding.is_none());
        assert!(
            app.profiles[1].entries.is_empty(),
            "cancelling a hold must not insert the part into another profile"
        );
        assert_eq!(
            app.profiles[0].entries.len(),
            1,
            "cancelling a hold must leave the part in its origin profile"
        );
        assert!(db::load_profile(&app.conn, "other")
            .unwrap()
            .entries
            .is_empty());
        assert_eq!(
            db::load_profile(&app.conn, "default")
                .unwrap()
                .entries
                .len(),
            1
        );
    }

    #[test]
    fn duplicating_part_with_y_is_blocked_for_scalar_vars() {
        let mut terminal = test_terminal();
        let mut app = test_app();
        app.add_env_var(Entry::CC("gcc".to_string())).unwrap();
        app.active_view = View::Parts;
        select_var_row(&mut app, "CC");
        app.parts_list_state.select(Some(0));

        press(&mut terminal, &mut app, 'y');

        assert_eq!(
            select::current_var_parts(&app, "CC").len(),
            1,
            "a scalar var must never gain a second entry via y"
        );
        assert!(
            app.status.contains("scalar"),
            "blocked duplicate must be reported on the status line, got: {}",
            app.status
        );
    }

    #[test]
    fn vars_view_p_drops_held_part_into_selected_var() {
        let mut terminal = test_terminal();
        let mut app = test_app();
        app.add_env_var(path_entry("/opt/gcc/bin", "gcc", "13"))
            .unwrap();
        app.active_view = View::Parts;
        app.parts_list_state.select(Some(0));
        press(&mut terminal, &mut app, 'm');

        app.active_view = View::Vars;
        let rows = select::compute_var_rows(&app);
        let cpath_row = rows.iter().position(|r| r.name == "CPATH").unwrap();
        app.vars_list_state.select(Some(cpath_row));
        press(&mut terminal, &mut app, 'p');

        assert!(app.holding.is_none(), "drop must consume the hold");
        let cpath = select::current_var_parts(&app, "CPATH");
        assert_eq!(
            cpath.len(),
            1,
            "held part must be appended to the target var"
        );
        assert!(matches!(&cpath[0], Entry::CPath(v) if v == "/opt/gcc/bin"));
        assert!(
            select::current_var_parts(&app, "PATH").is_empty(),
            "moving must remove the part from its origin var"
        );
    }

    #[test]
    fn live_filter_moves_parts_context_to_highlighted_var() {
        let mut terminal = test_terminal();
        let mut app = test_app();
        app.add_env_var(path_entry("/bin", "", "")).unwrap();
        app.add_env_var(Entry::CPath("/inc".to_string())).unwrap();
        app.active_view = View::Vars;

        // Filter down to CPATH without any j/k movement; the highlight lands
        // on the only remaining row.
        press(&mut terminal, &mut app, '/');
        for c in "cpath".chars() {
            press(&mut terminal, &mut app, c);
        }
        let enter = KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE);
        handle_key_event(&mut terminal, &mut app, enter).unwrap();

        // Parts must now operate on the highlighted var (CPATH), not on a
        // stale selection from before the filter.
        app.active_view = View::Parts;
        app.parts_list_state.select(Some(0));
        press(&mut terminal, &mut app, 'd');

        assert!(
            select::current_var_parts(&app, "CPATH").is_empty(),
            "d must delete a part of the var highlighted in the Vars list"
        );
        assert_eq!(
            select::current_var_parts(&app, "PATH").len(),
            1,
            "the previously-selected var must not be touched"
        );
    }

    #[test]
    fn jump_to_bottom_moves_parts_context_to_highlighted_var() {
        let mut terminal = test_terminal();
        let mut app = test_app();
        app.add_env_var(path_entry("/bin", "", "")).unwrap();
        app.add_env_var(Entry::Strip("strip".to_string())).unwrap();
        app.active_view = View::Vars;

        // g jumps to the bottom row without any j/k movement.
        press(&mut terminal, &mut app, 'g');
        let rows = select::compute_var_rows(&app);
        let highlighted = rows[app.vars_list_state.selected().unwrap()].name.clone();
        assert_eq!(highlighted, "STRIP", "bottom row of the builtin var list");

        app.active_view = View::Parts;
        app.parts_list_state.select(Some(0));
        press(&mut terminal, &mut app, 'd');

        assert!(
            select::current_var_parts(&app, "STRIP").is_empty(),
            "d must delete a part of the var highlighted in the Vars list"
        );
        assert_eq!(
            select::current_var_parts(&app, "PATH").len(),
            1,
            "the previously-selected var must not be touched"
        );
    }

    fn path_order(app: &AppState) -> Vec<String> {
        select::current_var_parts(app, "PATH")
            .iter()
            .map(|e| match e {
                Entry::Path(pe) => pe.path.clone(),
                _ => unreachable!(),
            })
            .collect()
    }

    #[test]
    fn reorder_is_blocked_while_parts_filter_is_active() {
        let mut terminal = test_terminal();
        let mut app = test_app();
        for p in ["/keep-one", "/hidden-a", "/keep-two", "/hidden-b"] {
            app.add_env_var(path_entry(p, "", "")).unwrap();
        }
        app.active_view = View::Parts;
        app.parts_filter = "keep".to_string();
        // Visible rows: [/keep-one, /keep-two]; highlight the second one.
        app.parts_list_state.select(Some(1));

        let original = vec!["/keep-one", "/hidden-a", "/keep-two", "/hidden-b"];

        press(&mut terminal, &mut app, 'K');
        assert_eq!(
            path_order(&app),
            original,
            "K under an active filter must not swap with a hidden part"
        );
        assert!(
            app.status.contains("clear filter"),
            "blocked reorder must be reported on the status line, got: {}",
            app.status
        );

        press(&mut terminal, &mut app, 'J');
        assert_eq!(
            path_order(&app),
            original,
            "J under an active filter must not swap with a hidden part"
        );

        // Nothing may be persisted either.
        let loaded = db::load_profile(&app.conn, "default").unwrap();
        let db_order: Vec<String> = loaded
            .entries
            .iter()
            .map(|e| match e {
                Entry::Path(pe) => pe.path.clone(),
                _ => unreachable!(),
            })
            .collect();
        assert_eq!(db_order, original);
    }

    #[test]
    fn vars_view_p_respects_scalar_guard_for_held_part() {
        let mut terminal = test_terminal();
        let mut app = test_app();
        app.add_env_var(path_entry("/opt/gcc/bin", "gcc", "13"))
            .unwrap();
        app.active_view = View::Parts;
        app.parts_list_state.select(Some(0));
        press(&mut terminal, &mut app, 'm');

        app.active_view = View::Vars;
        let rows = select::compute_var_rows(&app);
        let cc_row = rows.iter().position(|r| r.name == "CC").unwrap();
        app.vars_list_state.select(Some(cc_row));
        press(&mut terminal, &mut app, 'p');

        assert!(
            select::current_var_parts(&app, "CC").is_empty(),
            "a held part must not be dropped into a scalar var"
        );
        assert!(
            matches!(app.holding, Some(Holding::Part { .. })),
            "a blocked drop must keep the hold"
        );
        assert_eq!(select::current_var_parts(&app, "PATH").len(), 1);
    }
}