pacsea 0.8.2

A fast, friendly TUI for browsing and installing Arch and AUR packages with built-in news and security scanning
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
use std::collections::HashMap;

use crossterm::event::{KeyCode, KeyModifiers};
use ratatui::style::Color;

/// Application theme palette used by rendering code.
///
/// All colors are provided as [`ratatui::style::Color`] and are suitable for
/// direct use with widgets and styles.
#[derive(Clone, Copy, Debug)]
pub struct Theme {
    /// Primary background color for the canvas.
    pub base: Color,
    /// Slightly lighter background layer used behind panels.
    pub mantle: Color,
    /// Darkest background shade for deep contrast areas.
    pub crust: Color,
    /// Subtle surface color for component backgrounds (level 1).
    pub surface1: Color,
    /// Subtle surface color for component backgrounds (level 2).
    pub surface2: Color,
    /// Muted overlay line/border color (primary).
    pub overlay1: Color,
    /// Muted overlay line/border color (secondary).
    pub overlay2: Color,
    /// Primary foreground text color.
    pub text: Color,
    /// Secondary text for less prominent content.
    pub subtext0: Color,
    /// Tertiary text for captions and low-emphasis content.
    pub subtext1: Color,
    /// Accent color commonly used for selection and interactive highlights.
    pub sapphire: Color,
    /// Accent color for emphasized headings or selections.
    pub mauve: Color,
    /// Success/positive state color.
    pub green: Color,
    /// Warning/attention state color.
    pub yellow: Color,
    /// Error/danger state color.
    pub red: Color,
    /// Accent color for subtle emphasis and borders.
    pub lavender: Color,
}

/// User-configurable application settings parsed from `pacsea.conf`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PackageMarker {
    /// Color the entire line for the marked package.
    FullLine,
    /// Add a marker at the front of the line.
    Front,
    /// Add a marker at the end of the line.
    End,
}

