Merge pull request #6 from MustafaSalih1993/dev

Added sound/volume and it can be customized through  the rsblocks.yml…
This commit is contained in:
Mustafa Salih 2020-12-24 04:36:50 +03:00 committed by GitHub
commit 330e95ed1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 117 additions and 42 deletions

View File

@ -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

View File

@ -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: ''

View File

@ -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,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,
},
}
}

View File

@ -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();

24
src/sound.rs Normal file
View File

@ -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)
}