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
//! Options menu content handling (optional deps building).

use crate::state::AppState;

/// What: Check if a tool is installed either as a package or available on PATH.
///
/// Inputs:
/// - `pkg`: Package name to check
/// - `bin`: Binary name to check on PATH
///
/// Output:
/// - `true` if the tool is installed or available on PATH
///
/// Details:
/// - Checks both package installation and PATH availability
#[allow(clippy::missing_const_for_fn)]
fn is_tool_installed(pkg: &str, bin: &str) -> bool {
    crate::index::is_installed(pkg) || crate::install::command_on_path(bin)
}

/// What: Create an `OptionalDepRow` with standard fields.
///
/// Inputs:
/// - `app`: Application state for i18n
/// - `category_key`: i18n key for the category
/// - `label_suffix`: Suffix to append to category label
/// - `package`: Package name
/// - `installed`: Whether the tool is installed
/// - `note`: Optional note string
///
/// Output:
/// - An `OptionalDepRow` with the specified fields
///
/// Details:
/// - Sets `selectable` to `!installed` (only selectable if not installed)
fn create_optional_dep_row(
    app: &AppState,
    category_key: &str,
    label_suffix: &str,
    package: String,
    installed: bool,
    note: Option<String>,
) -> crate::state::types::OptionalDepRow {
    crate::state::types::OptionalDepRow {
        label: format!("{}: {label_suffix}", crate::i18n::t(app, category_key)),
        package,
        installed,
        selectable: !installed,
        note,
    }
}

/// What: Find the first installed candidate from a list of (binary, package) pairs.
///
/// Inputs:
/// - `candidates`: Slice of (`binary_name`, `package_name`) tuples
///
/// Output:
/// - `Some((binary, package))` if an installed candidate is found, `None` otherwise
///
/// Details:
/// - Checks both PATH and package installation for each candidate
fn find_first_installed_candidate<'a>(
    candidates: &'a [(&'a str, &'a str)],
) -> Option<(&'a str, &'a str)> {
    for (bin, pkg) in candidates {
        if is_tool_installed(pkg, bin) {
            return Some((*bin, *pkg));
        }
    }
    None
}

/// What: Check if helix editor is installed (handles hx/helix aliases).
///
/// Inputs:
/// - `pkg`: Package name (should be "helix")
/// - `bin`: Binary name to check
///
/// Output:
/// - `true` if helix is installed via any alias
///
/// Details:
/// - Checks both hx and helix binaries for helix package
fn is_helix_installed(pkg: &str, bin: &str) -> bool {
    if pkg == "helix" {
        is_tool_installed(pkg, bin) || is_tool_installed(pkg, "hx")
    } else {
        is_tool_installed(pkg, bin)
    }
}

/// What: Check if emacs editor is installed (handles emacs/emacsclient aliases).
///
/// Inputs:
/// - `pkg`: Package name (should be "emacs")
/// - `bin`: Binary name to check
///
/// Output:
/// - `true` if emacs is installed via any alias
///
/// Details:
/// - Checks both emacs and emacsclient binaries for emacs package
fn is_emacs_installed(pkg: &str, bin: &str) -> bool {
    if pkg == "emacs" {
        is_tool_installed(pkg, bin) || is_tool_installed(pkg, "emacsclient")
    } else {
        is_tool_installed(pkg, bin)
    }
}

/// What: Check if an editor candidate is installed (handles special aliases).
///
/// Inputs:
/// - `pkg`: Package name
/// - `bin`: Binary name
///
/// Output:
/// - `true` if the editor is installed
///
/// Details:
/// - Handles helix and emacs aliases specially
fn is_editor_installed(pkg: &str, bin: &str) -> bool {
    if pkg == "helix" {
        is_helix_installed(pkg, bin)
    } else if pkg == "emacs" {
        is_emacs_installed(pkg, bin)
    } else {
        is_tool_installed(pkg, bin)
    }
}

