diff options
Diffstat (limited to 'src/utils.rs')
-rw-r--r-- | src/utils.rs | 130 |
1 files changed, 109 insertions, 21 deletions
diff --git a/src/utils.rs b/src/utils.rs index e6ec6ac..2453aa6 100644 --- a/src/utils.rs +++ b/src/utils.rs | |||
@@ -1,37 +1,117 @@ | |||
1 | use cursive::theme::{BaseColor, Color}; | 1 | use cursive::theme::{BaseColor, Color}; |
2 | use directories::ProjectDirs; | 2 | use directories::ProjectDirs; |
3 | use std::fs; | 3 | use serde::{Deserialize, Serialize}; |
4 | |||
5 | use std; | ||
6 | use std::default::Default; | ||
7 | use std::fs::{self, File, OpenOptions}; | ||
8 | use std::io::{Read, Write}; | ||
4 | use std::path::PathBuf; | 9 | use std::path::PathBuf; |
5 | 10 | ||
6 | pub struct AppConfig { | 11 | pub const VIEW_WIDTH: usize = 25; |
12 | pub const VIEW_HEIGHT: usize = 8; | ||
13 | pub const GRID_WIDTH: usize = 3; | ||
14 | |||
15 | #[derive(Serialize, Deserialize)] | ||
16 | pub struct Characters { | ||
17 | #[serde(default = "base_char")] | ||
7 | pub true_chr: char, | 18 | pub true_chr: char, |
19 | #[serde(default = "base_char")] | ||
8 | pub false_chr: char, | 20 | pub false_chr: char, |
21 | #[serde(default = "base_char")] | ||
9 | pub future_chr: char, | 22 | pub future_chr: char, |
23 | } | ||
10 | 24 | ||
11 | // view dimensions | 25 | fn base_char() -> char { |
12 | pub view_width: usize, | 26 | '·' |
13 | pub view_height: usize, | 27 | } |
14 | 28 | ||
15 | // app dimensions | 29 | impl Default for Characters { |
16 | pub grid_width: usize, | 30 | fn default() -> Self { |
31 | Characters { | ||
32 | true_chr: '·', | ||
33 | false_chr: '·', | ||
34 | future_chr: '·', | ||
35 | } | ||
36 | } | ||
37 | } | ||
17 | 38 | ||
18 | pub reached_color: Color, | 39 | #[derive(Serialize, Deserialize)] |
19 | pub todo_color: Color, | 40 | pub struct Colors { |
20 | pub future_color: Color, | 41 | #[serde(default = "cyan")] |
42 | pub reached: String, | ||
43 | #[serde(default = "magenta")] | ||
44 | pub todo: String, | ||
45 | #[serde(default = "light_black")] | ||
46 | pub inactive: String, | ||
47 | } | ||
48 | |||
49 | fn cyan() -> String { | ||
50 | "cyan".into() | ||
51 | } | ||
52 | fn magenta() -> String { | ||
53 | "magenta".into() | ||
54 | } | ||
55 | fn light_black() -> String { | ||
56 | "light black".into() | ||
57 | } | ||
58 | |||
59 | impl Default for Colors { | ||
60 | fn default() -> Self { | ||
61 | Colors { | ||
62 | reached: cyan(), | ||
63 | todo: magenta(), | ||
64 | inactive: light_black(), | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | |||
69 | #[derive(Serialize, Deserialize)] | ||
70 | pub struct AppConfig { | ||
71 | #[serde(default)] | ||
72 | pub look: Characters, | ||
73 | |||
74 | #[serde(default)] | ||
75 | pub colors: Colors, | ||
76 | } | ||
77 | |||
78 | impl Default for AppConfig { | ||
79 | fn default() -> Self { | ||
80 | AppConfig { | ||
81 | look: Default::default(), | ||
82 | colors: Default::default(), | ||
83 | } | ||
84 | } | ||
85 | } | ||
86 | |||
87 | impl AppConfig { | ||
88 | // TODO: implement string parsing from config.json | ||
89 | pub fn reached_color(&self) -> Color { | ||
90 | return Color::parse(&self.colors.reached).unwrap_or(Color::Dark(BaseColor::Cyan)); | ||
91 | } | ||
92 | pub fn todo_color(&self) -> Color { | ||
93 | return Color::parse(&self.colors.todo).unwrap_or(Color::Dark(BaseColor::Magenta)); | ||
94 | } | ||
95 | pub fn inactive_color(&self) -> Color { | ||
96 | return Color::parse(&self.colors.inactive).unwrap_or(Color::Light(BaseColor::Black)); | ||
97 | } | ||
21 | } | 98 | } |
22 | 99 | ||
23 | pub fn load_configuration_file() -> AppConfig { | 100 | pub fn load_configuration_file() -> AppConfig { |
24 | return AppConfig { | 101 | let cf = config_file(); |
25 | true_chr: '·', | 102 | if let Ok(ref mut f) = File::open(&cf) { |
26 | false_chr: '·', | 103 | let mut j = String::new(); |
27 | future_chr: '·', | 104 | f.read_to_string(&mut j); |
28 | view_width: 25, | 105 | return toml::from_str(&j).unwrap(); |
29 | view_height: 8, | 106 | } else { |
30 | grid_width: 3, | 107 | if let Ok(dc) = toml::to_string(&AppConfig::default()) { |
31 | reached_color: Color::Dark(BaseColor::Cyan), | 108 | match OpenOptions::new().create(true).write(true).open(&cf) { |
32 | todo_color: Color::Dark(BaseColor::Magenta), | 109 | Ok(ref mut file) => file.write(dc.as_bytes()).unwrap(), |
33 | future_color: Color::Light(BaseColor::Black), | 110 | Err(_) => 0, |
34 | }; | 111 | }; |
112 | } | ||
113 | return Default::default(); | ||
114 | } | ||
35 | } | 115 | } |
36 | 116 | ||
37 | fn project_dirs() -> ProjectDirs { | 117 | fn project_dirs() -> ProjectDirs { |
@@ -39,6 +119,14 @@ fn project_dirs() -> ProjectDirs { | |||
39 | .unwrap_or_else(|| panic!("Invalid home directory!")) | 119 | .unwrap_or_else(|| panic!("Invalid home directory!")) |
40 | } | 120 | } |
41 | 121 | ||
122 | pub fn config_file() -> PathBuf { | ||
123 | let proj_dirs = project_dirs(); | ||
124 | let mut data_file = PathBuf::from(proj_dirs.config_dir()); | ||
125 | fs::create_dir_all(&data_file); | ||
126 | data_file.push("config.toml"); | ||
127 | return data_file; | ||
128 | } | ||
129 | |||
42 | pub fn habit_file() -> PathBuf { | 130 | pub fn habit_file() -> PathBuf { |
43 | let proj_dirs = project_dirs(); | 131 | let proj_dirs = project_dirs(); |
44 | let mut data_file = PathBuf::from(proj_dirs.data_dir()); | 132 | let mut data_file = PathBuf::from(proj_dirs.data_dir()); |