diff options
Diffstat (limited to 'src/load_config.rs')
-rw-r--r-- | src/load_config.rs | 96 |
1 files changed, 62 insertions, 34 deletions
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, }, } } |