Reads network bandwith (#39)
* updated crate version * updated crate version * rsblocks now reads netspeed per second
This commit is contained in:
parent
9525a10751
commit
6c0dfddead
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -446,7 +446,7 @@ checksum = "4e1b7878800220a76a08f32c057829511440f65528b63b940f2f2bc145d7ac68"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rsblocks"
|
name = "rsblocks"
|
||||||
version = "0.1.9"
|
version = "0.1.11"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"alsa",
|
"alsa",
|
||||||
"breadx",
|
"breadx",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rsblocks"
|
name = "rsblocks"
|
||||||
version = "0.1.9"
|
version = "0.1.11"
|
||||||
authors = ["mustafa salih <mustafasalih1991@gmail.com>"]
|
authors = ["mustafa salih <mustafasalih1991@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
@ -11,6 +11,7 @@ A multi threaded fast status bar for dwm window manager written in **Rust** 🦀
|
|||||||
## Features
|
## Features
|
||||||
* Multi Threads
|
* Multi Threads
|
||||||
* Time/Date
|
* Time/Date
|
||||||
|
* Net Usage
|
||||||
* Memory Usage
|
* Memory Usage
|
||||||
* Disk Usage
|
* Disk Usage
|
||||||
* Weather Temperature
|
* Weather Temperature
|
||||||
|
10
rsblocks.yml
10
rsblocks.yml
@ -1,8 +1,6 @@
|
|||||||
# This is the full configuration template available for rsblocks.
|
# This is the full configuration template available for rsblocks.
|
||||||
|
|
||||||
# NOTE: the (delay) is in **SECONDS** and the float point "." is required.
|
# NOTE: the (delay) is in **SECONDS** and the float point "." is required.
|
||||||
|
|
||||||
general:
|
|
||||||
seperator: '┃'
|
seperator: '┃'
|
||||||
|
|
||||||
|
|
||||||
@ -13,6 +11,14 @@ time:
|
|||||||
delay: 1.0
|
delay: 1.0
|
||||||
|
|
||||||
|
|
||||||
|
# ethernet/wifi bandwith
|
||||||
|
netspeed:
|
||||||
|
transmit_icon: ''
|
||||||
|
recieve_icon: ''
|
||||||
|
interface: 'wlan0'
|
||||||
|
enabled: false
|
||||||
|
|
||||||
|
|
||||||
memory:
|
memory:
|
||||||
icon: '▦'
|
icon: '▦'
|
||||||
enabled: true
|
enabled: true
|
||||||
|
@ -1,26 +1,28 @@
|
|||||||
mod battery;
|
mod battery;
|
||||||
mod cputemp;
|
mod cputemp;
|
||||||
mod disk;
|
mod disk;
|
||||||
|
mod loadavg;
|
||||||
mod memory;
|
mod memory;
|
||||||
mod mpd;
|
mod mpd;
|
||||||
|
mod netspeed;
|
||||||
|
mod spotify;
|
||||||
mod time;
|
mod time;
|
||||||
mod uptime;
|
mod uptime;
|
||||||
mod volume;
|
mod volume;
|
||||||
mod weather;
|
mod weather;
|
||||||
mod spotify;
|
|
||||||
mod loadavg;
|
|
||||||
|
|
||||||
use self::mpd::Mpd;
|
use self::mpd::Mpd;
|
||||||
use battery::Battery;
|
use battery::Battery;
|
||||||
use cputemp::CpuTemp;
|
use cputemp::CpuTemp;
|
||||||
use disk::Disk;
|
use disk::Disk;
|
||||||
|
use loadavg::LoadAvg;
|
||||||
use memory::Memory;
|
use memory::Memory;
|
||||||
|
use netspeed::NetSpeed;
|
||||||
|
use spotify::Spotify;
|
||||||
use time::Time;
|
use time::Time;
|
||||||
use uptime::Uptime;
|
use uptime::Uptime;
|
||||||
use volume::Volume;
|
use volume::Volume;
|
||||||
use weather::Weather;
|
use weather::Weather;
|
||||||
use spotify::Spotify;
|
|
||||||
use loadavg::LoadAvg;
|
|
||||||
|
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
@ -36,7 +38,8 @@ lazy_static! {
|
|||||||
|
|
||||||
match File::open(&yml_source) {
|
match File::open(&yml_source) {
|
||||||
Ok(mut file) => {
|
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")
|
serde_yaml::from_str(&data).expect("Failed to parse config")
|
||||||
}
|
}
|
||||||
Err(_) => Config::default(),
|
Err(_) => Config::default(),
|
||||||
@ -80,7 +83,10 @@ pub struct Config {
|
|||||||
pub spotify: Spotify,
|
pub spotify: Spotify,
|
||||||
|
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub loadavg: LoadAvg
|
pub loadavg: LoadAvg,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub netspeed: NetSpeed,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
@ -97,7 +103,8 @@ impl Default for Config {
|
|||||||
uptime: Default::default(),
|
uptime: Default::default(),
|
||||||
mpd: Default::default(),
|
mpd: Default::default(),
|
||||||
spotify: Default::default(),
|
spotify: Default::default(),
|
||||||
loadavg: Default::default()
|
loadavg: Default::default(),
|
||||||
|
netspeed: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
21
src/config/netspeed.rs
Normal file
21
src/config/netspeed.rs
Normal file
@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
src/run.rs
31
src/run.rs
@ -1,12 +1,10 @@
|
|||||||
|
use crate::config::CONFIG;
|
||||||
use crate::types::*;
|
use crate::types::*;
|
||||||
use crate::utils::*;
|
use crate::utils::*;
|
||||||
use crate::config::CONFIG;
|
|
||||||
use std::sync::mpsc;
|
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(mut blocks: Blocks) {
|
pub fn run(mut blocks: Blocks) {
|
||||||
let (tx, rx) = mpsc::channel();
|
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
|
// Disk thread
|
||||||
if CONFIG.disk.enabled {
|
if CONFIG.disk.enabled {
|
||||||
let disk_tx = tx.clone();
|
let disk_tx = tx.clone();
|
||||||
@ -122,7 +130,7 @@ pub fn run(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(""); 11];
|
let mut bar: Vec<String> = vec![String::from(""); 12];
|
||||||
//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 {
|
||||||
@ -130,13 +138,14 @@ pub fn run(mut blocks: Blocks) {
|
|||||||
ThreadsData::Mpd(x) => bar[1] = x,
|
ThreadsData::Mpd(x) => bar[1] = x,
|
||||||
ThreadsData::Sound(x) => bar[2] = x,
|
ThreadsData::Sound(x) => bar[2] = x,
|
||||||
ThreadsData::Weather(x) => bar[3] = x,
|
ThreadsData::Weather(x) => bar[3] = x,
|
||||||
ThreadsData::Disk(x) => bar[4] = x,
|
ThreadsData::NetSpeed(x) => bar[4] = x,
|
||||||
ThreadsData::Memory(x) => bar[5] = x,
|
ThreadsData::Disk(x) => bar[5] = x,
|
||||||
ThreadsData::CpuTemp(x) => bar[6] = x,
|
ThreadsData::Memory(x) => bar[6] = x,
|
||||||
ThreadsData::LoadAvg(x) => bar[7] = x,
|
ThreadsData::CpuTemp(x) => bar[7] = x,
|
||||||
ThreadsData::Battery(x) => bar[8] = x,
|
ThreadsData::LoadAvg(x) => bar[8] = x,
|
||||||
ThreadsData::Uptime(x) => bar[9] = x,
|
ThreadsData::Battery(x) => bar[9] = x,
|
||||||
ThreadsData::Time(x) => bar[10] = x,
|
ThreadsData::Uptime(x) => bar[10] = x,
|
||||||
|
ThreadsData::Time(x) => bar[11] = x,
|
||||||
}
|
}
|
||||||
|
|
||||||
// match ends here
|
// match ends here
|
||||||
|
@ -13,6 +13,7 @@ pub enum ThreadsData {
|
|||||||
Uptime(String),
|
Uptime(String),
|
||||||
Spotify(String),
|
Spotify(String),
|
||||||
LoadAvg(String),
|
LoadAvg(String),
|
||||||
|
NetSpeed(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -113,6 +114,14 @@ pub struct LoadAvg {
|
|||||||
pub delay: f64,
|
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 struct Blocks {
|
||||||
pub disp: Display<name::NameConnection>,
|
pub disp: Display<name::NameConnection>,
|
||||||
pub root: Window,
|
pub root: Window,
|
||||||
|
@ -4,6 +4,7 @@ pub mod disk;
|
|||||||
pub mod load_average;
|
pub mod load_average;
|
||||||
pub mod memory;
|
pub mod memory;
|
||||||
pub mod mpd;
|
pub mod mpd;
|
||||||
|
pub mod netspeed;
|
||||||
pub mod spotify;
|
pub mod spotify;
|
||||||
pub mod time;
|
pub mod time;
|
||||||
pub mod uptime;
|
pub mod uptime;
|
||||||
|
45
src/utils/netspeed.rs
Normal file
45
src/utils/netspeed.rs
Normal file
@ -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::<u64>()
|
||||||
|
.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])
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user