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

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

/// What: Parse YAML array lines from i18n strings into help lines.
///
/// Inputs:
/// - `yaml_text`: YAML array string from i18n
///
/// Output:
/// - Vector of Lines extracted from YAML format
///
/// Details:
/// - Handles quoted and unquoted YAML list items, stripping prefixes and quotes.
fn parse_yaml_lines(yaml_text: &str) -> Vec<Line<'static>> {
    let mut result = Vec::new();
    for line in yaml_text.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("- \"") || trimmed.starts_with("- '") {
            let content = trimmed
                .strip_prefix("- \"")
                .or_else(|| trimmed.strip_prefix("- '"))
                .and_then(|s| s.strip_suffix('"').or_else(|| s.strip_suffix('\'')))
                .unwrap_or(trimmed);
            result.push(Line::from(Span::raw(content.to_string())));
        } else if trimmed.starts_with("- ") {
            result.push(Line::from(Span::raw(
                trimmed.strip_prefix("- ").unwrap_or(trimmed).to_string(),
            )));
        }
    }
    result
}

/// What: Add a section header to the lines vector.
///
/// Inputs:
/// - `lines`: Mutable reference to lines vector
/// - `app`: Application state for i18n
/// - `th`: Theme reference
/// - `key`: i18n key for section title
///
/// Output:
/// - Adds empty line and styled section header to lines
///
/// Details:
/// - Formats section headers with consistent styling.
fn add_section_header(
    lines: &mut Vec<Line<'static>>,
    app: &AppState,
    th: &crate::theme::Theme,
    key: &str,
) {
    lines.push(Line::from(""));
    lines.push(Line::from(Span::styled(
        i18n::t(app, key),
        Style::default()
            .fg(th.overlay1)
            .add_modifier(Modifier::BOLD),
    )));
}

/// What: Conditionally add a binding line if the key exists.
///
/// Inputs:
/// - `lines`: Mutable reference to lines vector
/// - `app`: Application state for i18n
/// - `th`: Theme reference
/// - `key_opt`: Optional keybinding
/// - `label_key`: i18n key for label
///
/// Output:
/// - Adds formatted binding line if key exists
///
/// Details:
/// - Uses fmt closure to format binding consistently.
fn add_binding_if_some(
    lines: &mut Vec<Line<'static>>,
    app: &AppState,
    th: &crate::theme::Theme,
    key_opt: Option<KeyChord>,
    label_key: &str,
) {
    if let Some(k) = key_opt {
        let fmt = |label: &str, chord: KeyChord| -> Line<'static> {
            Line::from(vec![
                Span::styled(
                    format!("{label:18}"),
                    Style::default()
                        .fg(th.overlay1)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::raw("  "),
                Span::styled(
                    format!("[{}]", chord.label()),
                    Style::default().fg(th.text).add_modifier(Modifier::BOLD),
                ),
            ])
        };
        lines.push(fmt(&i18n::t(app, label_key), k));
    }
}

/// What: Build global keybindings section.
///
/// Inputs:
/// - `lines`: Mutable reference to lines vector
/// - `app`: Application state
/// - `th`: Theme reference
/// - `km`: Keymap reference
///
/// Output:
/// - Adds global bindings to lines
///
/// Details:
/// - Formats all global application keybindings.
fn build_global_bindings(
    lines: &mut Vec<Line<'static>>,
    app: &AppState,
    th: &crate::theme::Theme,
    km: &crate::theme::KeyMap,
) {
    add_binding_if_some(
        lines,
        app,
        th,
        km.help_overlay.first().copied(),
        "app.modals.help.key_labels.help_overlay",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.exit.first().copied(),
        "app.modals.help.key_labels.exit",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.reload_config.first().copied(),
        "app.modals.help.key_labels.reload_config",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.pane_next.first().copied(),
        "app.modals.help.key_labels.next_pane",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.pane_left.first().copied(),
        "app.modals.help.key_labels.focus_left",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.pane_right.first().copied(),
        "app.modals.help.key_labels.focus_right",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.show_pkgbuild.first().copied(),
        "app.modals.help.key_labels.show_pkgbuild",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.comments_toggle.first().copied(),
        "app.modals.help.key_labels.show_comments",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.cycle_pkgbuild_sections.first().copied(),
        "app.modals.help.key_labels.cycle_pkgbuild_sections",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.change_sort.first().copied(),
        "app.modals.help.key_labels.change_sorting",
    );
}

