aboutsummaryrefslogtreecommitdiff
path: root/src/app
diff options
context:
space:
mode:
Diffstat (limited to 'src/app')
-rw-r--r--src/app/impl_self.rs41
-rw-r--r--src/app/impl_view.rs19
2 files changed, 37 insertions, 23 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 => {}
diff --git a/src/app/impl_view.rs b/src/app/impl_view.rs
index 892b00c..0dfd20b 100644
--- a/src/app/impl_view.rs
+++ b/src/app/impl_view.rs
@@ -102,25 +102,6 @@ impl View for App {
102 self.set_focus(Absolute::Down); 102 self.set_focus(Absolute::Down);
103 return EventResult::Consumed(None); 103 return EventResult::Consumed(None);
104 } 104 }
105 Event::Char('d') => {
106 if self.habits.is_empty() {
107 return EventResult::Consumed(None);
108 }
109 self.habits.remove(self.focus);
110 self.focus = self.focus.checked_sub(1).unwrap_or(0);
111 return EventResult::Consumed(None);
112 }
113 Event::Char('w') => {
114 // helper bind to test write to file
115 let j = serde_json::to_string_pretty(&self.habits).unwrap();
116 let mut file = File::create("foo.txt").unwrap();
117 file.write_all(j.as_bytes()).unwrap();
118 return EventResult::Consumed(None);
119 }
120 Event::Char('q') => {
121 self.save_state();
122 return EventResult::with_cb(|s| s.quit());
123 }
124 Event::Char('v') => { 105 Event::Char('v') => {
125 if self.habits.is_empty() { 106 if self.habits.is_empty() {
126 return EventResult::Consumed(None); 107 return EventResult::Consumed(None);