diff --git a/src/config.rs b/src/config.rs index b54f8f1..5c76c6a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -21,8 +21,21 @@ pub fn load_config() -> Result { } }; file.read_to_string(&mut data)?; - let yml_content = &YamlLoader::load_from_str(&data).unwrap()[0]; - let config = parse_config(yml_content); + + // checking if rsblocks.yml is empty + let yml_content = match YamlLoader::load_from_str(&data) { + Ok(content) => { + if content.len() > 0 { + content[0].clone() + } else { + eprintln!("configuration file looks empty, loading defaults!"); + return Ok(load_defaults()); + } + } + _ => return Ok(load_defaults()), + }; + + let config = parse_config(&yml_content); Ok(config) } diff --git a/src/main.rs b/src/main.rs index 8d986d2..e412025 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,15 @@ mod config; mod run; mod types; mod utils; +use std::env; +use std::process; fn main() { + // if X display is not found then exit the program + if env::var("DISPLAY").is_err() { + eprintln!("Error: No Display Running!"); + process::exit(1); + }; let config = config::load_config().unwrap(); let blocks = types::Blocks::new(); run::run(config, blocks); diff --git a/src/run.rs b/src/run.rs index 1131884..c3d877d 100644 --- a/src/run.rs +++ b/src/run.rs @@ -4,7 +4,6 @@ use std::sync::mpsc; use std::thread; use std::time::Duration; -// FIXME: The most part i hate is this, looks really ugly, fix me you dumb fuck pub fn run(config: Config, mut blocks: Blocks) { let (tx, rx) = mpsc::channel(); @@ -12,10 +11,9 @@ pub fn run(config: Config, mut blocks: Blocks) { if config.spotify.enabled { let spotify_tx = tx.clone(); let configcp = config.clone(); - let mut spotify_data = ThreadsData::Spotify(spotify::get_spotify(&configcp)); thread::spawn(move || loop { + let spotify_data = ThreadsData::Spotify(spotify::get_spotify(&configcp)); spotify_tx.send(spotify_data).unwrap(); - spotify_data = ThreadsData::Spotify(spotify::get_spotify(&configcp)); thread::sleep(Duration::from_secs_f64(configcp.spotify.delay)) }); } @@ -24,10 +22,9 @@ pub fn run(config: Config, mut blocks: Blocks) { if config.mpd.enabled { let mpd_tx = tx.clone(); let configcp = config.clone(); - let mut mpd_data = ThreadsData::Mpd(mpd::get_mpd_current(&configcp)); thread::spawn(move || loop { + let mpd_data = ThreadsData::Mpd(mpd::get_mpd_current(&configcp)); mpd_tx.send(mpd_data).unwrap(); - mpd_data = ThreadsData::Mpd(mpd::get_mpd_current(&configcp)); thread::sleep(Duration::from_secs_f64(configcp.mpd.delay)) }); } @@ -36,10 +33,9 @@ pub fn run(config: Config, mut blocks: Blocks) { if config.volume.enabled { let volume_tx = tx.clone(); let configcp = config.clone(); - let mut vol_data = ThreadsData::Sound(volume::get_volume(&configcp)); thread::spawn(move || loop { + let vol_data = ThreadsData::Sound(volume::get_volume(&configcp)); volume_tx.send(vol_data).unwrap(); - vol_data = ThreadsData::Sound(volume::get_volume(&configcp)); thread::sleep(Duration::from_secs_f64(configcp.volume.delay)) }); } @@ -48,10 +44,9 @@ pub fn run(config: Config, mut blocks: Blocks) { if config.disk.enabled { let disk_tx = tx.clone(); let configcp = config.clone(); - let mut disk_data = ThreadsData::Disk(disk::get_disk(&configcp)); thread::spawn(move || loop { + let disk_data = ThreadsData::Disk(disk::get_disk(&configcp)); disk_tx.send(disk_data).unwrap(); - disk_data = ThreadsData::Disk(disk::get_disk(&configcp)); thread::sleep(Duration::from_secs_f64(configcp.disk.delay)) }); } @@ -60,11 +55,9 @@ pub fn run(config: Config, mut blocks: Blocks) { if config.memory.enabled { let memory_tx = tx.clone(); let configcp = config.clone(); - let memory_data = memory::get_memory(&configcp).unwrap(); - let mut memory_data = ThreadsData::Memory(memory_data); thread::spawn(move || loop { + let memory_data = ThreadsData::Memory(memory::get_memory(&configcp).unwrap()); memory_tx.send(memory_data).unwrap(); - memory_data = ThreadsData::Memory(memory::get_memory(&configcp).unwrap()); thread::sleep(Duration::from_secs_f64(configcp.memory.delay)) }); } @@ -73,11 +66,9 @@ pub fn run(config: Config, mut blocks: Blocks) { if config.weather.enabled { let weather_tx = tx.clone(); let configcp = config.clone(); - let weather_data = weather::get_weather(&configcp); - let mut weather_data = ThreadsData::Weather(weather_data); thread::spawn(move || loop { + let weather_data = ThreadsData::Weather(weather::get_weather(&configcp)); weather_tx.send(weather_data).unwrap(); - weather_data = ThreadsData::Weather(weather::get_weather(&configcp)); thread::sleep(Duration::from_secs_f64(configcp.weather.delay)) }); } @@ -86,11 +77,9 @@ pub fn run(config: Config, mut blocks: Blocks) { if config.battery.enabled { let battery_tx = tx.clone(); let configcp = config.clone(); - let battery_data = battery::get_battery(&configcp).unwrap(); - let mut battery_data = ThreadsData::Battery(battery_data); thread::spawn(move || loop { + let battery_data = ThreadsData::Battery(battery::get_battery(&configcp).unwrap()); battery_tx.send(battery_data).unwrap(); - battery_data = ThreadsData::Battery(battery::get_battery(&configcp).unwrap()); thread::sleep(Duration::from_secs_f64(configcp.battery.delay)) }); } @@ -99,11 +88,9 @@ pub fn run(config: Config, mut blocks: Blocks) { if config.cpu_temperature.enabled { let cpu_temp_tx = tx.clone(); let configcp = config.clone(); - let cpu_temp_data = cpu::get_cpu_temp(&configcp).unwrap(); - let mut cpu_temp_data = ThreadsData::CpuTemp(cpu_temp_data); thread::spawn(move || loop { + let cpu_temp_data = ThreadsData::CpuTemp(cpu::get_cpu_temp(&configcp).unwrap()); cpu_temp_tx.send(cpu_temp_data).unwrap(); - cpu_temp_data = ThreadsData::CpuTemp(cpu::get_cpu_temp(&configcp).unwrap()); thread::sleep(Duration::from_secs_f64(configcp.cpu_temperature.delay)) }); } @@ -112,11 +99,9 @@ pub fn run(config: Config, mut blocks: Blocks) { if config.uptime.enabled { let uptime_tx = tx.clone(); let configcp = config.clone(); - let uptime_data = uptime::get_uptime(&configcp).unwrap(); - let mut uptime_data = ThreadsData::Uptime(uptime_data); thread::spawn(move || loop { + let uptime_data = ThreadsData::Uptime(uptime::get_uptime(&configcp).unwrap()); uptime_tx.send(uptime_data).unwrap(); - uptime_data = ThreadsData::Uptime(uptime::get_uptime(&configcp).unwrap()); thread::sleep(Duration::from_secs_f64(configcp.uptime.delay)) }); } @@ -125,10 +110,9 @@ pub fn run(config: Config, mut blocks: Blocks) { { let time_tx = tx; let configcp = config; - let mut time_data = ThreadsData::Time(time::get_time(&configcp)); thread::spawn(move || loop { + let time_data = ThreadsData::Time(time::get_time(&configcp)); time_tx.send(time_data).unwrap(); - time_data = ThreadsData::Time(time::get_time(&configcp)); thread::sleep(Duration::from_secs_f64(configcp.time.delay)) }); } @@ -136,7 +120,7 @@ pub fn run(config: Config, mut blocks: Blocks) { //Main { // NOTE: order matters to the final format - let mut bar: Vec = vec!["".to_string(); 10]; + let mut bar: Vec = vec![String::from(""); 10]; //iterating the values recieved from the threads for data in rx { match data { diff --git a/src/utils/battery.rs b/src/utils/battery.rs index b831773..a38d3b4 100644 --- a/src/utils/battery.rs +++ b/src/utils/battery.rs @@ -16,7 +16,6 @@ pub fn get_battery(config: &Config) -> Result { let mut buf = String::new(); - // FIXME: ugly error handling AGAIN fixing later, im lazy match File::open(&battery_full_cap_file) { Ok(mut file) => file.read_to_string(&mut buf)?, Err(_) => return Ok(String::from("check your battery source name")), @@ -24,7 +23,7 @@ pub fn get_battery(config: &Config) -> Result { let full_design = buf.trim().parse::().unwrap(); buf.clear(); - // NOTE: no need to error check if passed the above match + // No need to error check if passed the above match File::open(&battery_charge_now_file)?.read_to_string(&mut buf)?; let charge_now = buf.trim().parse::().unwrap(); diff --git a/src/utils/disk.rs b/src/utils/disk.rs index 82785b4..2aa7b63 100644 --- a/src/utils/disk.rs +++ b/src/utils/disk.rs @@ -1,6 +1,5 @@ use crate::types::Config; -// getting disk usage pub fn get_disk(config: &Config) -> String { const GB: u64 = (1024 * 1024) * 1024; let statvfs = nix::sys::statvfs::statvfs("/").unwrap(); diff --git a/src/utils/uptime.rs b/src/utils/uptime.rs index 027827a..90f52a7 100644 --- a/src/utils/uptime.rs +++ b/src/utils/uptime.rs @@ -4,7 +4,10 @@ use std::io::Read; pub fn get_uptime(config: &Config) -> Result { let mut buf = String::new(); - File::open("/proc/uptime")?.read_to_string(&mut buf)?; + match File::open("/proc/uptime") { + Ok(mut file) => file.read_to_string(&mut buf)?, + _ => return Ok("cant find uptime file!".to_string()), + }; let buf: f32 = buf.split(' ').collect::>()[0].parse().unwrap();