aboutsummaryrefslogtreecommitdiff
path: root/src/app
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-02-06 13:30:40 +0000
committerAkshay <[email protected]>2021-02-06 13:30:40 +0000
commit77368eed146ec8278609e04e395c7ef04c386313 (patch)
tree440eed647438cd8de03e91609e1daa43caf04bca /src/app
parent9cdef4e296c77fb94d99553de05ba1aaa6c81ed8 (diff)
deprecate view_month_offset in favor of cursor
Diffstat (limited to 'src/app')
-rw-r--r--src/app/cursor.rs22
-rw-r--r--src/app/impl_self.rs41
-rw-r--r--src/app/impl_view.rs10
3 files changed, 47 insertions, 26 deletions
diff --git a/src/app/cursor.rs b/src/app/cursor.rs
index ed6bd65..f76d591 100644
--- a/src/app/cursor.rs
+++ b/src/app/cursor.rs
@@ -16,7 +16,7 @@ impl Cursor {
16 0: Local::now().naive_local().date(), 16 0: Local::now().naive_local().date(),
17 } 17 }
18 } 18 }
19 pub fn do_move(&mut self, d: Absolute) { 19 pub fn small_seek(&mut self, d: Absolute) {
20 let today = Local::now().naive_local().date(); 20 let today = Local::now().naive_local().date();
21 let cursor = self.0; 21 let cursor = self.0;
22 match d { 22 match d {
@@ -48,4 +48,24 @@ impl Cursor {
48 Absolute::None => {} 48 Absolute::None => {}
49 } 49 }
50 } 50 }
51 fn long_seek(&mut self, offset: Duration) {
52 let cursor = self.0;
53 let today = Local::now().naive_local().date();
54 let next = cursor.checked_add_signed(offset).unwrap_or(cursor);
55
56 if next <= today {
57 self.0 = next;
58 } else {
59 self.0 = today;
60 }
61 }
62 pub fn month_forward(&mut self) {
63 self.long_seek(Duration::weeks(4));
64 }
65 pub fn month_backward(&mut self) {
66 self.long_seek(Duration::weeks(-4));
67 }
68 pub fn reset(&mut self) {
69 self.0 = Local::now().naive_local().date();
70 }
51} 71}
diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs
index abf5209..fec7219 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};
@@ -54,42 +54,42 @@ impl App {
54 if self.habits.is_empty() { 54 if self.habits.is_empty() {
55 return ViewMode::Day; 55 return ViewMode::Day;
56 } 56 }
57 return self.habits[self.focus].view_mode(); 57 return self.habits[self.focus].inner_data_ref().view_mode();
58 } 58 }
59 59
60 pub fn set_mode(&mut self, mode: ViewMode) { 60 pub fn set_mode(&mut self, mode: ViewMode) {
61 if !self.habits.is_empty() { 61 if !self.habits.is_empty() {
62 self.habits[self.focus].set_view_mode(mode); 62 self.habits[self.focus]
63 .inner_data_mut_ref()
64 .set_view_mode(mode);
63 } 65 }
64 } 66 }
65 67
66 pub fn set_view_month_offset(&mut self, offset: u32) { 68 pub fn sift_backward(&mut self) {
67 self.view_month_offset = offset; 69 self.cursor.month_backward();
68 for v in self.habits.iter_mut() { 70 for v in self.habits.iter_mut() {
69 v.set_view_month_offset(offset); 71 v.inner_data_mut_ref().cursor.month_backward();
70 } 72 }
71 } 73 }
72 74
73 pub fn sift_backward(&mut self) { 75 pub fn sift_forward(&mut self) {
74 self.view_month_offset += 1; 76 self.cursor.month_forward();
75 for v in self.habits.iter_mut() { 77 for v in self.habits.iter_mut() {
76 v.set_view_month_offset(self.view_month_offset); 78 v.inner_data_mut_ref().cursor.month_forward();
77 } 79 }
78 } 80 }
79 81
80 pub fn sift_forward(&mut self) { 82 pub fn reset_cursor(&mut self) {
81 if self.view_month_offset > 0 { 83 self.cursor.reset();
82 self.view_month_offset -= 1; 84 for v in self.habits.iter_mut() {
83 for v in self.habits.iter_mut() { 85 v.inner_data_mut_ref().cursor.reset();
84 v.set_view_month_offset(self.view_month_offset);
85 }
86 } 86 }
87 } 87 }
88 88
89 pub fn move_cursor(&mut self, d: Absolute) { 89 pub fn move_cursor(&mut self, d: Absolute) {
90 self.cursor.do_move(d); 90 self.cursor.small_seek(d);
91 for v in self.habits.iter_mut() { 91 for v in self.habits.iter_mut() {
92 v.move_cursor(d); 92 v.inner_data_mut_ref().move_cursor(d);
93 } 93 }
94 } 94 }
95 95
@@ -133,11 +133,12 @@ impl App {
133 let total = self.habits.iter().map(|h| h.goal()).sum::<u32>(); 133 let total = self.habits.iter().map(|h| h.goal()).sum::<u32>();
134 let completed = total - remaining; 134 let completed = total - remaining;
135 135
136 let timestamp = if self.view_month_offset == 0 { 136 let timestamp = if self.cursor.0 == today {
137 format!("{}", Local::now().naive_local().date().format("%d/%b/%y"),) 137 format!("{}", Local::now().naive_local().date().format("%d/%b/%y"),)
138 } else { 138 } else {
139 let months = self.view_month_offset; 139 let since = NaiveDate::signed_duration_since(today, self.cursor.0).num_days();
140 format!("{}", format!("{} month(s) ago", months),) 140 let plural = if since == 1 { "" } else { "s" };
141 format!("{} ({} day{} ago)", self.cursor.0, since, plural)
141 }; 142 };
142 143
143 StatusLine { 144 StatusLine {
diff --git a/src/app/impl_view.rs b/src/app/impl_view.rs
index 0ec47f1..db05432 100644
--- a/src/app/impl_view.rs
+++ b/src/app/impl_view.rs
@@ -117,7 +117,7 @@ impl View for App {
117 if self.habits.is_empty() { 117 if self.habits.is_empty() {
118 return EventResult::Consumed(None); 118 return EventResult::Consumed(None);
119 } 119 }
120 if self.habits[self.focus].view_mode() == ViewMode::Week { 120 if self.habits[self.focus].inner_data_ref().view_mode() == ViewMode::Week {
121 self.set_mode(ViewMode::Day) 121 self.set_mode(ViewMode::Day)
122 } else { 122 } else {
123 self.set_mode(ViewMode::Week) 123 self.set_mode(ViewMode::Week)
@@ -126,13 +126,13 @@ impl View for App {
126 } 126 }
127 Event::Char('V') => { 127 Event::Char('V') => {
128 for habit in self.habits.iter_mut() { 128 for habit in self.habits.iter_mut() {
129 habit.set_view_mode(ViewMode::Week); 129 habit.inner_data_mut_ref().set_view_mode(ViewMode::Week);
130 } 130 }
131 return EventResult::Consumed(None); 131 return EventResult::Consumed(None);
132 } 132 }
133 Event::Key(Key::Esc) => { 133 Event::Key(Key::Esc) => {
134 for habit in self.habits.iter_mut() { 134 for habit in self.habits.iter_mut() {
135 habit.set_view_mode(ViewMode::Day); 135 habit.inner_data_mut_ref().set_view_mode(ViewMode::Day);
136 } 136 }
137 return EventResult::Consumed(None); 137 return EventResult::Consumed(None);
138 } 138 }
@@ -149,7 +149,7 @@ impl View for App {
149 return EventResult::Consumed(None); 149 return EventResult::Consumed(None);
150 } 150 }
151 Event::Char('}') => { 151 Event::Char('}') => {
152 self.set_view_month_offset(0); 152 self.reset_cursor();
153 return EventResult::Consumed(None); 153 return EventResult::Consumed(None);
154 } 154 }
155 Event::CtrlChar('l') => { 155 Event::CtrlChar('l') => {
@@ -166,7 +166,7 @@ impl View for App {
166 if self.habits.is_empty() { 166 if self.habits.is_empty() {
167 return EventResult::Ignored; 167 return EventResult::Ignored;
168 } 168 }
169 self.set_view_month_offset(0); 169 self.reset_cursor();
170 self.habits[self.focus].on_event(e) 170 self.habits[self.focus].on_event(e)
171 } 171 }
172 } 172 }