Added sound/volume and it can be customized through the rsblocks.yml file

This commit is contained in:
mustafa salih 2020-12-24 04:34:32 +03:00
parent d34ef0dd03
commit ffad8074c4
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 * Time/Date
* Used Memory * Used Memory
* Used Disk space * Used Disk space
* Sound volume _reads from `amixer` for now_
* Easy to configure * Easy to configure
* Minimal * Minimal

View File

@ -9,7 +9,14 @@ time:
format: '%d %b, %I:%M:%S %p' format: '%d %b, %I:%M:%S %p'
memory: memory:
enable: true
icon: '▦' icon: '▦'
disk: disk:
enable: true
icon: '' icon: ''
# reads from amixer
volume:
enable: false
icon: ''

View File

@ -9,10 +9,17 @@ pub struct Time {
pub struct Memory { pub struct Memory {
pub icon: String, pub icon: String,
pub enabled: bool,
} }
pub struct Disk { pub struct Disk {
pub icon: String, pub icon: String,
pub enabled: bool,
}
pub struct Volume {
pub icon: String,
pub enabled: bool,
} }
pub struct Settings { pub struct Settings {
@ -20,6 +27,7 @@ pub struct Settings {
pub time: Time, pub time: Time,
pub memory: Memory, pub memory: Memory,
pub disk: Disk, pub disk: Disk,
pub volume: Volume,
} }
pub fn load() -> Result<Settings, Error> { pub fn load() -> Result<Settings, Error> {
@ -34,43 +42,24 @@ pub fn load() -> Result<Settings, Error> {
}; };
file.read_to_string(&mut data)?; file.read_to_string(&mut data)?;
let yml_doc = &YamlLoader::load_from_str(&data).unwrap()[0]; let yml_content = &YamlLoader::load_from_str(&data).unwrap()[0];
let settings = gen_settings(yml_doc); let settings = gen_settings(yml_content);
Ok(settings) Ok(settings)
} }
fn gen_settings(doc: &yaml::Yaml) -> Settings { fn gen_settings(doc: &yaml::Yaml) -> Settings {
let seperator: String; // setting icons
let time_format: String; let seperator = get_or_set_string(doc, "general", "seperator", "|");
let time_icon: String; let time_icon = get_or_set_string(doc, "time", "icon", "");
let mem_icon: String; let time_format = get_or_set_string(doc, "time", "format", "%T");
let disk_icon: String; 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() { // setting enable status, everything false by default
seperator = String::from("|"); let disk_enabled = get_or_set_bool(doc, "disk", "enable");
} else { let memory_enabled = get_or_set_bool(doc, "memory", "enable");
seperator = String::from(doc["general"]["seperator"].as_str().unwrap()); let volume_enabled = get_or_set_bool(doc, "volume", "enable");
}
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());
}
Settings { Settings {
seperator, seperator,
@ -78,11 +67,44 @@ fn gen_settings(doc: &yaml::Yaml) -> Settings {
format: time_format, format: time_format,
icon: time_icon, icon: time_icon,
}, },
memory: Memory { icon: mem_icon }, memory: Memory {
disk: Disk { icon: disk_icon }, 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 { fn load_defaults() -> Settings {
Settings { Settings {
seperator: String::from("|"), seperator: String::from("|"),
@ -92,9 +114,15 @@ fn load_defaults() -> Settings {
}, },
memory: Memory { memory: Memory {
icon: String::from(""), icon: String::from(""),
enabled: false,
}, },
disk: Disk { disk: Disk {
icon: String::from(""), icon: String::from(""),
enabled: false,
},
volume: Volume {
icon: String::from(""),
enabled: false,
}, },
} }
} }

View File

@ -6,21 +6,36 @@ mod date;
mod disk; mod disk;
mod load_config; mod load_config;
mod mem; mod mem;
mod sound;
fn main() { fn main() {
let settings = load_config::load().unwrap(); let settings = load_config::load().unwrap();
sound::get_sound(&settings);
loop { loop {
let args = format!( let mut bar = String::from("");
"{}{}{}",
disk::disk_free(&settings), // the order of the IF's below matters to the final format
mem::mem(&settings).unwrap(),
date::fmt_date(&settings) 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") Command::new("xsetroot")
.arg("-name") .arg("-name")
.arg(args) .arg(bar)
.output() .output()
.unwrap(); .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)
}