showing current public ip address (#43)

This commit is contained in:
Mustafa Salih 2021-02-02 02:23:37 +03:00 committed by GitHub
parent 6c0dfddead
commit 303808ea19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 140 additions and 62 deletions

56
Cargo.lock generated
View File

@ -111,9 +111,9 @@ dependencies = [
[[package]] [[package]]
name = "breadx" name = "breadx"
version = "0.1.11" version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9bff92e2ec549fe3f20386d73efb30263be7467463def0c2a780d7cee5dbf5e3" checksum = "31dd6e33f086d25e35f64029cf4680bc1117b4d33053971e7943ae4c1b70fa89"
dependencies = [ dependencies = [
"async-io", "async-io",
"async-net", "async-net",
@ -138,9 +138,9 @@ checksum = "40e38929add23cdf8a366df9b0e088953150724bcbe5fc330b0d8eb3b328eec8"
[[package]] [[package]]
name = "bytemuck" name = "bytemuck"
version = "1.4.1" version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41aa2ec95ca3b5c54cf73c91acf06d24f4495d5f1b1c12506ae3483d646177ac" checksum = "5a4bad0c5981acc24bc09e532f35160f952e35422603f0563cd7a73c2c2e65a0"
[[package]] [[package]]
name = "cache-padded" name = "cache-padded"
@ -227,15 +227,15 @@ dependencies = [
[[package]] [[package]]
name = "futures-core" name = "futures-core"
version = "0.3.8" version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "847ce131b72ffb13b6109a221da9ad97a64cbe48feb1028356b836b47b8f1748" checksum = "79e5145dde8da7d1b3892dad07a9c98fc04bc39892b1ecc9692cf53e2b780a65"
[[package]] [[package]]
name = "futures-io" name = "futures-io"
version = "0.3.8" version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "611834ce18aaa1bd13c4b374f5d653e1027cf99b6b502584ff8c9a64413b30bb" checksum = "28be053525281ad8259d47e4de5de657b25e7bac113458555bb4b70bc6870500"
[[package]] [[package]]
name = "futures-lite" name = "futures-lite"
@ -278,9 +278,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.81" version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" checksum = "1cca32fa0182e8c0989459524dc356b8f2b5c10f1b9eb521b7d182c03cf8c5ff"
[[package]] [[package]]
name = "libdbus-sys" name = "libdbus-sys"
@ -293,17 +293,17 @@ dependencies = [
[[package]] [[package]]
name = "linked-hash-map" name = "linked-hash-map"
version = "0.5.3" version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a" checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.11" version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
dependencies = [ dependencies = [
"cfg-if 0.1.10", "cfg-if 1.0.0",
] ]
[[package]] [[package]]
@ -314,9 +314,9 @@ checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
[[package]] [[package]]
name = "minreq" name = "minreq"
version = "2.2.1" version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2466d0a7e6bfcd54f69e4a17d4a4318985aaaf7fe3df4cd3b6f11ff551129ca3" checksum = "85b112458b07e0864c7415847c423fe758c7189a28e72347e95d92c4bfd33f5d"
[[package]] [[package]]
name = "mpd" name = "mpd"
@ -397,9 +397,9 @@ checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72"
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.0" version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b063f57ec186e6140e2b8b6921e5f1bd89c7356dda5b33acc5401203ca6131c" checksum = "439697af366c49a6d0a010c56a0d97685bc140ce0d377b13a2ea2aa42d64a827"
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
@ -446,7 +446,7 @@ checksum = "4e1b7878800220a76a08f32c057829511440f65528b63b940f2f2bc145d7ac68"
[[package]] [[package]]
name = "rsblocks" name = "rsblocks"
version = "0.1.11" version = "0.1.12"
dependencies = [ dependencies = [
"alsa", "alsa",
"breadx", "breadx",
@ -468,18 +468,18 @@ checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.120" version = "1.0.123"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "166b2349061381baf54a58e4b13c89369feb0ef2eaa57198899e2312aac30aab" checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.120" version = "1.0.123"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ca2a8cb5805ce9e3b95435e3765b7b553cecc762d938d409434338386cb5775" checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -500,9 +500,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.58" version = "1.0.60"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5" checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -522,9 +522,9 @@ dependencies = [
[[package]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.1.0" version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf8dbc19eb42fba10e8feaaec282fb50e2c14b2726d6301dbfeed0f73306a6f" checksum = "317cca572a0e89c3ce0ca1f1bdc9369547fe318a683418e42ac8f59d14701023"
dependencies = [ dependencies = [
"tinyvec_macros", "tinyvec_macros",
] ]

View File

@ -1,6 +1,6 @@
[package] [package]
name = "rsblocks" name = "rsblocks"
version = "0.1.11" version = "0.1.12"
authors = ["mustafa salih <mustafasalih1991@gmail.com>"] authors = ["mustafa salih <mustafasalih1991@gmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"

View File

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

View File

@ -6,9 +6,24 @@ seperator: '┃'
# Time always running, no enabled option for this # Time always running, no enabled option for this
time: time:
icon: ''
format: '%d %b, %I:%M:%S %p'
delay: 1.0 delay: 1.0
format: '%d %b, %I:%M:%S %p'
icon: ''
# public ip address
pub_ip:
delay: 120.0
enabled: true
icon: ''
# ethernet/wifi bandwith
netspeed:
enabled: false
interface: 'wlan0'
recieve_icon: ''
transmit_icon: ''
# ethernet/wifi bandwith # ethernet/wifi bandwith
@ -20,56 +35,56 @@ netspeed:
memory: memory:
icon: '▦'
enabled: true
delay: 2.0 delay: 2.0
enabled: true
icon: '▦'
disk: disk:
icon: ''
enabled: true
delay: 120.0 delay: 120.0
enabled: true
icon: ''
battery: battery:
icon: ''
enabled: false
source: 'BAT0'
delay: 120.0 delay: 120.0
enabled: false
icon: ''
source: 'BAT0'
cpu_temperature: cpu_temperature:
icon: ''
enabled: true
delay: 120.0 delay: 120.0
enabled: true
icon: ''
# cpu load average # cpu load average
loadavg: loadavg:
icon: ''
enabled: false
delay: 2.0 delay: 2.0
enabled: false
icon: ''
mpd: mpd:
icon: ''
host: '127.0.0.1'
port: '6600'
enabled: false
delay: 5.0 delay: 5.0
enabled: false
host: '127.0.0.1'
icon: ''
port: '6600'
volume: volume:
icon: ''
enabled: true
delay: 0.18
card: 'PULSE' card: 'PULSE'
delay: 0.18
enabled: true
icon: ''
spotify: spotify:
icon: ''
enabled: false
delay: 0.5 delay: 0.5
enabled: false
icon: ''
# weather format options is available on # weather format options is available on
@ -79,8 +94,8 @@ spotify:
# - if you have multiple formats and want to insert space between them use '+' instead. # - if you have multiple formats and want to insert space between them use '+' instead.
# - giving (city) a value is recommended. # - giving (city) a value is recommended.
weather: weather:
enabled: true
icon: ''
city: '' city: ''
format: '%l:+%t'
delay: 7200.0 # 7200 seconds = 2 hours delay: 7200.0 # 7200 seconds = 2 hours
enabled: true
format: '%l:+%t'
icon: ''

View File

@ -5,6 +5,7 @@ mod loadavg;
mod memory; mod memory;
mod mpd; mod mpd;
mod netspeed; mod netspeed;
mod pub_ip;
mod spotify; mod spotify;
mod time; mod time;
mod uptime; mod uptime;
@ -18,6 +19,7 @@ use disk::Disk;
use loadavg::LoadAvg; use loadavg::LoadAvg;
use memory::Memory; use memory::Memory;
use netspeed::NetSpeed; use netspeed::NetSpeed;
use pub_ip::PubIp;
use spotify::Spotify; use spotify::Spotify;
use time::Time; use time::Time;
use uptime::Uptime; use uptime::Uptime;
@ -87,6 +89,9 @@ pub struct Config {
#[serde(default)] #[serde(default)]
pub netspeed: NetSpeed, pub netspeed: NetSpeed,
#[serde(default)]
pub pub_ip: PubIp,
} }
impl Default for Config { impl Default for Config {
@ -105,6 +110,7 @@ impl Default for Config {
spotify: Default::default(), spotify: Default::default(),
loadavg: Default::default(), loadavg: Default::default(),
netspeed: Default::default(), netspeed: Default::default(),
pub_ip: Default::default(),
} }
} }
} }

19
src/config/pub_ip.rs Normal file
View File

@ -0,0 +1,19 @@
use serde::{Deserialize, Serialize};
use std::default::Default;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PubIp {
pub icon: String,
pub enabled: bool,
pub delay: f64,
}
impl Default for PubIp {
fn default() -> Self {
PubIp {
icon: String::from(""),
enabled: false,
delay: 120.0,
}
}
}

View File

@ -17,6 +17,16 @@ pub fn run(mut blocks: Blocks) {
thread::sleep(Duration::from_secs_f64(CONFIG.loadavg.delay)) thread::sleep(Duration::from_secs_f64(CONFIG.loadavg.delay))
}); });
} }
// public ip thread
if CONFIG.pub_ip.enabled {
let pub_ip_tx = tx.clone();
thread::spawn(move || loop {
let pub_ip_data = ThreadsData::PubIp(pub_ip::get_pub_ip());
pub_ip_tx.send(pub_ip_data).unwrap();
thread::sleep(Duration::from_secs_f64(CONFIG.pub_ip.delay))
});
}
// spotify thread // spotify thread
if CONFIG.spotify.enabled { if CONFIG.spotify.enabled {
let spotify_tx = tx.clone(); let spotify_tx = tx.clone();
@ -130,7 +140,8 @@ 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(""); 12];
let mut bar: Vec<String> = vec![String::from(""); 13];
//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 {
@ -139,13 +150,14 @@ pub fn run(mut blocks: Blocks) {
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::NetSpeed(x) => bar[4] = x, ThreadsData::NetSpeed(x) => bar[4] = x,
ThreadsData::Disk(x) => bar[5] = x, ThreadsData::PubIp(x) => bar[5] = x,
ThreadsData::Memory(x) => bar[6] = x, ThreadsData::Disk(x) => bar[6] = x,
ThreadsData::CpuTemp(x) => bar[7] = x, ThreadsData::Memory(x) => bar[7] = x,
ThreadsData::LoadAvg(x) => bar[8] = x, ThreadsData::CpuTemp(x) => bar[8] = x,
ThreadsData::Battery(x) => bar[9] = x, ThreadsData::LoadAvg(x) => bar[9] = x,
ThreadsData::Uptime(x) => bar[10] = x, ThreadsData::Battery(x) => bar[10] = x,
ThreadsData::Time(x) => bar[11] = x, ThreadsData::Uptime(x) => bar[11] = x,
ThreadsData::Time(x) => bar[12] = x,
} }
// match ends here // match ends here

View File

@ -14,6 +14,7 @@ pub enum ThreadsData {
Spotify(String), Spotify(String),
LoadAvg(String), LoadAvg(String),
NetSpeed(String), NetSpeed(String),
PubIp(String),
} }
#[derive(Clone)] #[derive(Clone)]
@ -30,6 +31,7 @@ pub struct Config {
pub mpd: Mpd, pub mpd: Mpd,
pub spotify: Spotify, pub spotify: Spotify,
pub loadavg: LoadAvg, pub loadavg: LoadAvg,
pub pub_ip: PubIp,
} }
#[derive(Clone)] #[derive(Clone)]
@ -122,6 +124,13 @@ pub struct NetSpeed {
pub enabled: bool, pub enabled: bool,
} }
#[derive(Clone)]
pub struct PubIp {
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,

View File

@ -5,6 +5,7 @@ pub mod load_average;
pub mod memory; pub mod memory;
pub mod mpd; pub mod mpd;
pub mod netspeed; pub mod netspeed;
pub mod pub_ip;
pub mod spotify; pub mod spotify;
pub mod time; pub mod time;
pub mod uptime; pub mod uptime;

15
src/utils/pub_ip.rs Normal file
View File

@ -0,0 +1,15 @@
use crate::config::CONFIG;
pub fn get_pub_ip() -> String {
let url = format!("http://api.ipify.org");
let err_string = String::from("Error");
let res = match minreq::get(url).send() {
Ok(resp) => match resp.as_str() {
Ok(res_str) => res_str.trim().to_string(),
Err(_) => err_string,
},
Err(_) => err_string,
};
format!(" {} {} {}", CONFIG.pub_ip.icon, res, CONFIG.seperator)
}