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 +++ 4 files changed, 35 insertions(+), 3 deletions(-) (limited to 'src/app') 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, } -- cgit v1.2.3