aboutsummaryrefslogtreecommitdiff
path: root/src/app/cursor.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-02-21 05:13:32 +0000
committerAkshay <[email protected]>2021-02-21 05:13:32 +0000
commit38d9dfee224ebcd088c0ebecd9e1243994eea409 (patch)
tree1215766f07b1c2eaf68f20aba284e710eb9a24a3 /src/app/cursor.rs
parentad5bf181a176e64c9f70a292cad870e6e8110f09 (diff)
parent53f7a679a0cf7a510de13d67cf370988f71c0d08 (diff)
Merge branch 'cursor' into master
Diffstat (limited to 'src/app/cursor.rs')
-rw-r--r--src/app/cursor.rs71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/app/cursor.rs b/src/app/cursor.rs
new file mode 100644
index 0000000..f76d591
--- /dev/null
+++ b/src/app/cursor.rs
@@ -0,0 +1,71 @@
1use chrono::{Duration, Local, NaiveDate};
2use cursive::direction::Absolute;
3
4#[derive(Debug, Copy, Clone)]
5pub struct Cursor(pub NaiveDate);
6
7impl std::default::Default for Cursor {
8 fn default() -> Self {
9 Cursor::new()
10 }
11}
12
13impl Cursor {
14 pub fn new() -> Self {
15 Cursor {
16 0: Local::now().naive_local().date(),
17 }
18 }
19 pub fn small_seek(&mut self, d: Absolute) {
20 let today = Local::now().naive_local().date();
21 let cursor = self.0;
22 match d {
23 Absolute::Right => {
24 // forward by 1 day
25 let next = cursor.succ_opt().unwrap_or(cursor);
26 if next <= today {
27 self.0 = next;
28 }
29 }
30 Absolute::Left => {
31 // backward by 1 day
32 // assumes an infinite past
33 self.0 = cursor.pred_opt().unwrap_or(cursor);
34 }
35 Absolute::Down => {
36 // forward by 1 week
37 let next = cursor.checked_add_signed(Duration::weeks(1)).unwrap();
38 if next <= today {
39 self.0 = next;
40 }
41 }
42 Absolute::Up => {
43 // backward by 1 week
44 // assumes an infinite past
45 let next = cursor.checked_sub_signed(Duration::weeks(1)).unwrap();
46 self.0 = next;
47 }
48 Absolute::None => {}
49 }
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 }
71}