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
#[allow(unused_imports)]
use std::process::Command;

use crate::state::modal::CascadeMode;

/// What: Check for configuration directories in `$HOME/PACKAGE_NAME` and `$HOME/.config/PACKAGE_NAME`.
///
/// Inputs:
/// - `package_name`: Name of the package to check for config directories.
/// - `home`: Home directory path.
///
/// Output:
/// - Vector of found config directory paths.
///
/// Details:
/// - Checks both `$HOME/PACKAGE_NAME` and `$HOME/.config/PACKAGE_NAME`.
/// - Only returns directories that actually exist.
#[must_use]
pub fn check_config_directories(package_name: &str, home: &str) -> Vec<std::path::PathBuf> {
    use std::path::PathBuf;
    let mut found_dirs = Vec::new();

    // Check $HOME/PACKAGE_NAME
    let home_pkg_dir = PathBuf::from(home).join(package_name);
    if home_pkg_dir.exists() && home_pkg_dir.is_dir() {
        found_dirs.push(home_pkg_dir);
    }

    // Check $HOME/.config/PACKAGE_NAME
    let config_pkg_dir = PathBuf::from(home).join(".config").join(package_name);
    if config_pkg_dir.exists() && config_pkg_dir.is_dir() {
        found_dirs.push(config_pkg_dir);
    }

    found_dirs
}

#[cfg(not(target_os = "windows"))]
use super::utils::{choose_terminal_index_prefer_path, command_on_path, shell_single_quote};

#[cfg(not(target_os = "windows"))]
/// What: Configure terminal-specific environment variables for a command.
///
/// Input:
/// - `cmd`: Command builder to configure.
/// - `term`: Terminal name to check for special handling.
///
/// Output:
/// - Modifies `cmd` with environment variables for konsole, gnome-console, or kgx if needed.
///
/// Details:
/// - Sets Wayland-specific environment for konsole when running under Wayland.
/// - Sets rendering environment for gnome-console and kgx to ensure compatibility.
fn configure_terminal_env(cmd: &mut Command, term: &str) {
    if term == "konsole" && std::env::var_os("WAYLAND_DISPLAY").is_some() {
        cmd.env("QT_LOGGING_RULES", "qt.qpa.wayland.textinput=false");
    }
    if term == "gnome-console" || term == "kgx" {
        cmd.env("GSK_RENDERER", "cairo");
        cmd.env("LIBGL_ALWAYS_SOFTWARE", "1");
    }
}

#[cfg(not(target_os = "windows"))]
/// What: Configure test output environment variable for a command.
///
/// Input:
/// - `cmd`: Command builder to configure.
///
/// Output:
/// - Sets `PACSEA_TEST_OUT` environment variable if present, creating parent directory if needed.
///
/// Details:
/// - Only applies when `PACSEA_TEST_OUT` is set in the environment.
fn configure_test_env(cmd: &mut Command) {
    if let Ok(p) = std::env::var("PACSEA_TEST_OUT") {
        if let Some(parent) = std::path::Path::new(&p).parent() {
            let _ = std::fs::create_dir_all(parent);
        }
        cmd.env("PACSEA_TEST_OUT", p);
    }
}

#[cfg(not(target_os = "windows"))]
/// What: Logging context for terminal spawning operations.
///
/// Details:
/// - Groups related logging parameters to reduce function argument count.
struct SpawnContext<'a> {
    /// Comma-separated package names string.
    names_str: &'a str,
    /// Number of package names.
    names_len: usize,
    /// Whether this is a dry-run operation.
    dry_run: bool,
    /// Cascade removal mode.
    cascade_mode: CascadeMode,
}

#[cfg(not(target_os = "windows"))]
/// What: Attempt to spawn a terminal with the given configuration.
///
/// Input:
/// - `term`: Terminal executable name.
/// - `args`: Arguments to pass before the command string.
/// - `needs_xfce_command`: Whether this terminal needs special xfce4-terminal command format.
/// - `cmd_str`: The command string to execute.
/// - `ctx`: Logging context for the operation.
///
/// Output:
/// - `true` if the terminal was successfully spawned, `false` otherwise.
///
/// Details:
/// - Configures command arguments based on terminal type.
/// - Sets up environment variables and test output handling.
/// - Logs success or failure appropriately.
fn try_spawn_terminal(
    term: &str,
    args: &[&str],
    needs_xfce_command: bool,
    cmd_str: &str,
    ctx: &SpawnContext<'_>,
) -> bool {
    let mut cmd = Command::new(term);
    if needs_xfce_command && term == "xfce4-terminal" {
        let quoted = shell_single_quote(cmd_str);
        cmd.arg("--command").arg(format!("bash -lc {quoted}"));
    } else {
        cmd.args(args.iter().copied()).arg(cmd_str);
    }
    configure_test_env(&mut cmd);
    configure_terminal_env(&mut cmd, term);

    match cmd.spawn() {
        Ok(_) => {
            tracing::info!(
                terminal = %term,
                names = %ctx.names_str,
                total = ctx.names_len,
                dry_run = ctx.dry_run,
                mode = ?ctx.cascade_mode,
                "launched terminal for removal"
            );
            true
        }
        Err(e) => {
            tracing::warn!(
                terminal = %term,
                error = %e,
                names = %ctx.names_str,
                "failed to spawn terminal, trying next"
            );
            false
        }
    }
}