/// User-configurable application settings parsed from `pacsea.conf`.
#[derive(Clone, Debug)]
#[allow(clippy::struct_excessive_bools)]
pub struct Settings {
    /// Percentage width allocated to the Recent pane (left column).
    pub layout_left_pct: u16,
    /// Percentage width allocated to the Search pane (center column).
    pub layout_center_pct: u16,
    /// Percentage width allocated to the Install pane (right column).
    pub layout_right_pct: u16,
    /// Vertical order of the main stack: results list, middle search row, package info (each once).
    pub main_pane_order: [crate::state::MainVerticalPane; 3],
    /// Minimum terminal rows for the results list band.
    pub vertical_min_results: u16,
    /// Maximum terminal rows for the results list band.
    pub vertical_max_results: u16,
    /// Minimum terminal rows for the middle (search) row.
    pub vertical_min_middle: u16,
    /// Maximum terminal rows for the middle row.
    pub vertical_max_middle: u16,
    /// Minimum terminal rows for package info when that band is shown.
    pub vertical_min_package_info: u16,
    /// Default value for the application's dry-run mode on startup.
    /// This can be toggled via the `--dry-run` CLI flag.
    pub app_dry_run_default: bool,
    /// Configurable key bindings parsed from `pacsea.conf`
    pub keymap: KeyMap,
    /// Initial sort mode for results list.
    pub sort_mode: crate::state::SortMode,
    /// Text appended when copying PKGBUILD to clipboard.
    pub clipboard_suffix: String,
    /// Whether the Search history pane should be shown on startup.
    pub show_recent_pane: bool,
    /// Whether the Install/Remove pane should be shown on startup.
    pub show_install_pane: bool,
    /// Whether the keybinds footer should be shown on startup.
    pub show_keybinds_footer: bool,
    /// Selected countries used when updating mirrors (comma-separated or multiple).
    pub selected_countries: String,
    /// Number of mirrors to fetch/rank when updating.
    pub mirror_count: u16,
    /// `VirusTotal` API key for security scanning.
    pub virustotal_api_key: String,
    /// Whether to run `ClamAV` scan on AUR packages.
    pub scan_do_clamav: bool,
    /// Whether to run `Trivy` scan on AUR packages.
    pub scan_do_trivy: bool,
    /// Whether to run `Semgrep` scan on AUR packages.
    pub scan_do_semgrep: bool,
    /// Whether to run `ShellCheck` scan on AUR packages.
    pub scan_do_shellcheck: bool,
    /// Whether to run `VirusTotal` scan on AUR packages.
    pub scan_do_virustotal: bool,
    /// Whether to run custom scan on AUR packages.
    pub scan_do_custom: bool,
    /// Whether to run Sleuth scan on AUR packages.
    pub scan_do_sleuth: bool,
    /// Comma-separated `ShellCheck` rule IDs to pass as `--exclude` when running PKGBUILD static checks.
    /// Empty means `ShellCheck` runs without `--exclude`.
    pub pkgbuild_shellcheck_exclude: String,
    /// When true, the PKGBUILD details pane may show the expandable `Raw output:` block from static checks.
    /// When false, findings and status still show; raw command output is hidden. Defaults to false.
    pub pkgbuild_checks_show_raw_output: bool,
    /// Whether to start the app in News mode (true) or Package mode (false).
    pub start_in_news: bool,
    /// Whether to show Arch news items in the News view.
    pub news_filter_show_arch_news: bool,
    /// Whether to show security advisories in the News view.
    pub news_filter_show_advisories: bool,
    /// Whether to show installed package update items in the News view.
    pub news_filter_show_pkg_updates: bool,
    /// Whether to show AUR package update items in the News view.
    pub news_filter_show_aur_updates: bool,
    /// Whether to show installed AUR comment items in the News view.
    pub news_filter_show_aur_comments: bool,
    /// Whether to restrict advisories to installed packages in the News view.
    pub news_filter_installed_only: bool,
    /// Maximum age of news items in days (None = unlimited).
    pub news_max_age_days: Option<u32>,
    /// Whether startup news popup setup has been completed.
    pub startup_news_configured: bool,
    /// Whether to show Arch news in startup news popup.
    pub startup_news_show_arch_news: bool,
    /// Whether to show security advisories in startup news popup.
    pub startup_news_show_advisories: bool,
    /// Whether to show AUR updates in startup news popup.
    pub startup_news_show_aur_updates: bool,
    /// Whether to show AUR comments in startup news popup.
    pub startup_news_show_aur_comments: bool,
    /// Whether to show official package updates in startup news popup.
    pub startup_news_show_pkg_updates: bool,
    /// Maximum age of news items in days for startup news popup (None = unlimited).
    pub startup_news_max_age_days: Option<u32>,
    /// How many days to keep Arch news and advisories cached on disk.
    /// Default is 7 days. Helps reduce network requests on startup.
    pub news_cache_ttl_days: u32,
    /// Visual marker style for packages added to Install/Remove/Downgrade lists.
    pub package_marker: PackageMarker,
    /// Symbol used to mark a news item as read in the News modal.
    pub news_read_symbol: String,
    /// Symbol used to mark a news item as unread in the News modal.
    pub news_unread_symbol: String,
    /// Preferred terminal binary name to spawn for shell commands (e.g., "alacritty", "kitty", "gnome-terminal").
    /// When empty, Pacsea auto-detects from available terminals.
    pub preferred_terminal: String,
    /// When true, skip the Preflight modal and execute actions directly (install/remove/downgrade).
    /// Defaults to false to preserve the safer, review-first workflow.
    pub skip_preflight: bool,
    /// Locale code for translations (e.g., "de-DE", "en-US").
    /// Empty string means auto-detect from system locale.
    pub locale: String,
    /// Search input mode on startup.
    /// When false, starts in insert mode (default).
    /// When true, starts in normal mode.
    pub search_startup_mode: bool,
    /// Whether fuzzy search is enabled by default on startup.
    /// When false, uses normal substring search (default).
    /// When true, uses fuzzy matching (fzf-style).
    pub fuzzy_search: bool,
    /// Refresh interval in seconds for pacman -Qu and AUR helper checks.
    /// Default is 30 seconds. Set to a higher value to reduce resource usage on slow systems.
    pub updates_refresh_interval: u64,
    /// Filter mode for installed packages display.
    /// `LeafOnly` shows explicitly installed packages with no dependents.
    /// `AllExplicit` shows all explicitly installed packages.
    pub installed_packages_mode: crate::state::InstalledPackagesMode,
    /// Whether to fetch remote announcements from GitHub Gist.
    /// If `true`, fetches announcements from the configured Gist URL.
    /// If `false`, remote announcements are disabled (version announcements still show).
    pub get_announcement: bool,
    /// Whether to use passwordless sudo for install operations when available.
    /// If `false` (default), password prompt is always shown even if passwordless sudo is configured.
    /// If `true`, passwordless sudo is used when available, skipping the password prompt.
    /// This acts as an additional safety barrier requiring explicit opt-in.
    pub use_passwordless_sudo: bool,
    /// Privilege escalation tool selection mode.
    /// Controls which tool (sudo/doas) is used for privileged operations.
    /// `Auto` (default): prefer doas if available, fall back to sudo.
    /// `Sudo`: always use sudo. `Doas`: always use doas.
    pub privilege_mode: crate::logic::privilege::PrivilegeMode,
    /// Authentication mode for privilege escalation.
    /// Controls how Pacsea handles password/authentication before privileged operations.
    /// `Prompt` (default): Pacsea captures password only for stdin-capable tools (sudo).
    /// For doas, prompt mode is coerced to `Interactive` because doas cannot read stdin passwords.
    /// `PasswordlessOnly`: Skip prompt only when `{tool} -n true` succeeds.
    /// `Interactive`: Let the privilege tool handle auth directly (fingerprint via PAM, etc.).
    pub auth_mode: crate::logic::privilege::AuthMode,
    /// Whether to use the terminal's theme colors instead of theme.conf.
    /// If `true`, Pacsea queries the terminal for foreground/background colors via OSC 10/11.
    /// If `false` (default), uses theme.conf colors.
    /// Note: Terminal theme is also used automatically when theme.conf is missing/invalid
    /// and the terminal is on the supported list (alacritty, kitty, konsole, ghostty, xterm,
    /// gnome-terminal, xfce4-terminal, tilix, mate-terminal, wezterm-gui, `WezTerm`).
    pub use_terminal_theme: bool,
    /// Whether AUR voting via SSH is enabled.
    /// Requires an SSH key uploaded to the user's AUR account.
    pub aur_vote_enabled: bool,
    /// SSH connect timeout in seconds for AUR vote commands.
    pub aur_vote_ssh_timeout_seconds: u32,
    /// SSH binary path or name for AUR vote commands.
    /// Defaults to `"ssh"`. Override for non-standard SSH setups.
    pub aur_vote_ssh_command: String,
    /// Dynamic results-list toggles from `repos.conf` filter ids (canonical keys, see `repos` module).
    ///
    /// Keys match canonical `results_filter` tokens from repos.conf (e.g. `vendor_pkgs` for `results_filter_show_vendor_pkgs`).
    pub results_filter_toggles: HashMap<String, bool>,
}

