Merge pull request #25 from MustafaSalih1993/dev

Added uptime
This commit is contained in:
Mustafa Salih 2021-01-10 06:15:46 +03:00 committed by GitHub
commit 944b45fe7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 7 deletions

View File

@ -17,6 +17,7 @@ A multi threaded fast status bar for dwm window manager written in **Rust** 🦀
* Sound Volume * Sound Volume
* Battery Percentage * Battery Percentage
* Cpu Temperature * Cpu Temperature
* Uptime
* Easy to configure with `rsblocks.yml` file * Easy to configure with `rsblocks.yml` file

View File

@ -1,7 +1,6 @@
use alsa::mixer::{Mixer, SelemChannelId, SelemId}; use alsa::mixer::{Mixer, SelemChannelId, SelemId};
use breadx::{display::*, window::Window}; use breadx::{display::*, window::Window};
use chrono::prelude::*; use chrono::prelude::*;
use minreq;
use std::fs::File; use std::fs::File;
use std::io::Error; use std::io::Error;
use std::io::Read; use std::io::Read;
@ -19,6 +18,7 @@ pub enum ThreadsData {
Weather(String), Weather(String),
Battery(String), Battery(String),
CpuTemp(String), CpuTemp(String),
Uptime(String),
} }
#[derive(Clone)] #[derive(Clone)]
@ -31,6 +31,7 @@ pub struct Config {
pub weather: Weather, pub weather: Weather,
pub battery: Battery, pub battery: Battery,
pub cpu_temperature: CpuTemp, pub cpu_temperature: CpuTemp,
pub uptime: Uptime,
} }
#[derive(Clone)] #[derive(Clone)]
@ -85,6 +86,12 @@ pub struct CpuTemp {
pub delay: f64, pub delay: f64,
} }
#[derive(Clone)]
pub struct Uptime {
pub icon: String,
pub enabled: bool,
pub delay: f64,
}
/* TODOS /* TODOS
TODO 1: Error handling required if rsblocks.yml file is empty. TODO 1: Error handling required if rsblocks.yml file is empty.
@ -167,6 +174,11 @@ fn load_defaults() -> Config {
enabled: false, enabled: false,
delay: 120.0, delay: 120.0,
}, },
uptime: Uptime {
icon: String::from(""),
enabled: false,
delay: 60.0,
},
} }
} }
@ -185,6 +197,7 @@ fn parse_config(doc: &yaml::Yaml) -> Config {
let weather_icon = get_or_set_string(doc, "weather", "icon", ""); let weather_icon = get_or_set_string(doc, "weather", "icon", "");
let battery_icon = get_or_set_string(doc, "battery", "icon", ""); let battery_icon = get_or_set_string(doc, "battery", "icon", "");
let cpu_temperature_icon = get_or_set_string(doc, "cpu_temperature", "icon", ""); let cpu_temperature_icon = get_or_set_string(doc, "cpu_temperature", "icon", "");
let uptime_icon = get_or_set_string(doc, "uptime", "icon", "");
//parsing formats and city weather //parsing formats and city weather
let time_format = get_or_set_string(doc, "time", "format", "%T"); let time_format = get_or_set_string(doc, "time", "format", "%T");
@ -198,6 +211,7 @@ fn parse_config(doc: &yaml::Yaml) -> Config {
let weather_enabled = get_or_set_bool(doc, "weather", "enable"); let weather_enabled = get_or_set_bool(doc, "weather", "enable");
let battery_enabled = get_or_set_bool(doc, "battery", "enable"); let battery_enabled = get_or_set_bool(doc, "battery", "enable");
let cpu_temperature_enabled = get_or_set_bool(doc, "cpu_temperature", "enable"); let cpu_temperature_enabled = get_or_set_bool(doc, "cpu_temperature", "enable");
let uptime_enabled = get_or_set_bool(doc, "uptime", "enable");
// parsing update_delay state (should be all seconds in f64 type) // parsing update_delay state (should be all seconds in f64 type)
let time_delay = get_or_set_f64(doc, "time", "delay", 1.0); let time_delay = get_or_set_f64(doc, "time", "delay", 1.0);
@ -207,6 +221,7 @@ fn parse_config(doc: &yaml::Yaml) -> Config {
let weather_delay = get_or_set_f64(doc, "weather", "delay", 7200.0); let weather_delay = get_or_set_f64(doc, "weather", "delay", 7200.0);
let battery_delay = get_or_set_f64(doc, "battery", "delay", 120.0); let battery_delay = get_or_set_f64(doc, "battery", "delay", 120.0);
let cpu_temperature_delay = get_or_set_f64(doc, "cpu_temperature", "delay", 120.0); let cpu_temperature_delay = get_or_set_f64(doc, "cpu_temperature", "delay", 120.0);
let uptime_delay = get_or_set_f64(doc, "uptime", "delay", 60.0);
// parsing card for volume, ALSA or PULSE // parsing card for volume, ALSA or PULSE
let volume_card = get_or_set_string(doc, "volume", "card", "ALSA"); let volume_card = get_or_set_string(doc, "volume", "card", "ALSA");
@ -255,6 +270,11 @@ fn parse_config(doc: &yaml::Yaml) -> Config {
enabled: cpu_temperature_enabled, enabled: cpu_temperature_enabled,
delay: cpu_temperature_delay, delay: cpu_temperature_delay,
}, },
uptime: Uptime {
icon: uptime_icon,
enabled: uptime_enabled,
delay: uptime_delay,
},
} }
} }
@ -310,6 +330,12 @@ impl Blocks {
} }
} }
impl Default for Blocks {
fn default() -> Self {
Self::new()
}
}
pub fn run(config: Config, mut blocks: Blocks) { pub fn run(config: Config, mut blocks: Blocks) {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
// volume thread // volume thread
@ -388,10 +414,22 @@ pub fn run(config: Config, mut blocks: Blocks) {
}); });
} }
// Uptime thread
if config.uptime.enabled {
let uptime_tx = tx.clone();
let configcp = config.clone();
let uptime_data = get_uptime(&configcp).unwrap();
let mut uptime_data = ThreadsData::Uptime(uptime_data);
thread::spawn(move || loop {
uptime_tx.send(uptime_data).unwrap();
uptime_data = ThreadsData::Uptime(get_uptime(&configcp).unwrap());
thread::sleep(Duration::from_secs_f64(configcp.uptime.delay))
});
}
// Time thread // Time thread
{ {
let time_tx = tx; let time_tx = tx;
let configcp = config.clone(); let configcp = config;
let mut time_data = ThreadsData::Time(get_time(&configcp)); let mut time_data = ThreadsData::Time(get_time(&configcp));
thread::spawn(move || loop { thread::spawn(move || loop {
time_tx.send(time_data).unwrap(); time_tx.send(time_data).unwrap();
@ -403,7 +441,7 @@ pub fn run(config: Config, mut blocks: Blocks) {
//Main //Main
{ {
// NOTE: order matters to the final format // NOTE: order matters to the final format
let mut bar: Vec<String> = vec!["".to_string(); 7]; let mut bar: Vec<String> = vec!["".to_string(); 8];
//iterating the values recieved from the threads //iterating the values recieved from the threads
for data in rx { for data in rx {
match data { match data {
@ -413,7 +451,8 @@ pub fn run(config: Config, mut blocks: Blocks) {
ThreadsData::Memory(x) => bar[3] = x, ThreadsData::Memory(x) => bar[3] = x,
ThreadsData::CpuTemp(x) => bar[4] = x, ThreadsData::CpuTemp(x) => bar[4] = x,
ThreadsData::Battery(x) => bar[5] = x, ThreadsData::Battery(x) => bar[5] = x,
ThreadsData::Time(x) => bar[6] = x, ThreadsData::Uptime(x) => bar[6] = x,
ThreadsData::Time(x) => bar[7] = x,
} }
// match ends here // match ends here
@ -422,7 +461,7 @@ pub fn run(config: Config, mut blocks: Blocks) {
} }
} }
pub fn update(bar: &Vec<String>, blocks: &mut Blocks) { pub fn update(bar: &[String], blocks: &mut Blocks) {
// TODO: FIX ME, this solution sucks // TODO: FIX ME, this solution sucks
let mut x = String::new(); let mut x = String::new();
for i in bar.iter() { for i in bar.iter() {
@ -655,3 +694,22 @@ pub fn get_cpu_temp(config: &Config) -> Result<String, std::io::Error> {
); );
Ok(result) Ok(result)
} }
pub fn get_uptime(config: &Config) -> Result<String, std::io::Error> {
let mut buf = String::new();
File::open("/proc/uptime")?.read_to_string(&mut buf)?;
let buf: f32 = buf.split(' ').collect::<Vec<&str>>()[0].parse().unwrap();
let hour = buf.round() as u32 / 3600;
let rem = buf as u32 - hour * 3600;
let minutes = rem / 60;
let uptime = if hour > 0 {
format!("{}:{}", hour, minutes)
} else {
format!("{} min", minutes)
};
let result = format!(" {} {} {}", config.uptime.icon, uptime, config.seperator);
Ok(result)
}

View File

@ -1,5 +1,3 @@
use rsblocks;
fn main() { fn main() {
let config = rsblocks::load_config().unwrap(); let config = rsblocks::load_config().unwrap();
let blocks = rsblocks::Blocks::new(); let blocks = rsblocks::Blocks::new();