#[cfg(not(target_os = "windows"))]
/// What: Spawn a terminal to remove all given packages with pacman.
///
/// Input:
/// - names slice of package names; `dry_run` prints the removal command instead of executing
///
/// Output:
/// - Launches a terminal (or bash) to run sudo pacman -Rns for the provided names.
///
/// Details:
/// - Prefers common terminals (GNOME Console/Terminal, kitty, alacritty, xterm, xfce4-terminal, etc.); falls back to bash. Appends a hold tail so the window remains open after command completion.
/// - During tests, this is a no-op to avoid opening real terminal windows.
pub fn spawn_remove_all(names: &[String], dry_run: bool, cascade_mode: CascadeMode) {
    // Skip actual spawning during tests unless PACSEA_TEST_OUT is set (indicates a test with fake terminal)
    #[cfg(test)]
    if std::env::var("PACSEA_TEST_OUT").is_err() {
        return;
    }

    let names_str = names.join(" ");
    tracing::info!(
        names = %names_str,
        total = names.len(),
        dry_run = dry_run,
        mode = ?cascade_mode,
        "spawning removal"
    );
    let flag = cascade_mode.flag();
    let hold_tail = "; echo; echo 'Finished.'; echo 'Press any key to close...'; read -rn1 -s _ || (echo; echo 'Press Ctrl+C to close'; sleep infinity)";
    let tool = match crate::logic::privilege::active_tool() {
        Ok(t) => t,
        Err(err) => {
            tracing::error!(error = %err, "privilege tool resolution failed for removal");
            return;
        }
    };
    let base = format!("pacman {flag} --noconfirm {}", names.join(" "));
    let cmd_str = if dry_run {
        let cmd = format!(
            "{}{hold_tail}",
            crate::logic::privilege::build_privilege_command(tool, &base)
        );
        let quoted = shell_single_quote(&cmd);
        format!("echo DRY RUN: {quoted}")
    } else {
        format!(
            "{}{hold_tail}",
            crate::logic::privilege::build_privilege_command(tool, &base)
        )
    };

    let terms_gnome_first: &[(&str, &[&str], bool)] = &[
        ("gnome-terminal", &["--", "bash", "-lc"], false),
        ("gnome-console", &["--", "bash", "-lc"], false),
        ("kgx", &["--", "bash", "-lc"], false),
        ("alacritty", &["-e", "bash", "-lc"], false),
        ("ghostty", &["-e", "bash", "-lc"], false),
        ("kitty", &["bash", "-lc"], false),
        ("konsole", &["-e", "bash", "-lc"], false),
        ("xterm", &["-hold", "-e", "bash", "-lc"], false),
        ("xfce4-terminal", &[], true),
        ("tilix", &["--", "bash", "-lc"], false),
        ("mate-terminal", &["--", "bash", "-lc"], false),
    ];
    let terms_default: &[(&str, &[&str], bool)] = &[
        ("alacritty", &["-e", "bash", "-lc"], false),
        ("ghostty", &["-e", "bash", "-lc"], false),
        ("kitty", &["bash", "-lc"], false),
        ("konsole", &["-e", "bash", "-lc"], false),
        ("gnome-terminal", &["--", "bash", "-lc"], false),
        ("gnome-console", &["--", "bash", "-lc"], false),
        ("kgx", &["--", "bash", "-lc"], false),
        ("xterm", &["-hold", "-e", "bash", "-lc"], false),
        ("xfce4-terminal", &[], true),
        ("tilix", &["--", "bash", "-lc"], false),
        ("mate-terminal", &["--", "bash", "-lc"], false),
    ];

    let is_gnome = std::env::var("XDG_CURRENT_DESKTOP")
        .ok()
        .is_some_and(|v| v.to_uppercase().contains("GNOME"));
    let terms = if is_gnome {
        terms_gnome_first
    } else {
        terms_default
    };

    let ctx = SpawnContext {
        names_str: &names_str,
        names_len: names.len(),
        dry_run,
        cascade_mode,
    };

    let mut launched = choose_terminal_index_prefer_path(terms).is_some_and(|idx| {
        let (term, args, needs_xfce_command) = terms[idx];
        try_spawn_terminal(term, args, needs_xfce_command, &cmd_str, &ctx)
    });

    if !launched {
        for (term, args, needs_xfce_command) in terms {
            if command_on_path(term) {
                launched = try_spawn_terminal(term, args, *needs_xfce_command, &cmd_str, &ctx);
                if launched {
                    break;
                }
            }
        }
    }

    if !launched {
        let res = Command::new("bash").args(["-lc", &cmd_str]).spawn();
        if let Err(e) = res {
            tracing::error!(error = %e, names = %names_str, "failed to spawn bash to run removal command");
        } else {
            tracing::info!(
                names = %names_str,
                total = names.len(),
                dry_run = dry_run,
                mode = ?cascade_mode,
                "launched bash for removal"
            );
        }
    }
}