/// What: Build search pane keybindings section.
///
/// Inputs:
/// - `lines`: Mutable reference to lines vector
/// - `app`: Application state
/// - `th`: Theme reference
/// - `km`: Keymap reference
///
/// Output:
/// - Adds search bindings to lines
///
/// Details:
/// - Formats search pane navigation and action keybindings.
fn build_search_bindings(
    lines: &mut Vec<Line<'static>>,
    app: &AppState,
    th: &crate::theme::Theme,
    km: &crate::theme::KeyMap,
) {
    let fmt = |label: &str, chord: KeyChord| -> Line<'static> {
        Line::from(vec![
            Span::styled(
                format!("{label:18}"),
                Style::default()
                    .fg(th.overlay1)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw("  "),
            Span::styled(
                format!("[{}]", chord.label()),
                Style::default().fg(th.text).add_modifier(Modifier::BOLD),
            ),
        ])
    };

    if let (Some(up), Some(dn)) = (km.search_move_up.first(), km.search_move_down.first()) {
        lines.push(fmt(
            &i18n::t(app, "app.modals.help.key_labels.move"),
            KeyChord {
                code: up.code,
                mods: up.mods,
            },
        ));
        lines.push(fmt(
            &i18n::t(app, "app.modals.help.key_labels.move"),
            KeyChord {
                code: dn.code,
                mods: dn.mods,
            },
        ));
    }
    if let (Some(pu), Some(pd)) = (km.search_page_up.first(), km.search_page_down.first()) {
        lines.push(fmt(
            &i18n::t(app, "app.modals.help.key_labels.page"),
            KeyChord {
                code: pu.code,
                mods: pu.mods,
            },
        ));
        lines.push(fmt(
            &i18n::t(app, "app.modals.help.key_labels.page"),
            KeyChord {
                code: pd.code,
                mods: pd.mods,
            },
        ));
    }
    add_binding_if_some(
        lines,
        app,
        th,
        km.search_add.first().copied(),
        "app.modals.help.key_labels.add",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.search_install.first().copied(),
        "app.modals.help.key_labels.install",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.search_backspace.first().copied(),
        "app.modals.help.key_labels.delete",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.search_insert_clear.first().copied(),
        "app.modals.help.key_labels.clear_input",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.toggle_fuzzy.first().copied(),
        "app.modals.help.key_labels.toggle_fuzzy",
    );
}

/// What: Build search normal mode keybindings section.
///
/// Inputs:
/// - `lines`: Mutable reference to lines vector
/// - `app`: Application state
/// - `th`: Theme reference
/// - `km`: Keymap reference
///
/// Output:
/// - Adds search normal mode bindings to lines if any exist
///
/// Details:
/// - Only renders section if at least one normal mode binding is configured.
fn build_search_normal_bindings(
    lines: &mut Vec<Line<'static>>,
    app: &AppState,
    th: &crate::theme::Theme,
    km: &crate::theme::KeyMap,
) {
    let has_normal_bindings = !km.search_normal_toggle.is_empty()
        || !km.search_normal_insert.is_empty()
        || !km.search_normal_select_left.is_empty()
        || !km.search_normal_select_right.is_empty()
        || !km.search_normal_delete.is_empty()
        || !km.search_normal_clear.is_empty()
        || !km.search_normal_open_status.is_empty()
        || !km.config_menu_toggle.is_empty()
        || !km.options_menu_toggle.is_empty()
        || !km.panels_menu_toggle.is_empty();

    if !has_normal_bindings {
        return;
    }

    add_section_header(lines, app, th, "app.modals.help.sections.search_normal");

    add_binding_if_some(
        lines,
        app,
        th,
        km.search_normal_toggle.first().copied(),
        "app.modals.help.key_labels.toggle_normal",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.search_normal_insert.first().copied(),
        "app.modals.help.key_labels.insert_mode",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.search_normal_select_left.first().copied(),
        "app.modals.help.key_labels.select_left",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.search_normal_select_right.first().copied(),
        "app.modals.help.key_labels.select_right",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.search_normal_delete.first().copied(),
        "app.modals.help.key_labels.delete",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.search_normal_clear.first().copied(),
        "app.modals.help.key_labels.clear_input",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.search_normal_open_status.first().copied(),
        "app.modals.help.key_labels.open_arch_status",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.config_menu_toggle.first().copied(),
        "app.modals.help.key_labels.config_lists_menu",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.options_menu_toggle.first().copied(),
        "app.modals.help.key_labels.options_menu",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.panels_menu_toggle.first().copied(),
        "app.modals.help.key_labels.panels_menu",
    );
}

