From 3596f13cc353aac4a675086af05643d2bad91ac1 Mon Sep 17 00:00:00 2001 From: mustafa salih Date: Mon, 18 Jan 2021 09:42:18 +0300 Subject: [PATCH] added reading load average --- README.md | 1 + rsblocks.yml | 7 +++++++ src/config.rs | 15 +++++++++++++++ src/run.rs | 21 +++++++++++++++++---- src/types.rs | 9 +++++++++ src/utils/load_average.rs | 16 ++++++++++++++++ src/utils/mod.rs | 1 + 7 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 src/utils/load_average.rs diff --git a/README.md b/README.md index 212ed3f..e061a7d 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ A multi threaded fast status bar for dwm window manager written in **Rust** 🦀 * Sound Volume * Battery Percentage * Cpu Temperature +* Load Average * Uptime * Mpd Current Song * Spotify Current Song diff --git a/rsblocks.yml b/rsblocks.yml index 65a5569..ff173ea 100644 --- a/rsblocks.yml +++ b/rsblocks.yml @@ -38,6 +38,13 @@ cpu_temperature: delay: 120.0 +# cpu load average +loadavg: + icon: '' + enable: false + delay: 2.0 + + mpd: icon: '' host: '127.0.0.1' diff --git a/src/config.rs b/src/config.rs index 5c76c6a..bd6805e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -101,6 +101,11 @@ fn load_defaults() -> Config { enabled: false, delay: 15.0, }, + loadavg: LoadAvg { + icon: String::from(""), + enabled: false, + delay: 60.0, + }, } } @@ -171,6 +176,11 @@ fn parse_config(doc: &yaml::Yaml) -> Config { let spotify_enabled = get_or_set_bool(doc, "spotify", "enable"); let spotify_delay = get_or_set_f64(doc, "spotify", "delay", 10.0); + //Load Avrage values + let loadavg_icon = get_or_set_string(doc, "loadavg", "icon", ""); + let loadavg_enabled = get_or_set_bool(doc, "loadavg", "enable"); + let loadavg_delay = get_or_set_f64(doc, "loadavg", "delay", 60.0); + Config { seperator, time: Time { @@ -229,6 +239,11 @@ fn parse_config(doc: &yaml::Yaml) -> Config { enabled: spotify_enabled, delay: spotify_delay, }, + loadavg: LoadAvg { + icon: loadavg_icon, + enabled: loadavg_enabled, + delay: loadavg_delay, + }, } } diff --git a/src/run.rs b/src/run.rs index c3d877d..1e23d54 100644 --- a/src/run.rs +++ b/src/run.rs @@ -4,9 +4,21 @@ use std::sync::mpsc; use std::thread; use std::time::Duration; +/* This is ugly, maybe i will try to impliment a threadpool */ + pub fn run(config: Config, mut blocks: Blocks) { let (tx, rx) = mpsc::channel(); + // loadavrage thread + if config.loadavg.enabled { + let loadavg_tx = tx.clone(); + let configcp = config.clone(); + thread::spawn(move || loop { + let loadavg_data = ThreadsData::LoadAvg(load_average::get_load_avg(&configcp)); + loadavg_tx.send(loadavg_data).unwrap(); + thread::sleep(Duration::from_secs_f64(configcp.loadavg.delay)) + }); + } // spotify thread if config.spotify.enabled { let spotify_tx = tx.clone(); @@ -120,7 +132,7 @@ pub fn run(config: Config, mut blocks: Blocks) { //Main { // NOTE: order matters to the final format - let mut bar: Vec = vec![String::from(""); 10]; + let mut bar: Vec = vec![String::from(""); 11]; //iterating the values recieved from the threads for data in rx { match data { @@ -131,9 +143,10 @@ pub fn run(config: Config, mut blocks: Blocks) { ThreadsData::Disk(x) => bar[4] = x, ThreadsData::Memory(x) => bar[5] = x, ThreadsData::CpuTemp(x) => bar[6] = x, - ThreadsData::Battery(x) => bar[7] = x, - ThreadsData::Uptime(x) => bar[8] = x, - ThreadsData::Time(x) => bar[9] = x, + ThreadsData::LoadAvg(x) => bar[7] = x, + ThreadsData::Battery(x) => bar[8] = x, + ThreadsData::Uptime(x) => bar[9] = x, + ThreadsData::Time(x) => bar[10] = x, } // match ends here diff --git a/src/types.rs b/src/types.rs index 99e0701..a4d597e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -12,6 +12,7 @@ pub enum ThreadsData { CpuTemp(String), Uptime(String), Spotify(String), + LoadAvg(String), } #[derive(Clone)] @@ -27,6 +28,7 @@ pub struct Config { pub uptime: Uptime, pub mpd: Mpd, pub spotify: Spotify, + pub loadavg: LoadAvg, } #[derive(Clone)] @@ -104,6 +106,13 @@ pub struct Spotify { pub delay: f64, } +#[derive(Clone)] +pub struct LoadAvg { + pub icon: String, + pub enabled: bool, + pub delay: f64, +} + pub struct Blocks { pub disp: Display, pub root: Window, diff --git a/src/utils/load_average.rs b/src/utils/load_average.rs new file mode 100644 index 0000000..2cd271f --- /dev/null +++ b/src/utils/load_average.rs @@ -0,0 +1,16 @@ +use crate::types::Config; +use std::fs::File; +use std::io::Read; + +pub fn get_load_avg(config: &Config) -> String { + let mut buf = String::new(); + match File::open("/proc/loadavg") { + Ok(mut file) => match file.read_to_string(&mut buf) { + Ok(data) => data, + _ => return String::from(""), + }, + _ => return String::from("Error"), + }; + let buf = buf.split_whitespace().collect::>()[0]; + format!(" {} {} {}", config.loadavg.icon, buf, config.seperator) +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index b497999..6834d49 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,6 +1,7 @@ pub mod battery; pub mod cpu; pub mod disk; +pub mod load_average; pub mod memory; pub mod mpd; pub mod spotify;