aboutsummaryrefslogtreecommitdiff
path: root/src/app.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/app.rs')
-rw-r--r--src/app.rs36
1 files changed, 33 insertions, 3 deletions
diff --git a/src/app.rs b/src/app.rs
index 4ea342f..7495dd8 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -58,12 +58,23 @@ impl App {
58 self.habits.push(h); 58 self.habits.push(h);
59 } 59 }
60 60
61 pub fn delete_by_name(&mut self, name: &str) {
62 self.habits.retain(|h| h.get_name() != name);
63 }
64
61 pub fn set_mode(&mut self, set_mode: ViewMode) { 65 pub fn set_mode(&mut self, set_mode: ViewMode) {
62 if set_mode != self.view_mode { 66 if set_mode != self.view_mode {
63 self.view_mode = set_mode; 67 self.view_mode = set_mode;
64 } 68 }
65 } 69 }
66 70
71 pub fn set_view_month_offset(&mut self, offset: u32) {
72 self.view_month_offset = offset;
73 for v in self.habits.iter_mut() {
74 v.set_view_month_offset(offset);
75 }
76 }
77
67 pub fn sift_backward(&mut self) { 78 pub fn sift_backward(&mut self) {
68 self.view_month_offset += 1; 79 self.view_month_offset += 1;
69 for v in self.habits.iter_mut() { 80 for v in self.habits.iter_mut() {
@@ -175,6 +186,10 @@ impl App {
175 self.add_habit(Box::new(Bit::new(name))); 186 self.add_habit(Box::new(Bit::new(name)));
176 } 187 }
177 } 188 }
189 Command::Delete(name) => {
190 self.delete_by_name(&name);
191 self.focus = 0;
192 }
178 Command::MonthNext => self.sift_forward(), 193 Command::MonthNext => self.sift_forward(),
179 Command::MonthPrev => self.sift_backward(), 194 Command::MonthPrev => self.sift_backward(),
180 _ => { 195 _ => {
@@ -255,8 +270,11 @@ impl View for App {
255 return EventResult::Consumed(None); 270 return EventResult::Consumed(None);
256 } 271 }
257 Event::Char('d') => { 272 Event::Char('d') => {
273 if self.habits.is_empty() {
274 return EventResult::Consumed(None);
275 }
258 self.habits.remove(self.focus); 276 self.habits.remove(self.focus);
259 self.focus = 0; 277 self.focus = self.focus.checked_sub(1).unwrap_or(0);
260 return EventResult::Consumed(None); 278 return EventResult::Consumed(None);
261 } 279 }
262 Event::Char('w') => { 280 Event::Char('w') => {
@@ -266,9 +284,13 @@ impl View for App {
266 return EventResult::Consumed(None); 284 return EventResult::Consumed(None);
267 } 285 }
268 Event::Char('q') => { 286 Event::Char('q') => {
269 self.save_state(); 287 // self.save_state();
270 return EventResult::with_cb(|s| s.quit()); 288 return EventResult::with_cb(|s| s.quit());
271 } 289 }
290
291 /* We want sifting to be an app level function,
292 * that later trickles down into each habit
293 * */
272 Event::CtrlChar('f') => { 294 Event::CtrlChar('f') => {
273 self.sift_forward(); 295 self.sift_forward();
274 return EventResult::Consumed(None); 296 return EventResult::Consumed(None);
@@ -277,7 +299,15 @@ impl View for App {
277 self.sift_backward(); 299 self.sift_backward();
278 return EventResult::Consumed(None); 300 return EventResult::Consumed(None);
279 } 301 }
280 _ => self.habits[self.focus].on_event(e), 302
303 /* Every keybind that is not caught by App trickle
304 * s down to the focused Habit We sift back to today
305 * before performing any action, "refocusing" the cursor
306 * */
307 _ => {
308 self.set_view_month_offset(0);
309 self.habits[self.focus].on_event(e)
310 }
281 } 311 }
282 } 312 }
283} 313}