impl Default for Settings {
    /// What: Provide the built-in baseline configuration for Pacsea settings.
    ///
    /// Inputs:
    /// - None.
    ///
    /// Output:
    /// - Returns a `Settings` instance populated with Pacsea's preferred defaults.
    ///
    /// Details:
    /// - Sets balanced pane layout percentages and enables all panes by default.
    /// - Enables all scan types and uses Catppuccin-inspired news glyphs.
    fn default() -> Self {
        Self {
            layout_left_pct: 20,
            layout_center_pct: 60,
            layout_right_pct: 20,
            main_pane_order: crate::state::DEFAULT_MAIN_PANE_ORDER,
            vertical_min_results: 3,
            vertical_max_results: 17,
            vertical_min_middle: 3,
            vertical_max_middle: 5,
            vertical_min_package_info: 3,
            app_dry_run_default: false,
            keymap: KeyMap::default(),
            sort_mode: crate::state::SortMode::RepoThenName,
            clipboard_suffix: "Check PKGBUILD and source for suspicious and malicious activities"
                .to_string(),
            show_recent_pane: true,
            show_install_pane: true,
            show_keybinds_footer: true,
            selected_countries: "Worldwide".to_string(),
            mirror_count: 20,
            virustotal_api_key: String::new(),
            scan_do_clamav: true,
            scan_do_trivy: true,
            scan_do_semgrep: true,
            scan_do_shellcheck: true,
            scan_do_virustotal: true,
            scan_do_custom: true,
            scan_do_sleuth: true,
            pkgbuild_shellcheck_exclude: "SC2034, SC2164, SC2148, SC2154".to_string(),
            pkgbuild_checks_show_raw_output: false,
            start_in_news: false,
            news_filter_show_arch_news: true,
            news_filter_show_advisories: true,
            news_filter_show_pkg_updates: true,
            news_filter_show_aur_updates: true,
            news_filter_show_aur_comments: true,
            news_filter_installed_only: false,
            news_max_age_days: Some(30),
            startup_news_configured: false,
            startup_news_show_arch_news: true,
            startup_news_show_advisories: true,
            startup_news_show_aur_updates: true,
            startup_news_show_aur_comments: true,
            startup_news_show_pkg_updates: true,
            startup_news_max_age_days: Some(7),
            news_cache_ttl_days: 7,
            package_marker: PackageMarker::Front,
            news_read_symbol: "".to_string(),
            news_unread_symbol: "".to_string(),
            preferred_terminal: String::new(),
            skip_preflight: false,
            locale: String::new(),        // Empty means auto-detect from system
            search_startup_mode: false,   // Default to insert mode
            fuzzy_search: false,          // Default to normal substring search
            updates_refresh_interval: 30, // Default to 30 seconds
            installed_packages_mode: crate::state::InstalledPackagesMode::LeafOnly,
            get_announcement: true, // Default to fetching remote announcements
            use_passwordless_sudo: false, // Default to always showing password prompt (safety barrier)
            privilege_mode: crate::logic::privilege::PrivilegeMode::Auto, // Default to auto-detect (prefer doas, fallback sudo)
            auth_mode: crate::logic::privilege::AuthMode::Prompt, // Default to Pacsea password modal
            use_terminal_theme: false, // Default to using theme.conf colors
            aur_vote_enabled: true,    // Enabled by default; requires SSH key configured on AUR
            aur_vote_ssh_timeout_seconds: 10,
            aur_vote_ssh_command: "ssh".to_string(),
            results_filter_toggles: HashMap::new(),
        }
    }
}

