diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/blockmanager.rs | 54 | ||||
-rw-r--r-- | src/main.rs | 12 | ||||
-rw-r--r-- | src/run.rs | 158 | ||||
-rw-r--r-- | src/types.rs | 19 | ||||
-rw-r--r-- | src/utils/battery.rs | 2 | ||||
-rw-r--r-- | src/utils/bitcoins.rs | 2 | ||||
-rw-r--r-- | src/utils/cpu.rs | 2 | ||||
-rw-r--r-- | src/utils/disk.rs | 2 | ||||
-rw-r--r-- | src/utils/load_average.rs | 2 | ||||
-rw-r--r-- | src/utils/memory.rs | 2 | ||||
-rw-r--r-- | src/utils/mod.rs | 1 | ||||
-rw-r--r-- | src/utils/mpd.rs | 2 | ||||
-rw-r--r-- | src/utils/netspeed.rs | 6 | ||||
-rw-r--r-- | src/utils/pub_ip.rs | 2 | ||||
-rw-r--r-- | src/utils/spotify.rs | 2 | ||||
-rw-r--r-- | src/utils/time.rs | 3 | ||||
-rw-r--r-- | src/utils/uptime.rs | 2 | ||||
-rw-r--r-- | src/utils/volume.rs | 2 | ||||
-rw-r--r-- | src/utils/weather.rs | 2 |
19 files changed, 152 insertions, 125 deletions
diff --git a/src/blockmanager.rs b/src/blockmanager.rs new file mode 100644 index 0000000..b0b3b5e --- /dev/null +++ b/src/blockmanager.rs @@ -0,0 +1,54 @@ +use breadx::{display::*, window::Window}; +use crate::types::ThreadsData; + +pub struct BlockManager { + pub disp: Display<name::NameConnection>, + blocks: Vec<String>, + pub root: Window, +} + +impl BlockManager { + pub fn new() -> Self { + let disp = Display::create(None, None).expect("Failed to create x11 connection"); + let root = disp.default_screen().root; + Self { + disp, + blocks: vec![String::from(""); 14], + root, + } + } + + pub fn update(&mut self, data: ThreadsData) { + match data { + ThreadsData::Spotify(x) => self.blocks[0] = x, + ThreadsData::Mpd(x) => self.blocks[1] = x, + ThreadsData::Sound(x) => self.blocks[2] = x, + ThreadsData::Weather(x) => self.blocks[3] = x, + 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, + } + let mut x = String::new(); + for i in self.blocks.iter() { + x.push_str(i.as_str()); + } + + self.root + .set_title(&mut self.disp, &x) + .expect("Failed to set title"); + + } +} + +impl Default for BlockManager { + fn default() -> Self { + Self::new() + } +} diff --git a/src/main.rs b/src/main.rs index a2ed5d0..3cfd2e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,17 @@ mod config; mod run; mod types; mod utils; +mod blockmanager; + use std::env; use std::process; +use blockmanager::BlockManager; + use lazy_static::initialize; -fn main() { +#[async_std::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { initialize(&config::CONFIG); @@ -17,6 +22,7 @@ fn main() { process::exit(1); }; - let blocks = types::Blocks::new(); - run::run(blocks); + let blocks = BlockManager::new(); + run::run(blocks).await; + Ok(()) } @@ -1,135 +1,121 @@ +use crate::blockmanager::*; use crate::config::CONFIG; use crate::types::*; use crate::utils::*; -use std::sync::mpsc; -use std::thread; +use async_std::channel::{unbounded, Sender}; +use async_std::task; +use async_std::task::sleep; +use futures::future; +use futures::stream::StreamExt; use std::time::Duration; +use std::future::Future; -fn spawn_thread_loop<F>(tx: std::sync::mpsc::Sender<ThreadsData>, data: F, delay: f64) + + +async fn init_block<F, Fut>(tx: Sender<ThreadsData>, block: F, delay: f64) where - F: Fn() -> ThreadsData + Send + 'static, + F: Fn() -> Fut, + Fut: Future<Output=ThreadsData> { - thread::spawn(move || loop { - tx.send(data()).unwrap(); - thread::sleep(Duration::from_secs_f64(delay)); - }); + loop { + let _ = tx.send(block().await).await; + sleep(Duration::from_secs_f64(delay)).await; + } } -pub fn run(mut blocks: Blocks) { - let (tx, rx) = mpsc::channel(); +pub async fn run(mut blocks: BlockManager) { + let (tx, rx) = unbounded(); - // loadavrage thread + // loadavrage task if CONFIG.loadavg.enabled { - spawn_thread_loop(tx.clone(), load_average::get_load_avg, CONFIG.loadavg.delay); + let b = init_block(tx.clone(), load_average::get_load_avg, CONFIG.loadavg.delay); + task::spawn(b); } - // public ip thread + // public ip task if CONFIG.pub_ip.enabled { - spawn_thread_loop(tx.clone(), pub_ip::get_pub_ip, CONFIG.pub_ip.delay); + let b = init_block(tx.clone(), pub_ip::get_pub_ip, CONFIG.pub_ip.delay); + task::spawn(b); } - // spotify thread + // spotify task if CONFIG.spotify.enabled { - spawn_thread_loop(tx.clone(), spotify::get_spotify, CONFIG.spotify.delay); + let b = init_block(tx.clone(), spotify::get_spotify, CONFIG.spotify.delay); + task::spawn(b); } - // mpd thread + // mpd task if CONFIG.mpd.enabled { - spawn_thread_loop(tx.clone(), mpd::get_mpd_current, CONFIG.mpd.delay); + let b = init_block(tx.clone(), mpd::get_mpd_current, CONFIG.mpd.delay); + task::spawn(b); } - // volume thread + // volume task if CONFIG.volume.enabled { - spawn_thread_loop(tx.clone(), volume::get_volume, CONFIG.volume.delay); + let b = init_block(tx.clone(), volume::get_volume, CONFIG.volume.delay); + task::spawn(b); } - // Disk thread - if CONFIG.disk.enabled { - spawn_thread_loop(tx.clone(), disk::get_disk, CONFIG.disk.delay); + // Disk task + if + /*CONFIG.disk.enabled*/ + false { + let b = init_block(tx.clone(), disk::get_disk, CONFIG.disk.delay); + task::spawn(b); } - // Memory thread - if CONFIG.memory.enabled { - spawn_thread_loop(tx.clone(), memory::get_memory, CONFIG.memory.delay); + // Memory task + if + /*CONFIG.memory.enabled*/ + false { + let b = init_block(tx.clone(), memory::get_memory, CONFIG.memory.delay); + task::spawn(b); } - // Weather thread + // Weather task if CONFIG.weather.enabled { - spawn_thread_loop(tx.clone(), weather::get_weather, CONFIG.weather.delay); + let b = init_block(tx.clone(), weather::get_weather, CONFIG.weather.delay); + task::spawn(b); } - // Battery thread + // Battery task if CONFIG.battery.enabled { - spawn_thread_loop(tx.clone(), battery::get_battery, CONFIG.battery.delay); + let b = init_block(tx.clone(), battery::get_battery, CONFIG.battery.delay); + task::spawn(b); } - // Cpu temperature thread + // Cpu temperature task if CONFIG.cpu_temperature.enabled { - spawn_thread_loop(tx.clone(), cpu::get_cpu_temp, CONFIG.cpu_temperature.delay); + let b = init_block(tx.clone(), cpu::get_cpu_temp, CONFIG.cpu_temperature.delay); + task::spawn(b); } - // Uptime thread + // Uptime task if CONFIG.uptime.enabled { - spawn_thread_loop(tx.clone(), uptime::get_uptime, CONFIG.uptime.delay); + let b = init_block(tx.clone(), uptime::get_uptime, CONFIG.uptime.delay); + task::spawn(b); } - // BTC thread + // BTC task if CONFIG.bitcoins.enabled { - spawn_thread_loop(tx.clone(), bitcoins::get_price, CONFIG.bitcoins.delay); + let b = init_block(tx.clone(), bitcoins::get_price, CONFIG.bitcoins.delay); + task::spawn(b); } - // net speed thread - // get_netspeed will sleep inside the function + // net speed task if CONFIG.netspeed.enabled { - let net_tx = tx.clone(); - thread::spawn(move || loop { - let net_data = netspeed::get_netspeed(); - net_tx.send(net_data).unwrap(); - }); - } - - // Time thread - { - spawn_thread_loop(tx, time::get_time, CONFIG.time.delay); + let b = init_block(tx.clone(), netspeed::get_netspeed, 0.); + task::spawn(b); } - //Main - { - // NOTE: order matters to the final format - - let mut bar: Vec<String> = vec![String::from(""); 14]; - //iterating the values recieved from the threads - for data in rx { - match data { - ThreadsData::Spotify(x) => bar[0] = x, - ThreadsData::Mpd(x) => bar[1] = x, - ThreadsData::Sound(x) => bar[2] = x, - ThreadsData::Weather(x) => bar[3] = x, - ThreadsData::NetSpeed(x) => bar[4] = x, - ThreadsData::BitCoins(x) => bar[5] = x, - ThreadsData::PubIp(x) => bar[6] = x, - ThreadsData::Disk(x) => bar[7] = x, - ThreadsData::Memory(x) => bar[8] = x, - ThreadsData::CpuTemp(x) => bar[9] = x, - ThreadsData::LoadAvg(x) => bar[10] = x, - ThreadsData::Battery(x) => bar[11] = x, - ThreadsData::Uptime(x) => bar[12] = x, - ThreadsData::Time(x) => bar[13] = x, - } - - // match ends here - update(&bar, &mut blocks); - } - } -} + // Time task + let b = init_block(tx, time::get_time, CONFIG.time.delay); + task::spawn(b); -fn update(bar: &[String], blocks: &mut Blocks) { - let mut x = String::new(); - for i in bar.iter() { - x.push_str(i.as_str()); - } + // NOTE: order matters to the final format - blocks - .root - .set_title(&mut blocks.disp, &x) - .expect("Failed to set title of root"); + rx.for_each(|data| { + blocks.update(data); + future::ready(()) + }) + .await; } diff --git a/src/types.rs b/src/types.rs index 9b31946..9fd834e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,4 +1,3 @@ -use breadx::{display::*, window::Window}; #[derive(Debug, Clone)] pub enum ThreadsData { @@ -140,21 +139,3 @@ pub struct BitCoins { pub delay: f64, } -pub struct Blocks { - pub disp: Display<name::NameConnection>, - pub root: Window, -} - -impl Blocks { - pub fn new() -> Self { - let disp = Display::create(None, None).expect("Failed to create x11 connection"); - let root = disp.default_screen().root; - Self { disp, root } - } -} - -impl Default for Blocks { - fn default() -> Self { - Self::new() - } -} diff --git a/src/utils/battery.rs b/src/utils/battery.rs index d3688e7..432e22a 100644 --- a/src/utils/battery.rs +++ b/src/utils/battery.rs @@ -5,7 +5,7 @@ use battery::Manager; // TODO: better error handeling // getting battery percentage -pub fn get_battery() -> ThreadsData { +pub async fn get_battery() -> ThreadsData { let battery_manager = if let Ok(manager) = Manager::new() { manager } else { diff --git a/src/utils/bitcoins.rs b/src/utils/bitcoins.rs index 465d907..94a0a3b 100644 --- a/src/utils/bitcoins.rs +++ b/src/utils/bitcoins.rs @@ -7,7 +7,7 @@ struct Response { price_24h: f64, } -pub fn get_price() -> ThreadsData { +pub async fn get_price() -> ThreadsData { let url = format!( "https://api.blockchain.com/v3/exchange/tickers/{}", CONFIG.bitcoins.symbol diff --git a/src/utils/cpu.rs b/src/utils/cpu.rs index 2047a07..f49fda0 100644 --- a/src/utils/cpu.rs +++ b/src/utils/cpu.rs @@ -3,7 +3,7 @@ use crate::types::ThreadsData; use std::fs::read_to_string; // getting cpu temperature -pub fn get_cpu_temp() -> ThreadsData { +pub async fn get_cpu_temp() -> ThreadsData { let buf = match read_to_string("/sys/class/thermal/thermal_zone0/temp") { Ok(data) => data, _ => return ThreadsData::CpuTemp(String::from("Error reading temp")), diff --git a/src/utils/disk.rs b/src/utils/disk.rs index 2250911..33b6449 100644 --- a/src/utils/disk.rs +++ b/src/utils/disk.rs @@ -1,7 +1,7 @@ use crate::config::CONFIG; use crate::types::ThreadsData; -pub fn get_disk() -> ThreadsData { +pub async fn get_disk() -> ThreadsData { const GB: u64 = (1024 * 1024) * 1024; let statvfs = nix::sys::statvfs::statvfs("/").unwrap(); let mut disk_used = String::new(); diff --git a/src/utils/load_average.rs b/src/utils/load_average.rs index 43dc88b..337875c 100644 --- a/src/utils/load_average.rs +++ b/src/utils/load_average.rs @@ -3,7 +3,7 @@ use crate::types::ThreadsData; use nix::libc::{c_double, c_int, getloadavg}; -pub fn get_load_avg() -> ThreadsData { +pub async fn get_load_avg() -> ThreadsData { let mut data: [c_double; 3] = [0f64; 3]; unsafe { getloadavg(data.as_mut_ptr(), data.len() as c_int) }; let [load, _, _] = data; diff --git a/src/utils/memory.rs b/src/utils/memory.rs index 3d51fd5..46b571b 100644 --- a/src/utils/memory.rs +++ b/src/utils/memory.rs @@ -7,7 +7,7 @@ mem_used = (mem_total + shmem - mem_free - mem_buffers - mem_cached - mem_srecl thanks for htop's developer on stackoverflow for providing this algorithm to calculate used memory. */ -pub fn get_memory() -> ThreadsData { +pub async fn get_memory() -> ThreadsData { let buf = match read_to_string("/proc/meminfo") { Ok(data) => data, _ => return ThreadsData::Memory(String::from("Error Reading memory!")), diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 22b4bad..6e0d944 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -12,3 +12,4 @@ pub mod time; pub mod uptime; pub mod volume; pub mod weather; + diff --git a/src/utils/mpd.rs b/src/utils/mpd.rs index 678b59d..95af768 100644 --- a/src/utils/mpd.rs +++ b/src/utils/mpd.rs @@ -3,7 +3,7 @@ use crate::types::ThreadsData; use mpd::{Client, Song}; // getting mpd song file -pub fn get_mpd_current() -> ThreadsData { +pub async fn get_mpd_current() -> ThreadsData { let stream_path = format!("{}:{}", CONFIG.mpd.host, CONFIG.mpd.port); let empty_data = ThreadsData::Mpd(String::from("")); let mut conn = match Client::connect(&stream_path) { diff --git a/src/utils/netspeed.rs b/src/utils/netspeed.rs index 16caaa4..fae36b5 100644 --- a/src/utils/netspeed.rs +++ b/src/utils/netspeed.rs @@ -1,13 +1,13 @@ use crate::config::CONFIG; use crate::types::ThreadsData; use std::fs::read_to_string; -use std::thread; +use async_std::task::sleep; use std::time::Duration; -pub fn get_netspeed() -> ThreadsData { +pub async fn get_netspeed() -> ThreadsData { let tx1: u64 = parse_speed_file("tx_bytes"); let rx1: u64 = parse_speed_file("rx_bytes"); - thread::sleep(Duration::from_secs(1)); + sleep(Duration::from_secs(1)).await; let tx2: u64 = parse_speed_file("tx_bytes"); let rx2: u64 = parse_speed_file("rx_bytes"); diff --git a/src/utils/pub_ip.rs b/src/utils/pub_ip.rs index 4f1335f..dde354a 100644 --- a/src/utils/pub_ip.rs +++ b/src/utils/pub_ip.rs @@ -1,7 +1,7 @@ use crate::config::CONFIG; use crate::types::ThreadsData; -pub fn get_pub_ip() -> ThreadsData { +pub async fn get_pub_ip() -> ThreadsData { let url = "http://api.ipify.org".to_string(); let _err = String::from("Error"); let res = match minreq::get(url).send() { diff --git a/src/utils/spotify.rs b/src/utils/spotify.rs index c48471d..5da9fea 100644 --- a/src/utils/spotify.rs +++ b/src/utils/spotify.rs @@ -5,7 +5,7 @@ use dbus::{arg, blocking::Connection}; use std::time::Duration; // getting spotify current artist and title. -pub fn get_spotify() -> ThreadsData { +pub async fn get_spotify() -> ThreadsData { let empty_data = ThreadsData::Spotify(String::from("")); let conn = match Connection::new_session() { Ok(conn) => conn, diff --git a/src/utils/time.rs b/src/utils/time.rs index 191d91b..903f301 100644 --- a/src/utils/time.rs +++ b/src/utils/time.rs @@ -2,7 +2,7 @@ use crate::config::CONFIG; use crate::types::ThreadsData; use chrono::prelude::*; -pub fn get_time() -> ThreadsData { +pub async fn get_time() -> ThreadsData { let now = Local::now(); let data = format!( @@ -11,6 +11,5 @@ pub fn get_time() -> ThreadsData { now.format(&CONFIG.time.format), CONFIG.seperator ); - ThreadsData::Time(data) } diff --git a/src/utils/uptime.rs b/src/utils/uptime.rs index f635b6b..e575e78 100644 --- a/src/utils/uptime.rs +++ b/src/utils/uptime.rs @@ -2,7 +2,7 @@ use crate::config::CONFIG; use crate::types::ThreadsData; use nix::time::clock_gettime; -pub fn get_uptime() -> ThreadsData { +pub async fn get_uptime() -> ThreadsData { let (_, hour, minutes, seconds) = get_uptime_data(); let uptime = if hour > 0 { format!("{}:{}:{}", hour, minutes, seconds) diff --git a/src/utils/volume.rs b/src/utils/volume.rs index 05c1dbe..47b4ed3 100644 --- a/src/utils/volume.rs +++ b/src/utils/volume.rs @@ -3,7 +3,7 @@ use crate::types::ThreadsData; use alsa::mixer::{Mixer, SelemChannelId, SelemId}; // getting volume percentage -pub fn get_volume() -> ThreadsData { +pub async fn get_volume() -> ThreadsData { let card = if CONFIG.volume.card == "PULSE" { "pulse" } else { diff --git a/src/utils/weather.rs b/src/utils/weather.rs index 11e446f..10c887c 100644 --- a/src/utils/weather.rs +++ b/src/utils/weather.rs @@ -2,7 +2,7 @@ use crate::config::CONFIG; use crate::types::ThreadsData; // will make a GET request from wttr.in -pub fn get_weather() -> ThreadsData { +pub async fn get_weather() -> ThreadsData { let format = if CONFIG.weather.format.is_empty() { String::from("%l:+%t") } else { |