diff options
author | Akshay <[email protected]> | 2020-08-02 13:00:51 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-08-02 13:00:51 +0100 |
commit | 3cc2fafe84ec2c8f40a9348f9fe9a5408606db6e (patch) | |
tree | 7b5e05c85ce5c34766cc0e28bde7b931c698c6f7 /src/utils.rs | |
parent | 83cba9dae6e4cc6a348ef0b9ee7e2c7f747409f5 (diff) | |
parent | 7f65b6960b300727bc16dedc39d5793d4ea83a0a (diff) |
Merge branch 'load-config-json-app-config' of https://github.com/jjn2009/dijo into feature/config-file
Diffstat (limited to 'src/utils.rs')
-rw-r--r-- | src/utils.rs | 43 |
1 files changed, 37 insertions, 6 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()); |