diff options
author | rabite <rabite@posteo.de> | 2019-04-03 15:35:29 +0200 |
---|---|---|
committer | rabite <rabite@posteo.de> | 2019-04-03 15:35:29 +0200 |
commit | 10d9a5462cd3c64ce8a1e3e6f724fb1528cc99ce (patch) | |
tree | f6acef7291ffcdacc88b2086bb590964041d18eb /src/config.rs | |
parent | 8bfc707a596c45ce134a427d8c800620042ba78c (diff) |
configurable hidden files/animation
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..4801286 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,58 @@ +use crate::paths; +use crate::fail::{HError, HResult, ErrorLog}; + +#[derive(Debug, Clone)] +pub struct Config { + pub animation: bool, + pub show_hidden: bool, +} + + +impl Config { + pub fn new() -> Config { + Config { + animation: true, + show_hidden: false + } + } + + pub fn load() -> HResult<Config> { + let config_path = paths::config_path()?; + + if !config_path.exists() { + return Ok(Config::new()); + } + + let config_string = std::fs::read_to_string(config_path)?; + + let config = config_string.lines().fold(Config::new(), |mut config, line| { + match Config::prep_line(line) { + Ok(("animation", "on")) => { config.animation = true; }, + Ok(("animation", "off")) => { config.animation = false; }, + Ok(("show_hidden", "on")) => { config.show_hidden = true; }, + Ok(("show_hidden", "off")) => { config.show_hidden = false; }, + _ => { HError::config_error::<Config>(line.to_string()).log(); } + } + config + }); + Ok(config) + } + + fn prep_line<'a>(line: &'a str) -> HResult<(&'a str, &'a str)> { + let setting = line.split("=").collect::<Vec<&str>>(); + if setting.len() == 2 { + Ok((setting[0], setting[1])) + } else { + HError::config_error(line.to_string()) + } + + } + + pub fn animate(&self) -> bool { + self.animation + } + + pub fn show_hidden(&self) -> bool { + self.show_hidden + } +} |