Major code cleanup (idk how this works, thx chris)

This commit is contained in:
Sebastian Cabrera 2024-06-23 02:33:36 -04:00
parent 674fc191b7
commit 2884a7c1b2
No known key found for this signature in database
GPG key ID: 9C70960A46161D08

View file

@ -1,9 +1,14 @@
use sysinfo::System;
use std::fmt::Display;
fn main() {
get_sys_info();
}
fn printify<A: AsRef<str>, B: Display>(a: A, b: B) {
println!("{:30}{}", a.as_ref(), b);
}
fn get_sys_info() {
let bytes_per_gib: u64 = 2u64.pow(30);
@ -15,18 +20,39 @@ fn get_sys_info() {
// First we update all information of our `System` struct.
sys.refresh_all();
// Store all system information:
let hostname = System::host_name().unwrap();
let os_name = System::name().unwrap();
let os_version = System::os_version().unwrap();
let uptime_hours = System::uptime() / 3600;
let uptime_minutes = (System::uptime() % 3600) / 60;
let uptime = format!("{} hour(s) and {} minute(s)", uptime_hours, uptime_minutes);
let kernel_version = System::kernel_version().unwrap();
let cpu_model = sys.cpus()[0].brand();
let cpu_cores = sys.physical_core_count().unwrap();
let cpu_threads = sys.cpus().len();
let cpu_topology = format!("{} cores {} threads", cpu_cores, cpu_threads);
let cpu_architecture = System::cpu_arch().unwrap();
let total_memory = sys.total_memory() as f64 / bytes_per_gib as f64;
let memory = format!("{:.2} GB", total_memory);
// Print system information:
println!("System Name: {}", System::name().unwrap());
println!("Kernel Version: {}", System::kernel_version().unwrap());
println!("System OS Version: {}", System::os_version().unwrap());
println!("System Hostname: {}", System::host_name().unwrap());
printify("Hostname:", hostname);
printify("OS Name:", os_name);
printify("OS Version:", os_version);
printify("Uptime:", uptime);
printify("Kernel Version:", kernel_version);
// Print CPU information:
println!("CPU Model: {}", sys.cpus()[0].brand());
println!("CPU Architecture: {}", System::cpu_arch().unwrap());
println!("Number of Cores: {}", sys.physical_core_count().unwrap());
println!("Number of Cores + Threads: {}", sys.cpus().len());
printify("CPU Model:", cpu_model);
printify("CPU Topology:", cpu_topology);
printify("CPU Architecture:", cpu_architecture);
// Print memory information:
println!("Total Memory: {:.2} GB", sys.total_memory() as f64 / bytes_per_gib as f64);
printify("Total Memory:", memory);
}