/// What: Build editor rows for the optional deps modal.
///
/// Inputs:
/// - `app`: Application state for i18n
/// - `rows`: Mutable vector to append rows to
///
/// Output:
/// - Appends editor rows to the provided vector
///
/// Details:
/// - Shows first installed editor, or all candidates if none installed
/// - Handles helix (hx/helix) and emacs (emacs/emacsclient) aliases
fn build_editor_rows(app: &AppState, rows: &mut Vec<crate::state::types::OptionalDepRow>) {
    let editor_candidates: &[(&str, &str)] = &[
        ("nvim", "neovim"),
        ("vim", "vim"),
        ("hx", "helix"),
        ("helix", "helix"),
        ("emacsclient", "emacs"),
        ("emacs", "emacs"),
        ("nano", "nano"),
    ];

    if let Some((bin, pkg)) = find_first_installed_candidate(editor_candidates) {
        let installed = is_editor_installed(pkg, bin);
        rows.push(create_optional_dep_row(
            app,
            "app.optional_deps.categories.editor",
            bin,
            pkg.to_string(),
            installed,
            None,
        ));
    } else {
        // Show unique packages (avoid hx+helix duplication)
        let mut seen = std::collections::HashSet::new();
        for (bin, pkg) in editor_candidates {
            if seen.insert(*pkg) {
                let installed = is_editor_installed(pkg, bin);
                rows.push(create_optional_dep_row(
                    app,
                    "app.optional_deps.categories.editor",
                    bin,
                    (*pkg).to_string(),
                    installed,
                    None,
                ));
            }
        }
    }
}

/// What: Build terminal rows for the optional deps modal.
///
/// Inputs:
/// - `app`: Application state for i18n
/// - `rows`: Mutable vector to append rows to
///
/// Output:
/// - Appends terminal rows to the provided vector
///
/// Details:
/// - Shows first installed terminal, or all candidates if none installed
fn build_terminal_rows(app: &AppState, rows: &mut Vec<crate::state::types::OptionalDepRow>) {
    let term_candidates: &[(&str, &str)] = &[
        ("alacritty", "alacritty"),
        ("ghostty", "ghostty"),
        ("kitty", "kitty"),
        ("xterm", "xterm"),
        ("gnome-terminal", "gnome-terminal"),
        ("konsole", "konsole"),
        ("xfce4-terminal", "xfce4-terminal"),
        ("tilix", "tilix"),
        ("mate-terminal", "mate-terminal"),
    ];

    if let Some((bin, pkg)) = find_first_installed_candidate(term_candidates) {
        let installed = is_tool_installed(pkg, bin);
        rows.push(create_optional_dep_row(
            app,
            "app.optional_deps.categories.terminal",
            bin,
            pkg.to_string(),
            installed,
            None,
        ));
    } else {
        for (bin, pkg) in term_candidates {
            let installed = is_tool_installed(pkg, bin);
            rows.push(create_optional_dep_row(
                app,
                "app.optional_deps.categories.terminal",
                bin,
                (*pkg).to_string(),
                installed,
                None,
            ));
        }
    }
}

/// What: Check if KDE session is active.
///
/// Inputs:
/// - None (reads environment variables)
///
/// Output:
/// - `true` if KDE session is detected
///
/// Details:
/// - Checks `KDE_FULL_SESSION`, `XDG_CURRENT_DESKTOP`, and `klipper` command
fn is_kde_session() -> bool {
    std::env::var("KDE_FULL_SESSION").is_ok()
        || std::env::var("XDG_CURRENT_DESKTOP").ok().is_some_and(|v| {
            let u = v.to_uppercase();
            u.contains("KDE") || u.contains("PLASMA")
        })
        || crate::install::command_on_path("klipper")
}

/// What: Build clipboard rows for the optional deps modal.
///
/// Inputs:
/// - `app`: Application state for i18n
/// - `rows`: Mutable vector to append rows to
///
/// Output:
/// - Appends clipboard rows to the provided vector
///
/// Details:
/// - Prefers Klipper for KDE, then wl-clipboard for Wayland, else xclip for X11
fn build_clipboard_rows(app: &AppState, rows: &mut Vec<crate::state::types::OptionalDepRow>) {
    if is_kde_session() {
        let pkg = "plasma-workspace";
        let installed = is_tool_installed(pkg, "klipper");
        rows.push(create_optional_dep_row(
            app,
            "app.optional_deps.categories.clipboard",
            "Klipper (KDE)",
            pkg.to_string(),
            installed,
            Some("KDE Plasma".to_string()),
        ));
    } else if std::env::var("WAYLAND_DISPLAY").is_ok() {
        let pkg = "wl-clipboard";
        let installed = is_tool_installed(pkg, "wl-copy");
        rows.push(create_optional_dep_row(
            app,
            "app.optional_deps.categories.clipboard",
            "wl-clipboard",
            pkg.to_string(),
            installed,
            Some("Wayland".to_string()),
        ));
    } else {
        let pkg = "xclip";
        let installed = is_tool_installed(pkg, "xclip");
        rows.push(create_optional_dep_row(
            app,
            "app.optional_deps.categories.clipboard",
            "xclip",
            pkg.to_string(),
            installed,
            Some("X11".to_string()),
        ));
    }
}

/// What: Build mirror manager rows for the optional deps modal.
///
/// Inputs:
/// - `rows`: Mutable vector to append rows to
///
/// Output:
/// - Appends mirror manager rows to the provided vector
///
/// Details:
/// - Detects Manjaro (pacman-mirrors), Artix (rate-mirrors), or default (reflector)
fn build_mirror_rows(rows: &mut Vec<crate::state::types::OptionalDepRow>) {
    let os_release = std::fs::read_to_string("/etc/os-release").unwrap_or_default();
    let manjaro = os_release.contains("Manjaro");
    let artix = os_release.contains("Artix");

    if manjaro {
        let pkg = "pacman-mirrors";
        let installed = crate::index::is_installed(pkg);
        rows.push(crate::state::types::OptionalDepRow {
            label: "Mirrors: pacman-mirrors".to_string(),
            package: pkg.to_string(),
            installed,
            selectable: !installed,
            note: Some("Manjaro".to_string()),
        });
    } else if artix {
        let pkg = "rate-mirrors";
        let installed = is_tool_installed(pkg, "rate-mirrors");
        rows.push(crate::state::types::OptionalDepRow {
            label: "Mirrors: rate mirrors".to_string(),
            package: pkg.to_string(),
            installed,
            selectable: !installed,
            note: Some("Artix".to_string()),
        });
    } else {
        let pkg = "reflector";
        let installed = crate::index::is_installed(pkg);
        rows.push(crate::state::types::OptionalDepRow {
            label: "Mirrors: reflector".to_string(),
            package: pkg.to_string(),
            installed,
            selectable: !installed,
            note: None,
        });
    }
}

/// What: Build AUR helper rows for the optional deps modal.
///
/// Inputs:
/// - `app`: Application state for i18n
/// - `rows`: Mutable vector to append rows to
///
/// Output:
/// - Appends AUR helper rows to the provided vector
///
/// Details:
/// - Shows installed paru/yay if present, or both if neither installed
fn build_aur_helper_rows(app: &AppState, rows: &mut Vec<crate::state::types::OptionalDepRow>) {
    let paru_inst = is_tool_installed("paru", "paru");
    let yay_inst = is_tool_installed("yay", "yay");

    if paru_inst {
        rows.push(create_optional_dep_row(
            app,
            "app.optional_deps.categories.aur_helper",
            "paru",
            "paru".to_string(),
            true,
            None,
        ));
    } else if yay_inst {
        rows.push(create_optional_dep_row(
            app,
            "app.optional_deps.categories.aur_helper",
            "yay",
            "yay".to_string(),
            true,
            None,
        ));
    } else {
        let note = Some("Install via git clone + makepkg -si".to_string());
        rows.push(create_optional_dep_row(
            app,
            "app.optional_deps.categories.aur_helper",
            "paru",
            "paru".to_string(),
            false,
            note.clone(),
        ));
        rows.push(create_optional_dep_row(
            app,
            "app.optional_deps.categories.aur_helper",
            "yay",
            "yay".to_string(),
            false,
            note,
        ));
    }
}

