mirror of
https://github.com/NotAShelf/microfetch.git
synced 2024-11-01 06:41:15 +00:00
speeding it up
This commit is contained in:
parent
ef2f6dc56e
commit
81cdc0a281
5 changed files with 24 additions and 70 deletions
|
@ -4,7 +4,7 @@ version = "0.2.0"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
nix = {version = "0.29", features = ["fs"]}
|
||||
nix = {version = "0.29", features = ["fs", "hostname"]}
|
||||
color-eyre = { version = "0.6", default-features = false }
|
||||
|
||||
[profile.release]
|
||||
|
@ -13,6 +13,3 @@ opt-level = "z"
|
|||
lto = true
|
||||
codegen-units = 1
|
||||
panic = "abort"
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ mod release;
|
|||
mod system;
|
||||
mod uptime;
|
||||
|
||||
use color_eyre::{Report, Result};
|
||||
use nix::sys::sysinfo::sysinfo;
|
||||
|
||||
use crate::colors::{BLUE, CYAN, RESET};
|
||||
use crate::desktop::get_desktop_info;
|
||||
|
@ -32,8 +32,6 @@ fn main() -> Result<(), Report> {
|
|||
&memory_usage,
|
||||
&storage,
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_system_info(
|
||||
|
|
|
@ -1,38 +1,27 @@
|
|||
use color_eyre::Result;
|
||||
use std::fs::{self, read_to_string};
|
||||
use std::io::{self, Read};
|
||||
use std::io;
|
||||
|
||||
// Try to detect OS type as accurately as possible and without depending on uname.
|
||||
// /etc/os-release should generally imply Linux, and /etc/bsd-release would imply BSD system.
|
||||
fn detect_os() -> Result<String, io::Error> {
|
||||
fn detect_os() -> Result<&'static str, io::Error> {
|
||||
if fs::metadata("/etc/os-release").is_ok() || fs::metadata("/usr/lib/os-release").is_ok() {
|
||||
Ok("Linux".to_string())
|
||||
Ok("Linux")
|
||||
} else if fs::metadata("/etc/rc.conf").is_ok() || fs::metadata("/etc/bsd-release").is_ok() {
|
||||
Ok("BSD".to_string())
|
||||
Ok("BSD")
|
||||
} else {
|
||||
Ok("Unknown".to_string())
|
||||
Ok("Unknown")
|
||||
}
|
||||
}
|
||||
|
||||
fn get_architecture() -> Result<String, io::Error> {
|
||||
// Read architecture from /proc/sys/kernel/arch
|
||||
let mut arch = String::new();
|
||||
fs::File::open("/proc/sys/kernel/arch")?.read_to_string(&mut arch)?;
|
||||
let arch = arch.trim().to_string();
|
||||
Ok(arch)
|
||||
}
|
||||
|
||||
pub fn get_system_info() -> Result<String, io::Error> {
|
||||
let system = detect_os()?;
|
||||
|
||||
let mut kernel_release = String::new();
|
||||
fs::File::open("/proc/sys/kernel/osrelease")?.read_to_string(&mut kernel_release)?;
|
||||
let kernel_release = kernel_release.trim().to_string();
|
||||
let kernel_release = read_to_string("/proc/sys/kernel/osrelease")?;
|
||||
let kernel_release = kernel_release.trim();
|
||||
|
||||
let mut cpuinfo = String::new();
|
||||
fs::File::open("/proc/cpuinfo")?.read_to_string(&mut cpuinfo)?;
|
||||
|
||||
let architecture = get_architecture()?;
|
||||
let architecture = read_to_string("/proc/sys/kernel/arch")?;
|
||||
let architecture = architecture.trim();
|
||||
|
||||
let result = format!("{system} {kernel_release} ({architecture})");
|
||||
Ok(result)
|
||||
|
|
|
@ -1,17 +1,15 @@
|
|||
use nix::sys::statvfs::statvfs;
|
||||
use nix::sys::sysinfo::SysInfo;
|
||||
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
use std::io::{self};
|
||||
|
||||
use crate::colors::{CYAN, GREEN, RED, RESET, YELLOW};
|
||||
|
||||
pub fn get_username_and_hostname() -> Result<String, io::Error> {
|
||||
let username = env::var("USER").unwrap_or_else(|_| "unknown_user".to_string());
|
||||
let output = Command::new("hostname").output()?;
|
||||
let hostname = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
||||
let hostname = nix::unistd::gethostname()?;
|
||||
let hostname = hostname.to_string_lossy();
|
||||
|
||||
Ok(format!("{YELLOW}{username}{RED}@{GREEN}{hostname}"))
|
||||
}
|
||||
|
@ -34,49 +32,21 @@ pub fn get_root_disk_usage() -> Result<String, io::Error> {
|
|||
))
|
||||
}
|
||||
|
||||
pub fn get_memory_usage() -> Result<String, io::Error> {
|
||||
fn parse_memory_info() -> Result<(f64, f64), io::Error> {
|
||||
let path = Path::new("/proc/meminfo");
|
||||
let file = File::open(path)?;
|
||||
let reader = io::BufReader::new(file);
|
||||
|
||||
let mut total_memory_kb = 0.0;
|
||||
let mut available_memory_kb = 0.0;
|
||||
|
||||
for line in reader.lines() {
|
||||
let line = line?;
|
||||
if line.starts_with("MemTotal:") {
|
||||
total_memory_kb = line
|
||||
.split_whitespace()
|
||||
.nth(1)
|
||||
.ok_or_else(|| {
|
||||
io::Error::new(io::ErrorKind::InvalidData, "Failed to parse MemTotal")
|
||||
})?
|
||||
.parse::<f64>()
|
||||
.unwrap_or(0.0);
|
||||
} else if line.starts_with("MemAvailable:") {
|
||||
available_memory_kb = line
|
||||
.split_whitespace()
|
||||
.nth(1)
|
||||
.ok_or_else(|| {
|
||||
io::Error::new(io::ErrorKind::InvalidData, "Failed to parse MemAvailable")
|
||||
})?
|
||||
.parse::<f64>()
|
||||
.unwrap_or(0.0);
|
||||
}
|
||||
}
|
||||
pub fn get_memory_usage(info: SysInfo) -> String {
|
||||
#[inline(always)]
|
||||
fn parse_memory_info(info: SysInfo) -> (f64, f64) {
|
||||
let total_memory_kb = (info.ram_total() / 1024) as f64;
|
||||
let available_memory_kb = (info.ram_unused() / 1024) as f64;
|
||||
|
||||
let total_memory_gb = total_memory_kb / (1024.0 * 1024.0);
|
||||
let available_memory_gb = available_memory_kb / (1024.0 * 1024.0);
|
||||
let used_memory_gb = total_memory_gb - available_memory_gb;
|
||||
|
||||
Ok((used_memory_gb, total_memory_gb))
|
||||
(used_memory_gb, total_memory_gb)
|
||||
}
|
||||
|
||||
let (used_memory, total_memory) = parse_memory_info()?;
|
||||
let (used_memory, total_memory) = parse_memory_info(info);
|
||||
let percentage_used = (used_memory / total_memory * 100.0).round() as u64;
|
||||
|
||||
Ok(format!(
|
||||
"{used_memory:.2} GiB / {total_memory:.2} GiB ({CYAN}{percentage_used}%{RESET})"
|
||||
))
|
||||
format!("{used_memory:.2} GiB / {total_memory:.2} GiB ({CYAN}{percentage_used}%{RESET})")
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ pub fn get_current() -> Result<String, io::Error> {
|
|||
let hours = (total_minutes % (60 * 24)) / 60;
|
||||
let minutes = total_minutes % 60;
|
||||
|
||||
let mut parts = Vec::new();
|
||||
let mut parts = Vec::with_capacity(3);
|
||||
if days > 0 {
|
||||
parts.push(format!("{days} days"));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue