Merge pull request #44 from MustafaSalih1993/dev

removed some doublicated code
This commit is contained in:
Mustafa Salih 2021-02-03 03:43:31 +03:00 committed by GitHub
commit c4cd23f471
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 120 additions and 149 deletions

View File

@ -5,136 +5,86 @@ use std::sync::mpsc;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
fn spawn_thread_loop<F>(tx: std::sync::mpsc::Sender<ThreadsData>, data: F, delay: f64)
where
F: Fn() -> ThreadsData + Send + 'static,
{
thread::spawn(move || loop {
tx.send(data()).unwrap();
thread::sleep(Duration::from_secs_f64(delay));
});
}
pub fn run(mut blocks: Blocks) { pub fn run(mut blocks: Blocks) {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
// loadavrage thread // loadavrage thread
if CONFIG.loadavg.enabled { if CONFIG.loadavg.enabled {
let loadavg_tx = tx.clone(); spawn_thread_loop(tx.clone(), load_average::get_load_avg, CONFIG.loadavg.delay);
thread::spawn(move || loop {
let loadavg_data = ThreadsData::LoadAvg(load_average::get_load_avg());
loadavg_tx.send(loadavg_data).unwrap();
thread::sleep(Duration::from_secs_f64(CONFIG.loadavg.delay))
});
} }
// public ip thread // public ip thread
if CONFIG.pub_ip.enabled { if CONFIG.pub_ip.enabled {
let pub_ip_tx = tx.clone(); spawn_thread_loop(tx.clone(), pub_ip::get_pub_ip, CONFIG.pub_ip.delay);
thread::spawn(move || loop {
let pub_ip_data = ThreadsData::PubIp(pub_ip::get_pub_ip());
pub_ip_tx.send(pub_ip_data).unwrap();
thread::sleep(Duration::from_secs_f64(CONFIG.pub_ip.delay))
});
} }
// spotify thread // spotify thread
if CONFIG.spotify.enabled { if CONFIG.spotify.enabled {
let spotify_tx = tx.clone(); spawn_thread_loop(tx.clone(), spotify::get_spotify, CONFIG.spotify.delay);
thread::spawn(move || loop {
let spotify_data = ThreadsData::Spotify(spotify::get_spotify());
spotify_tx.send(spotify_data).unwrap();
thread::sleep(Duration::from_secs_f64(CONFIG.spotify.delay))
});
} }
// mpd thread // mpd thread
if CONFIG.mpd.enabled { if CONFIG.mpd.enabled {
let mpd_tx = tx.clone(); spawn_thread_loop(tx.clone(), mpd::get_mpd_current, CONFIG.mpd.delay);
thread::spawn(move || loop {
let mpd_data = ThreadsData::Mpd(mpd::get_mpd_current());
mpd_tx.send(mpd_data).unwrap();
thread::sleep(Duration::from_secs_f64(CONFIG.mpd.delay))
});
} }
// volume thread // volume thread
if CONFIG.volume.enabled { if CONFIG.volume.enabled {
let volume_tx = tx.clone(); spawn_thread_loop(tx.clone(), volume::get_volume, CONFIG.volume.delay);
thread::spawn(move || loop {
let vol_data = ThreadsData::Sound(volume::get_volume());
volume_tx.send(vol_data).unwrap();
thread::sleep(Duration::from_secs_f64(CONFIG.volume.delay))
});
}
// net speed thread
if CONFIG.netspeed.enabled {
let net_tx = tx.clone();
thread::spawn(move || loop {
// get_netspeed will sleep inside the function
let net_data = ThreadsData::NetSpeed(netspeed::get_netspeed());
net_tx.send(net_data).unwrap();
});
} }
// Disk thread // Disk thread
if CONFIG.disk.enabled { if CONFIG.disk.enabled {
let disk_tx = tx.clone(); spawn_thread_loop(tx.clone(), disk::get_disk, CONFIG.disk.delay);
thread::spawn(move || loop {
let disk_data = ThreadsData::Disk(disk::get_disk());
disk_tx.send(disk_data).unwrap();
thread::sleep(Duration::from_secs_f64(CONFIG.disk.delay))
});
} }
// Memory thread // Memory thread
if CONFIG.memory.enabled { if CONFIG.memory.enabled {
let memory_tx = tx.clone(); spawn_thread_loop(tx.clone(), memory::get_memory, CONFIG.memory.delay);
thread::spawn(move || loop {
let memory_data = ThreadsData::Memory(memory::get_memory().unwrap());
memory_tx.send(memory_data).unwrap();
thread::sleep(Duration::from_secs_f64(CONFIG.memory.delay))
});
} }
// Weather thread // Weather thread
if CONFIG.weather.enabled { if CONFIG.weather.enabled {
let weather_tx = tx.clone(); spawn_thread_loop(tx.clone(), weather::get_weather, CONFIG.weather.delay);
thread::spawn(move || loop {
let weather_data = ThreadsData::Weather(weather::get_weather());
weather_tx.send(weather_data).unwrap();
thread::sleep(Duration::from_secs_f64(CONFIG.weather.delay))
});
} }
// Battery thread // Battery thread
if CONFIG.battery.enabled { if CONFIG.battery.enabled {
let battery_tx = tx.clone(); spawn_thread_loop(tx.clone(), battery::get_battery, CONFIG.battery.delay);
thread::spawn(move || loop {
let battery_data = ThreadsData::Battery(battery::get_battery().unwrap());
battery_tx.send(battery_data).unwrap();
thread::sleep(Duration::from_secs_f64(CONFIG.battery.delay))
});
} }
// Cpu temperature thread // Cpu temperature thread
if CONFIG.cpu_temperature.enabled { if CONFIG.cpu_temperature.enabled {
let cpu_temp_tx = tx.clone(); spawn_thread_loop(tx.clone(), cpu::get_cpu_temp, CONFIG.cpu_temperature.delay);
thread::spawn(move || loop {
let cpu_temp_data = ThreadsData::CpuTemp(cpu::get_cpu_temp().unwrap());
cpu_temp_tx.send(cpu_temp_data).unwrap();
thread::sleep(Duration::from_secs_f64(CONFIG.cpu_temperature.delay))
});
} }
// Uptime thread // Uptime thread
if CONFIG.uptime.enabled { if CONFIG.uptime.enabled {
let uptime_tx = tx.clone(); spawn_thread_loop(tx.clone(), uptime::get_uptime, CONFIG.uptime.delay);
}
// net speed thread
// get_netspeed will sleep inside the function
if CONFIG.netspeed.enabled {
let net_tx = tx.clone();
thread::spawn(move || loop { thread::spawn(move || loop {
let uptime_data = ThreadsData::Uptime(uptime::get_uptime().unwrap()); let net_data = netspeed::get_netspeed();
uptime_tx.send(uptime_data).unwrap(); net_tx.send(net_data).unwrap();
thread::sleep(Duration::from_secs_f64(CONFIG.uptime.delay))
}); });
} }
// Time thread // Time thread
{ {
let time_tx = tx; spawn_thread_loop(tx, time::get_time, CONFIG.time.delay);
thread::spawn(move || loop {
let time_data = ThreadsData::Time(time::get_time());
time_tx.send(time_data).unwrap();
thread::sleep(Duration::from_secs_f64(CONFIG.time.delay))
});
} }
//Main //Main

View File

@ -1,10 +1,11 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use std::fs::File; use crate::types::ThreadsData;
use std::io::Error; use std::fs::read_to_string;
use std::io::Read;
// getting battery percentage // getting battery percentage
pub fn get_battery() -> Result<String, Error> { pub fn get_battery() -> ThreadsData {
let error = ThreadsData::Battery(String::from("check your battery source name"));
let battery_full_cap_file = format!( let battery_full_cap_file = format!(
"/sys/class/power_supply/{}/charge_full_design", "/sys/class/power_supply/{}/charge_full_design",
CONFIG.battery.source CONFIG.battery.source
@ -14,17 +15,16 @@ pub fn get_battery() -> Result<String, Error> {
CONFIG.battery.source CONFIG.battery.source
); );
let mut buf = String::new(); let buf = match read_to_string(battery_full_cap_file) {
Ok(file) => file,
match File::open(&battery_full_cap_file) { Err(_) => return error,
Ok(mut file) => file.read_to_string(&mut buf)?,
Err(_) => return Ok(String::from("check your battery source name")),
}; };
let full_design = buf.trim().parse::<u32>().unwrap(); let full_design = buf.trim().parse::<u32>().unwrap();
buf.clear();
// No need to error check if passed the above match let buf = match read_to_string(&battery_charge_now_file) {
File::open(&battery_charge_now_file)?.read_to_string(&mut buf)?; Ok(data) => data,
_ => return error,
};
let charge_now = buf.trim().parse::<u32>().unwrap(); let charge_now = buf.trim().parse::<u32>().unwrap();
@ -33,5 +33,5 @@ pub fn get_battery() -> Result<String, Error> {
" {} {:.0}% {}", " {} {:.0}% {}",
CONFIG.battery.icon, battery_percentage, CONFIG.seperator CONFIG.battery.icon, battery_percentage, CONFIG.seperator
); );
Ok(result) ThreadsData::Battery(result)
} }

View File

@ -1,11 +1,14 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use std::fs::File; use crate::types::ThreadsData;
use std::io::Read; use std::fs::read_to_string;
// getting cpu temperature // getting cpu temperature
pub fn get_cpu_temp() -> Result<String, std::io::Error> { pub fn get_cpu_temp() -> ThreadsData {
let mut buf = String::new(); let buf = match read_to_string("/sys/class/thermal/thermal_zone0/temp") {
File::open("/sys/class/thermal/thermal_zone0/temp")?.read_to_string(&mut buf)?; Ok(data) => data,
_ => return ThreadsData::CpuTemp(String::from("Error reading temp")),
};
let value = buf.trim().parse::<f32>().unwrap(); let value = buf.trim().parse::<f32>().unwrap();
let result = format!( let result = format!(
@ -14,5 +17,5 @@ pub fn get_cpu_temp() -> Result<String, std::io::Error> {
value / 1000.0, value / 1000.0,
CONFIG.seperator CONFIG.seperator
); );
Ok(result) ThreadsData::CpuTemp(result)
} }

View File

@ -1,6 +1,7 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::types::ThreadsData;
pub fn get_disk() -> String { pub fn get_disk() -> ThreadsData {
const GB: u64 = (1024 * 1024) * 1024; const GB: u64 = (1024 * 1024) * 1024;
let statvfs = nix::sys::statvfs::statvfs("/").unwrap(); let statvfs = nix::sys::statvfs::statvfs("/").unwrap();
let mut disk_used = String::new(); let mut disk_used = String::new();
@ -10,8 +11,9 @@ pub fn get_disk() -> String {
let used = total - available; let used = total - available;
disk_used.push_str(&format!("{}G", used)); disk_used.push_str(&format!("{}G", used));
format!( let data = format!(
" {} {} {}", " {} {} {}",
CONFIG.disk.icon, disk_used, CONFIG.seperator CONFIG.disk.icon, disk_used, CONFIG.seperator
) );
ThreadsData::Disk(data)
} }

View File

@ -1,16 +1,18 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::types::ThreadsData;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
pub fn get_load_avg() -> String { pub fn get_load_avg() -> ThreadsData {
let mut buf = String::new(); let mut buf = String::new();
match File::open("/proc/loadavg") { match File::open("/proc/loadavg") {
Ok(mut file) => match file.read_to_string(&mut buf) { Ok(mut file) => match file.read_to_string(&mut buf) {
Ok(data) => data, Ok(data) => data,
_ => return String::from(""), _ => return ThreadsData::LoadAvg(String::from("")),
}, },
_ => return String::from("Error"), _ => return ThreadsData::LoadAvg(String::from("Error")),
}; };
let buf = buf.split_whitespace().collect::<Vec<_>>()[0]; let buf = buf.split_whitespace().collect::<Vec<_>>()[0];
format!(" {} {} {}", CONFIG.loadavg.icon, buf, CONFIG.seperator) let data = format!(" {} {} {}", CONFIG.loadavg.icon, buf, CONFIG.seperator);
ThreadsData::LoadAvg(data)
} }

View File

@ -1,16 +1,17 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use std::fs::File; use crate::types::ThreadsData;
use std::io::Read; use std::fs::read_to_string;
/* /*
mem_used = (mem_total + shmem - mem_free - mem_buffers - mem_cached - mem_srecl 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 thanks for htop's developer on stackoverflow for providing this algorithm to
calculate used memory. calculate used memory.
*/ */
pub fn get_memory() -> Result<String, std::io::Error> { pub fn get_memory() -> ThreadsData {
let mut buf = String::new(); let buf = match read_to_string("/proc/meminfo") {
Ok(data) => data,
File::open("/proc/meminfo")?.read_to_string(&mut buf)?; _ => return ThreadsData::Memory(String::from("Error Reading memory!")),
};
let mut mem_total: u32 = 0; let mut mem_total: u32 = 0;
let mut shmem: u32 = 0; let mut shmem: u32 = 0;
@ -66,7 +67,7 @@ pub fn get_memory() -> Result<String, std::io::Error> {
CONFIG.memory.icon, mem_used, CONFIG.seperator CONFIG.memory.icon, mem_used, CONFIG.seperator
); );
} }
Ok(result) ThreadsData::Memory(result)
} }
/* /*

View File

@ -1,21 +1,22 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::types::ThreadsData;
use mpd::{Client, Song}; use mpd::{Client, Song};
// yes, error handling looks fucking sucks! // yes, error handling looks fucking sucks!
// getting mpd song file // getting mpd song file
pub fn get_mpd_current() -> String { pub fn get_mpd_current() -> ThreadsData {
let stream_path = format!("{}:{}", CONFIG.mpd.host, CONFIG.mpd.port); let stream_path = format!("{}:{}", CONFIG.mpd.host, CONFIG.mpd.port);
let empty_string = String::from(""); let empty_data = ThreadsData::Mpd(String::from(""));
let mut conn = match Client::connect(&stream_path) { let mut conn = match Client::connect(&stream_path) {
Ok(connection) => connection, Ok(connection) => connection,
_ => return empty_string, _ => return empty_data,
}; };
let current: Song = match conn.currentsong() { let current: Song = match conn.currentsong() {
Ok(opt) => match opt { Ok(opt) => match opt {
Some(song) => song, Some(song) => song,
_ => return empty_string, _ => return empty_data,
}, },
_ => return empty_string, _ => return empty_data,
}; };
let result = format!( let result = format!(
@ -23,5 +24,5 @@ pub fn get_mpd_current() -> String {
CONFIG.mpd.icon, current.file, CONFIG.seperator CONFIG.mpd.icon, current.file, CONFIG.seperator
); );
result ThreadsData::Mpd(result)
} }

View File

@ -1,9 +1,10 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::types::ThreadsData;
use std::fs::read_to_string; use std::fs::read_to_string;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
pub fn get_netspeed() -> String { pub fn get_netspeed() -> ThreadsData {
let tx1: u64 = parse_speed_file("tx_bytes"); let tx1: u64 = parse_speed_file("tx_bytes");
let rx1: u64 = parse_speed_file("rx_bytes"); let rx1: u64 = parse_speed_file("rx_bytes");
thread::sleep(Duration::from_secs(1)); thread::sleep(Duration::from_secs(1));
@ -16,10 +17,11 @@ pub fn get_netspeed() -> String {
let tx = calculate(tx_bps); let tx = calculate(tx_bps);
let rx = calculate(rx_bps); let rx = calculate(rx_bps);
format!( let data = format!(
" {} {} {} {} {}", " {} {} {} {} {}",
CONFIG.netspeed.recieve_icon, rx, CONFIG.netspeed.transmit_icon, tx, CONFIG.seperator CONFIG.netspeed.recieve_icon, rx, CONFIG.netspeed.transmit_icon, tx, CONFIG.seperator
) );
ThreadsData::NetSpeed(data)
} }
fn parse_speed_file(pth: &str) -> u64 { fn parse_speed_file(pth: &str) -> u64 {

View File

@ -1,15 +1,17 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::types::ThreadsData;
pub fn get_pub_ip() -> String { pub fn get_pub_ip() -> ThreadsData {
let url = format!("http://api.ipify.org"); let url = format!("http://api.ipify.org");
let err_string = String::from("Error"); let _err = String::from("Error");
let res = match minreq::get(url).send() { let res = match minreq::get(url).send() {
Ok(resp) => match resp.as_str() { Ok(resp) => match resp.as_str() {
Ok(res_str) => res_str.trim().to_string(), Ok(res_str) => res_str.trim().to_string(),
Err(_) => err_string, Err(_) => _err,
}, },
Err(_) => err_string, Err(_) => _err,
}; };
format!(" {} {} {}", CONFIG.pub_ip.icon, res, CONFIG.seperator) let data = format!(" {} {} {}", CONFIG.pub_ip.icon, res, CONFIG.seperator);
ThreadsData::PubIp(data)
} }

View File

@ -1,14 +1,15 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::types::ThreadsData;
use dbus::blocking::stdintf::org_freedesktop_dbus::Properties; use dbus::blocking::stdintf::org_freedesktop_dbus::Properties;
use dbus::{arg, blocking::Connection}; use dbus::{arg, blocking::Connection};
use std::time::Duration; use std::time::Duration;
// getting spotify current artist and title. // getting spotify current artist and title.
// FIXME: I know im lazy asshole, this error handling looks ugly, i dont like it too, need to fix soon.
pub fn get_spotify() -> String { pub fn get_spotify() -> ThreadsData {
let empty_data = ThreadsData::Spotify(String::from(""));
let conn = match Connection::new_session() { let conn = match Connection::new_session() {
Ok(conn) => conn, Ok(conn) => conn,
_ => return String::from(""), _ => return empty_data,
}; };
let p = conn.with_proxy( let p = conn.with_proxy(
@ -19,7 +20,7 @@ pub fn get_spotify() -> String {
let metadata: arg::PropMap = match p.get("org.mpris.MediaPlayer2.Player", "Metadata") { let metadata: arg::PropMap = match p.get("org.mpris.MediaPlayer2.Player", "Metadata") {
Ok(data) => data, Ok(data) => data,
_ => return String::from(""), _ => return empty_data,
}; };
let title: Option<&String> = arg::prop_cast(&metadata, "xesam:title"); let title: Option<&String> = arg::prop_cast(&metadata, "xesam:title");
@ -38,8 +39,9 @@ pub fn get_spotify() -> String {
None => "", None => "",
}; };
format!( let data = format!(
" {} {} - {} {}", " {} {} - {} {}",
CONFIG.spotify.icon, artist, title, CONFIG.seperator CONFIG.spotify.icon, artist, title, CONFIG.seperator
) );
ThreadsData::Spotify(data)
} }

View File

@ -1,13 +1,16 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::types::ThreadsData;
use chrono::prelude::*; use chrono::prelude::*;
pub fn get_time() -> String { pub fn get_time() -> ThreadsData {
let now = Local::now(); let now = Local::now();
format!( let data = format!(
" {} {} {}", " {} {} {}",
CONFIG.time.icon, CONFIG.time.icon,
now.format(&CONFIG.time.format), now.format(&CONFIG.time.format),
CONFIG.seperator CONFIG.seperator
) );
ThreadsData::Time(data)
} }

View File

@ -1,12 +1,11 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use std::fs::File; use crate::types::ThreadsData;
use std::io::Read; use std::fs::read_to_string;
pub fn get_uptime() -> Result<String, std::io::Error> { pub fn get_uptime() -> ThreadsData {
let mut buf = String::new(); let buf = match read_to_string("/proc/uptime") {
match File::open("/proc/uptime") { Ok(data) => data,
Ok(mut file) => file.read_to_string(&mut buf)?, _ => return ThreadsData::Uptime("cant find uptime file!".to_string()),
_ => return Ok("cant find uptime file!".to_string()),
}; };
let buf: f32 = buf.split(' ').collect::<Vec<&str>>()[0].parse().unwrap(); let buf: f32 = buf.split(' ').collect::<Vec<&str>>()[0].parse().unwrap();
@ -21,5 +20,5 @@ pub fn get_uptime() -> Result<String, std::io::Error> {
format!("{} min", minutes) format!("{} min", minutes)
}; };
let result = format!(" {} {} {}", CONFIG.uptime.icon, uptime, CONFIG.seperator); let result = format!(" {} {} {}", CONFIG.uptime.icon, uptime, CONFIG.seperator);
Ok(result) ThreadsData::Uptime(result)
} }

View File

@ -1,8 +1,9 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::types::ThreadsData;
use alsa::mixer::{Mixer, SelemChannelId, SelemId}; use alsa::mixer::{Mixer, SelemChannelId, SelemId};
// getting volume percentage // getting volume percentage
pub fn get_volume() -> String { pub fn get_volume() -> ThreadsData {
let card = if CONFIG.volume.card == "PULSE" { let card = if CONFIG.volume.card == "PULSE" {
"pulse" "pulse"
} else { } else {
@ -27,5 +28,6 @@ pub fn get_volume() -> String {
((raw_volume as f64 / range as f64) * 100.) as u64 ((raw_volume as f64 / range as f64) * 100.) as u64
}; };
format!(" {} {}% {}", CONFIG.volume.icon, vol, CONFIG.seperator) let data = format!(" {} {}% {}", CONFIG.volume.icon, vol, CONFIG.seperator);
ThreadsData::Sound(data)
} }

View File

@ -1,7 +1,8 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::types::ThreadsData;
// will make a GET request from wttr.in // will make a GET request from wttr.in
pub fn get_weather() -> String { pub fn get_weather() -> ThreadsData {
let format = if CONFIG.weather.format.is_empty() { let format = if CONFIG.weather.format.is_empty() {
String::from("%l:+%t") String::from("%l:+%t")
} else { } else {
@ -18,5 +19,6 @@ pub fn get_weather() -> String {
Err(_) => err_string, Err(_) => err_string,
}; };
format!(" {} {} {}", CONFIG.weather.icon, res, CONFIG.seperator) let data = format!(" {} {} {}", CONFIG.weather.icon, res, CONFIG.seperator);
ThreadsData::Weather(data)
} }