Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ However, maintaining clean code is also a priority.. <br>
<img width="40%" alt="image" src="https://github.com/user-attachments/assets/85c006b2-c604-4346-b24a-8a515a1b3fd2" />


### supports the following package managers

- xbps ( [Void Linux](https://voidlinux.org/) )
- apk ( [Alpine Linux](https://wiki.alpinelinux.org/wiki/Main_Page) )
- pacman ( [Arch Linux](https://archlinux.org/) )
- emerge ( [Gentoo Linux](https://www.gentoo.org/) )

btw, i'll be happy if somebody send some cool ascii cat arts in issues :point_right::point_left:

### installation
Expand Down
9 changes: 3 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod host;
mod memory;
mod package_managers;
mod packages;
mod uptime;
use crate::{host::get_host, package_managers::PackageManagers};
use crate::host::get_host;
use colored::*;

#[cfg(not(unix))]
Expand All @@ -27,10 +27,7 @@ fn main() {
host = get_host().unwrap_or("Unknown".to_owned()),
uptime = uptime::get().unwrap_or_default(),
memory = memory::get().unwrap_or_default(),
pkgs = PackageManagers::new()
.count_pkgs_by_distro()
.expect("Failed to get package manager for this distro !")
.expect("Failed to count packages in this distro !"),
pkgs = packages::get().unwrap_or("Unknown".to_owned()),
os_field = wrap_field("os"),
host_field = wrap_field("host"),
memory_field = wrap_field("memory"),
Expand Down
31 changes: 0 additions & 31 deletions src/package_managers/alpine.rs

This file was deleted.

28 changes: 0 additions & 28 deletions src/package_managers/emerge.rs

This file was deleted.

58 changes: 0 additions & 58 deletions src/package_managers/mod.rs

This file was deleted.

7 changes: 0 additions & 7 deletions src/package_managers/package_manager.rs

This file was deleted.

34 changes: 0 additions & 34 deletions src/package_managers/pacman.rs

This file was deleted.

40 changes: 0 additions & 40 deletions src/package_managers/xbps.rs

This file was deleted.

9 changes: 9 additions & 0 deletions src/packages/apk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::io;

use super::read_lines;

pub fn count() -> io::Result<u64> {
Ok(read_lines("/lib/apk/db/installed")?
.filter(|line| line.starts_with("P:"))
.count() as u64)
}
9 changes: 9 additions & 0 deletions src/packages/emerge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::{fs, io};

pub fn count() -> io::Result<u64> {
Ok(fs::read_dir("/var/db/pkg")?
.flatten()
.filter(|entry| entry.metadata().is_ok_and(|v| v.is_dir()))
.flat_map(|category| fs::read_dir(category.path()).map(|v| v.count() as u64))
.sum())
}
47 changes: 47 additions & 0 deletions src/packages/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
mod apk;
mod emerge;
mod pacman;
mod xbps;

use std::{
fs::File,
io::{self, BufRead, BufReader},
path::Path,
};

type Count = fn() -> io::Result<u64>;

const MANAGERS: [(&str, Count); 4] = [
("xbps", xbps::count),
("apk", apk::count),
("pacman", pacman::count),
("emerge", emerge::count),
];

// Counts installed packages of every package manager found on the system.
// A single manager is shown as just the count, several as "xbps(632), apk(251)".
pub fn get() -> io::Result<String> {
let counts: Vec<_> = MANAGERS
.iter()
.filter_map(|(name, count)| count().ok().map(|count| (name, count)))
.collect();

match counts.as_slice() {
[] => Err(io::Error::new(
io::ErrorKind::NotFound,
"no supported package manager found",
)),
[(_, count)] => Ok(count.to_string()),
_ => Ok(counts
.iter()
.map(|(name, count)| format!("{name}({count})"))
.collect::<Vec<_>>()
.join(", ")),
}
}

fn read_lines(path: impl AsRef<Path>) -> io::Result<impl Iterator<Item = String>> {
Ok(BufReader::new(File::open(path)?)
.lines()
.map_while(Result::ok))
}
10 changes: 10 additions & 0 deletions src/packages/pacman.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::{fs, io};

// Every installed package is a "<name>-<version>-<rel>" directory;
// the only other entry is the ALPM_DB_VERSION file.
pub fn count() -> io::Result<u64> {
Ok(fs::read_dir("/var/lib/pacman/local")?
.flatten()
.filter(|entry| entry.file_type().is_ok_and(|v| v.is_dir()))
.count() as u64)
}
14 changes: 14 additions & 0 deletions src/packages/xbps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::{fs, io, os::unix::ffi::OsStrExt};

use super::read_lines;

pub fn count() -> io::Result<u64> {
let pkgdb = fs::read_dir("/var/db/xbps")?
.flatten()
.find(|entry| entry.file_name().as_bytes().starts_with(b"pkgdb-"))
.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "xbps pkgdb not found"))?;

Ok(read_lines(pkgdb.path())?
.filter(|line| line.contains("<key>repository</key>"))
.count() as u64)
}
Loading