/// What: Build security scanner rows for the optional deps modal.
///
/// Inputs:
/// - `app`: Application state for i18n
/// - `rows`: Mutable vector to append rows to
///
/// Output:
/// - Appends security scanner rows to the provided vector
///
/// Details:
/// - Includes `ClamAV`, `Trivy`, `Semgrep`, `ShellCheck`, `VirusTotal API`, `aur-sleuth`, and sudo cache setup
fn build_security_scanner_rows(
    app: &AppState,
    rows: &mut Vec<crate::state::types::OptionalDepRow>,
) {
    // ClamAV
    let installed = is_tool_installed("clamav", "clamscan");
    rows.push(create_optional_dep_row(
        app,
        "app.optional_deps.categories.security",
        "clamav",
        "clamav".to_string(),
        installed,
        None,
    ));

    // Trivy
    let installed = is_tool_installed("trivy", "trivy");
    rows.push(create_optional_dep_row(
        app,
        "app.optional_deps.categories.security",
        "trivy",
        "trivy".to_string(),
        installed,
        None,
    ));

    // Semgrep
    let installed = is_tool_installed("semgrep-bin", "semgrep");
    rows.push(create_optional_dep_row(
        app,
        "app.optional_deps.categories.security",
        "semgrep-bin",
        "semgrep-bin".to_string(),
        installed,
        Some("AUR".to_string()),
    ));

    // ShellCheck
    let installed = is_tool_installed("shellcheck", "shellcheck");
    rows.push(create_optional_dep_row(
        app,
        "app.optional_deps.categories.security",
        "shellcheck",
        "shellcheck".to_string(),
        installed,
        None,
    ));

    // VirusTotal API setup
    let vt_key_present = !crate::theme::settings().virustotal_api_key.is_empty();
    rows.push(create_optional_dep_row(
        app,
        "app.optional_deps.categories.security",
        "VirusTotal API",
        "virustotal-setup".to_string(),
        vt_key_present,
        Some("Setup".to_string()),
    ));

    // aur-sleuth setup
    let sleuth_installed = {
        let onpath = crate::install::command_on_path("aur-sleuth");
        let home = std::env::var("HOME").ok();
        let user_local = home.as_deref().is_some_and(|h| {
            std::path::Path::new(h)
                .join(".local/bin/aur-sleuth")
                .exists()
        });
        let system_local = std::path::Path::new("/usr/local/bin/aur-sleuth").exists();
        onpath || user_local || system_local
    };
    rows.push(create_optional_dep_row(
        app,
        "app.optional_deps.categories.security",
        "aur-sleuth",
        "aur-sleuth-setup".to_string(),
        sleuth_installed,
        Some("Setup".to_string()),
    ));

    // AUR SSH setup helper for vote/unvote flow.
    let ssh_setup_ready = crate::logic::ssh_setup::is_aur_ssh_setup_configured();
    rows.push(create_optional_dep_row(
        app,
        "app.optional_deps.categories.security",
        "AUR SSH setup",
        "aur-ssh-setup".to_string(),
        ssh_setup_ready,
        Some("Setup".to_string()),
    ));

    let active_tool = crate::logic::privilege::active_tool().ok();
    // Optional sudo credential cache (`sudoers` drop-in) for long install/update sessions.
    if matches!(
        active_tool,
        Some(crate::logic::privilege::PrivilegeTool::Sudo)
    ) {
        let sudo_ts_configured =
            crate::logic::sudo_timestamp_setup::pacsea_sudo_timestamp_drop_in_present();
        rows.push({
            let label = crate::i18n::t(app, "app.optional_deps.items.sudo_timestamp_setup");
            create_optional_dep_row(
                app,
                "app.optional_deps.categories.privilege",
                &label,
                "sudo-timestamp-setup".to_string(),
                sudo_ts_configured,
                Some(if sudo_ts_configured {
                    "Configured".to_string()
                } else {
                    "Setup".to_string()
                }),
            )
        });
    }
    if matches!(
        active_tool,
        Some(crate::logic::privilege::PrivilegeTool::Doas)
    ) {
        let doas_persist_configured =
            crate::logic::doas_persist_setup::pacsea_doas_persist_configured();
        rows.push({
            let label = crate::i18n::t(app, "app.optional_deps.items.doas_persist_setup");
            create_optional_dep_row(
                app,
                "app.optional_deps.categories.privilege",
                &label,
                "doas-persist-setup".to_string(),
                doas_persist_configured,
                Some(if doas_persist_configured {
                    "Configured".to_string()
                } else {
                    "Setup".to_string()
                }),
            )
        });
    }
}

