use crate::args::i18n;
pub fn handle_list(exp: bool, imp: bool, all: bool) -> ! {
use std::process::{Command, Stdio};
let exp = if !exp && !imp && !all {
tracing::info!("No list option specified, defaulting to --exp");
true
} else {
exp
};
tracing::info!(
exp = exp,
imp = imp,
all = all,
"List installed packages requested from CLI"
);
let all_packages = match Command::new("pacman")
.args(["-Qq"])
.stdin(Stdio::null())
.output()
{
Ok(output) => {
if !output.status.success() {
eprintln!("{}", i18n::t("app.cli.list.query_failed"));
tracing::error!("pacman -Qq failed");
std::process::exit(1);
}
let packages: std::collections::HashSet<String> =
String::from_utf8_lossy(&output.stdout)
.lines()
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect();
packages
}
Err(e) => {
eprintln!("{}", i18n::t_fmt1("app.cli.list.pacman_exec_failed", &e));
tracing::error!(error = %e, "Failed to execute pacman");
std::process::exit(1);
}
};
let explicit_packages = match Command::new("pacman")
.args(["-Qetq"])
.stdin(Stdio::null())
.output()
{
Ok(output) => {
if !output.status.success() {
eprintln!("{}", i18n::t("app.cli.list.query_explicit_failed"));
tracing::error!("pacman -Qetq failed");
std::process::exit(1);
}
let packages: std::collections::HashSet<String> =
String::from_utf8_lossy(&output.stdout)
.lines()
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect();
packages
}
Err(e) => {
eprintln!("{}", i18n::t_fmt1("app.cli.list.pacman_exec_failed", &e));
tracing::error!(error = %e, "Failed to execute pacman");
std::process::exit(1);
}
};
let implicit_packages: std::collections::HashSet<String> = all_packages
.difference(&explicit_packages)
.cloned()
.collect();
let mut packages_to_list = Vec::new();
if all {
packages_to_list.extend(all_packages.iter().cloned());
}
if exp {
packages_to_list.extend(explicit_packages.iter().cloned());
}
if imp {
packages_to_list.extend(implicit_packages.iter().cloned());
}
let mut unique_packages: std::collections::HashSet<String> =
packages_to_list.into_iter().collect();
let mut sorted_packages: Vec<String> = unique_packages.drain().collect();
sorted_packages.sort();
let count = sorted_packages.len();
for pkg in sorted_packages {
println!("{pkg}");
}
tracing::info!(count = count, "Listed installed packages");
std::process::exit(0);
}