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 +++++++++++++++++++++- src/app/impl_self.rs | 42 +++++++++++++++++++++--------------------- src/app/impl_view.rs | 21 ++++++++++----------- src/app/mod.rs | 1 - 4 files changed, 52 insertions(+), 34 deletions(-) (limited to 'src/app') 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(); + } } 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; use std::sync::mpsc::channel; use std::time::Duration; -use chrono::Local; +use chrono::{Local, NaiveDate}; use cursive::direction::Absolute; use cursive::Vec2; use notify::{watcher, RecursiveMode, Watcher}; @@ -27,7 +27,6 @@ impl App { focus: 0, _file_watcher: watcher, file_event_recv: rx, - view_month_offset: 0, cursor: Cursor::new(), message: Message::startup(), }; @@ -54,42 +53,42 @@ impl App { if self.habits.is_empty() { return ViewMode::Day; } - return self.habits[self.focus].view_mode(); + return self.habits[self.focus].inner_data_ref().view_mode(); } pub fn set_mode(&mut self, mode: ViewMode) { if !self.habits.is_empty() { - self.habits[self.focus].set_view_mode(mode); + self.habits[self.focus] + .inner_data_mut_ref() + .set_view_mode(mode); } } - pub fn set_view_month_offset(&mut self, offset: u32) { - self.view_month_offset = offset; + pub fn sift_backward(&mut self) { + self.cursor.month_backward(); for v in self.habits.iter_mut() { - v.set_view_month_offset(offset); + v.inner_data_mut_ref().cursor.month_backward(); } } - pub fn sift_backward(&mut self) { - self.view_month_offset += 1; + pub fn sift_forward(&mut self) { + self.cursor.month_forward(); for v in self.habits.iter_mut() { - v.set_view_month_offset(self.view_month_offset); + v.inner_data_mut_ref().cursor.month_forward(); } } - pub fn sift_forward(&mut self) { - if self.view_month_offset > 0 { - self.view_month_offset -= 1; - for v in self.habits.iter_mut() { - v.set_view_month_offset(self.view_month_offset); - } + pub fn reset_cursor(&mut self) { + self.cursor.reset(); + for v in self.habits.iter_mut() { + v.inner_data_mut_ref().cursor.reset(); } } pub fn move_cursor(&mut self, d: Absolute) { - self.cursor.do_move(d); + self.cursor.small_seek(d); for v in self.habits.iter_mut() { - v.move_cursor(d); + v.inner_data_mut_ref().move_cursor(d); } } @@ -133,11 +132,12 @@ impl App { let total = self.habits.iter().map(|h| h.goal()).sum::(); let completed = total - remaining; - let timestamp = if self.view_month_offset == 0 { + let timestamp = if self.cursor.0 == today { format!("{}", Local::now().naive_local().date().format("%d/%b/%y"),) } else { - let months = self.view_month_offset; - format!("{}", format!("{} month(s) ago", months),) + let since = NaiveDate::signed_duration_since(today, self.cursor.0).num_days(); + let plural = if since == 1 { "" } else { "s" }; + format!("{} ({} day{} ago)", self.cursor.0, since, plural) }; 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 { return EventResult::Consumed(None); } - Event::Char('w') => { + Event::Char('K') => { self.move_cursor(Absolute::Up); return EventResult::Consumed(None); } - Event::Char('a') => { + Event::Char('H') => { self.move_cursor(Absolute::Left); return EventResult::Consumed(None); } - Event::Char('s') => { + Event::Char('J') => { self.move_cursor(Absolute::Down); return EventResult::Consumed(None); } - Event::Char('d') => { + Event::Char('L') => { self.move_cursor(Absolute::Right); return EventResult::Consumed(None); } @@ -117,7 +117,7 @@ impl View for App { if self.habits.is_empty() { return EventResult::Consumed(None); } - if self.habits[self.focus].view_mode() == ViewMode::Week { + if self.habits[self.focus].inner_data_ref().view_mode() == ViewMode::Week { self.set_mode(ViewMode::Day) } else { self.set_mode(ViewMode::Week) @@ -126,14 +126,15 @@ impl View for App { } Event::Char('V') => { for habit in self.habits.iter_mut() { - habit.set_view_mode(ViewMode::Week); + habit.inner_data_mut_ref().set_view_mode(ViewMode::Week); } return EventResult::Consumed(None); } Event::Key(Key::Esc) => { for habit in self.habits.iter_mut() { - habit.set_view_mode(ViewMode::Day); + habit.inner_data_mut_ref().set_view_mode(ViewMode::Day); } + self.reset_cursor(); return EventResult::Consumed(None); } @@ -149,7 +150,7 @@ impl View for App { return EventResult::Consumed(None); } Event::Char('}') => { - self.set_view_month_offset(0); + self.reset_cursor(); return EventResult::Consumed(None); } Event::CtrlChar('l') => { @@ -159,14 +160,12 @@ impl View for App { } /* Every keybind that is not caught by App trickles - * down to the focused habit. We sift back to today - * before performing any action, "refocusing" the cursor + * down to the focused habit. * */ _ => { if self.habits.is_empty() { return EventResult::Ignored; } - self.set_view_month_offset(0); self.habits[self.focus].on_event(e) } } 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 { _file_watcher: RecommendedWatcher, file_event_recv: Receiver, focus: usize, - view_month_offset: u32, cursor: Cursor, message: Message, } -- cgit v1.2.3