/// What: Build install pane keybindings section.
///
/// Inputs:
/// - `lines`: Mutable reference to lines vector
/// - `app`: Application state
/// - `th`: Theme reference
/// - `km`: Keymap reference
///
/// Output:
/// - Adds install bindings to lines
///
/// Details:
/// - Formats install pane navigation and action keybindings.
fn build_install_bindings(
    lines: &mut Vec<Line<'static>>,
    app: &AppState,
    th: &crate::theme::Theme,
    km: &crate::theme::KeyMap,
) {
    let fmt = |label: &str, chord: KeyChord| -> Line<'static> {
        Line::from(vec![
            Span::styled(
                format!("{label:18}"),
                Style::default()
                    .fg(th.overlay1)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw("  "),
            Span::styled(
                format!("[{}]", chord.label()),
                Style::default().fg(th.text).add_modifier(Modifier::BOLD),
            ),
        ])
    };

    if let (Some(up), Some(dn)) = (km.install_move_up.first(), km.install_move_down.first()) {
        lines.push(fmt(
            &i18n::t(app, "app.modals.help.key_labels.move"),
            KeyChord {
                code: up.code,
                mods: up.mods,
            },
        ));
        lines.push(fmt(
            &i18n::t(app, "app.modals.help.key_labels.move"),
            KeyChord {
                code: dn.code,
                mods: dn.mods,
            },
        ));
    }
    add_binding_if_some(
        lines,
        app,
        th,
        km.install_confirm.first().copied(),
        "app.modals.help.key_labels.confirm",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.install_remove.first().copied(),
        "app.modals.help.key_labels.remove",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.install_clear.first().copied(),
        "app.modals.help.key_labels.clear",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.install_find.first().copied(),
        "app.modals.help.key_labels.find",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.install_to_search.first().copied(),
        "app.modals.help.key_labels.to_search",
    );
}

/// What: Build recent pane keybindings section.
///
/// Inputs:
/// - `lines`: Mutable reference to lines vector
/// - `app`: Application state
/// - `th`: Theme reference
/// - `km`: Keymap reference
///
/// Output:
/// - Adds recent bindings to lines
///
/// Details:
/// - Formats recent pane navigation and action keybindings, including explicit Shift+Del.
fn build_recent_bindings(
    lines: &mut Vec<Line<'static>>,
    app: &AppState,
    th: &crate::theme::Theme,
    km: &crate::theme::KeyMap,
) {
    let fmt = |label: &str, chord: KeyChord| -> Line<'static> {
        Line::from(vec![
            Span::styled(
                format!("{label:18}"),
                Style::default()
                    .fg(th.overlay1)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw("  "),
            Span::styled(
                format!("[{}]", chord.label()),
                Style::default().fg(th.text).add_modifier(Modifier::BOLD),
            ),
        ])
    };

    if let (Some(up), Some(dn)) = (km.recent_move_up.first(), km.recent_move_down.first()) {
        lines.push(fmt(
            &i18n::t(app, "app.modals.help.key_labels.move"),
            KeyChord {
                code: up.code,
                mods: up.mods,
            },
        ));
        lines.push(fmt(
            &i18n::t(app, "app.modals.help.key_labels.move"),
            KeyChord {
                code: dn.code,
                mods: dn.mods,
            },
        ));
    }
    add_binding_if_some(
        lines,
        app,
        th,
        km.recent_use.first().copied(),
        "app.modals.help.key_labels.use",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.recent_add.first().copied(),
        "app.modals.help.key_labels.add",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.recent_find.first().copied(),
        "app.modals.help.key_labels.find",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.recent_to_search.first().copied(),
        "app.modals.help.key_labels.to_search",
    );
    add_binding_if_some(
        lines,
        app,
        th,
        km.recent_remove.first().copied(),
        "app.modals.help.key_labels.remove",
    );
    // Explicit: Shift+Del clears Recent (display only)
    lines.push(fmt(
        &i18n::t(app, "app.modals.help.key_labels.clear"),
        KeyChord {
            code: crossterm::event::KeyCode::Delete,
            mods: crossterm::event::KeyModifiers::SHIFT,
        },
    ));
}

