aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/blockmanager.rs20
-rw-r--r--src/config/local_ip.rs21
-rw-r--r--src/config/mod.rs6
-rw-r--r--src/run.rs22
-rw-r--r--src/types.rs11
-rw-r--r--src/utils/local_ip.rs28
-rw-r--r--src/utils/mod.rs2
7 files changed, 85 insertions, 25 deletions
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<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");
-
}
}
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;
@@ -102,6 +104,9 @@ pub struct Config {
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<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(())
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,
}
@@ -132,10 +133,16 @@ pub struct PubIp {
}
#[derive(Clone)]
+pub struct LocalIp {
+ pub icon: String,
+ pub enabled: bool,
+ pub delay: f64,
+}
+
+#[derive(Clone)]
pub struct BitCoins {
pub icon: String,
pub symbol: String,
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;
-