diff options
author | Akshay <nerdy@peppe.rs> | 2021-01-25 14:54:40 +0530 |
---|---|---|
committer | Akshay <nerdy@peppe.rs> | 2021-01-25 14:54:40 +0530 |
commit | 665fd3fb61891b73175690158cde38cf7f94ebc7 (patch) | |
tree | e4afd16384a9242a17440c79849ef563536afb0b | |
parent | f4de2e0219ef2ce96cd30c02eee18eedd22b556a (diff) |
add basic cursor actions
-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 @@ +use chrono::{Duration, Local, NaiveDate}; +use cursive::direction::Absolute; + +#[derive(Debug, Copy, Clone)] +pub struct Cursor(pub NaiveDate); + +impl std::default::Default for Cursor { + fn default() -> Self { + Cursor::new() + } +} + +impl Cursor { + pub fn new() -> Self { + Cursor { + 0: Local::now().naive_local().date(), + } + } + pub fn do_move(&mut self, d: Absolute) { + let today = Local::now().naive_local().date(); + let cursor = self.0; + match d { + Absolute::Right => { + // forward by 1 day + let next = cursor.succ_opt().unwrap_or(cursor); + if next <= today { + self.0 = next; + } + } + Absolute::Left => { + // backward by 1 day + // assumes an infinite past + self.0 = cursor.pred_opt().unwrap_or(cursor); + } + Absolute::Down => { + // forward by 1 week + let next = cursor.checked_add_signed(Duration::weeks(1)).unwrap(); + if next <= today { + self.0 = next; + } + } + Absolute::Up => { + // backward by 1 week + // assumes an infinite past + let next = cursor.checked_sub_signed(Duration::weeks(1)).unwrap(); + self.0 = next; + } + Absolute::None => {} + } + } +} |