Merge pull request #37 from MustafaSalih1993/dev

added reading load average
This commit is contained in:
Mustafa Salih 2021-01-19 06:38:03 +03:00 committed by GitHub
commit e6f9ce2ed5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
* Battery Percentage
* Cpu Temperature
* Load Average
* Uptime
* Mpd Current Song
* Spotify Current Song

View File

@ -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'

View File

@ -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,
},
}
}

View File

@ -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<String> = vec![String::from(""); 10];
let mut bar: Vec<String> = 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

View File

@ -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<name::NameConnection>,
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 cpu;
pub mod disk;
pub mod load_average;
pub mod memory;
pub mod mpd;
pub mod spotify;