Skip to content

Commit

Permalink
Add xbps package manager to packages block (#2136)
Browse files Browse the repository at this point in the history
* Add support for xbps package manager

* Add `xbps` to cspell

---------

Co-authored-by: TheWalkingForest <[email protected]>
  • Loading branch information
thewalkingforest and TheWalkingForest authored Feb 12, 2025
1 parent 27bd364 commit 0afe30d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions cspell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ words:
- wlsunset
- wofi
- wttr
- xbps
- xclip
- xcolors
- xesam
Expand Down
32 changes: 30 additions & 2 deletions src/blocks/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! - `pacman` for Arch based system
//! - `aur` for Arch based system
//! - `dnf` for Fedora based system
//! - `xbps` for Void Linux
//!
//! # Configuration
//!
Expand All @@ -28,6 +29,7 @@
//! `pacman` | Number of updates available in Arch based system | Number | -
//! `aur` | Number of updates available in Arch based system | Number | -
//! `dnf` | Number of updates available in Fedora based system | Number | -
//! `xbps` | Number of updates available in Void Linux | Number | -
//! `total` | Number of updates available in all package manager listed | Number | -
//!
//! # Apt
Expand Down Expand Up @@ -146,16 +148,33 @@
//! cmd = "dnf list -q --upgrades | tail -n +2 | rofi -dmenu"
//! ```
//!
//!
//! Xbps only config:
//!
//! ```toml
//! [[block]]
//! block = "packages"
//! package_manager = ["xbps"]
//! interval = 1800
//! format = " $icon $xbps.eng(w:1) updates available "
//! format_singular = " $icon One update available "
//! format_up_to_date = " $icon system up to date "
//! [[block.click]]
//! # shows dmenu with available updates. Any dmenu alternative should also work.
//! button = "left"
//! cmd = "xbps-install -Mun | dmenu -l 10"
//! ```
//!
//! Multiple package managers config:
//!
//! Update the list of pending updates every thirty minutes (1800 seconds):
//!
//! ```toml
//! [[block]]
//! block = "packages"
//! package_manager = ["apt", "pacman", "aur","dnf"]
//! package_manager = ["apt", "pacman", "aur", "dnf", "xbps"]
//! interval = 1800
//! format = " $icon $apt + $pacman + $aur + $dnf = $total updates available "
//! format = " $icon $apt + $pacman + $aur + $dnf + $xbps = $total updates available "
//! format_singular = " $icon One update available "
//! format_up_to_date = " $icon system up to date "
//! # If a linux update is available, but no ZFS package, it won't be possible to
Expand All @@ -179,6 +198,9 @@ use pacman::{Aur, Pacman};
pub mod dnf;
use dnf::Dnf;

pub mod xbps;
use xbps::Xbps;

use regex::Regex;

use super::prelude::*;
Expand Down Expand Up @@ -206,6 +228,7 @@ pub enum PackageManager {
Pacman,
Aur,
Dnf,
Xbps,
}

pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
Expand All @@ -232,6 +255,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let aur = any_format_contains!("aur");
let pacman = any_format_contains!("pacman");
let dnf = any_format_contains!("dnf");
let xbps = any_format_contains!("xbps");

if !config.package_manager.contains(&PackageManager::Apt) && apt {
config.package_manager.push(PackageManager::Apt);
Expand All @@ -245,6 +269,9 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
if !config.package_manager.contains(&PackageManager::Dnf) && dnf {
config.package_manager.push(PackageManager::Dnf);
}
if !config.package_manager.contains(&PackageManager::Xbps) && xbps {
config.package_manager.push(PackageManager::Xbps);
}

let warning_updates_regex = config
.warning_updates_regex
Expand Down Expand Up @@ -275,6 +302,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
config.aur_command.clone().error("aur_command is not set")?,
)),
PackageManager::Dnf => Box::new(Dnf::new()),
PackageManager::Xbps => Box::new(Xbps::new()),
});
}

Expand Down
38 changes: 38 additions & 0 deletions src/blocks/packages/xbps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use tokio::process::Command;

use super::*;

#[derive(Default)]
pub struct Xbps;

impl Xbps {
pub fn new() -> Self {
Default::default()
}
}

#[async_trait]
impl Backend for Xbps {
fn name(&self) -> Cow<'static, str> {
"xbps".into()
}

async fn get_updates_list(&self) -> Result<Vec<String>> {
let stdout = Command::new("xbps-install")
.env("LC_LANG", "C")
.args(["-M", "-u", "-n"])
.output()
.await
.error("Problem running xbps-install command")?
.stdout;

let updates = String::from_utf8(stdout).expect("xbps-install produced non-UTF8 output");
let updates_list: Vec<String> = updates
.lines()
.filter(|line| line.len() > 1)
.map(|line| line.to_string())
.collect();

Ok(updates_list)
}
}

0 comments on commit 0afe30d

Please sign in to comment.