From ffad8074c4b4a1e7a7af281126637499d1242ee7 Mon Sep 17 00:00:00 2001 From: mustafa salih Date: Thu, 24 Dec 2020 04:34:32 +0300 Subject: [PATCH] Added sound/volume and it can be customized through the rsblocks.yml file --- README.md | 1 + rsblocks.yml | 7 ++++ src/load_config.rs | 96 ++++++++++++++++++++++++++++++---------------- src/main.rs | 31 +++++++++++---- src/sound.rs | 24 ++++++++++++ 5 files changed, 117 insertions(+), 42 deletions(-) create mode 100644 src/sound.rs diff --git a/README.md b/README.md index 9bdc3c7..0e3c6bb 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ A minimal fast dwm status bar written in **Rust** 🦀 * Time/Date * Used Memory * Used Disk space +* Sound volume _reads from `amixer` for now_ * Easy to configure * Minimal diff --git a/rsblocks.yml b/rsblocks.yml index 86ea00e..7a6f910 100644 --- a/rsblocks.yml +++ b/rsblocks.yml @@ -9,7 +9,14 @@ time: format: '%d %b, %I:%M:%S %p' memory: + enable: true icon: '▦' disk: + enable: true icon: '' + +# reads from amixer +volume: + enable: false + icon: '' diff --git a/src/load_config.rs b/src/load_config.rs index 46a630e..763b635 100644 --- a/src/load_config.rs +++ b/src/load_config.rs @@ -9,10 +9,17 @@ pub struct Time { pub struct Memory { pub icon: String, + pub enabled: bool, } pub struct Disk { pub icon: String, + pub enabled: bool, +} + +pub struct Volume { + pub icon: String, + pub enabled: bool, } pub struct Settings { @@ -20,6 +27,7 @@ pub struct Settings { pub time: Time, pub memory: Memory, pub disk: Disk, + pub volume: Volume, } pub fn load() -> Result { @@ -34,43 +42,24 @@ pub fn load() -> Result { }; file.read_to_string(&mut data)?; - let yml_doc = &YamlLoader::load_from_str(&data).unwrap()[0]; - let settings = gen_settings(yml_doc); + let yml_content = &YamlLoader::load_from_str(&data).unwrap()[0]; + let settings = gen_settings(yml_content); Ok(settings) } fn gen_settings(doc: &yaml::Yaml) -> Settings { - let seperator: String; - let time_format: String; - let time_icon: String; - let mem_icon: String; - let disk_icon: String; + // setting icons + let seperator = get_or_set_string(doc, "general", "seperator", "|"); + let time_icon = get_or_set_string(doc, "time", "icon", ""); + let time_format = get_or_set_string(doc, "time", "format", "%T"); + let mem_icon = get_or_set_string(doc, "memory", "icon", ""); + let disk_icon = get_or_set_string(doc, "disk", "icon", ""); + let volume_icon = get_or_set_string(doc, "volume", "icon", ""); - if doc["general"]["seperator"].is_badvalue() { - seperator = String::from("|"); - } else { - seperator = String::from(doc["general"]["seperator"].as_str().unwrap()); - } - if doc["time"]["icon"].is_badvalue() { - time_icon = String::from("") - } else { - time_icon = String::from(doc["time"]["icon"].as_str().unwrap()); - } - if doc["time"]["format"].is_badvalue() { - time_format = String::from("%T") - } else { - time_format = String::from(doc["time"]["format"].as_str().unwrap()) - } - if doc["memory"]["icon"].is_badvalue() { - mem_icon = String::from("") - } else { - mem_icon = String::from(doc["memory"]["icon"].as_str().unwrap()); - } - if doc["disk"]["icon"].is_badvalue() { - disk_icon = String::from("") - } else { - disk_icon = String::from(doc["disk"]["icon"].as_str().unwrap()); - } + // setting enable status, everything false by default + let disk_enabled = get_or_set_bool(doc, "disk", "enable"); + let memory_enabled = get_or_set_bool(doc, "memory", "enable"); + let volume_enabled = get_or_set_bool(doc, "volume", "enable"); Settings { seperator, @@ -78,11 +67,44 @@ fn gen_settings(doc: &yaml::Yaml) -> Settings { format: time_format, icon: time_icon, }, - memory: Memory { icon: mem_icon }, - disk: Disk { icon: disk_icon }, + memory: Memory { + icon: mem_icon, + enabled: memory_enabled, + }, + disk: Disk { + icon: disk_icon, + enabled: disk_enabled, + }, + volume: Volume { + icon: volume_icon, + enabled: volume_enabled, + }, } } +// getting the bool value from rsblocks.yml file or set it false if it does not exist +fn get_or_set_bool(doc: &yaml::Yaml, parent: &str, child: &str) -> bool { + let val: bool; + if doc[parent][child].is_badvalue() { + val = false; + } else { + val = doc[parent][child].as_bool().unwrap() + } + val +} + +// getting the value from the rsblocks.yml file or set the default in the last parameter +fn get_or_set_string(doc: &yaml::Yaml, parent: &str, child: &str, default_val: &str) -> String { + let val: String; + if doc[parent][child].is_badvalue() { + val = String::from(default_val) + } else { + val = String::from(doc[parent][child].as_str().unwrap()); + } + + val +} + fn load_defaults() -> Settings { Settings { seperator: String::from("|"), @@ -92,9 +114,15 @@ fn load_defaults() -> Settings { }, memory: Memory { icon: String::from(""), + enabled: false, }, disk: Disk { icon: String::from(""), + enabled: false, + }, + volume: Volume { + icon: String::from(""), + enabled: false, }, } } diff --git a/src/main.rs b/src/main.rs index 23960af..bed2045 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,21 +6,36 @@ mod date; mod disk; mod load_config; mod mem; +mod sound; fn main() { let settings = load_config::load().unwrap(); - + sound::get_sound(&settings); loop { - let args = format!( - "{}{}{}", - disk::disk_free(&settings), - mem::mem(&settings).unwrap(), - date::fmt_date(&settings) - ); + let mut bar = String::from(""); + + // the order of the IF's below matters to the final format + + if settings.volume.enabled { + // volume return String + bar.push_str(&sound::get_sound(&settings)); + } + + if settings.disk.enabled { + // disk_free return String + bar.push_str(&disk::disk_free(&settings)); + } + + if settings.memory.enabled { + // mem return Result + bar.push_str(&mem::mem(&settings).unwrap()); + } + + bar.push_str(&date::fmt_date(&settings)); Command::new("xsetroot") .arg("-name") - .arg(args) + .arg(bar) .output() .unwrap(); diff --git a/src/sound.rs b/src/sound.rs new file mode 100644 index 0000000..7a7ce63 --- /dev/null +++ b/src/sound.rs @@ -0,0 +1,24 @@ +use crate::load_config::Settings; +use std::process::Command; + +// TODO: what a horrible solution to get the sound, i dont like it +// find another way you dumb fuck + +pub fn get_sound(setting: &Settings) -> String { + let cmd_content = Command::new("amixer") + .arg("get") + .arg("Master") + .output() + .unwrap(); + + let vol: String = String::from_utf8_lossy(&cmd_content.stdout) + .split('\n') + .collect::>()[4] + .split('[') + .collect::>()[1] + .split(']') + .collect::>()[0] + .to_string(); + + format!(" {} {} {}", setting.volume.icon, vol, setting.seperator) +}