From 24af00ab15830915d77168d89ede652a68839ba5 Mon Sep 17 00:00:00 2001 From: Jakob Ledig Date: Mon, 23 Oct 2023 12:22:52 +0200 Subject: [PATCH] Updated dependencies, fixed megabyte display --- .gitignore | 1 + Cargo.toml | 6 +++--- src/main.rs | 32 +++++++++++++++++++++++++++----- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 22d3516..a5f0c7b 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ Cargo.lock # Added by cargo /target +/.idea/** diff --git a/Cargo.toml b/Cargo.toml index 3812721..b24275d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,6 @@ publish = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -colored = "2.0.0" -whoami = "1.1.1" -sysinfo = { version = "0.23.2", default-features = false } +colored = "2.0.4" +whoami = "1.4.1" +sysinfo = { version = "0.29.10", default-features = false } diff --git a/src/main.rs b/src/main.rs index 93ab3d9..071ce2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use colored::*; use std::string::ToString; -use sysinfo::{System, SystemExt, RefreshKind, ProcessorExt}; +use sysinfo::{System, SystemExt, RefreshKind, CpuExt}; const FERRIS_ART: &[&str] = &[ " ", @@ -21,6 +21,15 @@ const FERRIS_ART: &[&str] = &[ " ", ]; +const BYTES_TO_MEGABYTES_FACTOR: u64 = 1024 * 1024; + +/// Some values are hard to interpret when displayed as a numbers of bytes, so this function +/// converts a number of byte as number of megabytes. +fn bytes_to_megabytes(nob: u64) -> u64 { + nob / BYTES_TO_MEGABYTES_FACTOR + +} + fn exc(exc: &str) -> Result { let mut exc = exc.split_whitespace(); let mut cmd = std::process::Command::new(exc.next().unwrap()); @@ -66,12 +75,14 @@ fn main() { art = false; } - let mut sys = System::new_with_specifics(RefreshKind::new().with_cpu()); + let mut sys = System::new_with_specifics(RefreshKind::new()); sys.refresh_all(); let kernel = sys.kernel_version().unwrap_or_else(|| "Unknown".into()); - let total_ram = sys.total_memory(); - let used_ram = sys.used_memory(); - let cpu = sys.processors()[0].brand(); + + let total_ram = bytes_to_megabytes(sys.total_memory()); + let used_ram = bytes_to_megabytes(sys.used_memory()); + + let cpu = sys.cpus()[0].brand(); let rustc_ver = get_ver("rustc -V"); let cargo_ver = get_ver("cargo -V"); @@ -145,3 +156,14 @@ fn main() { ], ); } + +#[cfg(test)] +mod tests { + use crate::bytes_to_megabytes; + + #[test] + fn bytes_to_megabytes_test() { + let bytes = 32 * 1024 * 1024; + assert_eq!(bytes_to_megabytes(bytes), 32); + } +} \ No newline at end of file