diff options
author | Akshay <[email protected]> | 2020-07-23 17:53:59 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-07-23 17:53:59 +0100 |
commit | 91cb3ebf5b3af666b13f4821ff25f219fdccd7b9 (patch) | |
tree | c91d6cffd4d215b325e81bedfdcfdfdcea76a537 /src/app/impl_self.rs | |
parent | 02bba52346d948e90fbc1b351486f0f1307c243d (diff) | |
parent | d1d1aeb3d5aaa75f262467c5e683e76ce7a844ab (diff) |
Merge branch 'feature/windows' of https://github.com/filalex77/dijo into feature/windows
Diffstat (limited to 'src/app/impl_self.rs')
-rw-r--r-- | src/app/impl_self.rs | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs index 744f906..a806dc5 100644 --- a/src/app/impl_self.rs +++ b/src/app/impl_self.rs | |||
@@ -37,6 +37,10 @@ impl App { | |||
37 | self.habits.push(h); | 37 | self.habits.push(h); |
38 | } | 38 | } |
39 | 39 | ||
40 | pub fn list_habits(&self) -> Vec<String> { | ||
41 | self.habits.iter().map(|x| x.name()).collect::<Vec<_>>() | ||
42 | } | ||
43 | |||
40 | pub fn delete_by_name(&mut self, name: &str) { | 44 | pub fn delete_by_name(&mut self, name: &str) { |
41 | let old_len = self.habits.len(); | 45 | let old_len = self.habits.len(); |
42 | self.habits.retain(|h| h.name() != name); | 46 | self.habits.retain(|h| h.name() != name); |
@@ -118,13 +122,13 @@ impl App { | |||
118 | } | 122 | } |
119 | 123 | ||
120 | pub fn status(&self) -> StatusLine { | 124 | pub fn status(&self) -> StatusLine { |
121 | let today = chrono::Local::now().naive_utc().date(); | 125 | let today = chrono::Local::now().naive_local().date(); |
122 | let remaining = self.habits.iter().map(|h| h.remaining(today)).sum::<u32>(); | 126 | let remaining = self.habits.iter().map(|h| h.remaining(today)).sum::<u32>(); |
123 | let total = self.habits.iter().map(|h| h.goal()).sum::<u32>(); | 127 | let total = self.habits.iter().map(|h| h.goal()).sum::<u32>(); |
124 | let completed = total - remaining; | 128 | let completed = total - remaining; |
125 | 129 | ||
126 | let timestamp = if self.view_month_offset == 0 { | 130 | let timestamp = if self.view_month_offset == 0 { |
127 | format!("{}", Local::now().date().format("%d/%b/%y"),) | 131 | format!("{}", Local::now().naive_local().date().format("%d/%b/%y"),) |
128 | } else { | 132 | } else { |
129 | let months = self.view_month_offset; | 133 | let months = self.view_month_offset; |
130 | format!("{}", format!("{} months ago", months),) | 134 | format!("{}", format!("{} months ago", months),) |
@@ -207,12 +211,18 @@ impl App { | |||
207 | .iter_mut() | 211 | .iter_mut() |
208 | .find(|x| x.name() == name && x.is_auto()); | 212 | .find(|x| x.name() == name && x.is_auto()); |
209 | if let Some(h) = target_habit { | 213 | if let Some(h) = target_habit { |
210 | h.modify(Local::now().naive_utc().date(), event); | 214 | h.modify(Local::now().naive_local().date(), event); |
211 | } | 215 | } |
212 | }; | 216 | }; |
213 | match result { | 217 | match result { |
214 | Ok(c) => match c { | 218 | Ok(c) => match c { |
215 | Command::Add(name, goal, auto) => { | 219 | Command::Add(name, goal, auto) => { |
220 | if let Some(_) = self.habits.iter().find(|x| x.name() == name) { | ||
221 | self.message.set_kind(MessageKind::Error); | ||
222 | self.message | ||
223 | .set_message(format!("Habit `{}` already exist", &name)); | ||
224 | return; | ||
225 | } | ||
216 | let kind = if goal == Some(1) { "bit" } else { "count" }; | 226 | let kind = if goal == Some(1) { "bit" } else { "count" }; |
217 | if kind == "count" { | 227 | if kind == "count" { |
218 | self.add_habit(Box::new(Count::new(name, goal.unwrap_or(0), auto))); | 228 | self.add_habit(Box::new(Count::new(name, goal.unwrap_or(0), auto))); |
@@ -230,7 +240,30 @@ impl App { | |||
230 | Command::TrackDown(name) => { | 240 | Command::TrackDown(name) => { |
231 | _track(&name, TrackEvent::Decrement); | 241 | _track(&name, TrackEvent::Decrement); |
232 | } | 242 | } |
233 | Command::Quit => self.save_state(), | 243 | Command::Help(input) => { |
244 | if let Some(topic) = input.as_ref().map(String::as_ref) { | ||
245 | self.message.set_message( | ||
246 | match topic { | ||
247 | "a" | "add" => "add <habit-name> [goal] (alias: a)", | ||
248 | "aa" | "add-auto" => "add-auto <habit-name> [goal] (alias: aa)", | ||
249 | "d" | "delete" => "delete <habit-name> (alias: d)", | ||
250 | "mprev" | "month-prev" => "month-prev (alias: mprev)", | ||
251 | "mnext" | "month-next" => "month-next (alias: mnext)", | ||
252 | "tup" | "track-up" => "track-up <auto-habit-name> (alias: tup)", | ||
253 | "tdown" | "track-down" => "track-down <auto-habit-name> (alias: tdown)", | ||
254 | "q" | "quit" => "quit", | ||
255 | "h"|"?" | "help" => "help [<command>|commands|keys] (aliases: h, ?)", | ||
256 | "cmds" | "commands" => "add, add-auto, delete, month-{prev,next}, track-{up,down}, help, quit", | ||
257 | "keys" => "TODO", // TODO (view?) | ||
258 | _ => "unknown command or help topic.", | ||
259 | } | ||
260 | ) | ||
261 | } else { | ||
262 | // TODO (view?) | ||
263 | self.message.set_message("help <command>|commands|keys") | ||
264 | } | ||
265 | } | ||
266 | Command::Quit | Command::Write => self.save_state(), | ||
234 | Command::MonthNext => self.sift_forward(), | 267 | Command::MonthNext => self.sift_forward(), |
235 | Command::MonthPrev => self.sift_backward(), | 268 | Command::MonthPrev => self.sift_backward(), |
236 | Command::Blank => {} | 269 | Command::Blank => {} |