aboutsummaryrefslogtreecommitdiff
path: root/src/utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs130
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 @@
1use cursive::theme::{BaseColor, Color}; 1use cursive::theme::{BaseColor, Color};
2use directories::ProjectDirs; 2use directories::ProjectDirs;
3use std::fs; 3use serde::{Deserialize, Serialize};
4
5use std;
6use std::default::Default;
7use std::fs::{self, File, OpenOptions};
8use std::io::{Read, Write};
4use std::path::PathBuf; 9use std::path::PathBuf;
5 10
6pub struct AppConfig { 11pub const VIEW_WIDTH: usize = 25;
12pub const VIEW_HEIGHT: usize = 8;
13pub const GRID_WIDTH: usize = 3;
14
15#[derive(Serialize, Deserialize)]
16pub 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 25fn base_char() -> char {
12 pub view_width: usize, 26 '·'
13 pub view_height: usize, 27}
14 28
15 // app dimensions 29impl 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, 40pub 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
49fn cyan() -> String {
50 "cyan".into()
51}
52fn magenta() -> String {
53 "magenta".into()
54}
55fn light_black() -> String {
56 "light black".into()
57}
58
59impl 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)]
70pub struct AppConfig {
71 #[serde(default)]
72 pub look: Characters,
73
74 #[serde(default)]
75 pub colors: Colors,
76}
77
78impl Default for AppConfig {
79 fn default() -> Self {
80 AppConfig {
81 look: Default::default(),
82 colors: Default::default(),
83 }
84 }
85}
86
87impl 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
23pub fn load_configuration_file() -> AppConfig { 100pub 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
37fn project_dirs() -> ProjectDirs { 117fn 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
122pub 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
42pub fn habit_file() -> PathBuf { 130pub 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());