/// A single keyboard chord (modifiers + key).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct KeyChord {
    /// The key code (e.g., Char('a'), Enter, Esc).
    pub code: KeyCode,
    /// The modifier keys (e.g., Ctrl, Shift, Alt).
    pub mods: KeyModifiers,
}

impl KeyChord {
    /// Return a short display label such as "Ctrl+R", "F1", "Shift+Del", "+/ ?".
    #[must_use]
    pub fn label(&self) -> String {
        let mut parts: Vec<&'static str> = Vec::new();
        if self.mods.contains(KeyModifiers::CONTROL) {
            parts.push("Ctrl");
        }
        if self.mods.contains(KeyModifiers::ALT) {
            parts.push("Alt");
        }
        if self.mods.contains(KeyModifiers::SHIFT) {
            parts.push("Shift");
        }
        if self.mods.contains(KeyModifiers::SUPER) {
            parts.push("Super");
        }
        let key = match self.code {
            KeyCode::Char(ch) => {
                // Show uppercase character for display
                let up = ch.to_ascii_uppercase();
                if up == ' ' {
                    "Space".to_string()
                } else {
                    up.to_string()
                }
            }
            KeyCode::Enter => "Enter".to_string(),
            KeyCode::Esc => "Esc".to_string(),
            KeyCode::Backspace => "Backspace".to_string(),
            KeyCode::Tab => "Tab".to_string(),
            KeyCode::BackTab => "Shift+Tab".to_string(),
            KeyCode::Delete => "Del".to_string(),
            KeyCode::Insert => "Ins".to_string(),
            KeyCode::Home => "Home".to_string(),
            KeyCode::End => "End".to_string(),
            KeyCode::PageUp => "PgUp".to_string(),
            KeyCode::PageDown => "PgDn".to_string(),
            KeyCode::Up => "".to_string(),
            KeyCode::Down => "".to_string(),
            KeyCode::Left => "".to_string(),
            KeyCode::Right => "".to_string(),
            KeyCode::F(n) => format!("F{n}"),
            _ => "?".to_string(),
        };
        if parts.is_empty() || matches!(self.code, KeyCode::BackTab) {
            key
        } else {
            format!("{}+{key}", parts.join("+"))
        }
    }
}

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

    #[test]
    /// What: Ensure `KeyChord::label` renders user-facing text for modifier and key combinations.
    ///
    /// Inputs:
    /// - Sample chords covering control characters, space, function keys, Shift+BackTab, arrow keys, and multi-modifier chords.
    ///
    /// Output:
    /// - Labels such as `"Ctrl+R"`, `"Space"`, `"F5"`, `"Shift+Tab"`, arrow glyphs, and combined modifier strings.
    ///
    /// Details:
    /// - Protects the formatting logic that appears in keybinding help overlays.
    fn theme_keychord_label_variants() {
        let kc = KeyChord {
            code: KeyCode::Char('r'),
            mods: KeyModifiers::CONTROL,
        };
        assert_eq!(kc.label(), "Ctrl+R");

        let kc2 = KeyChord {
            code: KeyCode::Char(' '),
            mods: KeyModifiers::empty(),
        };
        assert_eq!(kc2.label(), "Space");

        let kc3 = KeyChord {
            code: KeyCode::F(5),
            mods: KeyModifiers::empty(),
        };
        assert_eq!(kc3.label(), "F5");

        let kc4 = KeyChord {
            code: KeyCode::BackTab,
            mods: KeyModifiers::SHIFT,
        };
        assert_eq!(kc4.label(), "Shift+Tab");

        let kc5 = KeyChord {
            code: KeyCode::Left,
            mods: KeyModifiers::empty(),
        };
        assert_eq!(kc5.label(), "");

        let kc6 = KeyChord {
            code: KeyCode::Char('x'),
            mods: KeyModifiers::ALT | KeyModifiers::SHIFT,
        };
        assert_eq!(kc6.label(), "Alt+Shift+X");
    }
}

