From 674f6811fe43dd0949052e225bf4c91a63de323e Mon Sep 17 00:00:00 2001 From: Mustafa Date: Tue, 11 May 2021 21:21:26 +0300 Subject: [PATCH] getting load-average programmatically, fully support BSD soon --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/utils/load_average.rs | 18 ++++++------------ 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 20c8bf8..8f5f388 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -527,7 +527,7 @@ dependencies = [ [[package]] name = "rsblocks" -version = "0.1.16" +version = "0.1.17" dependencies = [ "alsa", "battery", diff --git a/Cargo.toml b/Cargo.toml index 5e8c68e..cbc2a95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rsblocks" -version = "0.1.16" +version = "0.1.17" authors = ["mustafa salih "] edition = "2018" readme = "README.md" diff --git a/src/utils/load_average.rs b/src/utils/load_average.rs index 70e4a72..414ab6f 100644 --- a/src/utils/load_average.rs +++ b/src/utils/load_average.rs @@ -1,18 +1,12 @@ use crate::config::CONFIG; use crate::types::ThreadsData; -use std::fs::File; -use std::io::Read; +use nix::sys::sysinfo; pub fn get_load_avg() -> ThreadsData { - let mut buf = String::new(); - match File::open("/proc/loadavg") { - Ok(mut file) => match file.read_to_string(&mut buf) { - Ok(data) => data, - _ => return ThreadsData::LoadAvg(String::from("")), - }, - _ => return ThreadsData::LoadAvg(String::from("Error")), - }; - let buf = buf.split_whitespace().collect::>()[0]; - let data = format!(" {} {} {}", CONFIG.loadavg.icon, buf, CONFIG.seperator); + let load = sysinfo::sysinfo().unwrap().load_average().0; + let data = format!( + " {} {:.2} {}", + CONFIG.loadavg.icon, load, CONFIG.seperator + ); ThreadsData::LoadAvg(data) }