aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-08-02 13:00:51 +0100
committerAkshay <[email protected]>2020-08-02 13:00:51 +0100
commit3cc2fafe84ec2c8f40a9348f9fe9a5408606db6e (patch)
tree7b5e05c85ce5c34766cc0e28bde7b931c698c6f7 /src
parent83cba9dae6e4cc6a348ef0b9ee7e2c7f747409f5 (diff)
parent7f65b6960b300727bc16dedc39d5793d4ea83a0a (diff)
Merge branch 'load-config-json-app-config' of https://github.com/jjn2009/dijo into feature/config-file
Diffstat (limited to 'src')
-rw-r--r--src/utils.rs43
-rw-r--r--src/views.rs6
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 @@
1use cursive::theme::{BaseColor, Color}; 1use cursive::theme::{BaseColor, Color};
2use directories::ProjectDirs; 2use directories::ProjectDirs;
3use serde::Deserialize;
4use std;
3use std::fs; 5use std::fs;
6use std::fs::File;
7use std::io::Read;
4use std::path::PathBuf; 8use std::path::PathBuf;
5 9
10#[derive(Deserialize)]
11#[serde(default = "default_config")]
6pub struct AppConfig { 12pub 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, 25impl 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
23pub fn load_configuration_file() -> AppConfig { 38pub 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
49pub 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
65pub 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
42pub fn habit_file() -> PathBuf { 73pub 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