/// Application key bindings.
/// Each action can have multiple chords.
#[derive(Clone, Debug)]
pub struct KeyMap {
    // Global
    /// Key chords to show help overlay.
    pub help_overlay: Vec<KeyChord>,
    /// Key chords to reload configuration.
    pub reload_config: Vec<KeyChord>,
    /// Key chords to exit the application.
    pub exit: Vec<KeyChord>,
    /// Global: Show/Hide PKGBUILD viewer
    pub show_pkgbuild: Vec<KeyChord>,
    /// Global: Show/Hide AUR comments viewer
    pub comments_toggle: Vec<KeyChord>,
    /// Global: Run PKGBUILD static checks in preview panel.
    pub run_pkgbuild_checks: Vec<KeyChord>,
    /// Global: Cycle PKGBUILD pane between body, `ShellCheck`, and `Namcap` sections.
    pub cycle_pkgbuild_sections: Vec<KeyChord>,
    /// Global: Change results sorting mode
    pub change_sort: Vec<KeyChord>,
    /// Key chords to move to next pane.
    pub pane_next: Vec<KeyChord>,
    /// Key chords to move focus left.
    pub pane_left: Vec<KeyChord>,
    /// Key chords to move focus right.
    pub pane_right: Vec<KeyChord>,
    /// Global: Toggle Config/Lists dropdown
    pub config_menu_toggle: Vec<KeyChord>,
    /// Global: Toggle Options dropdown
    pub options_menu_toggle: Vec<KeyChord>,
    /// Global: Toggle Panels dropdown
    pub panels_menu_toggle: Vec<KeyChord>,

    // Search
    /// Key chords to move selection up in search results.
    pub search_move_up: Vec<KeyChord>,
    /// Key chords to move selection down in search results.
    pub search_move_down: Vec<KeyChord>,
    /// Key chords to page up in search results.
    pub search_page_up: Vec<KeyChord>,
    /// Key chords to page down in search results.
    pub search_page_down: Vec<KeyChord>,
    /// Key chords to add package to install list.
    pub search_add: Vec<KeyChord>,
    /// Key chords to install selected package.
    pub search_install: Vec<KeyChord>,
    /// Key chords to move focus left from search pane.
    pub search_focus_left: Vec<KeyChord>,
    /// Key chords to move focus right from search pane.
    pub search_focus_right: Vec<KeyChord>,
    /// Key chords for backspace in search input.
    pub search_backspace: Vec<KeyChord>,
    /// Insert mode: clear entire search input (default: Shift+Del)
    pub search_insert_clear: Vec<KeyChord>,

    // Search normal mode
    /// Toggle Search normal mode on/off (works from both insert/normal)
    pub search_normal_toggle: Vec<KeyChord>,
    /// Enter insert mode while in Search normal mode
    pub search_normal_insert: Vec<KeyChord>,
    /// Normal mode: extend selection to the left (default: h)
    pub search_normal_select_left: Vec<KeyChord>,
    /// Normal mode: extend selection to the right (default: l)
    pub search_normal_select_right: Vec<KeyChord>,
    /// Normal mode: delete selected text (default: d)
    pub search_normal_delete: Vec<KeyChord>,
    /// Normal mode: clear entire search input (default: Shift+Del)
    pub search_normal_clear: Vec<KeyChord>,
    /// Normal mode: open Arch status page in browser (default: Shift+S)
    pub search_normal_open_status: Vec<KeyChord>,
    /// Normal mode: trigger Import packages dialog
    pub search_normal_import: Vec<KeyChord>,
    /// Normal mode: trigger Export Install list
    pub search_normal_export: Vec<KeyChord>,
    /// Normal mode: open Available Updates window
    pub search_normal_updates: Vec<KeyChord>,
    /// Toggle fuzzy search mode on/off
    pub toggle_fuzzy: Vec<KeyChord>,

    // Recent
    /// Key chords to move selection up in recent queries.
    pub recent_move_up: Vec<KeyChord>,
    /// Key chords to move selection down in recent queries.
    pub recent_move_down: Vec<KeyChord>,
    /// Key chords to find/search in recent queries.
    pub recent_find: Vec<KeyChord>,
    /// Key chords to use selected recent query.
    pub recent_use: Vec<KeyChord>,
    /// Key chords to add package from recent to install list.
    pub recent_add: Vec<KeyChord>,
    /// Key chords to move focus from recent to search pane.
    pub recent_to_search: Vec<KeyChord>,
    /// Key chords to move focus right from recent pane.
    pub recent_focus_right: Vec<KeyChord>,
    /// Remove one entry from Recent
    pub recent_remove: Vec<KeyChord>,
    /// Clear all entries in Recent
    pub recent_clear: Vec<KeyChord>,

    // Install
    /// Key chords to move selection up in install list.
    pub install_move_up: Vec<KeyChord>,
    /// Key chords to move selection down in install list.
    pub install_move_down: Vec<KeyChord>,
    /// Key chords to confirm and execute install/remove operation.
    pub install_confirm: Vec<KeyChord>,
    /// Key chords to remove item from install list.
    pub install_remove: Vec<KeyChord>,
    /// Key chords to clear install list.
    pub install_clear: Vec<KeyChord>,
    /// Key chords to find/search in install list.
    pub install_find: Vec<KeyChord>,
    /// Key chords to move focus from install to search pane.
    pub install_to_search: Vec<KeyChord>,
    /// Key chords to move focus left from install pane.
    pub install_focus_left: Vec<KeyChord>,

