aboutsummaryrefslogtreecommitdiff
path: root/src/app/impl_self.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/app/impl_self.rs')
-rw-r--r--src/app/impl_self.rs50
1 files changed, 29 insertions, 21 deletions
diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs
index 1114d50..d5f93ff 100644
--- a/src/app/impl_self.rs
+++ b/src/app/impl_self.rs
@@ -6,7 +6,7 @@ use std::path::PathBuf;
6use std::sync::mpsc::channel; 6use std::sync::mpsc::channel;
7use std::time::Duration; 7use std::time::Duration;
8 8
9use chrono::Local; 9use chrono::{Local, NaiveDate};
10use cursive::direction::Absolute; 10use cursive::direction::Absolute;
11use cursive::Vec2; 11use cursive::Vec2;
12use notify::{watcher, RecursiveMode, Watcher}; 12use notify::{watcher, RecursiveMode, Watcher};
@@ -15,7 +15,7 @@ use crate::command::{Command, CommandLineError};
15use crate::habit::{Bit, Count, HabitWrapper, TrackEvent, ViewMode}; 15use crate::habit::{Bit, Count, HabitWrapper, TrackEvent, ViewMode};
16use crate::utils::{self, GRID_WIDTH, VIEW_HEIGHT, VIEW_WIDTH}; 16use crate::utils::{self, GRID_WIDTH, VIEW_HEIGHT, VIEW_WIDTH};
17 17
18use crate::app::{App, MessageKind, StatusLine}; 18use crate::app::{App, Cursor, Message, MessageKind, StatusLine};
19 19
20impl App { 20impl App {
21 pub fn new() -> Self { 21 pub fn new() -> Self {
@@ -27,8 +27,8 @@ impl App {
27 focus: 0, 27 focus: 0,
28 _file_watcher: watcher, 28 _file_watcher: watcher,
29 file_event_recv: rx, 29 file_event_recv: rx,
30 view_month_offset: 0, 30 cursor: Cursor::new(),
31 message: "Type :add <habit-name> <goal> to get started, Ctrl-L to dismiss".into(), 31 message: Message::startup(),
32 }; 32 };
33 } 33 }
34 34
@@ -53,35 +53,42 @@ impl App {
53 if self.habits.is_empty() { 53 if self.habits.is_empty() {
54 return ViewMode::Day; 54 return ViewMode::Day;
55 } 55 }
56 return self.habits[self.focus].view_mode(); 56 return self.habits[self.focus].inner_data_ref().view_mode();
57 } 57 }
58 58
59 pub fn set_mode(&mut self, mode: ViewMode) { 59 pub fn set_mode(&mut self, mode: ViewMode) {
60 if !self.habits.is_empty() { 60 if !self.habits.is_empty() {
61 self.habits[self.focus].set_view_mode(mode); 61 self.habits[self.focus]
62 .inner_data_mut_ref()
63 .set_view_mode(mode);
62 } 64 }
63 } 65 }
64 66
65 pub fn set_view_month_offset(&mut self, offset: u32) { 67 pub fn sift_backward(&mut self) {
66 self.view_month_offset = offset; 68 self.cursor.month_backward();
67 for v in self.habits.iter_mut() { 69 for v in self.habits.iter_mut() {
68 v.set_view_month_offset(offset); 70 v.inner_data_mut_ref().cursor.month_backward();
69 } 71 }
70 } 72 }
71 73
72 pub fn sift_backward(&mut self) { 74 pub fn sift_forward(&mut self) {
73 self.view_month_offset += 1; 75 self.cursor.month_forward();
74 for v in self.habits.iter_mut() { 76 for v in self.habits.iter_mut() {
75 v.set_view_month_offset(self.view_month_offset); 77 v.inner_data_mut_ref().cursor.month_forward();
76 } 78 }
77 } 79 }
78 80
79 pub fn sift_forward(&mut self) { 81 pub fn reset_cursor(&mut self) {
80 if self.view_month_offset > 0 { 82 self.cursor.reset();
81 self.view_month_offset -= 1; 83 for v in self.habits.iter_mut() {
82 for v in self.habits.iter_mut() { 84 v.inner_data_mut_ref().cursor.reset();
83 v.set_view_month_offset(self.view_month_offset); 85 }
84 } 86 }
87
88 pub fn move_cursor(&mut self, d: Absolute) {
89 self.cursor.small_seek(d);
90 for v in self.habits.iter_mut() {
91 v.inner_data_mut_ref().move_cursor(d);
85 } 92 }
86 } 93 }
87 94
@@ -125,11 +132,12 @@ impl App {
125 let total = self.habits.iter().map(|h| h.goal()).sum::<u32>(); 132 let total = self.habits.iter().map(|h| h.goal()).sum::<u32>();
126 let completed = total - remaining; 133 let completed = total - remaining;
127 134
128 let timestamp = if self.view_month_offset == 0 { 135 let timestamp = if self.cursor.0 == today {
129 format!("{}", Local::now().naive_local().date().format("%d/%b/%y"),) 136 format!("{}", Local::now().naive_local().date().format("%d/%b/%y"),)
130 } else { 137 } else {
131 let months = self.view_month_offset; 138 let since = NaiveDate::signed_duration_since(today, self.cursor.0).num_days();
132 format!("{}", format!("{} months ago", months),) 139 let plural = if since == 1 { "" } else { "s" };
140 format!("{} ({} day{} ago)", self.cursor.0, since, plural)
133 }; 141 };
134 142
135 StatusLine { 143 StatusLine {