From cd03d732b1f0df6c020a94135db2db4b690a4937 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 25 Jan 2021 14:54:51 +0530 Subject: handle cursor events and entry --- src/app/impl_self.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/app/impl_self.rs') diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs index 1114d50..abf5209 100644 --- a/src/app/impl_self.rs +++ b/src/app/impl_self.rs @@ -15,7 +15,7 @@ use crate::command::{Command, CommandLineError}; use crate::habit::{Bit, Count, HabitWrapper, TrackEvent, ViewMode}; use crate::utils::{self, GRID_WIDTH, VIEW_HEIGHT, VIEW_WIDTH}; -use crate::app::{App, MessageKind, StatusLine}; +use crate::app::{App, Cursor, Message, MessageKind, StatusLine}; impl App { pub fn new() -> Self { @@ -28,7 +28,8 @@ impl App { _file_watcher: watcher, file_event_recv: rx, view_month_offset: 0, - message: "Type :add to get started, Ctrl-L to dismiss".into(), + cursor: Cursor::new(), + message: Message::startup(), }; } @@ -85,6 +86,13 @@ impl App { } } + pub fn move_cursor(&mut self, d: Absolute) { + self.cursor.do_move(d); + for v in self.habits.iter_mut() { + v.move_cursor(d); + } + } + pub fn set_focus(&mut self, d: Absolute) { match d { Absolute::Right => { @@ -129,7 +137,7 @@ impl App { format!("{}", Local::now().naive_local().date().format("%d/%b/%y"),) } else { let months = self.view_month_offset; - format!("{}", format!("{} months ago", months),) + format!("{}", format!("{} month(s) ago", months),) }; StatusLine { -- 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/impl_self.rs | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'src/app/impl_self.rs') 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 { -- cgit v1.2.3