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
use ratatui::{
    Frame,
    prelude::Rect,
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Paragraph, Wrap},
};

use crate::i18n;
use crate::state::{AppState, Focus, RightPaneFocus};
use crate::theme::{KeyChord, Theme, theme};

/// What: Determine label color based on focus state.
///
/// Inputs:
/// - `is_focused`: Whether the section is currently focused
///
/// Output:
/// - Returns mauve if focused, overlay1 otherwise.
///
/// Details:
/// - Used to highlight active sections in the footer.
const fn get_label_color(is_focused: bool, th: &Theme) -> ratatui::style::Color {
    if is_focused { th.mauve } else { th.overlay1 }
}

/// What: Add a single keybind entry to spans if key exists.
///
/// Inputs:
/// - `spans`: Mutable reference to spans vector to append to
/// - `key_opt`: Optional key chord
/// - `key_style`: Style for the key label
/// - `text`: Text to display after the key
/// - `sep_style`: Style for the separator
///
/// Output:
/// - Appends keybind entry to spans if key exists.
///
/// Details:
/// - Only adds entry if key is present, reducing conditional complexity.
fn add_keybind_entry(
    spans: &mut Vec<Span<'static>>,
    key_opt: Option<&KeyChord>,
    key_style: Style,
    text: &str,
    sep_style: Style,
) {
    if let Some(k) = key_opt {
        spans.extend([
            Span::styled(format!("[{}]", k.label()), key_style),
            Span::raw(format!(" {text}")),
            Span::styled("  |  ", sep_style),
        ]);
    }
}

/// What: Add a dual keybind entry (up/down) to spans if both keys exist.
///
/// Inputs:
/// - `spans`: Mutable reference to spans vector to append to
/// - `up_opt`: Optional up key chord
/// - `down_opt`: Optional down key chord
/// - `key_style`: Style for the key label
/// - `text`: Text to display after the keys
/// - `sep_style`: Style for the separator
///
/// Output:
/// - Appends dual keybind entry to spans if both keys exist.
///
/// Details:
/// - Only adds entry if both keys are present.
fn add_dual_keybind_entry(
    spans: &mut Vec<Span<'static>>,
    up_opt: Option<&KeyChord>,
    down_opt: Option<&KeyChord>,
    key_style: Style,
    text: &str,
    sep_style: Style,
) {
    if let (Some(up), Some(dn)) = (up_opt, down_opt) {
        spans.extend([
            Span::styled(format!("[{} / {}]", up.label(), dn.label()), key_style),
            Span::raw(format!(" {text}")),
            Span::styled("  |  ", sep_style),
        ]);
    }
}

/// What: Add multiple keybind labels joined by " / " to spans.
///
/// Inputs:
/// - `spans`: Mutable reference to spans vector to append to
/// - `keys`: Vector of key chords
/// - `key_style`: Style for the key label
/// - `text`: Text to display after the keys
/// - `sep_style`: Style for the separator
///
/// Output:
/// - Appends multi-keybind entry to spans if keys exist.
///
/// Details:
/// - Only adds entry if keys vector is not empty.
fn add_multi_keybind_entry(
    spans: &mut Vec<Span<'static>>,
    keys: &[KeyChord],
    key_style: Style,
    text: &str,
    sep_style: Style,
) {
    if !keys.is_empty() {
        let keys_str = keys
            .iter()
            .map(KeyChord::label)
            .collect::<Vec<_>>()
            .join(" / ");
        spans.extend([
            Span::styled(format!("[{keys_str}]"), key_style),
            Span::raw(format!(" {text}")),
            Span::styled("  |  ", sep_style),
        ]);
    }
}

/// What: Build section header span with label and color.
///
/// Inputs:
/// - `label`: Section label text
/// - `label_color`: Color for the label
///
/// Output:
/// - Returns vector with styled label span and spacing.
///
/// Details:
/// - Creates bold, colored label followed by spacing.
fn build_section_header(label: String, label_color: ratatui::style::Color) -> Vec<Span<'static>> {
    vec![
        Span::styled(
            label,
            Style::default()
                .fg(label_color)
                .add_modifier(Modifier::BOLD),
        ),
        Span::raw("  "),
    ]
}