    // News modal
    /// Mark currently listed News items as read (without opening URL)
    pub news_mark_read: Vec<KeyChord>,
    /// Mark all listed News items as read
    pub news_mark_all_read: Vec<KeyChord>,
    /// Mark selected News Feed item as read.
    pub news_mark_read_feed: Vec<KeyChord>,
    /// Mark selected News Feed item as unread.
    pub news_mark_unread_feed: Vec<KeyChord>,
    /// Toggle read/unread for selected News Feed item.
    pub news_toggle_read_feed: Vec<KeyChord>,
}

/// Type alias for global key bindings tuple.
///
/// Contains 10 `Vec<KeyChord>` for `help_overlay`, `reload_config`, `exit`, `show_pkgbuild`, `comments_toggle`, `run_pkgbuild_checks`, `change_sort`, and pane navigation keys.
type GlobalKeys = (
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
);

/// Type alias for search key bindings tuple.
///
/// Contains 10 `Vec<KeyChord>` for search navigation, actions, and focus keys.
type SearchKeys = (
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
);

/// Type alias for search normal mode key bindings tuple.
///
/// Contains 10 `Vec<KeyChord>` for Vim-like normal mode search keys.
type SearchNormalKeys = (
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
);

/// Type alias for recent pane key bindings tuple.
///
/// Contains 9 `Vec<KeyChord>` for recent pane navigation and action keys.
type RecentKeys = (
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
);

/// Type alias for install list key bindings tuple.
///
/// Contains 8 `Vec<KeyChord>` for install list navigation and action keys.
type InstallKeys = (
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
    Vec<KeyChord>,
);

/// What: Create default global key bindings.
///
/// Inputs:
/// - `none`: Empty key modifiers
/// - `ctrl`: Control modifier
///
/// Output:
/// - Tuple of global key binding vectors
///
/// Details:
/// - Returns `help_overlay`, `reload_config`, `exit`, `show_pkgbuild`, `change_sort`, and pane navigation keys.
fn default_global_keys(none: KeyModifiers, ctrl: KeyModifiers) -> GlobalKeys {
    use KeyCode::{BackTab, Char, Left, Right, Tab};
    (
        vec![
            KeyChord {
                code: KeyCode::F(1),
                mods: none,
            },
            KeyChord {
                code: Char('?'),
                mods: none,
            },
        ],
        vec![KeyChord {
            code: Char('r'),
            mods: ctrl,
        }],
        vec![KeyChord {
            code: Char('c'),
            mods: ctrl,
        }],
        vec![KeyChord {
            code: Char('x'),
            mods: ctrl,
        }],
        vec![KeyChord {
            code: Char('t'),
            mods: ctrl,
        }],
        vec![KeyChord {
            code: Char('k'),
            mods: ctrl,
        }],
        vec![KeyChord {
            code: BackTab,
            mods: none,
        }],
        vec![KeyChord {
            code: Tab,
            mods: none,
        }],
        vec![KeyChord {
            code: Left,
            mods: none,
        }],
        vec![KeyChord {
            code: Right,
            mods: none,
        }],
    )
}

/// What: Create default dropdown toggle key bindings.
///
/// Inputs:
/// - `shift`: Shift modifier
///
/// Output:
/// - Tuple of dropdown toggle key binding vectors
///
/// Details:
/// - Returns `config_menu_toggle`, `options_menu_toggle`, and `panels_menu_toggle` keys.
fn default_dropdown_keys(shift: KeyModifiers) -> (Vec<KeyChord>, Vec<KeyChord>, Vec<KeyChord>) {
    use KeyCode::Char;
    (
        vec![KeyChord {
            code: Char('c'),
            mods: shift,
        }],
        vec![KeyChord {
            code: Char('o'),
            mods: shift,
        }],
        vec![KeyChord {
            code: Char('p'),
            mods: shift,
        }],
    )
}

/// What: Create default search key bindings.
///
/// Inputs:
/// - `none`: Empty key modifiers
///
/// Output:
/// - Tuple of search key binding vectors
///
/// Details:
/// - Returns all search-related key bindings for navigation, actions, and focus.
fn default_search_keys(none: KeyModifiers) -> SearchKeys {
    use KeyCode::{Backspace, Char, Down, Enter, Left, PageDown, PageUp, Right, Up};
    (
        vec![KeyChord {
            code: Up,
            mods: none,
        }],
        vec![KeyChord {
            code: Down,
            mods: none,
        }],
        vec![KeyChord {
            code: PageUp,
            mods: none,
        }],
        vec![KeyChord {
            code: PageDown,
            mods: none,
        }],
        vec![KeyChord {
            code: Char(' '),
            mods: none,
        }],
        vec![KeyChord {
            code: Enter,
            mods: none,
        }],
        vec![KeyChord {
            code: Left,
            mods: none,
        }],
        vec![KeyChord {
            code: Right,
            mods: none,
        }],
        vec![KeyChord {
            code: Backspace,
            mods: none,
        }],
        vec![KeyChord {
            code: KeyCode::Delete,
            mods: KeyModifiers::SHIFT,
        }],
    )
}

