use nix crate instead of libc

Provides a higher level and lighter alternative.
This commit is contained in:
raf 2024-08-03 20:56:02 +03:00
parent 29e4ccc806
commit aae5f71004
Signed by: NotAShelf
GPG key ID: AF26552424E53993
4 changed files with 48 additions and 42 deletions

32
Cargo.lock generated
View file

@ -32,6 +32,12 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "bitflags"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.1.7" version = "1.1.7"
@ -44,6 +50,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]] [[package]]
name = "color-eyre" name = "color-eyre"
version = "0.6.3" version = "0.6.3"
@ -67,12 +79,6 @@ dependencies = [
"once_cell", "once_cell",
] ]
[[package]]
name = "fs_extra"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
[[package]] [[package]]
name = "gimli" name = "gimli"
version = "0.28.1" version = "0.28.1"
@ -102,8 +108,8 @@ name = "microfetch"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"color-eyre", "color-eyre",
"fs_extra",
"libc", "libc",
"nix",
] ]
[[package]] [[package]]
@ -115,6 +121,18 @@ dependencies = [
"adler", "adler",
] ]
[[package]]
name = "nix"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [
"bitflags",
"cfg-if",
"cfg_aliases",
"libc",
]
[[package]] [[package]]
name = "object" name = "object"
version = "0.32.2" version = "0.32.2"

View file

@ -5,7 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
libc = "0.2" libc = "0.2"
fs_extra = "1.2" nix = {version = "0.29", features = ["fs"]}
color-eyre = { version = "0.6", default-features = false } color-eyre = { version = "0.6", default-features = false }

View file

@ -9,7 +9,7 @@ use color_eyre::{Report, Result};
use crate::colors::{BLUE, CYAN, RESET}; use crate::colors::{BLUE, CYAN, RESET};
use crate::desktop::get_desktop_info; use crate::desktop::get_desktop_info;
use crate::release::{get_os_pretty_name, get_system_info}; use crate::release::{get_os_pretty_name, get_system_info};
use crate::system::{get_disk_usage, get_memory_usage, get_username_and_hostname}; use crate::system::{get_memory_usage, get_root_disk_usage, get_username_and_hostname};
use crate::uptime::get_system_uptime; use crate::uptime::get_system_uptime;
fn main() -> Result<(), Report> { fn main() -> Result<(), Report> {
@ -21,7 +21,7 @@ fn main() -> Result<(), Report> {
let uptime = get_system_uptime().expect("Failed to get uptime"); let uptime = get_system_uptime().expect("Failed to get uptime");
let window_manager = get_desktop_info().expect("Failed to get desktop info"); let window_manager = get_desktop_info().expect("Failed to get desktop info");
let memory_usage = get_memory_usage().expect("Failed to get memory usage"); let memory_usage = get_memory_usage().expect("Failed to get memory usage");
let storage = get_disk_usage().expect("Failed to get storage info"); let storage = get_root_disk_usage().expect("Failed to get storage info");
// Construct the ASCII art with dynamic OS name // Construct the ASCII art with dynamic OS name
@ -33,7 +33,7 @@ fn main() -> Result<(), Report> {
{BLUE}🬋🬋🬫 {CYAN}🬛🬋🬋 {CYAN} {BLUE}Uptime{RESET} {} {BLUE}🬋🬋🬫 {CYAN}🬛🬋🬋 {CYAN} {BLUE}Uptime{RESET} {}
{BLUE} 🬷🮃{CYAN} {CYAN} {BLUE}WM{RESET} {} {BLUE} 🬷🮃{CYAN} {CYAN} {BLUE}WM{RESET} {}
{BLUE} 🮃 {CYAN}🬴{BLUE}🬴 {CYAN}󰍛 {BLUE}Memory{RESET} {} {BLUE} 🮃 {CYAN}🬴{BLUE}🬴 {CYAN}󰍛 {BLUE}Memory{RESET} {}
{CYAN} {BLUE} {CYAN}󱥎 {BLUE}Storage{RESET} {} {CYAN} {BLUE} {CYAN}󱥎 {BLUE}Storage (/){RESET} {}
", ",
user_info, os_name, kernel_version, uptime, window_manager, memory_usage, storage user_info, os_name, kernel_version, uptime, window_manager, memory_usage, storage
); );

View file

@ -1,6 +1,6 @@
use libc::statfs as libc_statfs_struct; use nix::sys::statvfs::statvfs;
use std::env; use std::env;
use std::ffi::CString;
use std::fs::File; use std::fs::File;
use std::io::{self, BufRead}; use std::io::{self, BufRead};
use std::path::Path; use std::path::Path;
@ -16,34 +16,22 @@ pub fn get_username_and_hostname() -> Result<String, io::Error> {
Ok(format!("{YELLOW}{}{RED}@{GREEN}{}", username, hostname)) Ok(format!("{YELLOW}{}{RED}@{GREEN}{}", username, hostname))
} }
pub fn get_disk_usage() -> Result<String, io::Error> { pub fn get_root_disk_usage() -> Result<String, Box<dyn std::error::Error>> {
let path = CString::new("/").expect("CString::new failed"); let vfs = statvfs("/")?;
let block_size = vfs.block_size() as u64;
let total_blocks = vfs.blocks();
let available_blocks = vfs.blocks_available();
let mut fs_stat: libc_statfs_struct = unsafe { std::mem::zeroed() }; let total_size = block_size * total_blocks;
let result = unsafe { libc::statfs(path.as_ptr(), &mut fs_stat) }; let used_size = total_size - (block_size * available_blocks);
if result != 0 { let total_size_gib = total_size as f64 / (1024.0 * 1024.0 * 1024.0);
return Err(io::Error::last_os_error()); let used_size_gib = used_size as f64 / (1024.0 * 1024.0 * 1024.0);
} let usage_percentage = (used_size as f64 / total_size as f64) * 100.0;
let block_size = fs_stat.f_bsize as u64;
let total_blocks = fs_stat.f_blocks as u64;
let free_blocks = fs_stat.f_bfree as u64;
let total_size_bytes = total_blocks * block_size;
let used_size_bytes = (total_blocks - free_blocks) * block_size;
let total_size_gib = total_size_bytes as f64 / (1024.0 * 1024.0 * 1024.0);
let used_size_gib = used_size_bytes as f64 / (1024.0 * 1024.0 * 1024.0);
let percentage_used = (used_size_bytes as f64 / total_size_bytes as f64) * 100.0;
let formatted_total_size = format!("{:.2}", total_size_gib);
let formatted_used_size = format!("{:.2}", used_size_gib);
let formatted_percentage_used = format!("{:.0}", percentage_used);
Ok(format!( Ok(format!(
"{} GiB / {} GiB ({CYAN}{}%{RESET})", "{:.2} GiB / {:.2} GiB ({CYAN}{:.0}%{RESET})",
formatted_used_size, formatted_total_size, formatted_percentage_used used_size_gib, total_size_gib, usage_percentage
)) ))
} }