Added local ip (#61)

* added new block/local ip

* added local ip block

* added localip in config file

* edited localip default to false
This commit is contained in:
Mustafa Salih 2021-06-04 22:14:02 +03:00 committed by GitHub
parent edea2fcb3d
commit a66c94a1f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 94 additions and 28 deletions

2
Cargo.lock generated
View File

@ -876,7 +876,7 @@ dependencies = [
[[package]] [[package]]
name = "rsblocks" name = "rsblocks"
version = "0.1.17" version = "0.1.18"
dependencies = [ dependencies = [
"alsa", "alsa",
"async-std", "async-std",

View File

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

View File

@ -23,8 +23,14 @@ pub_ip:
delay: 120.0 delay: 120.0
enabled: false enabled: false
icon: '' 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) # ethernet/wifi bandwith (no delay for this since it will calculate the bandwith every second)
netspeed: netspeed:
enabled: false enabled: false

View File

@ -1,5 +1,5 @@
use breadx::{display::*, Window};
use crate::types::ThreadsData; use crate::types::ThreadsData;
use breadx::{display::*, Window};
pub struct BlockManager { pub struct BlockManager {
pub disp: Display<name::NameConnection>, pub disp: Display<name::NameConnection>,
@ -13,7 +13,7 @@ impl BlockManager {
let root = disp.default_screen().root; let root = disp.default_screen().root;
Self { Self {
disp, disp,
blocks: vec![String::from(""); 14], blocks: vec![String::from(""); 15],
root, root,
} }
} }
@ -27,13 +27,14 @@ impl BlockManager {
ThreadsData::NetSpeed(x) => self.blocks[4] = x, ThreadsData::NetSpeed(x) => self.blocks[4] = x,
ThreadsData::BitCoins(x) => self.blocks[5] = x, ThreadsData::BitCoins(x) => self.blocks[5] = x,
ThreadsData::PubIp(x) => self.blocks[6] = x, ThreadsData::PubIp(x) => self.blocks[6] = x,
ThreadsData::Disk(x) => self.blocks[7] = x, ThreadsData::LocalIp(x) => self.blocks[7] = x,
ThreadsData::Memory(x) => self.blocks[8] = x, ThreadsData::Disk(x) => self.blocks[8] = x,
ThreadsData::CpuTemp(x) => self.blocks[9] = x, ThreadsData::Memory(x) => self.blocks[9] = x,
ThreadsData::LoadAvg(x) => self.blocks[10] = x, ThreadsData::CpuTemp(x) => self.blocks[10] = x,
ThreadsData::Battery(x) => self.blocks[11] = x, ThreadsData::LoadAvg(x) => self.blocks[11] = x,
ThreadsData::Uptime(x) => self.blocks[12] = x, ThreadsData::Battery(x) => self.blocks[12] = x,
ThreadsData::Time(x) => self.blocks[13] = x, ThreadsData::Uptime(x) => self.blocks[13] = x,
ThreadsData::Time(x) => self.blocks[14] = x,
} }
let mut x = String::new(); let mut x = String::new();
for i in self.blocks.iter() { for i in self.blocks.iter() {
@ -43,7 +44,6 @@ impl BlockManager {
self.root self.root
.set_title(&mut self.disp, &x) .set_title(&mut self.disp, &x)
.expect("Failed to set title"); .expect("Failed to set title");
} }
} }

21
src/config/local_ip.rs Normal file
View File

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

View File

@ -3,6 +3,7 @@ mod bitcoins;
mod cputemp; mod cputemp;
mod disk; mod disk;
mod loadavg; mod loadavg;
mod local_ip;
mod memory; mod memory;
mod mpd; mod mpd;
mod netspeed; mod netspeed;
@ -19,6 +20,7 @@ use bitcoins::BitCoins;
use cputemp::CpuTemp; use cputemp::CpuTemp;
use disk::Disk; use disk::Disk;
use loadavg::LoadAvg; use loadavg::LoadAvg;
use local_ip::LocalIp;
use memory::Memory; use memory::Memory;
use netspeed::NetSpeed; use netspeed::NetSpeed;
use pub_ip::PubIp; use pub_ip::PubIp;
@ -101,6 +103,9 @@ pub struct Config {
#[serde(default)] #[serde(default)]
pub pub_ip: PubIp, pub pub_ip: PubIp,
#[serde(default)]
pub local_ip: LocalIp,
#[serde(default)] #[serde(default)]
pub bitcoins: BitCoins, pub bitcoins: BitCoins,
} }
@ -122,6 +127,7 @@ impl Default for Config {
loadavg: Default::default(), loadavg: Default::default(),
netspeed: Default::default(), netspeed: Default::default(),
pub_ip: Default::default(), pub_ip: Default::default(),
local_ip: Default::default(),
bitcoins: Default::default(), bitcoins: Default::default(),
} }
} }