/// What: Create default search normal mode key bindings.
///
/// Inputs:
/// - `none`: Empty key modifiers
/// - `shift`: Shift modifier
///
/// Output:
/// - Tuple of search normal mode key binding vectors
///
/// Details:
/// - Returns all Vim-like normal mode key bindings for search.
fn default_search_normal_keys(none: KeyModifiers, shift: KeyModifiers) -> SearchNormalKeys {
    use KeyCode::{Char, Delete, Esc};
    (
        vec![KeyChord {
            code: Esc,
            mods: none,
        }],
        vec![KeyChord {
            code: Char('i'),
            mods: none,
        }],
        vec![KeyChord {
            code: Char('h'),
            mods: none,
        }],
        vec![KeyChord {
            code: Char('l'),
            mods: none,
        }],
        vec![KeyChord {
            code: Char('d'),
            mods: none,
        }],
        vec![KeyChord {
            code: Delete,
            mods: shift,
        }],
        vec![KeyChord {
            code: Char('s'),
            mods: shift,
        }],
        vec![KeyChord {
            code: Char('i'),
            mods: shift,
        }],
        vec![KeyChord {
            code: Char('e'),
            mods: shift,
        }],
        vec![KeyChord {
            code: Char('u'),
            mods: shift,
        }],
    )
}

/// What: Create default recent pane key bindings.
///
/// Inputs:
/// - `none`: Empty key modifiers
/// - `shift`: Shift modifier
///
/// Output:
/// - Tuple of recent pane key binding vectors
///
/// Details:
/// - Returns all recent pane navigation and action key bindings.
fn default_recent_keys(none: KeyModifiers, shift: KeyModifiers) -> RecentKeys {
    use KeyCode::{Char, Delete, Down, Enter, Esc, Right, Up};
    (
        vec![
            KeyChord {
                code: Char('k'),
                mods: none,
            },
            KeyChord {
                code: Up,
                mods: none,
            },
        ],
        vec![
            KeyChord {
                code: Char('j'),
                mods: none,
            },
            KeyChord {
                code: Down,
                mods: none,
            },
        ],
        vec![KeyChord {
            code: Char('/'),
            mods: none,
        }],
        vec![KeyChord {
            code: Enter,
            mods: none,
        }],
        vec![KeyChord {
            code: Char(' '),
            mods: none,
        }],
        vec![KeyChord {
            code: Esc,
            mods: none,
        }],
        vec![KeyChord {
            code: Right,
            mods: none,
        }],
        vec![
            KeyChord {
                code: Char('d'),
                mods: none,
            },
            KeyChord {
                code: Delete,
                mods: none,
            },
        ],
        vec![KeyChord {
            code: Delete,
            mods: shift,
        }],
    )
}

/// What: Create default install list key bindings.
///
/// Inputs:
/// - `none`: Empty key modifiers
/// - `shift`: Shift modifier
///
/// Output:
/// - Tuple of install list key binding vectors
///
/// Details:
/// - Returns all install list navigation and action key bindings.
fn default_install_keys(none: KeyModifiers, shift: KeyModifiers) -> InstallKeys {
    use KeyCode::{Char, Delete, Down, Enter, Esc, Left, Up};
    (
        vec![
            KeyChord {
                code: Char('k'),
                mods: none,
            },
            KeyChord {
                code: Up,
                mods: none,
            },
        ],
        vec![
            KeyChord {
                code: Char('j'),
                mods: none,
            },
            KeyChord {
                code: Down,
                mods: none,
            },
        ],
        vec![KeyChord {
            code: Enter,
            mods: none,
        }],
        vec![
            KeyChord {
                code: Delete,
                mods: none,
            },
            KeyChord {
                code: Char('d'),
                mods: none,
            },
        ],
        vec![KeyChord {
            code: Delete,
            mods: shift,
        }],
        vec![KeyChord {
            code: Char('/'),
            mods: none,
        }],
        vec![KeyChord {
            code: Esc,
            mods: none,
        }],
        vec![KeyChord {
            code: Left,
            mods: none,
        }],
    )
}

/// What: Create default news modal key bindings.
///
/// Inputs:
/// - `none`: Empty key modifiers
/// - `ctrl`: Control modifier
///
/// Output:
/// - Tuple of news modal key binding vectors
///
/// Details:
/// - Returns `news_mark_read` and `news_mark_all_read` key bindings.
fn default_news_keys(none: KeyModifiers, ctrl: KeyModifiers) -> (Vec<KeyChord>, Vec<KeyChord>) {
    use KeyCode::Char;
    (
        vec![KeyChord {
            code: Char('r'),
            mods: none,
        }],
        vec![KeyChord {
            code: Char('r'),
            mods: ctrl,
        }],
    )
}

