added reading load average

This commit is contained in:
mustafa salih 2021-01-18 09:42:18 +03:00
parent 4f83d10cf8
commit 3596f13cc3
7 changed files with 66 additions and 4 deletions

View File

@ -17,6 +17,7 @@ A multi threaded fast status bar for dwm window manager written in **Rust** 🦀
* Sound Volume * Sound Volume
* Battery Percentage * Battery Percentage
* Cpu Temperature * Cpu Temperature
* Load Average
* Uptime * Uptime
* Mpd Current Song * Mpd Current Song
* Spotify Current Song * Spotify Current Song

View File

@ -38,6 +38,13 @@ cpu_temperature:
delay: 120.0 delay: 120.0
# cpu load average
loadavg:
icon: ''
enable: false
delay: 2.0
mpd: mpd:
icon: '' icon: ''
host: '127.0.0.1' host: '127.0.0.1'

View File

@ -101,6 +101,11 @@ fn load_defaults() -> Config {
enabled: false, enabled: false,
delay: 15.0, 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_enabled = get_or_set_bool(doc, "spotify", "enable");
let spotify_delay = get_or_set_f64(doc, "spotify", "delay", 10.0); 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 { Config {
seperator, seperator,
time: Time { time: Time {
@ -229,6 +239,11 @@ fn parse_config(doc: &yaml::Yaml) -> Config {
enabled: spotify_enabled, enabled: spotify_enabled,
delay: spotify_delay, delay: spotify_delay,
}, },
loadavg: LoadAvg {
icon: loadavg_icon,
enabled: loadavg_enabled,
delay: loadavg_delay,
},
} }
} }

View File

@ -4,9 +4,21 @@ use std::sync::mpsc;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
/* This is ugly, maybe i will try to impliment a threadpool */
pub fn run(config: Config, mut blocks: Blocks) { pub fn run(config: Config, mut blocks: Blocks) {
let (tx, rx) = mpsc::channel(); 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 // spotify thread
if config.spotify.enabled { if config.spotify.enabled {
let spotify_tx = tx.clone(); let spotify_tx = tx.clone();
@ -120,7 +132,7 @@ pub fn run(config: Config, mut blocks: Blocks) {
//Main //Main
{ {
// NOTE: order matters to the final format // NOTE: order matters to the final format
let mut bar: Vec<String> = vec![String::from(""); 10]; let mut bar: Vec<String> = vec![String::from(""); 11];
//iterating the values recieved from the threads //iterating the values recieved from the threads
for data in rx { for data in rx {
match data { match data {
@ -131,9 +143,10 @@ pub fn run(config: Config, mut blocks: Blocks) {
ThreadsData::Disk(x) => bar[4] = x, ThreadsData::Disk(x) => bar[4] = x,
ThreadsData::Memory(x) => bar[5] = x, ThreadsData::Memory(x) => bar[5] = x,
ThreadsData::CpuTemp(x) => bar[6] = x, ThreadsData::CpuTemp(x) => bar[6] = x,
ThreadsData::Battery(x) => bar[7] = x, ThreadsData::LoadAvg(x) => bar[7] = x,
ThreadsData::Uptime(x) => bar[8] = x, ThreadsData::Battery(x) => bar[8] = x,
ThreadsData::Time(x) => bar[9] = x, ThreadsData::Uptime(x) => bar[9] = x,
ThreadsData::Time(x) => bar[10] = x,
} }
// match ends here // match ends here

View File

@ -12,6 +12,7 @@ pub enum ThreadsData {
CpuTemp(String), CpuTemp(String),
Uptime(String), Uptime(String),
Spotify(String), Spotify(String),
LoadAvg(String),
} }
#[derive(Clone)] #[derive(Clone)]
@ -27,6 +28,7 @@ pub struct Config {
pub uptime: Uptime, pub uptime: Uptime,
pub mpd: Mpd, pub mpd: Mpd,
pub spotify: Spotify, pub spotify: Spotify,
pub loadavg: LoadAvg,
} }
#[derive(Clone)] #[derive(Clone)]
@ -104,6 +106,13 @@ pub struct Spotify {
pub delay: f64, pub delay: f64,
} }
#[derive(Clone)]
pub struct LoadAvg {
pub icon: String,
pub enabled: bool,
pub delay: f64,
}
pub struct Blocks { pub struct Blocks {
pub disp: Display<name::NameConnection>, pub disp: Display<name::NameConnection>,
pub root: Window, pub root: Window,

16
src/utils/load_average.rs Normal file
View File

@ -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::<Vec<_>>()[0];
format!(" {} {} {}", config.loadavg.icon, buf, config.seperator)
}

View File

@ -1,6 +1,7 @@
pub mod battery; pub mod battery;
pub mod cpu; pub mod cpu;
pub mod disk; pub mod disk;
pub mod load_average;
pub mod memory; pub mod memory;
pub mod mpd; pub mod mpd;
pub mod spotify; pub mod spotify;