View File

@ -7,15 +7,13 @@ use async_std::task;
use async_std::task::sleep; use async_std::task::sleep;
use futures::future; use futures::future;
use futures::stream::StreamExt; use futures::stream::StreamExt;
use std::time::Duration;
use std::future::Future; use std::future::Future;
use std::time::Duration;
async fn init_block<F, Fut>(tx: Sender<ThreadsData>, block: F, delay: f64) async fn init_block<F, Fut>(tx: Sender<ThreadsData>, block: F, delay: f64)
where where
F: Fn() -> Fut, F: Fn() -> Fut,
Fut: Future<Output=ThreadsData> Fut: Future<Output = ThreadsData>,
{ {
loop { loop {
let _ = tx.send(block().await).await; let _ = tx.send(block().await).await;
@ -37,6 +35,12 @@ pub async fn run(mut blocks: BlockManager) {
task::spawn(b); 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 // spotify task
if CONFIG.spotify.enabled { if CONFIG.spotify.enabled {
let b = init_block(tx.clone(), spotify::get_spotify, CONFIG.spotify.delay); 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 // Disk task
if if CONFIG.disk.enabled {
/*CONFIG.disk.enabled*/
false {
let b = init_block(tx.clone(), disk::get_disk, CONFIG.disk.delay); let b = init_block(tx.clone(), disk::get_disk, CONFIG.disk.delay);
task::spawn(b); task::spawn(b);
} }
// Memory task // Memory task
if if CONFIG.memory.enabled {
/*CONFIG.memory.enabled*/
false {
let b = init_block(tx.clone(), memory::get_memory, CONFIG.memory.delay); let b = init_block(tx.clone(), memory::get_memory, CONFIG.memory.delay);
task::spawn(b); 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); let b = init_block(tx, time::get_time, CONFIG.time.delay);
task::spawn(b); task::spawn(b);
// NOTE: order matters to the final format
rx.for_each(|data| { rx.for_each(|data| {
blocks.update(data); blocks.update(data);
future::ready(()) future::ready(())

View File

@ -1,4 +1,3 @@
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum ThreadsData { pub enum ThreadsData {
Mpd(String), Mpd(String),
@ -14,6 +13,7 @@ pub enum ThreadsData {
LoadAvg(String), LoadAvg(String),
NetSpeed(String), NetSpeed(String),
PubIp(String), PubIp(String),
LocalIp(String),
BitCoins(String), BitCoins(String),
} }
@ -32,6 +32,7 @@ pub struct Config {
pub spotify: Spotify, pub spotify: Spotify,
pub loadavg: LoadAvg, pub loadavg: LoadAvg,
pub pub_ip: PubIp, pub pub_ip: PubIp,
pub local_ip: LocalIp,
pub bitcoins: BitCoins, pub bitcoins: BitCoins,
} }
@ -131,6 +132,13 @@ pub struct PubIp {
pub delay: f64, pub delay: f64,
} }
#[derive(Clone)]
pub struct LocalIp {
pub icon: String,
pub enabled: bool,
pub delay: f64,
}
#[derive(Clone)] #[derive(Clone)]
pub struct BitCoins { pub struct BitCoins {
pub icon: String, pub icon: String,
@ -138,4 +146,3 @@ pub struct BitCoins {
pub enabled: bool, pub enabled: bool,
pub delay: f64, pub delay: f64,
} }

28
src/utils/local_ip.rs Normal file
View File

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

View File

@ -3,6 +3,7 @@ pub mod bitcoins;
pub mod cpu; pub mod cpu;
pub mod disk; pub mod disk;
pub mod load_average; pub mod load_average;
pub mod local_ip;
pub mod memory; pub mod memory;
pub mod mpd; pub mod mpd;
pub mod netspeed; pub mod netspeed;
@ -12,4 +13,3 @@ pub mod time;
pub mod uptime; pub mod uptime;
pub mod volume; pub mod volume;
pub mod weather; pub mod weather;