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 +++++++++++--- src/app/impl_view.rs | 18 ++++++++++++++++++ src/app/message.rs | 3 +++ src/app/mod.rs | 3 +++ src/habit/bit.rs | 12 ++++++++++++ src/habit/count.rs | 12 ++++++++++++ src/habit/traits.rs | 15 ++++++++++++++- src/theme.rs | 9 ++++++++- src/views.rs | 31 ++++++++++++++++--------------- 9 files changed, 97 insertions(+), 20 deletions(-) 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 { diff --git a/src/app/impl_view.rs b/src/app/impl_view.rs index 395cac4..0ec47f1 100644 --- a/src/app/impl_view.rs +++ b/src/app/impl_view.rs @@ -95,6 +95,24 @@ impl View for App { self.set_focus(Absolute::Down); return EventResult::Consumed(None); } + + Event::Char('w') => { + self.move_cursor(Absolute::Up); + return EventResult::Consumed(None); + } + Event::Char('a') => { + self.move_cursor(Absolute::Left); + return EventResult::Consumed(None); + } + Event::Char('s') => { + self.move_cursor(Absolute::Down); + return EventResult::Consumed(None); + } + Event::Char('d') => { + self.move_cursor(Absolute::Right); + return EventResult::Consumed(None); + } + Event::Char('v') => { if self.habits.is_empty() { return EventResult::Consumed(None); diff --git a/src/app/message.rs b/src/app/message.rs index 65f0a5c..a1d3d57 100644 --- a/src/app/message.rs +++ b/src/app/message.rs @@ -35,6 +35,9 @@ pub struct Message { } impl Message { + pub fn startup() -> Self { + "Type :add to get started, Ctrl-L to dismiss".into() + } pub fn contents(&self) -> &str { &self.msg } diff --git a/src/app/mod.rs b/src/app/mod.rs index 2aecb33..9432f0d 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -5,11 +5,13 @@ use notify::{DebouncedEvent, RecommendedWatcher}; use crate::habit::HabitWrapper; +mod cursor; mod impl_self; mod impl_view; mod message; pub struct StatusLine(String, String); +pub use cursor::Cursor; pub use message::{Message, MessageKind}; pub struct App { @@ -20,6 +22,7 @@ pub struct App { file_event_recv: Receiver, focus: usize, view_month_offset: u32, + cursor: Cursor, message: Message, } diff --git a/src/habit/bit.rs b/src/habit/bit.rs index 2bbb0ac..7fe6fd9 100644 --- a/src/habit/bit.rs +++ b/src/habit/bit.rs @@ -1,8 +1,10 @@ use std::collections::HashMap; use chrono::NaiveDate; +use cursive::direction::Absolute; use serde::{Deserialize, Serialize}; +use crate::app::Cursor; use crate::habit::prelude::default_auto; use crate::habit::traits::Habit; use crate::habit::{TrackEvent, ViewMode}; @@ -44,6 +46,9 @@ pub struct Bit { #[serde(skip)] view_month_offset: u32, + #[serde(skip)] + cursor: Cursor, + #[serde(skip)] view_mode: ViewMode, } @@ -56,6 +61,7 @@ impl Bit { goal: CustomBool(true), auto, view_month_offset: 0, + cursor: Cursor::new(), view_mode: ViewMode::Day, }; } @@ -124,6 +130,12 @@ impl Habit for Bit { fn view_month_offset(&self) -> u32 { self.view_month_offset } + fn move_cursor(&mut self, d: Absolute) { + self.cursor.do_move(d); + } + fn cursor(&self) -> Cursor { + self.cursor + } fn set_view_mode(&mut self, mode: ViewMode) { self.view_mode = mode; } diff --git a/src/habit/count.rs b/src/habit/count.rs index d351758..b14354c 100644 --- a/src/habit/count.rs +++ b/src/habit/count.rs @@ -1,8 +1,10 @@ use std::collections::HashMap; use chrono::NaiveDate; +use cursive::direction::Absolute; use serde::{Deserialize, Serialize}; +use crate::app::Cursor; use crate::habit::prelude::default_auto; use crate::habit::traits::Habit; use crate::habit::{TrackEvent, ViewMode}; @@ -19,6 +21,9 @@ pub struct Count { #[serde(skip)] view_month_offset: u32, + #[serde(skip)] + cursor: Cursor, + #[serde(skip)] view_mode: ViewMode, } @@ -31,6 +36,7 @@ impl Count { goal, auto, view_month_offset: 0, + cursor: Cursor::new(), view_mode: ViewMode::Day, }; } @@ -101,6 +107,12 @@ impl Habit for Count { fn view_month_offset(&self) -> u32 { self.view_month_offset } + fn move_cursor(&mut self, d: Absolute) { + self.cursor.do_move(d); + } + fn cursor(&self) -> Cursor { + self.cursor + } fn set_view_mode(&mut self, mode: ViewMode) { self.view_mode = mode; } diff --git a/src/habit/traits.rs b/src/habit/traits.rs index 74fd00b..289fd95 100644 --- a/src/habit/traits.rs +++ b/src/habit/traits.rs @@ -1,10 +1,11 @@ use chrono::NaiveDate; -use cursive::direction::Direction; +use cursive::direction::{Absolute, Direction}; use cursive::event::{Event, EventResult}; use cursive::{Printer, Vec2}; use typetag; +use crate::app::Cursor; use crate::habit::{Bit, Count, TrackEvent, ViewMode}; use crate::views::ShadowView; @@ -24,6 +25,9 @@ pub trait Habit { fn set_view_month_offset(&mut self, offset: u32); fn view_month_offset(&self) -> u32; + fn move_cursor(&mut self, d: Absolute); + fn cursor(&self) -> Cursor; + fn set_view_mode(&mut self, mode: ViewMode); fn view_mode(&self) -> ViewMode; @@ -44,6 +48,9 @@ pub trait HabitWrapper: erased_serde::Serialize { fn set_view_month_offset(&mut self, offset: u32); fn view_month_offset(&self) -> u32; + fn move_cursor(&mut self, d: Absolute); + fn cursor(&self) -> Cursor; + fn set_view_mode(&mut self, mode: ViewMode); fn view_mode(&self) -> ViewMode; @@ -87,6 +94,12 @@ macro_rules! auto_habit_impl { fn view_month_offset(&self) -> u32 { Habit::view_month_offset(self) } + fn move_cursor(&mut self, d: Absolute) { + Habit::move_cursor(self, d) + } + fn cursor(&self) -> Cursor { + Habit::cursor(self) + } fn set_view_mode(&mut self, mode: ViewMode) { Habit::set_view_mode(self, mode) } diff --git a/src/theme.rs b/src/theme.rs index 1d2cc36..7ee65a1 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -1,6 +1,6 @@ use cursive::theme::Color::*; use cursive::theme::PaletteColor::*; -use cursive::theme::{BorderStyle, Palette, Theme}; +use cursive::theme::{BorderStyle, ColorStyle, Palette, Style, Theme}; pub fn pallete_gen() -> Palette { let mut p = Palette::default(); @@ -24,3 +24,10 @@ pub fn theme_gen() -> Theme { t.palette = pallete_gen(); return t; } + +pub fn cursor_gen() -> Style { + Style::from(ColorStyle::new( + Light(cursive::theme::BaseColor::Blue), + TerminalDefault, + )) +} diff --git a/src/views.rs b/src/views.rs index efd1391..4f78ca2 100644 --- a/src/views.rs +++ b/src/views.rs @@ -5,9 +5,10 @@ use cursive::view::View; use cursive::{Printer, Vec2}; use chrono::prelude::*; -use chrono::{Duration, Local, NaiveDate}; +use chrono::{Local, NaiveDate}; use crate::habit::{Bit, Count, Habit, TrackEvent, ViewMode}; +use crate::theme::cursor_gen; use crate::utils::VIEW_WIDTH; use crate::CONFIGURATION; @@ -27,13 +28,15 @@ where T::HabitType: std::fmt::Display, { fn draw(&self, printer: &Printer) { - let now = if self.view_month_offset() == 0 { - Local::today() - } else { - Local::today() - .checked_sub_signed(Duration::weeks(4 * self.view_month_offset() as i64)) - .unwrap() - }; + // let now = if self.view_month_offset() == 0 { + // Local::today() + // } else { + // Local::today() + // .checked_sub_signed(Duration::weeks(4 * self.view_month_offset() as i64)) + // .unwrap() + // }; + let now = self.cursor().0; + let is_today = now == Local::now().naive_local().date(); let year = now.year(); let month = now.month(); @@ -41,10 +44,10 @@ where let todo_style = Style::from(CONFIGURATION.todo_color()); let future_style = Style::from(CONFIGURATION.inactive_color()); + let cursor_style = cursor_gen(); let strikethrough = Style::from(Effect::Strikethrough); - let goal_status = - self.view_month_offset() == 0 && self.reached_goal(Local::now().naive_local().date()); + let goal_status = is_today && self.reached_goal(Local::now().naive_local().date()); printer.with_style( Style::merge(&[ @@ -110,11 +113,9 @@ where let draw_day = |printer: &Printer| { let mut i = 0; while let Some(d) = NaiveDate::from_ymd_opt(year, month, i + 1) { - let day_style; + let mut day_style = cursor_style.combine(todo_style); if self.reached_goal(d) { - day_style = goal_reached_style; - } else { - day_style = todo_style; + day_style = day_style.combine(goal_reached_style); } let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into(); if let Some(c) = self.get_by_date(d) { @@ -146,7 +147,7 @@ where } fn on_event(&mut self, e: Event) -> EventResult { - let now = Local::now().naive_local().date(); + let now = self.cursor().0; if self.is_auto() { return EventResult::Ignored; } -- cgit v1.2.3