getting load-average programmatically, fully support BSD soon

This commit is contained in:
Mustafa 2021-05-11 21:21:26 +03:00
parent 96b47f1999
commit 674f6811fe
No known key found for this signature in database
GPG Key ID: CDF68E7828991979
3 changed files with 8 additions and 14 deletions

2
Cargo.lock generated
View File

@ -527,7 +527,7 @@ dependencies = [
[[package]]
name = "rsblocks"
version = "0.1.16"
version = "0.1.17"
dependencies = [
"alsa",
"battery",

View File

@ -1,6 +1,6 @@
[package]
name = "rsblocks"
version = "0.1.16"
version = "0.1.17"
authors = ["mustafa salih <mustafasalih1991@gmail.com>"]
edition = "2018"
readme = "README.md"

View File

@ -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::<Vec<_>>()[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)
}