diff options
author | Akshay <[email protected]> | 2021-02-06 13:30:40 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2021-02-21 05:13:14 +0000 |
commit | 53f7a679a0cf7a510de13d67cf370988f71c0d08 (patch) | |
tree | b43b30df476d76d0a18feb7c6e7e00fd3ac4dae4 /src/app | |
parent | 9cdef4e296c77fb94d99553de05ba1aaa6c81ed8 (diff) |
deprecate view_month_offset in favor of cursor
Diffstat (limited to 'src/app')
-rw-r--r-- | src/app/cursor.rs | 22 | ||||
-rw-r--r-- | src/app/impl_self.rs | 42 | ||||
-rw-r--r-- | src/app/impl_view.rs | 21 | ||||
-rw-r--r-- | src/app/mod.rs | 1 |
4 files changed, 52 insertions, 34 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..d5f93ff 100644 --- a/src/app/impl_self.rs +++ b/src/app/impl_self.rs | |||
@@ -6,7 +6,7 @@ use std::path::PathBuf; | |||
6 | use std::sync::mpsc::channel; | 6 | use std::sync::mpsc::channel; |
7 | use std::time::Duration; | 7 | use std::time::Duration; |
8 | 8 | ||
9 | use chrono::Local; | 9 | use chrono::{Local, NaiveDate}; |
10 | use cursive::direction::Absolute; | 10 | use cursive::direction::Absolute; |
11 | use cursive::Vec2; | 11 | use cursive::Vec2; |
12 | use notify::{watcher, RecursiveMode, Watcher}; | 12 | use notify::{watcher, RecursiveMode, Watcher}; |
@@ -27,7 +27,6 @@ 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, | ||
31 | cursor: Cursor::new(), | 30 | cursor: Cursor::new(), |
32 | message: Message::startup(), | 31 | message: Message::startup(), |
33 | }; | 32 | }; |
@@ -54,42 +53,42 @@ impl App { | |||
54 | if self.habits.is_empty() { | 53 | if self.habits.is_empty() { |
55 | return ViewMode::Day; | 54 | return ViewMode::Day; |
56 | } | 55 | } |
57 | return self.habits[self.focus].view_mode(); | 56 | return self.habits[self.focus].inner_data_ref().view_mode(); |
58 | } | 57 | } |
59 | 58 | ||
60 | pub fn set_mode(&mut self, mode: ViewMode) { | 59 | pub fn set_mode(&mut self, mode: ViewMode) { |
61 | if !self.habits.is_empty() { | 60 | if !self.habits.is_empty() { |
62 | self.habits[self.focus].set_view_mode(mode); | 61 | self.habits[self.focus] |
62 | .inner_data_mut_ref() | ||
63 | .set_view_mode(mode); | ||
63 | } | 64 | } |
64 | } | 65 | } |
65 | 66 | ||
66 | pub fn set_view_month_offset(&mut self, offset: u32) { | 67 | pub fn sift_backward(&mut self) { |
67 | self.view_month_offset = offset; | 68 | self.cursor.month_backward(); |
68 | for v in self.habits.iter_mut() { | 69 | for v in self.habits.iter_mut() { |
69 | v.set_view_month_offset(offset); | 70 | v.inner_data_mut_ref().cursor.month_backward(); |
70 | } | 71 | } |
71 | } | 72 | } |
72 | 73 | ||
73 | pub fn sift_backward(&mut self) { | 74 | pub fn sift_forward(&mut self) { |
74 | self.view_month_offset += 1; | 75 | self.cursor.month_forward(); |
75 | for v in self.habits.iter_mut() { | 76 | for v in self.habits.iter_mut() { |
76 | v.set_view_month_offset(self.view_month_offset); | 77 | v.inner_data_mut_ref().cursor.month_forward(); |
77 | } | 78 | } |
78 | } | 79 | } |
79 | 80 | ||
80 | pub fn sift_forward(&mut self) { | 81 | pub fn reset_cursor(&mut self) { |
81 | if self.view_month_offset > 0 { | 82 | self.cursor.reset(); |
82 | self.view_month_offset -= 1; | 83 | for v in self.habits.iter_mut() { |
83 | for v in self.habits.iter_mut() { | 84 | v.inner_data_mut_ref().cursor.reset(); |
84 | v.set_view_month_offset(self.view_month_offset); | ||
85 | } | ||
86 | } | 85 | } |
87 | } | 86 | } |
88 | 87 | ||
89 | pub fn move_cursor(&mut self, d: Absolute) { | 88 | pub fn move_cursor(&mut self, d: Absolute) { |
90 | self.cursor.do_move(d); | 89 | self.cursor.small_seek(d); |
91 | for v in self.habits.iter_mut() { | 90 | for v in self.habits.iter_mut() { |
92 | v.move_cursor(d); | 91 | v.inner_data_mut_ref().move_cursor(d); |
93 | } | 92 | } |
94 | } | 93 | } |
95 | 94 | ||
@@ -133,11 +132,12 @@ impl App { | |||
133 | let total = self.habits.iter().map(|h| h.goal()).sum::<u32>(); | 132 | let total = self.habits.iter().map(|h| h.goal()).sum::<u32>(); |
134 | let completed = total - remaining; | 133 | let completed = total - remaining; |
135 | 134 | ||
136 | let timestamp = if self.view_month_offset == 0 { | 135 | let timestamp = if self.cursor.0 == today { |
137 | format!("{}", Local::now().naive_local().date().format("%d/%b/%y"),) | 136 | format!("{}", Local::now().naive_local().date().format("%d/%b/%y"),) |
138 | } else { | 137 | } else { |
139 | let months = self.view_month_offset; | 138 | let since = NaiveDate::signed_duration_since(today, self.cursor.0).num_days(); |
140 | format!("{}", format!("{} month(s) ago", months),) | 139 | let plural = if since == 1 { "" } else { "s" }; |
140 | format!("{} ({} day{} ago)", self.cursor.0, since, plural) | ||
141 | }; | 141 | }; |
142 | 142 | ||
143 | StatusLine { | 143 | StatusLine { |
diff --git a/src/app/impl_view.rs b/src/app/impl_view.rs index 0ec47f1..c369d8f 100644 --- a/src/app/impl_view.rs +++ b/src/app/impl_view.rs | |||
@@ -96,19 +96,19 @@ impl View for App { | |||
96 | return EventResult::Consumed(None); | 96 | return EventResult::Consumed(None); |
97 | } | 97 | } |
98 | 98 | ||
99 | Event::Char('w') => { | 99 | Event::Char('K') => { |
100 | self.move_cursor(Absolute::Up); | 100 | self.move_cursor(Absolute::Up); |
101 | return EventResult::Consumed(None); | 101 | return EventResult::Consumed(None); |
102 | } | 102 | } |
103 | Event::Char('a') => { | 103 | Event::Char('H') => { |
104 | self.move_cursor(Absolute::Left); | 104 | self.move_cursor(Absolute::Left); |
105 | return EventResult::Consumed(None); | 105 | return EventResult::Consumed(None); |
106 | } | 106 | } |
107 | Event::Char('s') => { | 107 | Event::Char('J') => { |
108 | self.move_cursor(Absolute::Down); | 108 | self.move_cursor(Absolute::Down); |
109 | return EventResult::Consumed(None); | 109 | return EventResult::Consumed(None); |
110 | } | 110 | } |
111 | Event::Char('d') => { | 111 | Event::Char('L') => { |
112 | self.move_cursor(Absolute::Right); | 112 | self.move_cursor(Absolute::Right); |
113 | return EventResult::Consumed(None); | 113 | return EventResult::Consumed(None); |
114 | } | 114 | } |
@@ -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,14 +126,15 @@ 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 | self.reset_cursor(); | ||
137 | return EventResult::Consumed(None); | 138 | return EventResult::Consumed(None); |
138 | } | 139 | } |
139 | 140 | ||
@@ -149,7 +150,7 @@ impl View for App { | |||
149 | return EventResult::Consumed(None); | 150 | return EventResult::Consumed(None); |
150 | } | 151 | } |
151 | Event::Char('}') => { | 152 | Event::Char('}') => { |
152 | self.set_view_month_offset(0); | 153 | self.reset_cursor(); |
153 | return EventResult::Consumed(None); | 154 | return EventResult::Consumed(None); |
154 | } | 155 | } |
155 | Event::CtrlChar('l') => { | 156 | Event::CtrlChar('l') => { |
@@ -159,14 +160,12 @@ impl View for App { | |||
159 | } | 160 | } |
160 | 161 | ||
161 | /* Every keybind that is not caught by App trickles | 162 | /* Every keybind that is not caught by App trickles |
162 | * down to the focused habit. We sift back to today | 163 | * down to the focused habit. |
163 | * before performing any action, "refocusing" the cursor | ||
164 | * */ | 164 | * */ |
165 | _ => { | 165 | _ => { |
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); | ||
170 | self.habits[self.focus].on_event(e) | 169 | self.habits[self.focus].on_event(e) |
171 | } | 170 | } |
172 | } | 171 | } |
diff --git a/src/app/mod.rs b/src/app/mod.rs index 9432f0d..726a656 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs | |||
@@ -21,7 +21,6 @@ pub struct App { | |||
21 | _file_watcher: RecommendedWatcher, | 21 | _file_watcher: RecommendedWatcher, |
22 | file_event_recv: Receiver<DebouncedEvent>, | 22 | file_event_recv: Receiver<DebouncedEvent>, |
23 | focus: usize, | 23 | focus: usize, |
24 | view_month_offset: u32, | ||
25 | cursor: Cursor, | 24 | cursor: Cursor, |
26 | message: Message, | 25 | message: Message, |
27 | } | 26 | } |