From 665fd3fb61891b73175690158cde38cf7f94ebc7 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 25 Jan 2021 14:54:40 +0530 Subject: add basic cursor actions --- src/app/cursor.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/app/cursor.rs (limited to 'src/app/cursor.rs') 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 => {} + } + } +} -- cgit v1.2.3 From 53f7a679a0cf7a510de13d67cf370988f71c0d08 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 6 Feb 2021 19:00:40 +0530 Subject: deprecate view_month_offset in favor of cursor --- src/app/cursor.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'src/app/cursor.rs') 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 { 0: Local::now().naive_local().date(), } } - pub fn do_move(&mut self, d: Absolute) { + pub fn small_seek(&mut self, d: Absolute) { let today = Local::now().naive_local().date(); let cursor = self.0; match d { @@ -48,4 +48,24 @@ impl Cursor { Absolute::None => {} } } + fn long_seek(&mut self, offset: Duration) { + let cursor = self.0; + let today = Local::now().naive_local().date(); + let next = cursor.checked_add_signed(offset).unwrap_or(cursor); + + if next <= today { + self.0 = next; + } else { + self.0 = today; + } + } + pub fn month_forward(&mut self) { + self.long_seek(Duration::weeks(4)); + } + pub fn month_backward(&mut self) { + self.long_seek(Duration::weeks(-4)); + } + pub fn reset(&mut self) { + self.0 = Local::now().naive_local().date(); + } } -- cgit v1.2.3