From 99d2a835b4f032de78d9cdbe27521363a25204bd Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 15 Jul 2020 15:30:52 +0530 Subject: display view mode in statusline --- src/app.rs | 43 ++++++++++++++++++++++++++++--------------- src/habit/prelude.rs | 15 ++++++++++++++- src/views.rs | 3 +++ 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/app.rs b/src/app.rs index 93e5def..2d83714 100644 --- a/src/app.rs +++ b/src/app.rs @@ -48,6 +48,13 @@ impl App { self.habits.retain(|h| h.get_name() != name); } + pub fn get_mode(&self) -> ViewMode { + if self.habits.is_empty() { + return ViewMode::Day; + } + return self.habits[self.focus].view_mode(); + } + pub fn set_mode(&mut self, mode: ViewMode) { if !self.habits.is_empty() { self.habits[self.focus].set_view_mode(mode); @@ -115,22 +122,19 @@ impl App { let completed = total - remaining; let timestamp = if self.view_month_offset == 0 { - format!( - "{:>width$}", - Local::now().date().format("%d/%b/%y"), - width = CONFIGURATION.view_width * CONFIGURATION.grid_width - ) + format!("{}", Local::now().date().format("%d/%b/%y"),) } else { let months = self.view_month_offset; - format!( - "{:>width$}", - format!("{} months ago", months), - width = CONFIGURATION.view_width * CONFIGURATION.grid_width - ) + format!("{}", format!("{} months ago", months),) }; StatusLine { - 0: format!("Today: {} completed, {} remaining", completed, remaining), + 0: format!( + "Today: {} completed, {} remaining --{}--", + completed, + remaining, + self.get_mode() + ), 1: timestamp, } } @@ -218,18 +222,27 @@ impl App { impl View for App { fn draw(&self, printer: &Printer) { let grid_width = CONFIGURATION.grid_width; + let view_width = CONFIGURATION.view_width; + let view_height = CONFIGURATION.view_height; let mut offset = Vec2::zero(); for (idx, i) in self.habits.iter().enumerate() { if idx >= grid_width && idx % grid_width == 0 { - offset = offset.map_y(|y| y + CONFIGURATION.view_height).map_x(|_| 0); + offset = offset.map_y(|y| y + view_height).map_x(|_| 0); } i.draw(&printer.offset(offset).focused(self.focus == idx)); - offset = offset.map_x(|x| x + CONFIGURATION.view_width + 2); + offset = offset.map_x(|x| x + view_width + 2); } offset = offset.map_x(|_| 0).map_y(|_| self.max_size().y - 2); - printer.print(offset, &self.status().1); // right status - printer.print(offset, &self.status().0); // left status + + let status = self.status(); + printer.print(offset, &status.0); // left status + + let full = grid_width * (view_width + 2); + offset = offset + .map_x(|_| full - status.1.len()) + .map_y(|_| self.max_size().y - 2); + printer.print(offset, &status.1); // right status } fn required_size(&mut self, _: Vec2) -> Vec2 { diff --git a/src/habit/prelude.rs b/src/habit/prelude.rs index b8b2bb2..19b00a7 100644 --- a/src/habit/prelude.rs +++ b/src/habit/prelude.rs @@ -1,4 +1,6 @@ use serde::{Deserialize, Serialize}; +use std::default; +use std::fmt; pub enum TrackEvent { Increment, @@ -13,12 +15,23 @@ pub enum ViewMode { Year, } -impl std::default::Default for ViewMode { +impl default::Default for ViewMode { fn default() -> Self { ViewMode::Day } } +impl fmt::Display for ViewMode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ViewMode::Day => write!(f, "DAY"), + ViewMode::Week => write!(f, "WEEK"), + ViewMode::Month => write!(f, "MONTH"), + ViewMode::Year => write!(f, "YEAR"), + } + } +} + pub fn default_auto() -> bool { false } diff --git a/src/views.rs b/src/views.rs index 54d5431..f06978c 100644 --- a/src/views.rs +++ b/src/views.rs @@ -135,6 +135,9 @@ where fn on_event(&mut self, e: Event) -> EventResult { let now = Local::now().naive_utc().date(); + if self.is_auto() { + return EventResult::Consumed(None); + } match e { Event::Key(Key::Enter) | Event::Char('n') => { self.modify(now, TrackEvent::Increment); -- cgit v1.2.3