mirror of
https://github.com/NotAShelf/microfetch.git
synced 2024-11-22 15:40:41 +00:00
use nix crate instead of libc
Provides a higher level and lighter alternative.
This commit is contained in:
parent
29e4ccc806
commit
aae5f71004
4 changed files with 48 additions and 42 deletions
32
Cargo.lock
generated
32
Cargo.lock
generated
|
@ -32,6 +32,12 @@ dependencies = [
|
|||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.1.7"
|
||||
|
@ -44,6 +50,12 @@ version = "1.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "cfg_aliases"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
|
||||
|
||||
[[package]]
|
||||
name = "color-eyre"
|
||||
version = "0.6.3"
|
||||
|
@ -67,12 +79,6 @@ dependencies = [
|
|||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fs_extra"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
|
||||
|
||||
[[package]]
|
||||
name = "gimli"
|
||||
version = "0.28.1"
|
||||
|
@ -102,8 +108,8 @@ name = "microfetch"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"color-eyre",
|
||||
"fs_extra",
|
||||
"libc",
|
||||
"nix",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -115,6 +121,18 @@ dependencies = [
|
|||
"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]]
|
||||
name = "object"
|
||||
version = "0.32.2"
|
||||
|
|
|
@ -5,7 +5,7 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
libc = "0.2"
|
||||
fs_extra = "1.2"
|
||||
nix = {version = "0.29", features = ["fs"]}
|
||||
color-eyre = { version = "0.6", default-features = false }
|
||||
|
||||
|
||||
|
|
16
src/main.rs
16
src/main.rs
|
@ -9,7 +9,7 @@ use color_eyre::{Report, Result};
|
|||
use crate::colors::{BLUE, CYAN, RESET};
|
||||
use crate::desktop::get_desktop_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;
|
||||
|
||||
fn main() -> Result<(), Report> {
|
||||
|
@ -21,19 +21,19 @@ fn main() -> Result<(), Report> {
|
|||
let uptime = get_system_uptime().expect("Failed to get uptime");
|
||||
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 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
|
||||
|
||||
println!(
|
||||
"
|
||||
{CYAN} ▗▄ {BLUE}▗▄ ▄▖ {} ~{RESET}
|
||||
{CYAN} ▄▄🬸█▄▄▄{BLUE}🬸█▛ {CYAN}▃ {CYAN} {BLUE}System{RESET} {}
|
||||
{BLUE} ▟▛ ▜{CYAN}▃▟🬕 {CYAN} {BLUE}Kernel{RESET} {}
|
||||
{BLUE}🬋🬋🬫█ {CYAN}█🬛🬋🬋 {CYAN} {BLUE}Uptime{RESET} {}
|
||||
{BLUE} 🬷▛🮃{CYAN}▙ ▟▛ {CYAN} {BLUE}WM{RESET} {}
|
||||
{BLUE} 🮃 {CYAN}▟█🬴{BLUE}▀▀▀█🬴▀▀ {CYAN} {BLUE}Memory{RESET} {}
|
||||
{CYAN} ▝▀ ▀▘ {BLUE}▀▘ {CYAN} {BLUE}Storage{RESET} {}
|
||||
{CYAN} ▄▄🬸█▄▄▄{BLUE}🬸█▛ {CYAN}▃ {CYAN} {BLUE}System{RESET} {}
|
||||
{BLUE} ▟▛ ▜{CYAN}▃▟🬕 {CYAN} {BLUE}Kernel{RESET} {}
|
||||
{BLUE}🬋🬋🬫█ {CYAN}█🬛🬋🬋 {CYAN} {BLUE}Uptime{RESET} {}
|
||||
{BLUE} 🬷▛🮃{CYAN}▙ ▟▛ {CYAN} {BLUE}WM{RESET} {}
|
||||
{BLUE} 🮃 {CYAN}▟█🬴{BLUE}▀▀▀█🬴▀▀ {CYAN} {BLUE}Memory{RESET} {}
|
||||
{CYAN} ▝▀ ▀▘ {BLUE}▀▘ {CYAN} {BLUE}Storage (/){RESET} {}
|
||||
",
|
||||
user_info, os_name, kernel_version, uptime, window_manager, memory_usage, storage
|
||||
);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use libc::statfs as libc_statfs_struct;
|
||||
use nix::sys::statvfs::statvfs;
|
||||
|
||||
use std::env;
|
||||
use std::ffi::CString;
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
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))
|
||||
}
|
||||
|
||||
pub fn get_disk_usage() -> Result<String, io::Error> {
|
||||
let path = CString::new("/").expect("CString::new failed");
|
||||
pub fn get_root_disk_usage() -> Result<String, Box<dyn std::error::Error>> {
|
||||
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 result = unsafe { libc::statfs(path.as_ptr(), &mut fs_stat) };
|
||||
let total_size = block_size * total_blocks;
|
||||
let used_size = total_size - (block_size * available_blocks);
|
||||
|
||||
if result != 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
|
||||
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);
|
||||
let total_size_gib = total_size as f64 / (1024.0 * 1024.0 * 1024.0);
|
||||
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;
|
||||
|
||||
Ok(format!(
|
||||
"{} GiB / {} GiB ({CYAN}{}%{RESET})",
|
||||
formatted_used_size, formatted_total_size, formatted_percentage_used
|
||||
"{:.2} GiB / {:.2} GiB ({CYAN}{:.0}%{RESET})",
|
||||
used_size_gib, total_size_gib, usage_percentage
|
||||
))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue