From a66c94a1f836b56897a3b5151f93aaa327efc6bd Mon Sep 17 00:00:00 2001 From: Mustafa Salih <37256744+MustafaSalih1993@users.noreply.github.com> Date: Fri, 4 Jun 2021 22:14:02 +0300 Subject: [PATCH] Added local ip (#61) * added new block/local ip * added local ip block * added localip in config file * edited localip default to false --- Cargo.lock | 2 +- Cargo.toml | 2 +- rsblocks.yml | 8 +++++++- src/blockmanager.rs | 20 ++++++++++---------- src/config/local_ip.rs | 21 +++++++++++++++++++++ src/config/mod.rs | 6 ++++++ src/run.rs | 22 ++++++++++------------ src/types.rs | 11 +++++++++-- src/utils/local_ip.rs | 28 ++++++++++++++++++++++++++++ src/utils/mod.rs | 2 +- 10 files changed, 94 insertions(+), 28 deletions(-) create mode 100644 src/config/local_ip.rs create mode 100644 src/utils/local_ip.rs diff --git a/Cargo.lock b/Cargo.lock index 03b5c6d..f0d9e6d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -876,7 +876,7 @@ dependencies = [ [[package]] name = "rsblocks" -version = "0.1.17" +version = "0.1.18" dependencies = [ "alsa", "async-std", diff --git a/Cargo.toml b/Cargo.toml index f040305..3e4f896 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rsblocks" -version = "0.1.17" +version = "0.1.18" authors = ["mustafa salih "] edition = "2018" readme = "README.md" diff --git a/rsblocks.yml b/rsblocks.yml index f730df6..f0d6bcf 100644 --- a/rsblocks.yml +++ b/rsblocks.yml @@ -23,8 +23,14 @@ pub_ip: delay: 120.0 enabled: false icon: '' - +# local ip address +local_ip: + icon: '' + enabled: true + interface: 'wlo1' #network interface required for now.. + delay: 120.0 + # ethernet/wifi bandwith (no delay for this since it will calculate the bandwith every second) netspeed: enabled: false diff --git a/src/blockmanager.rs b/src/blockmanager.rs index 4d40374..aa92b64 100644 --- a/src/blockmanager.rs +++ b/src/blockmanager.rs @@ -1,5 +1,5 @@ -use breadx::{display::*, Window}; use crate::types::ThreadsData; +use breadx::{display::*, Window}; pub struct BlockManager { pub disp: Display, @@ -13,7 +13,7 @@ impl BlockManager { let root = disp.default_screen().root; Self { disp, - blocks: vec![String::from(""); 14], + blocks: vec![String::from(""); 15], root, } } @@ -27,13 +27,14 @@ impl BlockManager { ThreadsData::NetSpeed(x) => self.blocks[4] = x, ThreadsData::BitCoins(x) => self.blocks[5] = x, ThreadsData::PubIp(x) => self.blocks[6] = x, - ThreadsData::Disk(x) => self.blocks[7] = x, - ThreadsData::Memory(x) => self.blocks[8] = x, - ThreadsData::CpuTemp(x) => self.blocks[9] = x, - ThreadsData::LoadAvg(x) => self.blocks[10] = x, - ThreadsData::Battery(x) => self.blocks[11] = x, - ThreadsData::Uptime(x) => self.blocks[12] = x, - ThreadsData::Time(x) => self.blocks[13] = x, + ThreadsData::LocalIp(x) => self.blocks[7] = x, + ThreadsData::Disk(x) => self.blocks[8] = x, + ThreadsData::Memory(x) => self.blocks[9] = x, + ThreadsData::CpuTemp(x) => self.blocks[10] = x, + ThreadsData::LoadAvg(x) => self.blocks[11] = x, + ThreadsData::Battery(x) => self.blocks[12] = x, + ThreadsData::Uptime(x) => self.blocks[13] = x, + ThreadsData::Time(x) => self.blocks[14] = x, } let mut x = String::new(); for i in self.blocks.iter() { @@ -43,7 +44,6 @@ impl BlockManager { self.root .set_title(&mut self.disp, &x) .expect("Failed to set title"); - } } diff --git a/src/config/local_ip.rs b/src/config/local_ip.rs new file mode 100644 index 0000000..8b3b63a --- /dev/null +++ b/src/config/local_ip.rs @@ -0,0 +1,21 @@ +use serde::{Deserialize, Serialize}; +use std::default::Default; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct LocalIp { + pub icon: String, + pub enabled: bool, + pub delay: f64, + pub interface: String, +} + +impl Default for LocalIp { + fn default() -> Self { + LocalIp { + icon: String::from(""), + enabled: false, + delay: 120.0, + interface: String::from("wlan0"), + } + } +} diff --git a/src/config/mod.rs b/src/config/mod.rs index f47a172..48809a6 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -3,6 +3,7 @@ mod bitcoins; mod cputemp; mod disk; mod loadavg; +mod local_ip; mod memory; mod mpd; mod netspeed; @@ -19,6 +20,7 @@ use bitcoins::BitCoins; use cputemp::CpuTemp; use disk::Disk; use loadavg::LoadAvg; +use local_ip::LocalIp; use memory::Memory; use netspeed::NetSpeed; use pub_ip::PubIp; @@ -101,6 +103,9 @@ pub struct Config { #[serde(default)] pub pub_ip: PubIp, + #[serde(default)] + pub local_ip: LocalIp, + #[serde(default)] pub bitcoins: BitCoins, } @@ -122,6 +127,7 @@ impl Default for Config { loadavg: Default::default(), netspeed: Default::default(), pub_ip: Default::default(), + local_ip: Default::default(), bitcoins: Default::default(), } } diff --git a/src/run.rs b/src/run.rs index 3a5186b..2ec6889 100644 --- a/src/run.rs +++ b/src/run.rs @@ -7,15 +7,13 @@ use async_std::task; use async_std::task::sleep; use futures::future; use futures::stream::StreamExt; -use std::time::Duration; use std::future::Future; - - +use std::time::Duration; async fn init_block(tx: Sender, block: F, delay: f64) where F: Fn() -> Fut, - Fut: Future + Fut: Future, { loop { let _ = tx.send(block().await).await; @@ -37,6 +35,12 @@ pub async fn run(mut blocks: BlockManager) { task::spawn(b); } + // local ip task + if CONFIG.local_ip.enabled { + let b = init_block(tx.clone(), local_ip::get_local_ip, CONFIG.local_ip.delay); + task::spawn(b); + } + // spotify task if CONFIG.spotify.enabled { let b = init_block(tx.clone(), spotify::get_spotify, CONFIG.spotify.delay); @@ -56,17 +60,13 @@ pub async fn run(mut blocks: BlockManager) { } // Disk task - if - /*CONFIG.disk.enabled*/ - false { + if CONFIG.disk.enabled { let b = init_block(tx.clone(), disk::get_disk, CONFIG.disk.delay); task::spawn(b); } // Memory task - if - /*CONFIG.memory.enabled*/ - false { + if CONFIG.memory.enabled { let b = init_block(tx.clone(), memory::get_memory, CONFIG.memory.delay); task::spawn(b); } @@ -111,8 +111,6 @@ pub async fn run(mut blocks: BlockManager) { let b = init_block(tx, time::get_time, CONFIG.time.delay); task::spawn(b); - // NOTE: order matters to the final format - rx.for_each(|data| { blocks.update(data); future::ready(()) diff --git a/src/types.rs b/src/types.rs index 9fd834e..3cadf01 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,4 +1,3 @@ - #[derive(Debug, Clone)] pub enum ThreadsData { Mpd(String), @@ -14,6 +13,7 @@ pub enum ThreadsData { LoadAvg(String), NetSpeed(String), PubIp(String), + LocalIp(String), BitCoins(String), } @@ -32,6 +32,7 @@ pub struct Config { pub spotify: Spotify, pub loadavg: LoadAvg, pub pub_ip: PubIp, + pub local_ip: LocalIp, pub bitcoins: BitCoins, } @@ -131,6 +132,13 @@ pub struct PubIp { pub delay: f64, } +#[derive(Clone)] +pub struct LocalIp { + pub icon: String, + pub enabled: bool, + pub delay: f64, +} + #[derive(Clone)] pub struct BitCoins { pub icon: String, @@ -138,4 +146,3 @@ pub struct BitCoins { pub enabled: bool, pub delay: f64, } - diff --git a/src/utils/local_ip.rs b/src/utils/local_ip.rs new file mode 100644 index 0000000..6df7016 --- /dev/null +++ b/src/utils/local_ip.rs @@ -0,0 +1,28 @@ +use crate::config::CONFIG; +use crate::types::ThreadsData; + +pub async fn get_local_ip() -> ThreadsData { + let addrs = nix::ifaddrs::getifaddrs().unwrap(); + let mut ip = String::new(); + for ifaddr in addrs { + match ifaddr.address { + Some(address) => { + if ifaddr.interface_name == CONFIG.local_ip.interface { + match address.family() { + nix::sys::socket::AddressFamily::Inet => { + ip = address.to_string().split(':').next().unwrap().to_string(); + break; + } + _ => continue, + }; + } + } + None => continue, + } + } + if ip.is_empty() { + ip = String::from("Error!") + } + let data = format!(" {} {} {}", CONFIG.local_ip.icon, ip, CONFIG.seperator); + ThreadsData::LocalIp(data) +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 6e0d944..cf9ea0d 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -3,6 +3,7 @@ pub mod bitcoins; pub mod cpu; pub mod disk; pub mod load_average; +pub mod local_ip; pub mod memory; pub mod mpd; pub mod netspeed; @@ -12,4 +13,3 @@ pub mod time; pub mod uptime; pub mod volume; pub mod weather; -