/// What: Build GLOBALS section spans.
///
/// Inputs:
/// - `app`: Application state
/// - `th`: Theme
/// - `key_style`: Style for keys
/// - `sep_style`: Style for separator
///
/// Output:
/// - Returns vector of spans for GLOBALS section.
///
/// Details:
/// - Includes exit, help, reload theme, show pkgbuild, show comments, change sort, and normal mode toggle.
fn build_globals_section(
    app: &AppState,
    th: &Theme,
    key_style: Style,
    sep_style: Style,
) -> Vec<Span<'static>> {
    let km = &app.keymap;
    let mut spans = build_section_header(
        format!("{}  ", i18n::t(app, "app.headings.globals")),
        th.overlay1,
    );

    add_keybind_entry(
        &mut spans,
        km.exit.first(),
        key_style,
        &i18n::t(app, "app.actions.exit"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.help_overlay.first(),
        key_style,
        &i18n::t(app, "app.actions.help"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.reload_config.first(),
        key_style,
        &i18n::t(app, "app.actions.reload_config"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.show_pkgbuild.first(),
        key_style,
        &i18n::t(app, "app.actions.show_hide_pkgbuild"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.comments_toggle.first(),
        key_style,
        &i18n::t(app, "app.actions.show_hide_comments"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.run_pkgbuild_checks.first(),
        key_style,
        &i18n::t(app, "app.actions.run_pkgbuild_checks"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.cycle_pkgbuild_sections.first(),
        key_style,
        &i18n::t(app, "app.actions.cycle_pkgbuild_sections"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.change_sort.first(),
        key_style,
        &i18n::t(app, "app.actions.change_sort_mode"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.search_normal_toggle.first(),
        key_style,
        &i18n::t(app, "app.actions.insert_mode"),
        sep_style,
    );

    spans
}

/// What: Build SEARCH section spans.
///
/// Inputs:
/// - `app`: Application state
/// - `th`: Theme
/// - `key_style`: Style for keys
/// - `sep_style`: Style for separator
///
/// Output:
/// - Returns vector of spans for SEARCH section.
///
/// Details:
/// - Includes move, page, add, and install actions.
fn build_search_section(
    app: &AppState,
    th: &Theme,
    key_style: Style,
    sep_style: Style,
) -> Vec<Span<'static>> {
    let km = &app.keymap;
    let search_label_color = get_label_color(matches!(app.focus, Focus::Search), th);
    let mut spans = build_section_header(
        format!("{}   ", i18n::t(app, "app.headings.search")),
        search_label_color,
    );

    add_dual_keybind_entry(
        &mut spans,
        km.search_move_up.first(),
        km.search_move_down.first(),
        key_style,
        &i18n::t(app, "app.actions.move"),
        sep_style,
    );
    add_dual_keybind_entry(
        &mut spans,
        km.search_page_up.first(),
        km.search_page_down.first(),
        key_style,
        &i18n::t(app, "app.actions.move_page"),
        sep_style,
    );
    let add_text = if app.installed_only_mode {
        i18n::t(app, "app.actions.add_to_remove")
    } else {
        i18n::t(app, "app.actions.add_to_install")
    };
    add_keybind_entry(
        &mut spans,
        km.search_add.first(),
        key_style,
        &add_text,
        sep_style,
    );
    if app.installed_only_mode {
        spans.extend([
            Span::styled("[Ctrl+Space]", key_style),
            Span::raw(format!(" {}", i18n::t(app, "app.actions.add_to_downgrade"))),
            Span::styled("  |  ", sep_style),
        ]);
    }
    add_keybind_entry(
        &mut spans,
        km.search_install.first(),
        key_style,
        &i18n::t(app, "app.actions.install"),
        sep_style,
    );
    // Show toggle based on search mode: toggle_normal label when fuzzy is active, toggle_fuzzy label when normal is active
    // Both use the same keybind (toggle_fuzzy) but with different labels
    if app.fuzzy_search_enabled {
        add_keybind_entry(
            &mut spans,
            km.toggle_fuzzy.first(),
            key_style,
            &i18n::t(app, "app.modals.help.key_labels.toggle_normal"),
            sep_style,
        );
    } else {
        add_keybind_entry(
            &mut spans,
            km.toggle_fuzzy.first(),
            key_style,
            &i18n::t(app, "app.modals.help.key_labels.toggle_fuzzy"),
            sep_style,
        );
    }
    // Show clear keybind based on mode: insert_clear for insert mode, normal_clear for normal mode
    if app.search_normal_mode {
        add_keybind_entry(
            &mut spans,
            km.search_normal_clear.first(),
            key_style,
            &i18n::t(app, "app.modals.help.key_labels.clear_input"),
            sep_style,
        );
    } else {
        add_keybind_entry(
            &mut spans,
            km.search_insert_clear.first(),
            key_style,
            &i18n::t(app, "app.modals.help.key_labels.clear_input"),
            sep_style,
        );
    }
    spans.extend([
        Span::styled("[Ctrl+E]", key_style),
        Span::raw(" Toggle AUR vote for selected package"),
        Span::styled("  |  ", sep_style),
    ]);

    spans
}

/// What: Build common right-pane action spans (install/downgrade/remove).
///
/// Inputs:
/// - `app`: Application state
/// - `label`: Section label
/// - `label_color`: Color for label
/// - `confirm_text`: Text for confirm action
/// - `key_style`: Style for keys
/// - `sep_style`: Style for separator
///
/// Output:
/// - Returns vector of spans for right-pane section.
///
/// Details:
/// - Includes move, confirm, remove, clear, find, and go to search actions.
fn build_right_pane_spans(
    app: &AppState,
    label: String,
    label_color: ratatui::style::Color,
    confirm_text: &str,
    key_style: Style,
    sep_style: Style,
) -> Vec<Span<'static>> {
    let km = &app.keymap;
    let mut spans = build_section_header(label, label_color);

    add_dual_keybind_entry(
        &mut spans,
        km.install_move_up.first(),
        km.install_move_down.first(),
        key_style,
        &i18n::t(app, "app.actions.move"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.install_confirm.first(),
        key_style,
        confirm_text,
        sep_style,
    );
    add_multi_keybind_entry(
        &mut spans,
        &km.install_remove,
        key_style,
        &i18n::t(app, "app.actions.remove_from_list"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.install_clear.first(),
        key_style,
        &i18n::t(app, "app.actions.clear"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.install_find.first(),
        key_style,
        &i18n::t(app, "app.details.footer.search_hint"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.install_to_search.first(),
        key_style,
        &i18n::t(app, "app.actions.go_to_search"),
        sep_style,
    );

    spans
}

/// What: Build INSTALL section spans (or DOWNGRADE/REMOVE in installed-only mode).
///
/// Inputs:
/// - `app`: Application state
/// - `th`: Theme
/// - `key_style`: Style for keys
/// - `sep_style`: Style for separator
///
/// Output:
/// - Returns tuple: (install line option, split lines option for downgrade/remove).
///
/// Details:
/// - Returns split lines in installed-only mode, single install line otherwise.
fn build_install_section(
    app: &AppState,
    th: &Theme,
    key_style: Style,
    sep_style: Style,
) -> (
    Option<Line<'static>>,
    Option<(Line<'static>, Line<'static>)>,
) {
    if app.installed_only_mode {
        let downgrade_color = get_label_color(
            matches!(app.focus, Focus::Install)
                && matches!(app.right_pane_focus, RightPaneFocus::Downgrade),
            th,
        );
        let remove_color = get_label_color(
            matches!(app.focus, Focus::Install)
                && matches!(app.right_pane_focus, RightPaneFocus::Remove),
            th,
        );

        let d_spans = build_right_pane_spans(
            app,
            format!("{}:", i18n::t(app, "app.headings.downgrade")),
            downgrade_color,
            &i18n::t(app, "app.details.footer.confirm_downgrade"),
            key_style,
            sep_style,
        );
        let r_spans = build_right_pane_spans(
            app,
            format!("{}:   ", i18n::t(app, "app.headings.remove")),
            remove_color,
            &i18n::t(app, "app.details.footer.confirm_removal"),
            key_style,
            sep_style,
        );
        (None, Some((Line::from(d_spans), Line::from(r_spans))))
    } else {
        let install_color = get_label_color(matches!(app.focus, Focus::Install), th);
        let i_spans = build_right_pane_spans(
            app,
            format!("{}  ", i18n::t(app, "app.headings.install")),
            install_color,
            &i18n::t(app, "app.details.footer.confirm_installation"),
            key_style,
            sep_style,
        );
        (Some(Line::from(i_spans)), None)
    }
}

/// What: Build RECENT section spans.
///
/// Inputs:
/// - `app`: Application state
/// - `th`: Theme
/// - `key_style`: Style for keys
/// - `sep_style`: Style for separator
///
/// Output:
/// - Returns vector of spans for RECENT section.
///
/// Details:
/// - Includes move, use, remove, clear, add, find, and go to search actions.
fn build_recent_section(
    app: &AppState,
    th: &Theme,
    key_style: Style,
    sep_style: Style,
) -> Vec<Span<'static>> {
    let km = &app.keymap;
    let recent_label_color = get_label_color(matches!(app.focus, Focus::Recent), th);
    let mut spans = build_section_header(
        format!("{}   ", i18n::t(app, "app.headings.recent")),
        recent_label_color,
    );

    add_dual_keybind_entry(
        &mut spans,
        km.recent_move_up.first(),
        km.recent_move_down.first(),
        key_style,
        &i18n::t(app, "app.actions.move"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.recent_use.first(),
        key_style,
        &i18n::t(app, "app.actions.add_to_search"),
        sep_style,
    );
    add_multi_keybind_entry(
        &mut spans,
        &km.recent_remove,
        key_style,
        &i18n::t(app, "app.actions.remove_from_list"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.recent_clear.first(),
        key_style,
        &i18n::t(app, "app.actions.clear"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.recent_add.first(),
        key_style,
        &i18n::t(app, "app.actions.add_first_match_to_install"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.recent_find.first(),
        key_style,
        &i18n::t(app, "app.actions.search_hint_enter_next_esc_cancel"),
        sep_style,
    );
    add_keybind_entry(
        &mut spans,
        km.recent_to_search.first(),
        key_style,
        &i18n::t(app, "app.actions.go_to_search"),
        sep_style,
    );

    spans
}

/// What: Build Normal Mode section spans.
///
/// Inputs:
/// - `app`: Application state
/// - `th`: Theme
/// - `key_style`: Style for keys
///
/// Output:
/// - Returns vector of lines for Normal Mode section (may be empty).
///
/// Details:
/// - Returns two lines: base normal mode help and optional menus/import-export line.
fn build_normal_mode_section(app: &AppState, th: &Theme, key_style: Style) -> Vec<Line<'static>> {
    let km = &app.keymap;
    let mut lines = Vec::new();

    let label =
        |v: &Vec<KeyChord>, def: &str| v.first().map_or_else(|| def.to_string(), KeyChord::label);
    let toggle_label = label(&km.search_normal_toggle, "Esc");
    let insert_label = label(&km.search_normal_insert, "i");
    let clear_label = label(&km.search_normal_clear, "Shift+Del");

    let normal_mode_label = i18n::t(app, "app.modals.help.normal_mode.label");
    let insert_mode_text = i18n::t(app, "app.modals.help.normal_mode.insert_mode");
    let move_text = i18n::t(app, "app.modals.help.normal_mode.move");
    let page_text = i18n::t(app, "app.modals.help.normal_mode.page");
    let clear_input_text = i18n::t(app, "app.modals.help.normal_mode.clear_input");

    let n_spans: Vec<Span> = vec![
        Span::styled(
            normal_mode_label,
            Style::default().fg(th.mauve).add_modifier(Modifier::BOLD),
        ),
        Span::raw("  "),
        Span::styled(format!("[{toggle_label} / {insert_label}]"), key_style),
        Span::raw(insert_mode_text),
        Span::styled("[j / k]", key_style),
        Span::raw(move_text),
        Span::styled("[Ctrl+d / Ctrl+u]", key_style),
        Span::raw(page_text),
        Span::styled(format!("[{clear_label}]"), key_style),
        Span::raw(clear_input_text),
    ];
    lines.push(Line::from(n_spans));

    // Second line: menus and import/export (if any)
    let mut second_line_spans: Vec<Span> = Vec::new();

    if !km.config_menu_toggle.is_empty()
        || !km.options_menu_toggle.is_empty()
        || !km.panels_menu_toggle.is_empty()
    {
        let open_config_list_text = i18n::t(app, "app.modals.help.normal_mode.open_config_list");
        let open_options_text = i18n::t(app, "app.modals.help.normal_mode.open_options");
        let open_panels_text = i18n::t(app, "app.modals.help.normal_mode.open_panels");

        if let Some(k) = km.config_menu_toggle.first() {
            second_line_spans.push(Span::raw("  •  "));
            second_line_spans.push(Span::styled(format!("[{}]", k.label()), key_style));
            second_line_spans.push(Span::raw(open_config_list_text));
        }
        if let Some(k) = km.options_menu_toggle.first() {
            second_line_spans.push(Span::raw("  •  "));
            second_line_spans.push(Span::styled(format!("[{}]", k.label()), key_style));
            second_line_spans.push(Span::raw(open_options_text));
        }
        if let Some(k) = km.panels_menu_toggle.first() {
            second_line_spans.push(Span::raw("  •  "));
            second_line_spans.push(Span::styled(format!("[{}]", k.label()), key_style));
            second_line_spans.push(Span::raw(open_panels_text));
        }
    }

    if !app.installed_only_mode
        && (!km.search_normal_import.is_empty()
            || !km.search_normal_export.is_empty()
            || !km.search_normal_updates.is_empty())
    {
        let install_list_text = i18n::t(app, "app.modals.help.normal_mode.install_list");
        let import_text = i18n::t(app, "app.modals.help.normal_mode.import");
        let export_text = i18n::t(app, "app.modals.help.normal_mode.export");
        let updates_text = i18n::t(app, "app.modals.help.normal_mode.updates");

        second_line_spans.push(Span::raw(install_list_text));
        if let Some(k) = km.search_normal_import.first() {
            second_line_spans.push(Span::styled(format!("[{}]", k.label()), key_style));
            second_line_spans.push(Span::raw(import_text));
            if let Some(k2) = km.search_normal_export.first() {
                second_line_spans.push(Span::raw(", "));
                second_line_spans.push(Span::styled(format!("[{}]", k2.label()), key_style));
                second_line_spans.push(Span::raw(export_text));
            }
            if let Some(k3) = km.search_normal_updates.first() {
                second_line_spans.push(Span::raw(", "));
                second_line_spans.push(Span::styled(format!("[{}]", k3.label()), key_style));
                second_line_spans.push(Span::raw(updates_text));
            }
        } else if let Some(k) = km.search_normal_export.first() {
            second_line_spans.push(Span::styled(format!("[{}]", k.label()), key_style));
            second_line_spans.push(Span::raw(export_text));
            if let Some(k2) = km.search_normal_updates.first() {
                second_line_spans.push(Span::raw(", "));
                second_line_spans.push(Span::styled(format!("[{}]", k2.label()), key_style));
                second_line_spans.push(Span::raw(updates_text));
            }
        } else if let Some(k) = km.search_normal_updates.first() {
            second_line_spans.push(Span::styled(format!("[{}]", k.label()), key_style));
            second_line_spans.push(Span::raw(updates_text));
        }
    }

    if !second_line_spans.is_empty() {
        lines.push(Line::from(second_line_spans));
    }

    lines
}

/// What: Render the keybind help footer inside the Package Info pane.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Application state providing focus, keymap, and pane flags
/// - `bottom_container`: Overall rect covering the package info + footer region
/// - `help_h`: Calculated footer height in rows
///
/// Output:
/// - Draws footer content and updates no state beyond rendering; uses rect geometry only locally.
///
/// Details:
/// - Highlights sections based on focused pane, reflects installed-only mode splits, and emits
///   optional Normal Mode help when active; fills background to avoid bleed-through.
pub fn render_footer(f: &mut Frame, app: &AppState, bottom_container: Rect, help_h: u16) {
    let th = theme();

    // Footer occupies the bottom rows of the bottom container using reserved height above
    let footer_container = bottom_container;
    if footer_container.height > help_h + 2 {
        let x = footer_container.x + 1; // inside border
        let y_top = footer_container.y + footer_container.height.saturating_sub(help_h);
        let w = footer_container.width.saturating_sub(2);
        let h = help_h;
        let footer_rect = Rect {
            x,
            y: y_top,
            width: w,
            height: h,
        };

        let key_style = Style::default()
            .fg(th.text)
            .bg(th.surface2)
            .add_modifier(Modifier::BOLD);
        let sep_style = Style::default().fg(th.overlay2);

        // Build all sections
        let g_spans = build_globals_section(app, &th, key_style, sep_style);
        let s_spans = build_search_section(app, &th, key_style, sep_style);
        let (right_lines_install, right_lines_split) =
            build_install_section(app, &th, key_style, sep_style);
        let r_spans = build_recent_section(app, &th, key_style, sep_style);

        // Assemble lines based on focus
        let mut lines: Vec<Line<'static>> = vec![Line::from(g_spans)];
        if matches!(app.focus, Focus::Search) {
            lines.push(Line::from(s_spans));
        }
        if matches!(app.focus, Focus::Install) {
            if let Some(i_line) = right_lines_install {
                lines.push(i_line);
            }
            if let Some((d_line, rm_line)) = right_lines_split {
                match app.right_pane_focus {
                    RightPaneFocus::Downgrade => lines.push(d_line),
                    RightPaneFocus::Remove => lines.push(rm_line),
                    RightPaneFocus::Install => {}
                }
            }
        }
        if matches!(app.focus, Focus::Recent) {
            lines.push(Line::from(r_spans));
        }
        if matches!(app.focus, Focus::Search) && app.search_normal_mode {
            lines.extend(build_normal_mode_section(app, &th, key_style));
        }

        // Bottom-align the content within the reserved footer area
        // Use full footer height (including buffer) for the keybind viewer
        let content_lines: u16 = h;
        let content_y = y_top;
        let content_rect = Rect {
            x,
            y: content_y,
            width: w,
            height: content_lines,
        };
        // Fill the whole reserved footer area with a uniform background
        f.render_widget(
            Block::default().style(Style::default().bg(th.base)),
            footer_rect,
        );
        let footer = Paragraph::new(lines)
            .style(Style::default().fg(th.subtext1))
            .wrap(Wrap { trim: true });
        f.render_widget(footer, content_rect);
    }
}

/// What: Build the lines for the news keybind footer (global, navigation, menus, news actions, optional normal mode).
///
/// Inputs:
/// - `app`: Application state providing keymap, focus, and mode flags
/// - `th`: Theme for styling
///
/// Output:
/// - Vector of lines ready to render in the news footer
///
/// Details:
/// - Always includes Global/Nav/Menus/News actions on the first line
/// - Adds Normal Mode helper lines when search is focused and normal mode is active
fn build_news_footer_lines(app: &AppState, th: &Theme) -> Vec<Line<'static>> {
    let key_style = Style::default()
        .fg(th.text)
        .bg(th.surface2)
        .add_modifier(Modifier::BOLD);
    let sep_style = Style::default().fg(th.overlay2);

    let mut lines: Vec<Line<'static>> = Vec::new();

    // Line 1: Global/help
    let mut global_spans: Vec<Span<'static>> = Vec::new();
    global_spans.extend(build_section_header("Global".to_string(), th.overlay1));
    add_keybind_entry(
        &mut global_spans,
        app.keymap.help_overlay.first(),
        key_style,
        "Help",
        sep_style,
    );
    add_keybind_entry(
        &mut global_spans,
        app.keymap.exit.first(),
        key_style,
        "Exit",
        sep_style,
    );
    lines.push(Line::from(global_spans));

    // Line 2: Navigation and Menus
    let mut nav_menus_spans: Vec<Span<'static>> = Vec::new();
    nav_menus_spans.extend(build_section_header("Nav".to_string(), th.overlay1));
    add_dual_keybind_entry(
        &mut nav_menus_spans,
        app.keymap.search_move_up.first(),
        app.keymap.search_move_down.first(),
        key_style,
        "Move",
        sep_style,
    );
    add_dual_keybind_entry(
        &mut nav_menus_spans,
        app.keymap.search_page_up.first(),
        app.keymap.search_page_down.first(),
        key_style,
        "Page",
        sep_style,
    );
    nav_menus_spans.extend(build_section_header("Menus".to_string(), th.overlay1));
    add_keybind_entry(
        &mut nav_menus_spans,
        app.keymap.options_menu_toggle.first(),
        key_style,
        "Options",
        sep_style,
    );
    add_keybind_entry(
        &mut nav_menus_spans,
        app.keymap.panels_menu_toggle.first(),
        key_style,
        "Panels",
        sep_style,
    );
    add_keybind_entry(
        &mut nav_menus_spans,
        app.keymap.config_menu_toggle.first(),
        key_style,
        "Config/Lists",
        sep_style,
    );
    lines.push(Line::from(nav_menus_spans));

    // Line 3: News actions
    let mut news_spans: Vec<Span<'static>> = Vec::new();
    news_spans.extend(build_section_header("News".to_string(), th.overlay1));
    add_multi_keybind_entry(
        &mut news_spans,
        &app.keymap.news_mark_read_feed,
        key_style,
        "Mark read",
        sep_style,
    );
    add_multi_keybind_entry(
        &mut news_spans,
        &app.keymap.news_mark_unread_feed,
        key_style,
        "Mark unread",
        sep_style,
    );
    add_multi_keybind_entry(
        &mut news_spans,
        &app.keymap.news_toggle_read_feed,
        key_style,
        "Toggle read",
        sep_style,
    );
    lines.push(Line::from(news_spans));

    // Normal mode specific help when search pane is focused
    if matches!(app.focus, Focus::Search) {
        if app.search_normal_mode {
            lines.extend(build_normal_mode_section(app, th, key_style));
        } else {
            let label = |v: &Vec<KeyChord>, def: &str| {
                v.first().map_or_else(|| def.to_string(), KeyChord::label)
            };
            let toggle_label = label(&app.keymap.search_normal_toggle, "Esc");
            let clear_label = label(&app.keymap.search_insert_clear, "Shift+Del");
            let fuzzy_label = label(&app.keymap.toggle_fuzzy, "Ctrl+f");
            let change_sort_label = label(&app.keymap.change_sort, "Shift+Tab");

            let mut insert_spans = build_section_header(
                i18n::t(app, "app.modals.help.normal_mode.insert_mode"),
                th.mauve,
            );
            insert_spans.push(Span::styled(format!("[{toggle_label}]"), key_style));
            insert_spans.push(Span::raw(format!(
                " {}",
                i18n::t(app, "app.modals.help.key_labels.toggle_normal")
            )));
            insert_spans.push(Span::styled("  |  ", sep_style));

            insert_spans.push(Span::styled(format!("[{clear_label}]"), key_style));
            insert_spans.push(Span::raw(format!(
                " {}",
                i18n::t(app, "app.modals.help.key_labels.clear_input")
            )));
            insert_spans.push(Span::styled("  |  ", sep_style));

            insert_spans.push(Span::styled(format!("[{fuzzy_label}]"), key_style));
            insert_spans.push(Span::raw(format!(
                " {}",
                i18n::t(app, "app.modals.help.key_labels.toggle_fuzzy")
            )));
            insert_spans.push(Span::styled("  |  ", sep_style));

            insert_spans.push(Span::styled(format!("[{change_sort_label}]"), key_style));
            insert_spans.push(Span::raw(format!(
                " {}",
                i18n::t(app, "app.actions.change_sort_mode")
            )));

            lines.push(Line::from(insert_spans));
        }
    }

    lines
}

/// What: Render a simplified keybind footer for news management mode.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Application state (keymap, focus)
/// - `area`: Full area of the news details pane
/// - `footer_height`: Reserved footer height
///
/// Output:
/// - Draws news-relevant keybinds (navigation, menus, mark read) along the bottom.
pub fn render_news_footer(f: &mut Frame, app: &AppState, area: Rect, footer_height: u16) {
    if footer_height == 0 || area.height < footer_height {
        return;
    }
    let th = theme();
    let lines = build_news_footer_lines(app, &th);
    if lines.is_empty() {
        return;
    }

    let footer_rect = Rect {
        x: area.x + 1, // inside border
        y: area
            .y
            .saturating_add(area.height.saturating_sub(footer_height)),
        width: area.width.saturating_sub(2),
        height: footer_height,
    };
    // Fill the whole reserved footer area with a uniform background
    f.render_widget(
        Block::default().style(Style::default().bg(th.base)),
        Rect {
            x: area.x,
            y: footer_rect.y,
            width: area.width,
            height: footer_height,
        },
    );
    let paragraph = Paragraph::new(lines)
        .style(Style::default().fg(th.subtext1))
        .wrap(Wrap { trim: true });
    f.render_widget(paragraph, footer_rect);
}

/// What: Compute the preferred height (in rows) for the news keybind footer.
///
/// Inputs:
/// - `app`: Application state (determines whether normal mode lines are included)
///
/// Output:
/// - Height in rows needed to render the footer without truncation (minimum 1 when enabled)
pub fn news_footer_height(app: &AppState) -> u16 {
    let th = theme();
    let line_count = build_news_footer_lines(app, &th).len();
    u16::try_from(line_count.max(1)).unwrap_or(1)
}