/// What: Build downgrade package row for the optional deps modal.
///
/// Inputs:
/// - `app`: Application state for i18n
/// - `rows`: Mutable vector to append rows to
///
/// Output:
/// - Appends downgrade row to the provided vector
///
/// Details:
/// - Checks if the downgrade package is installed and adds a row for it
fn build_downgrade_rows(app: &AppState, rows: &mut Vec<crate::state::types::OptionalDepRow>) {
    let installed = is_tool_installed("downgrade", "downgrade");
    rows.push(create_optional_dep_row(
        app,
        "app.optional_deps.categories.downgrade",
        "downgrade",
        "downgrade".to_string(),
        installed,
        None,
    ));
}

/// Build optional dependencies rows for the `OptionalDeps` modal.
///
/// What: Scan the system for installed editors, terminals, clipboard tools, mirror managers,
/// AUR helpers, and security scanners, then build a list of `OptionalDepRow` items for display.
///
/// Inputs:
/// - `app`: Application state (used for i18n translations)
///
/// Output:
/// - Vector of `OptionalDepRow` items ready to be displayed in the `OptionalDeps` modal.
///
/// Details:
/// - Editor: Shows the first installed editor found (`nvim`, `vim`, `hx`/`helix`, `emacsclient`/`emacs`, `nano`),
///   or all candidates if none installed. Handles helix (`hx`/`helix`) and emacs (`emacs`/`emacsclient`) aliases.
/// - Terminal: Shows the first installed terminal found, or all candidates if none installed.
/// - Clipboard: Detects `KDE` (`Klipper`), `Wayland` (`wl-clipboard`), or `X11` (`xclip`) and shows appropriate tool.
/// - Mirrors: Detects `Manjaro` (`pacman-mirrors`), `Artix` (`rate-mirrors`), or default (`reflector`).
/// - AUR helper: Shows installed `paru`/`yay` if present, or both if neither installed.
/// - Security scanners: Always includes `ClamAV`, `Trivy`, `Semgrep`, `ShellCheck`, `VirusTotal API` setup,
///   `aur-sleuth` setup, and optional sudo credential-cache (`sudoers`) setup. Marks installed items as non-selectable.
/// - Downgrade: Includes the `downgrade` package for package downgrade functionality.
pub fn build_optional_deps_rows(app: &AppState) -> Vec<crate::state::types::OptionalDepRow> {
    let mut rows: Vec<crate::state::types::OptionalDepRow> = Vec::new();

    build_editor_rows(app, &mut rows);
    build_terminal_rows(app, &mut rows);
    build_clipboard_rows(app, &mut rows);
    build_mirror_rows(&mut rows);
    build_aur_helper_rows(app, &mut rows);
    build_security_scanner_rows(app, &mut rows);
    build_downgrade_rows(app, &mut rows);

    rows
}