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]]
name = "rsblocks"
version = "0.1.17"
version = "0.1.18"
dependencies = [
"alsa",
"async-std",

View File

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

View File

@ -24,6 +24,12 @@ pub_ip:
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:

View File

@ -1,5 +1,5 @@
use breadx::{display::*, Window};
use crate::types::ThreadsData;
use breadx::{display::*, Window};
pub struct BlockManager {
pub disp: Display<name::NameConnection>,
@ -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");
}
}

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 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(),
}
}

View File

@ -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<F, Fut>(tx: Sender<ThreadsData>, block: F, delay: f64)
where
F: Fn() -> Fut,
Fut: Future<Output=ThreadsData>
Fut: Future<Output = ThreadsData>,
{
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(())

View File

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

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 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;