/// What: Create default News Feed key bindings.
///
/// Inputs:
/// - `none`: Empty key modifiers
///
/// Output:
/// - Tuple of news feed key binding vectors
///
/// Details:
/// - Returns `news_mark_read_feed`, `news_mark_unread_feed`, and `news_toggle_read_feed`.
fn default_news_feed_keys(none: KeyModifiers) -> (Vec<KeyChord>, Vec<KeyChord>, Vec<KeyChord>) {
    use KeyCode::Char;
    (
        vec![KeyChord {
            code: Char('r'),
            mods: none,
        }],
        vec![KeyChord {
            code: Char('u'),
            mods: none,
        }],
        vec![KeyChord {
            code: Char('t'),
            mods: none,
        }],
    )
}

/// What: Build the default `KeyMap` by constructing it from helper functions.
///
/// Inputs:
/// - None (uses internal modifier constants).
///
/// Output:
/// - Returns a fully constructed `KeyMap` with all default key bindings.
///
/// Details:
/// - Consolidates all key binding construction to reduce data flow complexity.
/// - All key bindings are constructed inline within the struct initialization.
fn build_default_keymap() -> KeyMap {
    let none = KeyModifiers::empty();
    let ctrl = KeyModifiers::CONTROL;
    let shift = KeyModifiers::SHIFT;

    let global = default_global_keys(none, ctrl);
    let dropdown = default_dropdown_keys(shift);
    let search = default_search_keys(none);
    let search_normal = default_search_normal_keys(none, shift);
    let recent = default_recent_keys(none, shift);
    let install = default_install_keys(none, shift);
    let news = default_news_keys(none, ctrl);
    let news_feed = default_news_feed_keys(none);

    KeyMap {
        help_overlay: global.0,
        reload_config: global.1,
        exit: global.2,
        show_pkgbuild: global.3,
        comments_toggle: global.4,
        run_pkgbuild_checks: global.5,
        cycle_pkgbuild_sections: vec![KeyChord {
            code: KeyCode::Char('d'),
            mods: ctrl,
        }],
        change_sort: global.6,
        pane_next: global.7,
        pane_left: global.8,
        pane_right: global.9,
        config_menu_toggle: dropdown.0,
        options_menu_toggle: dropdown.1,
        panels_menu_toggle: dropdown.2,
        search_move_up: search.0,
        search_move_down: search.1,
        search_page_up: search.2,
        search_page_down: search.3,
        search_add: search.4,
        search_install: search.5,
        search_focus_left: search.6,
        search_focus_right: search.7,
        search_backspace: search.8,
        search_insert_clear: search.9,
        search_normal_toggle: search_normal.0,
        search_normal_insert: search_normal.1,
        search_normal_select_left: search_normal.2,
        search_normal_select_right: search_normal.3,
        search_normal_delete: search_normal.4,
        search_normal_clear: search_normal.5,
        search_normal_open_status: search_normal.6,
        search_normal_import: search_normal.7,
        search_normal_export: search_normal.8,
        search_normal_updates: search_normal.9,
        toggle_fuzzy: vec![KeyChord {
            code: KeyCode::Char('f'),
            mods: ctrl,
        }],
        recent_move_up: recent.0,
        recent_move_down: recent.1,
        recent_find: recent.2,
        recent_use: recent.3,
        recent_add: recent.4,
        recent_to_search: recent.5,
        recent_focus_right: recent.6,
        recent_remove: recent.7,
        recent_clear: recent.8,
        install_move_up: install.0,
        install_move_down: install.1,
        install_confirm: install.2,
        install_remove: install.3,
        install_clear: install.4,
        install_find: install.5,
        install_to_search: install.6,
        install_focus_left: install.7,
        news_mark_read: news.0,
        news_mark_all_read: news.1,
        news_mark_read_feed: news_feed.0,
        news_mark_unread_feed: news_feed.1,
        news_toggle_read_feed: news_feed.2,
    }
}

impl Default for KeyMap {
    /// What: Supply the default key bindings for Pacsea interactions.
    ///
    /// Inputs:
    /// - None.
    ///
    /// Output:
    /// - Returns a `KeyMap` prefilling chord vectors for global, search, recent, install, and news actions.
    ///
    /// Details:
    /// - Encodes human-friendly defaults such as `F1` for help and `Ctrl+R` to reload the configuration.
    /// - Provides multiple bindings for certain actions (e.g., `F1` and `?` for help).
    /// - Delegates to `build_default_keymap()` to reduce data flow complexity.
    fn default() -> Self {
        build_default_keymap()
    }
}