From 22a4f568324a826784e1bdb5d14e46c19a57a8f2 Mon Sep 17 00:00:00 2001 From: Giuseppe Speranza Date: Mon, 13 Mar 2023 00:38:13 +0100 Subject: [PATCH] pages.rs: fix run of orphans packages (#53) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * pages.rs: fix run of orphans packages This avoid "error: no targets specified (use -h for help)" when `pacman -Qtdq` is empty. `pacman -Qtdq` can be started without root, and has a return value other than 0 if it finds no packages. So if it finds packages, it succeeds and goes to the true condition that it deletes packages. Otherwise do echo. (echo doesn't require root) Signed-off-by: Peppe289 * pages.rs: format with rustfmt Signed-off-by: Peppe289 * 👷 use msg dialog to inform user; use Exec::cmd --------- Signed-off-by: Peppe289 Co-authored-by: Vladislav Nepogodin --- src/pages.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/pages.rs b/src/pages.rs index bddd9a5..4a6817c 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -65,7 +65,26 @@ fn create_fixes_section() -> gtk::Box { remove_orphans_btn.connect_clicked(move |_| { // Spawn child process in separate thread. std::thread::spawn(move || { - let _ = utils::run_cmd_terminal(String::from("pacman -Rns $(pacman -Qtdq)"), true); + // check if you have orphans packages. + let mut orphan_pkgs = Exec::cmd("/sbin/pacman") + .arg("-Qtdq") + .stdout(Redirection::Pipe) + .capture() + .unwrap() + .stdout_str(); + + // get list of packages separated by space, + // and check if it's empty or not. + orphan_pkgs = orphan_pkgs.replace('\n', " "); + if orphan_pkgs.is_empty() { + let dialog = gtk::MessageDialog::builder() + .message_type(gtk::MessageType::Info) + .text("No orphan packages found!") + .build(); + dialog.show(); + return; + } + let _ = utils::run_cmd_terminal(format!("pacman -Rns {orphan_pkgs}"), true); }); }); clear_pkgcache_btn.connect_clicked(on_clear_pkgcache_btn_clicked);