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 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 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 From 85670bd3c1297053592c8c2863185bda681d5185 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 25 Jan 2021 14:55:37 +0530 Subject: fix nix-flake build --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index a046490..a51c554 100644 --- a/flake.nix +++ b/flake.nix @@ -38,7 +38,7 @@ nativeBuildInputs = [ rust pkgs.cargo - pkgs.cargo + pkgs.openssl pkgs.ncurses ]; }; -- cgit v1.2.3 From c26de6fbfd55cca906b9c184621a9f550cdcc0f1 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 25 Jan 2021 15:21:36 +0530 Subject: attempt to style cursor --- src/theme.rs | 5 +++-- src/views.rs | 8 +++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/theme.rs b/src/theme.rs index 7ee65a1..e373b72 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -25,9 +25,10 @@ pub fn theme_gen() -> Theme { return t; } -pub fn cursor_gen() -> Style { +pub fn cursor_gen(foreground: Style) -> Style { Style::from(ColorStyle::new( - Light(cursive::theme::BaseColor::Blue), TerminalDefault, + Light(cursive::theme::BaseColor::Blue), )) + .combine(foreground) } diff --git a/src/views.rs b/src/views.rs index 4f78ca2..a0beb2c 100644 --- a/src/views.rs +++ b/src/views.rs @@ -44,7 +44,6 @@ 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 = is_today && self.reached_goal(Local::now().naive_local().date()); @@ -113,9 +112,12 @@ where let draw_day = |printer: &Printer| { let mut i = 0; while let Some(d) = NaiveDate::from_ymd_opt(year, month, i + 1) { - let mut day_style = cursor_style.combine(todo_style); + let mut day_style = todo_style; if self.reached_goal(d) { - day_style = day_style.combine(goal_reached_style); + day_style = goal_reached_style; + } + if d == now && printer.focused { + day_style = day_style.combine(cursor_gen(day_style)); } let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into(); if let Some(c) = self.get_by_date(d) { -- cgit v1.2.3 From 9cdef4e296c77fb94d99553de05ba1aaa6c81ed8 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 25 Jan 2021 15:45:03 +0530 Subject: fix cursor coloring --- src/theme.rs | 10 +++------- src/views.rs | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/theme.rs b/src/theme.rs index e373b72..7ae9efc 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -1,4 +1,4 @@ -use cursive::theme::Color::*; +use cursive::theme::Color::{self, *}; use cursive::theme::PaletteColor::*; use cursive::theme::{BorderStyle, ColorStyle, Palette, Style, Theme}; @@ -25,10 +25,6 @@ pub fn theme_gen() -> Theme { return t; } -pub fn cursor_gen(foreground: Style) -> Style { - Style::from(ColorStyle::new( - TerminalDefault, - Light(cursive::theme::BaseColor::Blue), - )) - .combine(foreground) +pub fn cursor_bg() -> Color { + Light(cursive::theme::BaseColor::Black) } diff --git a/src/views.rs b/src/views.rs index a0beb2c..a306602 100644 --- a/src/views.rs +++ b/src/views.rs @@ -1,6 +1,6 @@ use cursive::direction::Direction; use cursive::event::{Event, EventResult, Key}; -use cursive::theme::{Effect, Style}; +use cursive::theme::{ColorStyle, ColorType, Effect, Style}; use cursive::view::View; use cursive::{Printer, Vec2}; @@ -8,7 +8,7 @@ use chrono::prelude::*; use chrono::{Local, NaiveDate}; use crate::habit::{Bit, Count, Habit, TrackEvent, ViewMode}; -use crate::theme::cursor_gen; +use crate::theme::cursor_bg; use crate::utils::VIEW_WIDTH; use crate::CONFIGURATION; @@ -112,12 +112,20 @@ where let draw_day = |printer: &Printer| { let mut i = 0; while let Some(d) = NaiveDate::from_ymd_opt(year, month, i + 1) { - let mut day_style = todo_style; + let mut day_style = Style::none(); + let mut fs = future_style; + let grs = ColorStyle::front(CONFIGURATION.reached_color()); + let ts = ColorStyle::front(CONFIGURATION.todo_color()); + let cs = ColorStyle::back(cursor_bg()); + if self.reached_goal(d) { - day_style = goal_reached_style; + day_style = day_style.combine(Style::from(grs)); + } else { + day_style = day_style.combine(Style::from(ts)); } if d == now && printer.focused { - day_style = day_style.combine(cursor_gen(day_style)); + day_style = day_style.combine(cs); + fs = fs.combine(cs); } let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into(); if let Some(c) = self.get_by_date(d) { @@ -125,7 +133,7 @@ where p.print(coords, &format!("{:^3}", c)); }); } else { - printer.with_style(future_style, |p| { + printer.with_style(fs, |p| { p.print(coords, &format!("{:^3}", CONFIGURATION.look.future_chr)); }); } -- 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 --- flake.nix | 10 ++++ src/app/cursor.rs | 22 +++++++- src/app/impl_self.rs | 42 +++++++-------- src/app/impl_view.rs | 21 ++++---- src/app/mod.rs | 1 - src/habit/bit.rs | 37 +++---------- src/habit/count.rs | 37 +++---------- src/habit/mod.rs | 27 ++++++++++ src/habit/traits.rs | 61 +++++++-------------- src/keybinds.rs | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/theme.rs | 2 +- src/utils.rs | 4 +- src/views.rs | 9 ++-- 13 files changed, 276 insertions(+), 143 deletions(-) create mode 100644 src/keybinds.rs diff --git a/flake.nix b/flake.nix index a51c554..4526047 100644 --- a/flake.nix +++ b/flake.nix @@ -19,6 +19,11 @@ channel = "nightly"; sha256 = "LbKHsCOFXWpg/SEyACfzZuWjKbkXdH6EJKOPSGoO01E="; # set zeros after modifying channel or date }).rust; + rust-src = (mozilla.rustChannelOf { + date = "2020-12-23"; + channel = "nightly"; + sha256 = "LbKHsCOFXWpg/SEyACfzZuWjKbkXdH6EJKOPSGoO01E="; # set zeros after modifying channel or date + }).rust-src; naersk-lib = naersk.lib."${system}".override { cargo = rust; @@ -37,10 +42,15 @@ devShell = pkgs.mkShell { nativeBuildInputs = [ rust + rust-src + pkgs.rust-analyzer pkgs.cargo pkgs.openssl pkgs.ncurses ]; + shellHook = '' + export RUST_SRC_PATH="${rust-src}/lib/rustlib/src/rust/library" + ''; }; }); } 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, } diff --git a/src/habit/bit.rs b/src/habit/bit.rs index 7fe6fd9..da64ece 100644 --- a/src/habit/bit.rs +++ b/src/habit/bit.rs @@ -1,13 +1,12 @@ use std::collections::HashMap; +use std::default::Default; 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}; +use crate::habit::{InnerData, TrackEvent}; use crate::CONFIGURATION; #[derive(Copy, Clone, Debug, Serialize, Deserialize)] @@ -44,13 +43,7 @@ pub struct Bit { auto: bool, #[serde(skip)] - view_month_offset: u32, - - #[serde(skip)] - cursor: Cursor, - - #[serde(skip)] - view_mode: ViewMode, + inner_data: InnerData, } impl Bit { @@ -60,9 +53,7 @@ impl Bit { stats: HashMap::new(), goal: CustomBool(true), auto, - view_month_offset: 0, - cursor: Cursor::new(), - view_mode: ViewMode::Day, + inner_data: Default::default(), }; } } @@ -124,23 +115,11 @@ impl Habit for Bit { } } } - fn set_view_month_offset(&mut self, offset: u32) { - self.view_month_offset = offset; - } - 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; + fn inner_data_ref(&self) -> &InnerData { + &self.inner_data } - fn view_mode(&self) -> ViewMode { - self.view_mode + fn inner_data_mut_ref(&mut self) -> &mut InnerData { + &mut self.inner_data } fn is_auto(&self) -> bool { self.auto diff --git a/src/habit/count.rs b/src/habit/count.rs index b14354c..09fd399 100644 --- a/src/habit/count.rs +++ b/src/habit/count.rs @@ -1,13 +1,12 @@ use std::collections::HashMap; +use std::default::Default; 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}; +use crate::habit::{InnerData, TrackEvent}; #[derive(Debug, Serialize, Deserialize)] pub struct Count { @@ -19,13 +18,7 @@ pub struct Count { auto: bool, #[serde(skip)] - view_month_offset: u32, - - #[serde(skip)] - cursor: Cursor, - - #[serde(skip)] - view_mode: ViewMode, + inner_data: InnerData, } impl Count { @@ -35,9 +28,7 @@ impl Count { stats: HashMap::new(), goal, auto, - view_month_offset: 0, - cursor: Cursor::new(), - view_mode: ViewMode::Day, + inner_data: Default::default(), }; } } @@ -101,23 +92,11 @@ impl Habit for Count { }; } } - fn set_view_month_offset(&mut self, offset: u32) { - self.view_month_offset = offset; - } - 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; + fn inner_data_ref(&self) -> &InnerData { + &self.inner_data } - fn view_mode(&self) -> ViewMode { - self.view_mode + fn inner_data_mut_ref(&mut self) -> &mut InnerData { + &mut self.inner_data } fn is_auto(&self) -> bool { self.auto diff --git a/src/habit/mod.rs b/src/habit/mod.rs index 75e734a..d51abe5 100644 --- a/src/habit/mod.rs +++ b/src/habit/mod.rs @@ -1,3 +1,5 @@ +use std::default::Default; + mod traits; pub use traits::{Habit, HabitWrapper}; @@ -9,3 +11,28 @@ pub use bit::Bit; mod prelude; pub use prelude::{TrackEvent, ViewMode}; + +use crate::app::Cursor; + +use cursive::direction::Absolute; + +#[derive(Debug, Default)] +pub struct InnerData { + pub cursor: Cursor, + pub view_mode: ViewMode, +} + +impl InnerData { + pub fn move_cursor(&mut self, d: Absolute) { + self.cursor.small_seek(d); + } + pub fn cursor(&self) -> Cursor { + self.cursor + } + pub fn set_view_mode(&mut self, mode: ViewMode) { + self.view_mode = mode; + } + pub fn view_mode(&self) -> ViewMode { + self.view_mode + } +} diff --git a/src/habit/traits.rs b/src/habit/traits.rs index 289fd95..24d941d 100644 --- a/src/habit/traits.rs +++ b/src/habit/traits.rs @@ -1,58 +1,45 @@ use chrono::NaiveDate; -use cursive::direction::{Absolute, Direction}; +use cursive::direction::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::habit::{Bit, Count, InnerData, TrackEvent}; use crate::views::ShadowView; pub trait Habit { type HabitType; - fn set_name(&mut self, name: impl AsRef); - fn set_goal(&mut self, goal: Self::HabitType); - fn name(&self) -> String; fn get_by_date(&self, date: NaiveDate) -> Option<&Self::HabitType>; + fn goal(&self) -> u32; fn insert_entry(&mut self, date: NaiveDate, val: Self::HabitType); + fn modify(&mut self, date: NaiveDate, event: TrackEvent); + fn name(&self) -> String; fn reached_goal(&self, date: NaiveDate) -> bool; fn remaining(&self, date: NaiveDate) -> u32; - fn goal(&self) -> u32; - fn modify(&mut self, date: NaiveDate, event: TrackEvent); - - 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_goal(&mut self, goal: Self::HabitType); + fn set_name(&mut self, name: impl AsRef); - fn set_view_mode(&mut self, mode: ViewMode); - fn view_mode(&self) -> ViewMode; + fn inner_data_ref(&self) -> &InnerData; + fn inner_data_mut_ref(&mut self) -> &mut InnerData; fn is_auto(&self) -> bool; } #[typetag::serde(tag = "type")] pub trait HabitWrapper: erased_serde::Serialize { - fn remaining(&self, date: NaiveDate) -> u32; + fn draw(&self, printer: &Printer); fn goal(&self) -> u32; fn modify(&mut self, date: NaiveDate, event: TrackEvent); - fn draw(&self, printer: &Printer); + fn name(&self) -> String; fn on_event(&mut self, event: Event) -> EventResult; + fn remaining(&self, date: NaiveDate) -> u32; fn required_size(&mut self, _: Vec2) -> Vec2; fn take_focus(&mut self, _: Direction) -> bool; - fn name(&self) -> String; - 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; + fn inner_data_ref(&self) -> &InnerData; + fn inner_data_mut_ref(&mut self) -> &mut InnerData; fn is_auto(&self) -> bool; } @@ -88,23 +75,11 @@ macro_rules! auto_habit_impl { fn name(&self) -> String { Habit::name(self) } - fn set_view_month_offset(&mut self, offset: u32) { - Habit::set_view_month_offset(self, offset) - } - 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) + fn inner_data_ref(&self) -> &InnerData { + Habit::inner_data_ref(self) } - fn view_mode(&self) -> ViewMode { - Habit::view_mode(self) + fn inner_data_mut_ref(&mut self) -> &mut InnerData { + Habit::inner_data_mut_ref(self) } fn is_auto(&self) -> bool { Habit::is_auto(self) diff --git a/src/keybinds.rs b/src/keybinds.rs new file mode 100644 index 0000000..1b9c234 --- /dev/null +++ b/src/keybinds.rs @@ -0,0 +1,146 @@ +use std::convert::From; + +use cursive::event::Event as CursiveEvent; +use serde::ser; +use serde::{self, Deserialize, Serialize, Serializer}; + +#[derive(Debug, PartialEq)] +struct Event(CursiveEvent); + +macro_rules! event { + ($thing:expr) => { + Event { 0: $thing }; + }; +} + +impl From for Event +where + T: AsRef, +{ + fn from(key: T) -> Self { + let key = key.as_ref(); + if key.len() == 1 { + // single key + return event!(CursiveEvent::Char(key.chars().nth(0).unwrap())); + } else if (key.starts_with("c-") || key.starts_with("C-")) && key.len() == 3 { + // ctrl-key + return event!(CursiveEvent::CtrlChar(key.chars().nth(2).unwrap())); + } else { + panic!( + r"Invalid keybind in configuration! + (I intend to handle this error gracefully in the near future)" + ); + } + } +} + +enum Bind { + Char(char), + CtrlChar(char), + AltChar(char), +} + +impl Serialize for Bind { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + match self { + Bind::Char(c) => serializer.serialize_newtype_variant("bind", 0, "regular", &c), + Bind::CtrlChar(c) => serializer.serialize_newtype_variant("bind", 0, "ctrl", &c), + Bind::AltChar(c) => serializer.serialize_newtype_variant("bind", 0, "alt", &c), + } + } +} + +impl Deserialize for Bind { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + eprintln!("hell = {:#?}", hell); + } +} + +impl From for CursiveEvent { + fn from(key: Bind) -> Self { + match key { + Bind::Char(c) => CursiveEvent::Char(c), + Bind::CtrlChar(c) => CursiveEvent::Char(c), + Bind::AltChar(c) => CursiveEvent::AltChar(c), + } + } +} + +#[derive(Serialize, Deserialize)] +pub struct KeyBinds { + grid: Movement, + cursor: Movement, + week_mode: Bind, + global_week_mode: Bind, +} + +#[derive(Serialize, Deserialize)] +pub struct Movement { + up: Bind, + down: Bind, + left: Bind, + right: Bind, +} + +impl Movement { + pub fn new(left: char, down: char, up: char, right: char) -> Self { + return Movement { + up: Bind::Char(up), + down: Bind::Char(down), + left: Bind::Char(left), + right: Bind::Char(right), + }; + } +} + +impl std::default::Default for KeyBinds { + fn default() -> Self { + let grid = Movement::new('h', 'j', 'k', 'l'); + let cursor = Movement::new('H', 'J', 'K', 'L'); + return KeyBinds { + grid, + cursor, + week_mode: Bind::Char('v'), + global_week_mode: Bind::Char('V'), + }; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn normal_keybind() { + let bind = "X"; + let expected = CursiveEvent::Char('X'); + assert_eq!(Event::from(bind), event!(expected)); + } + + #[test] + fn control_keybind() { + let bind = "C-x"; + let expected = CursiveEvent::CtrlChar('x'); + assert_eq!(Event::from(bind), event!(expected)); + } + + #[test] + fn lower_case_control_keybind() { + let bind = "c-x"; + let expected = CursiveEvent::CtrlChar('x'); + assert_eq!(Event::from(bind), event!(expected)); + } + + #[test] + #[should_panic] + fn very_long_and_wrong_keybind() { + let bind = "alksdjfalkjdf"; + Event::from(bind); + } +} diff --git a/src/theme.rs b/src/theme.rs index 7ae9efc..879584c 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -1,6 +1,6 @@ use cursive::theme::Color::{self, *}; use cursive::theme::PaletteColor::*; -use cursive::theme::{BorderStyle, ColorStyle, Palette, Style, Theme}; +use cursive::theme::{BorderStyle, Palette, Theme}; pub fn pallete_gen() -> Palette { let mut p = Palette::default(); diff --git a/src/utils.rs b/src/utils.rs index 2453aa6..f5a25c8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -102,12 +102,12 @@ pub fn load_configuration_file() -> AppConfig { if let Ok(ref mut f) = File::open(&cf) { let mut j = String::new(); f.read_to_string(&mut j); - return toml::from_str(&j).unwrap(); + return toml::from_str(&j).unwrap_or_else(|e| panic!("Invalid config file: `{}`", e)); } else { if let Ok(dc) = toml::to_string(&AppConfig::default()) { match OpenOptions::new().create(true).write(true).open(&cf) { Ok(ref mut file) => file.write(dc.as_bytes()).unwrap(), - Err(_) => 0, + Err(_) => panic!("Unable to write config file to disk!"), }; } return Default::default(); diff --git a/src/views.rs b/src/views.rs index a306602..b90ce2b 100644 --- a/src/views.rs +++ b/src/views.rs @@ -1,6 +1,6 @@ use cursive::direction::Direction; use cursive::event::{Event, EventResult, Key}; -use cursive::theme::{ColorStyle, ColorType, Effect, Style}; +use cursive::theme::{ColorStyle, Effect, Style}; use cursive::view::View; use cursive::{Printer, Vec2}; @@ -35,13 +35,12 @@ where // .checked_sub_signed(Duration::weeks(4 * self.view_month_offset() as i64)) // .unwrap() // }; - let now = self.cursor().0; + let now = self.inner_data_ref().cursor().0; let is_today = now == Local::now().naive_local().date(); let year = now.year(); let month = now.month(); let goal_reached_style = Style::from(CONFIGURATION.reached_color()); - let todo_style = Style::from(CONFIGURATION.todo_color()); let future_style = Style::from(CONFIGURATION.inactive_color()); let strikethrough = Style::from(Effect::Strikethrough); @@ -141,7 +140,7 @@ where } }; - match self.view_mode() { + match self.inner_data_ref().view_mode() { ViewMode::Day => draw_day(printer), ViewMode::Week => draw_week(printer), _ => draw_day(printer), @@ -157,7 +156,7 @@ where } fn on_event(&mut self, e: Event) -> EventResult { - let now = self.cursor().0; + let now = self.inner_data_mut_ref().cursor().0; if self.is_auto() { return EventResult::Ignored; } -- cgit v1.2.3