From 235a1cf038e07d6021b543dfa725df6000778c2f Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 27 Feb 2020 18:23:29 +0530 Subject: add statusline helpers, simplify drawing --- src/views/habitview.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/views/habitview.rs b/src/views/habitview.rs index ddeb456..c9ca505 100644 --- a/src/views/habitview.rs +++ b/src/views/habitview.rs @@ -9,6 +9,8 @@ use chrono::{Local, NaiveDate}; use crate::habit::{Habit, HabitTrait, HabitType, TrackEvent}; +use serde::Serialize; + pub struct HabitView { habit: Habit, // characters to use @@ -31,19 +33,25 @@ impl HabitView { true_chr: '·', false_chr: '·', future_chr: '·', - view_width: 21, - view_height: 9, + view_width: 25, + view_height: 8, reached_color: Color::Dark(BaseColor::Cyan), todo_color: Color::Dark(BaseColor::Magenta), future_color: Color::Light(BaseColor::Black), }; } - pub fn get_title(&self) -> String { + pub fn get_name(&self) -> String { return self.habit.get_name().to_owned(); } pub fn get_size(&self) -> Vec2 { (self.view_width, self.view_height).into() } + pub fn remaining(&self) -> u32 { + self.habit.remaining(Local::now().naive_utc().date()) + } + pub fn total(&self) -> u32 { + self.habit.total() + } } impl View for HabitView { @@ -56,6 +64,20 @@ impl View for HabitView { let todo_style = Style::from(self.todo_color); let future_style = Style::from(self.future_color); + printer.with_style( + if !printer.focused { + future_style + } else { + goal_reached_style + }, + |p| { + p.print( + (0, 0), + &format!("{:width$}", self.get_name(), width = self.get_size().x), + ) + }, + ); + for i in 1..=31 { let day = NaiveDate::from_ymd_opt(year, month, i); let mut day_style; @@ -66,7 +88,7 @@ impl View for HabitView { } else { day_style = todo_style; } - let coords = ((i % 7) * 3, i / 7 + 2); + let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into(); let day_chr: Box = match self.habit.get_by_date(d) { Some(val) => match val { HabitType::Bit(b) => { -- cgit v1.2.3