#[cfg(target_os = "windows")]
/// What: Present a placeholder removal message on Windows where pacman is unavailable.
///
/// Input:
/// - `names`: Packages the user requested to remove.
/// - `dry_run`: When `true`, uses `PowerShell` to simulate the removal operation.
/// - `cascade_mode`: Removal mode used for display consistency.
///
/// Output:
/// - Launches a detached `PowerShell` window (if available) for dry-run simulation, or `cmd` window otherwise.
///
/// Details:
/// - When `dry_run` is true and `PowerShell` is available, uses `PowerShell` to simulate the removal with Write-Host.
/// - Mirrors Unix logging by emitting an info trace, but performs no package operations.
/// - During tests, this is a no-op to avoid opening real terminal windows.
#[allow(unused_variables, clippy::missing_const_for_fn)]
pub fn spawn_remove_all(names: &[String], dry_run: bool, cascade_mode: CascadeMode) {
    #[cfg(not(test))]
    {
        let mut names = names.to_vec();
        if names.is_empty() {
            names.push("nothing".into());
        }
        let names_str = names.join(" ");
        let msg = if dry_run {
            format!("DRY RUN: Would remove packages: {names_str}")
        } else {
            format!("Cannot remove packages on Windows: {names_str}")
        };

        // Check if this is a dry-run operation
        if dry_run && super::utils::is_powershell_available() {
            // Use PowerShell to simulate the operation
            let escaped_msg = msg.replace('\'', "''");
            let powershell_cmd = format!(
                "Write-Host '{escaped_msg}' -ForegroundColor Yellow; Write-Host ''; Write-Host 'Press any key to close...'; $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')"
            );
            let _ = std::process::Command::new("powershell.exe")
                .args(["-NoProfile", "-Command", &powershell_cmd])
                .spawn();
        } else {
            let _ = std::process::Command::new("cmd")
                .args([
                    "/C",
                    "start",
                    "Pacsea Remove",
                    "cmd",
                    "/K",
                    &super::utils::cmd_echo_command(&msg),
                ])
                .spawn();
        }
    }
}

#[cfg(test)]
mod tests {
    #[test]
    #[cfg(unix)]
    /// What: Verify the removal helper prefers gnome-terminal and passes the expected dash handling.
    ///
    /// Inputs:
    /// - Fake `gnome-terminal` script injected into `PATH`.
    /// - `spawn_remove_all` invoked in dry-run cascade mode with two package names.
    ///
    /// Output:
    /// - Captured invocation arguments start with `--`, `bash`, `-lc` to ensure safe command parsing.
    ///
    /// Details:
    /// - Redirects `PACSEA_TEST_OUT` so the shim terminal records arguments, then restores environment variables.
    fn remove_all_uses_gnome_terminal_double_dash() {
        use std::fs;
        use std::os::unix::fs::PermissionsExt;
        use std::path::PathBuf;

        let mut dir: PathBuf = std::env::temp_dir();
        dir.push(format!(
            "pacsea_test_remove_gnome_{}_{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .expect("System time is before UNIX epoch")
                .as_nanos()
        ));
        let _ = fs::create_dir_all(&dir);
        let mut out_path = dir.clone();
        out_path.push("args.txt");
        let mut term_path = dir.clone();
        term_path.push("gnome-terminal");
        let script = "#!/bin/sh\n: > \"$PACSEA_TEST_OUT\"\nfor a in \"$@\"; do printf '%s\n' \"$a\" >> \"$PACSEA_TEST_OUT\"; done\n";
        fs::write(&term_path, script.as_bytes()).expect("failed to write test terminal script");
        let mut perms = fs::metadata(&term_path)
            .expect("failed to read test terminal script metadata")
            .permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&term_path, perms)
            .expect("failed to set test terminal script permissions");

        let orig_path = std::env::var_os("PATH");
        unsafe {
            std::env::set_var("PATH", dir.display().to_string());
            std::env::set_var("PACSEA_TEST_OUT", out_path.display().to_string());
        }

        let names = vec!["ripgrep".to_string(), "fd".to_string()];
        super::spawn_remove_all(
            &names,
            true,
            crate::state::modal::CascadeMode::CascadeWithConfigs,
        );
        std::thread::sleep(std::time::Duration::from_millis(50));

        let body = fs::read_to_string(&out_path).expect("fake terminal args file written");
        let lines: Vec<&str> = body.lines().collect();
        assert!(lines.len() >= 3, "expected at least 3 args, got: {body}");
        assert_eq!(lines[0], "--");
        assert_eq!(lines[1], "bash");
        assert_eq!(lines[2], "-lc");

        unsafe {
            if let Some(v) = orig_path {
                std::env::set_var("PATH", v);
            } else {
                std::env::remove_var("PATH");
            }
            std::env::remove_var("PACSEA_TEST_OUT");
        }
    }
}