diff options
Diffstat (limited to 'src/app/impl_self.rs')
-rw-r--r-- | src/app/impl_self.rs | 60 |
1 files changed, 45 insertions, 15 deletions
diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs index 744f906..5cd9616 100644 --- a/src/app/impl_self.rs +++ b/src/app/impl_self.rs | |||
@@ -13,8 +13,7 @@ use notify::{watcher, RecursiveMode, Watcher}; | |||
13 | 13 | ||
14 | use crate::command::{Command, CommandLineError}; | 14 | use crate::command::{Command, CommandLineError}; |
15 | use crate::habit::{Bit, Count, HabitWrapper, TrackEvent, ViewMode}; | 15 | use crate::habit::{Bit, Count, HabitWrapper, TrackEvent, ViewMode}; |
16 | use crate::utils; | 16 | use crate::utils::{self, GRID_WIDTH, VIEW_HEIGHT, VIEW_WIDTH}; |
17 | use crate::CONFIGURATION; | ||
18 | 17 | ||
19 | use crate::app::{App, MessageKind, StatusLine}; | 18 | use crate::app::{App, MessageKind, StatusLine}; |
20 | 19 | ||
@@ -37,6 +36,10 @@ impl App { | |||
37 | self.habits.push(h); | 36 | self.habits.push(h); |
38 | } | 37 | } |
39 | 38 | ||
39 | pub fn list_habits(&self) -> Vec<String> { | ||
40 | self.habits.iter().map(|x| x.name()).collect::<Vec<_>>() | ||
41 | } | ||
42 | |||
40 | pub fn delete_by_name(&mut self, name: &str) { | 43 | pub fn delete_by_name(&mut self, name: &str) { |
41 | let old_len = self.habits.len(); | 44 | let old_len = self.habits.len(); |
42 | self.habits.retain(|h| h.name() != name); | 45 | self.habits.retain(|h| h.name() != name); |
@@ -83,7 +86,6 @@ impl App { | |||
83 | } | 86 | } |
84 | 87 | ||
85 | pub fn set_focus(&mut self, d: Absolute) { | 88 | pub fn set_focus(&mut self, d: Absolute) { |
86 | let grid_width = CONFIGURATION.grid_width; | ||
87 | match d { | 89 | match d { |
88 | Absolute::Right => { | 90 | Absolute::Right => { |
89 | if self.focus != self.habits.len() - 1 { | 91 | if self.focus != self.habits.len() - 1 { |
@@ -96,15 +98,15 @@ impl App { | |||
96 | } | 98 | } |
97 | } | 99 | } |
98 | Absolute::Down => { | 100 | Absolute::Down => { |
99 | if self.focus + grid_width < self.habits.len() - 1 { | 101 | if self.focus + GRID_WIDTH < self.habits.len() - 1 { |
100 | self.focus += grid_width; | 102 | self.focus += GRID_WIDTH; |
101 | } else { | 103 | } else { |
102 | self.focus = self.habits.len() - 1; | 104 | self.focus = self.habits.len() - 1; |
103 | } | 105 | } |
104 | } | 106 | } |
105 | Absolute::Up => { | 107 | Absolute::Up => { |
106 | if self.focus as isize - grid_width as isize >= 0 { | 108 | if self.focus as isize - GRID_WIDTH as isize >= 0 { |
107 | self.focus -= grid_width; | 109 | self.focus -= GRID_WIDTH; |
108 | } else { | 110 | } else { |
109 | self.focus = 0; | 111 | self.focus = 0; |
110 | } | 112 | } |
@@ -118,13 +120,13 @@ impl App { | |||
118 | } | 120 | } |
119 | 121 | ||
120 | pub fn status(&self) -> StatusLine { | 122 | pub fn status(&self) -> StatusLine { |
121 | let today = chrono::Local::now().naive_utc().date(); | 123 | let today = chrono::Local::now().naive_local().date(); |
122 | let remaining = self.habits.iter().map(|h| h.remaining(today)).sum::<u32>(); | 124 | let remaining = self.habits.iter().map(|h| h.remaining(today)).sum::<u32>(); |
123 | let total = self.habits.iter().map(|h| h.goal()).sum::<u32>(); | 125 | let total = self.habits.iter().map(|h| h.goal()).sum::<u32>(); |
124 | let completed = total - remaining; | 126 | let completed = total - remaining; |
125 | 127 | ||
126 | let timestamp = if self.view_month_offset == 0 { | 128 | let timestamp = if self.view_month_offset == 0 { |
127 | format!("{}", Local::now().date().format("%d/%b/%y"),) | 129 | format!("{}", Local::now().naive_local().date().format("%d/%b/%y"),) |
128 | } else { | 130 | } else { |
129 | let months = self.view_month_offset; | 131 | let months = self.view_month_offset; |
130 | format!("{}", format!("{} months ago", months),) | 132 | format!("{}", format!("{} months ago", months),) |
@@ -142,12 +144,10 @@ impl App { | |||
142 | } | 144 | } |
143 | 145 | ||
144 | pub fn max_size(&self) -> Vec2 { | 146 | pub fn max_size(&self) -> Vec2 { |
145 | let grid_width = CONFIGURATION.grid_width; | 147 | let width = GRID_WIDTH * VIEW_WIDTH; |
146 | let width = grid_width * CONFIGURATION.view_width; | ||
147 | let height = { | 148 | let height = { |
148 | if !self.habits.is_empty() { | 149 | if !self.habits.is_empty() { |
149 | (CONFIGURATION.view_height as f64 | 150 | (VIEW_HEIGHT as f64 * (self.habits.len() as f64 / GRID_WIDTH as f64).ceil()) |
150 | * (self.habits.len() as f64 / grid_width as f64).ceil()) | ||
151 | as usize | 151 | as usize |
152 | } else { | 152 | } else { |
153 | 0 | 153 | 0 |
@@ -207,12 +207,18 @@ impl App { | |||
207 | .iter_mut() | 207 | .iter_mut() |
208 | .find(|x| x.name() == name && x.is_auto()); | 208 | .find(|x| x.name() == name && x.is_auto()); |
209 | if let Some(h) = target_habit { | 209 | if let Some(h) = target_habit { |
210 | h.modify(Local::now().naive_utc().date(), event); | 210 | h.modify(Local::now().naive_local().date(), event); |
211 | } | 211 | } |
212 | }; | 212 | }; |
213 | match result { | 213 | match result { |
214 | Ok(c) => match c { | 214 | Ok(c) => match c { |
215 | Command::Add(name, goal, auto) => { | 215 | Command::Add(name, goal, auto) => { |
216 | if let Some(_) = self.habits.iter().find(|x| x.name() == name) { | ||
217 | self.message.set_kind(MessageKind::Error); | ||
218 | self.message | ||
219 | .set_message(format!("Habit `{}` already exist", &name)); | ||
220 | return; | ||
221 | } | ||
216 | let kind = if goal == Some(1) { "bit" } else { "count" }; | 222 | let kind = if goal == Some(1) { "bit" } else { "count" }; |
217 | if kind == "count" { | 223 | if kind == "count" { |
218 | self.add_habit(Box::new(Count::new(name, goal.unwrap_or(0), auto))); | 224 | self.add_habit(Box::new(Count::new(name, goal.unwrap_or(0), auto))); |
@@ -230,7 +236,31 @@ impl App { | |||
230 | Command::TrackDown(name) => { | 236 | Command::TrackDown(name) => { |
231 | _track(&name, TrackEvent::Decrement); | 237 | _track(&name, TrackEvent::Decrement); |
232 | } | 238 | } |
233 | Command::Quit => self.save_state(), | 239 | Command::Help(input) => { |
240 | if let Some(topic) = input.as_ref().map(String::as_ref) { | ||
241 | self.message.set_message( | ||
242 | match topic { | ||
243 | "a" | "add" => "add <habit-name> [goal] (alias: a)", | ||
244 | "aa" | "add-auto" => "add-auto <habit-name> [goal] (alias: aa)", | ||
245 | "d" | "delete" => "delete <habit-name> (alias: d)", | ||
246 | "mprev" | "month-prev" => "month-prev (alias: mprev)", | ||
247 | "mnext" | "month-next" => "month-next (alias: mnext)", | ||
248 | "tup" | "track-up" => "track-up <auto-habit-name> (alias: tup)", | ||
249 | "tdown" | "track-down" => "track-down <auto-habit-name> (alias: tdown)", | ||
250 | "q" | "quit" => "quit dijo", | ||
251 | "w" | "write" => "write current state to disk (alias: w)", | ||
252 | "h"|"?" | "help" => "help [<command>|commands|keys] (aliases: h, ?)", | ||
253 | "cmds" | "commands" => "add, add-auto, delete, month-{prev,next}, track-{up,down}, help, quit", | ||
254 | "keys" => "TODO", // TODO (view?) | ||
255 | _ => "unknown command or help topic.", | ||
256 | } | ||
257 | ) | ||
258 | } else { | ||
259 | // TODO (view?) | ||
260 | self.message.set_message("help <command>|commands|keys") | ||
261 | } | ||
262 | } | ||
263 | Command::Quit | Command::Write => self.save_state(), | ||
234 | Command::MonthNext => self.sift_forward(), | 264 | Command::MonthNext => self.sift_forward(), |
235 | Command::MonthPrev => self.sift_backward(), | 265 | Command::MonthPrev => self.sift_backward(), |
236 | Command::Blank => {} | 266 | Command::Blank => {} |