/// What: Render the interactive help overlay summarizing keybindings and mouse tips.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Mutable application state (keymap, help scroll, rect tracking)
/// - `area`: Full screen area used to center the help modal
///
/// Output:
/// - Draws the help dialog, updates `app.help_rect`, and respects stored scroll offset.
///
/// Details:
/// - Formats bindings per pane, includes normal-mode guidance, and records clickable bounds to
///   enable mouse scrolling while using the current theme colors.
#[allow(clippy::many_single_char_names)]
pub fn render_help(f: &mut Frame, app: &mut AppState, area: Rect) {
    let th = theme();
    // Full-screen translucent help overlay
    let w = area.width.saturating_sub(6).min(96);
    let h = area.height.saturating_sub(4).min(28);
    let x = area.x + (area.width.saturating_sub(w)) / 2;
    let y = area.y + (area.height.saturating_sub(h)) / 2;
    let rect = ratatui::prelude::Rect {
        x,
        y,
        width: w,
        height: h,
    };
    f.render_widget(Clear, rect);
    // Record inner content rect (exclude borders) for mouse hit-testing
    app.help_rect = Some((
        rect.x + 1,
        rect.y + 1,
        rect.width.saturating_sub(2),
        rect.height.saturating_sub(2),
    ));
    let km = &app.keymap;

    let mut lines: Vec<Line<'static>> = Vec::new();
    lines.push(Line::from(Span::styled(
        i18n::t(app, "app.modals.help.heading"),
        Style::default().fg(th.mauve).add_modifier(Modifier::BOLD),
    )));
    lines.push(Line::from(""));

    // Build all sections using helper functions
    build_global_bindings(&mut lines, app, &th, km);
    lines.push(Line::from(""));

    add_section_header(&mut lines, app, &th, "app.modals.help.sections.search");
    build_search_bindings(&mut lines, app, &th, km);
    build_search_normal_bindings(&mut lines, app, &th, km);

    add_section_header(&mut lines, app, &th, "app.modals.help.sections.install");
    build_install_bindings(&mut lines, app, &th, km);

    add_section_header(&mut lines, app, &th, "app.modals.help.sections.recent");
    build_recent_bindings(&mut lines, app, &th, km);

    // Mouse and UI controls
    add_section_header(&mut lines, app, &th, "app.modals.help.sections.mouse");
    lines.extend(parse_yaml_lines(&i18n::t(
        app,
        "app.modals.help.mouse_lines",
    )));

    // Dialogs
    add_section_header(
        &mut lines,
        app,
        &th,
        "app.modals.help.sections.system_update_dialog",
    );
    lines.extend(parse_yaml_lines(&i18n::t(
        app,
        "app.modals.help.system_update_lines",
    )));

    add_section_header(&mut lines, app, &th, "app.modals.help.sections.news_dialog");
    lines.extend(parse_yaml_lines(&i18n::t(
        app,
        "app.modals.help.news_lines",
    )));

    add_section_header(
        &mut lines,
        app,
        &th,
        "app.modals.help.sections.repositories_modal",
    );
    lines.extend(parse_yaml_lines(&i18n::t(
        app,
        "app.modals.help.repositories_modal_lines",
    )));

    add_section_header(
        &mut lines,
        app,
        &th,
        "app.modals.help.sections.foreign_overlap_dialog",
    );
    lines.extend(parse_yaml_lines(&i18n::t(
        app,
        "app.modals.help.foreign_overlap_help_lines",
    )));

    lines.push(Line::from(""));
    lines.push(Line::from(Span::styled(
        i18n::t(app, "app.modals.help.close_hint"),
        Style::default().fg(th.subtext1),
    )));

    let help_title = format!(" {} ", i18n::t(app, "app.titles.help"));
    let boxw = Paragraph::new(lines)
        .style(Style::default().fg(th.text).bg(th.mantle))
        .wrap(Wrap { trim: true })
        .scroll((app.help_scroll, 0))
        .block(
            Block::default()
                .title(Span::styled(
                    &help_title,
                    Style::default().fg(th.mauve).add_modifier(Modifier::BOLD),
                ))
                .borders(Borders::ALL)
                .border_type(BorderType::Double)
                .border_style(Style::default().fg(th.mauve))
                .style(Style::default().bg(th.mantle)),
        );
    f.render_widget(boxw, rect);
}