aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMustafa Salih <37256744+MustafaSalih1993@users.noreply.github.com>2020-12-24 04:36:50 +0300
committerGitHub <noreply@github.com>2020-12-24 04:36:50 +0300
commit330e95ed1a9451d4ea23095ff9c5debeaa3943d7 (patch)
treeaecb1ca581bd64e279c8fb43dd894db1628bf5a2
parentf6affec55f1c20366e11accf76b99caf4528753f (diff)
parentffad8074c4b4a1e7a7af281126637499d1242ee7 (diff)
Merge pull request #6 from MustafaSalih1993/dev
Added sound/volume and it can be customized through the rsblocks.yml…
-rw-r--r--README.md1
-rw-r--r--rsblocks.yml7
-rw-r--r--src/load_config.rs96
-rw-r--r--src/main.rs31
-rw-r--r--src/sound.rs24
5 files changed, 117 insertions, 42 deletions
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<Settings, Error> {
@@ -34,43 +42,24 @@ pub fn load() -> Result<Settings, Error> {
};
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,9 +67,42 @@ 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 {
@@ -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::<Vec<&str>>()[4]
+ .split('[')
+ .collect::<Vec<&str>>()[1]
+ .split(']')
+ .collect::<Vec<&str>>()[0]
+ .to_string();
+
+ format!(" {} {} {}", setting.volume.icon, vol, setting.seperator)
+}