diff options
author | Jordan Jennings <[email protected]> | 2020-07-25 05:59:53 +0100 |
---|---|---|
committer | Jordan Jennings <[email protected]> | 2020-07-25 05:59:53 +0100 |
commit | 7f65b6960b300727bc16dedc39d5793d4ea83a0a (patch) | |
tree | 3a513fdf6bc20127080c8868f2cc940f4cd0ec98 /src | |
parent | 2dafbfe51b2b4ebafc21fe68a9b909ebe3a4e23b (diff) |
Add support for configuration file
Diffstat (limited to 'src')
-rw-r--r-- | src/utils.rs | 43 | ||||
-rw-r--r-- | 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 @@ | |||
1 | use cursive::theme::{BaseColor, Color}; | 1 | use cursive::theme::{BaseColor, Color}; |
2 | use directories::ProjectDirs; | 2 | use directories::ProjectDirs; |
3 | use serde::Deserialize; | ||
4 | use std; | ||
3 | use std::fs; | 5 | use std::fs; |
6 | use std::fs::File; | ||
7 | use std::io::Read; | ||
4 | use std::path::PathBuf; | 8 | use std::path::PathBuf; |
5 | 9 | ||
10 | #[derive(Deserialize)] | ||
11 | #[serde(default = "default_config")] | ||
6 | pub struct AppConfig { | 12 | pub struct AppConfig { |
7 | pub true_chr: char, | 13 | pub true_chr: char, |
8 | pub false_chr: char, | 14 | pub false_chr: char, |
@@ -14,13 +20,33 @@ pub struct AppConfig { | |||
14 | 20 | ||
15 | // app dimensions | 21 | // app dimensions |
16 | pub grid_width: usize, | 22 | pub grid_width: usize, |
23 | } | ||
17 | 24 | ||
18 | pub reached_color: Color, | 25 | impl AppConfig { |
19 | pub todo_color: Color, | 26 | // TODO: implement string parsing from config.json |
20 | pub future_color: Color, | 27 | pub fn reached_color(&self) -> Color { |
28 | return Color::Dark(BaseColor::Cyan); | ||
29 | } | ||
30 | pub fn todo_color(&self) -> Color { | ||
31 | return Color::Dark(BaseColor::Magenta); | ||
32 | } | ||
33 | pub fn future_color(&self) -> Color { | ||
34 | return Color::Dark(BaseColor::Magenta); | ||
35 | } | ||
21 | } | 36 | } |
22 | 37 | ||
23 | pub fn load_configuration_file() -> AppConfig { | 38 | pub fn load_configuration_file() -> AppConfig { |
39 | let config_f = config_file(); | ||
40 | if let Ok(ref mut f) = File::open(config_f) { | ||
41 | let mut j = String::new(); | ||
42 | f.read_to_string(&mut j); | ||
43 | return serde_json::from_str(&j).unwrap(); | ||
44 | } else { | ||
45 | return default_config(); | ||
46 | } | ||
47 | } | ||
48 | |||
49 | pub fn default_config() -> AppConfig { | ||
24 | return AppConfig { | 50 | return AppConfig { |
25 | true_chr: '·', | 51 | true_chr: '·', |
26 | false_chr: '·', | 52 | false_chr: '·', |
@@ -28,9 +54,6 @@ pub fn load_configuration_file() -> AppConfig { | |||
28 | view_width: 25, | 54 | view_width: 25, |
29 | view_height: 8, | 55 | view_height: 8, |
30 | grid_width: 3, | 56 | grid_width: 3, |
31 | reached_color: Color::Dark(BaseColor::Cyan), | ||
32 | todo_color: Color::Dark(BaseColor::Magenta), | ||
33 | future_color: Color::Light(BaseColor::Black), | ||
34 | }; | 57 | }; |
35 | } | 58 | } |
36 | 59 | ||
@@ -39,6 +62,14 @@ fn project_dirs() -> ProjectDirs { | |||
39 | .unwrap_or_else(|| panic!("Invalid home directory!")) | 62 | .unwrap_or_else(|| panic!("Invalid home directory!")) |
40 | } | 63 | } |
41 | 64 | ||
65 | pub fn config_file() -> PathBuf { | ||
66 | let proj_dirs = project_dirs(); | ||
67 | let mut data_file = PathBuf::from(proj_dirs.data_dir()); | ||
68 | fs::create_dir_all(&data_file); | ||
69 | data_file.push("config.json"); | ||
70 | return data_file; | ||
71 | } | ||
72 | |||
42 | pub fn habit_file() -> PathBuf { | 73 | pub fn habit_file() -> PathBuf { |
43 | let proj_dirs = project_dirs(); | 74 | let proj_dirs = project_dirs(); |
44 | let mut data_file = PathBuf::from(proj_dirs.data_dir()); | 75 | 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 | |||
36 | let year = now.year(); | 36 | let year = now.year(); |
37 | let month = now.month(); | 37 | let month = now.month(); |
38 | 38 | ||
39 | let goal_reached_style = Style::from(CONFIGURATION.reached_color); | 39 | let goal_reached_style = Style::from(CONFIGURATION.reached_color()); |
40 | let todo_style = Style::from(CONFIGURATION.todo_color); | 40 | let todo_style = Style::from(CONFIGURATION.todo_color()); |
41 | let future_style = Style::from(CONFIGURATION.future_color); | 41 | let future_style = Style::from(CONFIGURATION.future_color()); |
42 | 42 | ||
43 | let strikethrough = Style::from(Effect::Strikethrough); | 43 | let strikethrough = Style::from(Effect::Strikethrough); |
44 | 44 | ||