From 7f65b6960b300727bc16dedc39d5793d4ea83a0a Mon Sep 17 00:00:00 2001 From: Jordan Jennings Date: Fri, 24 Jul 2020 21:59:53 -0700 Subject: Add support for configuration file --- src/utils.rs | 43 +++++++++++++++++++++++++++++++++++++------ src/views.rs | 6 +++--- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index e6ec6ac..b45bbf3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,8 +1,14 @@ use cursive::theme::{BaseColor, Color}; use directories::ProjectDirs; +use serde::Deserialize; +use std; use std::fs; +use std::fs::File; +use std::io::Read; use std::path::PathBuf; +#[derive(Deserialize)] +#[serde(default = "default_config")] pub struct AppConfig { pub true_chr: char, pub false_chr: char, @@ -14,13 +20,33 @@ pub struct AppConfig { // app dimensions pub grid_width: usize, +} - pub reached_color: Color, - pub todo_color: Color, - pub future_color: Color, +impl AppConfig { + // TODO: implement string parsing from config.json + pub fn reached_color(&self) -> Color { + return Color::Dark(BaseColor::Cyan); + } + pub fn todo_color(&self) -> Color { + return Color::Dark(BaseColor::Magenta); + } + pub fn future_color(&self) -> Color { + return Color::Dark(BaseColor::Magenta); + } } pub fn load_configuration_file() -> AppConfig { + let config_f = config_file(); + if let Ok(ref mut f) = File::open(config_f) { + let mut j = String::new(); + f.read_to_string(&mut j); + return serde_json::from_str(&j).unwrap(); + } else { + return default_config(); + } +} + +pub fn default_config() -> AppConfig { return AppConfig { true_chr: '·', false_chr: '·', @@ -28,9 +54,6 @@ pub fn load_configuration_file() -> AppConfig { view_width: 25, view_height: 8, grid_width: 3, - reached_color: Color::Dark(BaseColor::Cyan), - todo_color: Color::Dark(BaseColor::Magenta), - future_color: Color::Light(BaseColor::Black), }; } @@ -39,6 +62,14 @@ fn project_dirs() -> ProjectDirs { .unwrap_or_else(|| panic!("Invalid home directory!")) } +pub fn config_file() -> PathBuf { + let proj_dirs = project_dirs(); + let mut data_file = PathBuf::from(proj_dirs.data_dir()); + fs::create_dir_all(&data_file); + data_file.push("config.json"); + return data_file; +} + pub fn habit_file() -> PathBuf { let proj_dirs = project_dirs(); let mut data_file = PathBuf::from(proj_dirs.data_dir()); diff --git a/src/views.rs b/src/views.rs index da077ac..7adf8c6 100644 --- a/src/views.rs +++ b/src/views.rs @@ -36,9 +36,9 @@ where let year = now.year(); let month = now.month(); - let goal_reached_style = Style::from(CONFIGURATION.reached_color); - let todo_style = Style::from(CONFIGURATION.todo_color); - let future_style = Style::from(CONFIGURATION.future_color); + let goal_reached_style = Style::from(CONFIGURATION.reached_color()); + let todo_style = Style::from(CONFIGURATION.todo_color()); + let future_style = Style::from(CONFIGURATION.future_color()); let strikethrough = Style::from(Effect::Strikethrough); -- cgit v1.2.3