diff --git a/Cargo.lock b/Cargo.lock index d868979..72786a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -446,7 +446,7 @@ checksum = "4e1b7878800220a76a08f32c057829511440f65528b63b940f2f2bc145d7ac68" [[package]] name = "rsblocks" -version = "0.1.9" +version = "0.1.11" dependencies = [ "alsa", "breadx", diff --git a/Cargo.toml b/Cargo.toml index 3c5abd8..37e34b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rsblocks" -version = "0.1.9" +version = "0.1.11" authors = ["mustafa salih "] edition = "2018" readme = "README.md" diff --git a/README.md b/README.md index e061a7d..a75e195 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ A multi threaded fast status bar for dwm window manager written in **Rust** 🦀 ## Features * Multi Threads * Time/Date +* Net Usage * Memory Usage * Disk Usage * Weather Temperature diff --git a/rsblocks.yml b/rsblocks.yml index 2e32148..d5981ff 100644 --- a/rsblocks.yml +++ b/rsblocks.yml @@ -1,9 +1,7 @@ # This is the full configuration template available for rsblocks. - # NOTE: the (delay) is in **SECONDS** and the float point "." is required. -general: - seperator: '┃' +seperator: '┃' # Time always running, no enabled option for this @@ -12,6 +10,14 @@ time: format: '%d %b, %I:%M:%S %p' delay: 1.0 + +# ethernet/wifi bandwith +netspeed: + transmit_icon: '' + recieve_icon: '' + interface: 'wlan0' + enabled: false + memory: icon: '▦' diff --git a/src/config/mod.rs b/src/config/mod.rs index 1a1ffd5..5741b11 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,26 +1,28 @@ mod battery; mod cputemp; mod disk; +mod loadavg; mod memory; mod mpd; +mod netspeed; +mod spotify; mod time; mod uptime; mod volume; mod weather; -mod spotify; -mod loadavg; use self::mpd::Mpd; use battery::Battery; use cputemp::CpuTemp; use disk::Disk; +use loadavg::LoadAvg; use memory::Memory; +use netspeed::NetSpeed; +use spotify::Spotify; use time::Time; use uptime::Uptime; use volume::Volume; use weather::Weather; -use spotify::Spotify; -use loadavg::LoadAvg; use std::default::Default; use std::fs::File; @@ -36,7 +38,8 @@ lazy_static! { match File::open(&yml_source) { Ok(mut file) => { - file.read_to_string(&mut data).expect("Failed to read config to string"); + file.read_to_string(&mut data) + .expect("Failed to read config to string"); serde_yaml::from_str(&data).expect("Failed to parse config") } Err(_) => Config::default(), @@ -80,7 +83,10 @@ pub struct Config { pub spotify: Spotify, #[serde(default)] - pub loadavg: LoadAvg + pub loadavg: LoadAvg, + + #[serde(default)] + pub netspeed: NetSpeed, } impl Default for Config { @@ -97,7 +103,8 @@ impl Default for Config { uptime: Default::default(), mpd: Default::default(), spotify: Default::default(), - loadavg: Default::default() + loadavg: Default::default(), + netspeed: Default::default(), } } } diff --git a/src/config/netspeed.rs b/src/config/netspeed.rs new file mode 100644 index 0000000..3e6a536 --- /dev/null +++ b/src/config/netspeed.rs @@ -0,0 +1,21 @@ +use serde::{Deserialize, Serialize}; +use std::default::Default; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct NetSpeed { + pub transmit_icon: String, + pub recieve_icon: String, + pub interface: String, + pub enabled: bool, +} + +impl Default for NetSpeed { + fn default() -> Self { + NetSpeed { + transmit_icon: String::from(""), + recieve_icon: String::from(""), + interface: String::from("wlan0"), + enabled: false, + } + } +} diff --git a/src/run.rs b/src/run.rs index c6c72a6..141e32d 100644 --- a/src/run.rs +++ b/src/run.rs @@ -1,12 +1,10 @@ +use crate::config::CONFIG; use crate::types::*; use crate::utils::*; -use crate::config::CONFIG; 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(mut blocks: Blocks) { let (tx, rx) = mpsc::channel(); @@ -49,6 +47,16 @@ pub fn run(mut blocks: Blocks) { }); } + // net speed thread + if CONFIG.netspeed.enabled { + let net_tx = tx.clone(); + thread::spawn(move || loop { + // get_netspeed will sleep inside the function + let net_data = ThreadsData::NetSpeed(netspeed::get_netspeed()); + net_tx.send(net_data).unwrap(); + }); + } + // Disk thread if CONFIG.disk.enabled { let disk_tx = tx.clone(); @@ -122,7 +130,7 @@ pub fn run(mut blocks: Blocks) { //Main { // NOTE: order matters to the final format - let mut bar: Vec = vec![String::from(""); 11]; + let mut bar: Vec = vec![String::from(""); 12]; //iterating the values recieved from the threads for data in rx { match data { @@ -130,13 +138,14 @@ pub fn run(mut blocks: Blocks) { ThreadsData::Mpd(x) => bar[1] = x, ThreadsData::Sound(x) => bar[2] = x, ThreadsData::Weather(x) => bar[3] = x, - ThreadsData::Disk(x) => bar[4] = x, - ThreadsData::Memory(x) => bar[5] = x, - ThreadsData::CpuTemp(x) => bar[6] = 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, + ThreadsData::NetSpeed(x) => bar[4] = x, + ThreadsData::Disk(x) => bar[5] = x, + ThreadsData::Memory(x) => bar[6] = x, + ThreadsData::CpuTemp(x) => bar[7] = x, + ThreadsData::LoadAvg(x) => bar[8] = x, + ThreadsData::Battery(x) => bar[9] = x, + ThreadsData::Uptime(x) => bar[10] = x, + ThreadsData::Time(x) => bar[11] = x, } // match ends here diff --git a/src/types.rs b/src/types.rs index a4d597e..7184d47 100644 --- a/src/types.rs +++ b/src/types.rs @@ -13,6 +13,7 @@ pub enum ThreadsData { Uptime(String), Spotify(String), LoadAvg(String), + NetSpeed(String), } #[derive(Clone)] @@ -113,6 +114,14 @@ pub struct LoadAvg { pub delay: f64, } +#[derive(Clone)] +pub struct NetSpeed { + pub transmit_icon: String, + pub recieve_icon: String, + pub interface: String, + pub enabled: bool, +} + pub struct Blocks { pub disp: Display, pub root: Window, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 6834d49..3b4eb7b 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -4,6 +4,7 @@ pub mod disk; pub mod load_average; pub mod memory; pub mod mpd; +pub mod netspeed; pub mod spotify; pub mod time; pub mod uptime; diff --git a/src/utils/netspeed.rs b/src/utils/netspeed.rs new file mode 100644 index 0000000..59f740f --- /dev/null +++ b/src/utils/netspeed.rs @@ -0,0 +1,45 @@ +use crate::config::CONFIG; +use std::fs::read_to_string; +use std::thread; +use std::time::Duration; + +pub fn get_netspeed() -> String { + let tx1: u64 = parse_speed_file("tx_bytes"); + let rx1: u64 = parse_speed_file("rx_bytes"); + thread::sleep(Duration::from_secs(1)); + let tx2: u64 = parse_speed_file("tx_bytes"); + let rx2: u64 = parse_speed_file("rx_bytes"); + + let tx_bps = tx2 - tx1; + let rx_bps = rx2 - rx1; + + let tx = calculate(tx_bps); + let rx = calculate(rx_bps); + + format!( + " {} {} {} {} {}", + CONFIG.netspeed.recieve_icon, rx, CONFIG.netspeed.transmit_icon, tx, CONFIG.seperator + ) +} + +fn parse_speed_file(pth: &str) -> u64 { + let base_path = format!("/sys/class/net/{}/statistics/", CONFIG.netspeed.interface); + let x: u64 = read_to_string(base_path.to_owned() + pth) + .unwrap() + .trim() + .parse::() + .unwrap(); + + x +} + +fn calculate(speed: u64) -> String { + let lookup = ["B", "kB", "MB"]; + let mut speed = speed as f64; + let mut idx = 0; + while speed >= 1024.0 && idx < lookup.len() { + speed /= 1024.0; + idx += 1; + } + format!("{:.1} {}", speed, lookup[idx]) +}