diff options
author | Akshay <[email protected]> | 2021-01-25 09:24:40 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2021-01-25 09:24:40 +0000 |
commit | 665fd3fb61891b73175690158cde38cf7f94ebc7 (patch) | |
tree | e4afd16384a9242a17440c79849ef563536afb0b /src/app/cursor.rs | |
parent | f4de2e0219ef2ce96cd30c02eee18eedd22b556a (diff) |
add basic cursor actions
Diffstat (limited to 'src/app/cursor.rs')
-rw-r--r-- | src/app/cursor.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/app/cursor.rs b/src/app/cursor.rs new file mode 100644 index 0000000..ed6bd65 --- /dev/null +++ b/src/app/cursor.rs | |||
@@ -0,0 +1,51 @@ | |||
1 | use chrono::{Duration, Local, NaiveDate}; | ||
2 | use cursive::direction::Absolute; | ||
3 | |||
4 | #[derive(Debug, Copy, Clone)] | ||
5 | pub struct Cursor(pub NaiveDate); | ||
6 | |||
7 | impl std::default::Default for Cursor { | ||
8 | fn default() -> Self { | ||
9 | Cursor::new() | ||
10 | } | ||
11 | } | ||
12 | |||
13 | impl Cursor { | ||
14 | pub fn new() -> Self { | ||
15 | Cursor { | ||
16 | 0: Local::now().naive_local().date(), | ||
17 | } | ||
18 | } | ||
19 | pub fn do_move(&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 | } | ||