diff options
author | Akshay <nerdypepper@tuta.io> | 2020-02-10 22:45:06 +0530 |
---|---|---|
committer | Akshay <nerdypepper@tuta.io> | 2020-02-10 22:45:06 +0530 |
commit | ecc2e2b296c4241b56d201012d7a7de5f96007db (patch) | |
tree | 48a6381ff9e8041ebc9881cd8ce8037186a8727c | |
parent | 5019b6f52e0ff6649d318d8e600e1cd52fb01c7f (diff) |
factor out count and bit views
-rw-r--r-- | src/views/bitview.rs (renamed from src/views.rs) | 2 | ||||
-rw-r--r-- | src/views/countview.rs | 93 | ||||
-rw-r--r-- | src/views/mod.rs | 2 |
3 files changed, 97 insertions, 0 deletions
diff --git a/src/views.rs b/src/views/bitview.rs index 4f1594a..16b3ad8 100644 --- a/src/views.rs +++ b/src/views/bitview.rs @@ -46,6 +46,7 @@ impl View for BitView { let true_style = Style::from(Color::Dark(BaseColor::Cyan)); let false_style = Style::from(Color::Dark(BaseColor::Magenta)); let future_style = Style::from(Color::Light(BaseColor::Black)); + let today_style = Style::from(Color::Dark(BaseColor::White)); for i in 1..=31 { let day = NaiveDate::from_ymd_opt(year, month, i); @@ -55,6 +56,7 @@ impl View for BitView { let day_status = self.habit.get_by_date(d).unwrap_or(&false); let coords = ((i % 7) * 3, i / 7 + 2); let day_chr; + if d <= now.naive_utc().date() { if *day_status { day_chr = self.true_chr; diff --git a/src/views/countview.rs b/src/views/countview.rs new file mode 100644 index 0000000..1e8855a --- /dev/null +++ b/src/views/countview.rs @@ -0,0 +1,93 @@ +use cursive::direction::Direction; +use cursive::event::{Event, EventResult, Key}; +use cursive::theme::{BaseColor, Color, Effect, Style}; +use cursive::utils::markup::StyledString; +use cursive::view::View; +use cursive::{Printer, Vec2}; + +use chrono::prelude::*; +use chrono::{Local, NaiveDate}; + +use crate::habit::Habit; + +pub struct CountView { + habit: Habit<u32>, + future_chr: char, + + view_width: u32, + view_height: u32, + // color config +} + +impl CountView { + pub fn new(habit: Habit<u32>) -> Self { + return CountView { + habit, + future_chr: 'ยท', + view_width: 21, + view_height: 9, + }; + } + pub fn get_title(&self) -> String { + return self.habit.get_name().to_owned(); + } +} + +impl View for CountView { + fn draw(&self, printer: &Printer) { + let now = Local::now(); + let year = now.year(); + let month = now.month(); + + let goal_reached_style = Style::from(Color::Dark(BaseColor::Cyan)); + let not_reached_style = Style::from(Color::Dark(BaseColor::Magenta)); + let future_style = Style::from(Color::Light(BaseColor::Black)); + + for i in 1..=31 { + let day = NaiveDate::from_ymd_opt(year, month, i); + let day_style; + + if let Some(d) = day { + let coords = ((i % 7) * 3, i / 7 + 2); + let mut day_count = self.habit.get_by_date(d).unwrap_or(&0).to_string(); + + if d <= now.naive_utc().date() { + if self.habit.reached_goal(d) { + day_style = goal_reached_style; + } else { + day_style = not_reached_style; + } + } else { + day_count = format!("{:^3}", self.future_chr); + day_style = future_style; + } + + printer.with_style(day_style, |p| { + p.print(coords, &format!("{:^3}", day_count)); + }); + } + } + } + + fn required_size(&mut self, _: Vec2) -> Vec2 { + (self.view_width, self.view_height).into() + } + + fn take_focus(&mut self, _: Direction) -> bool { + true + } + + fn on_event(&mut self, e: Event) -> EventResult { + match e { + Event::Key(Key::Enter) | Event::Char('n') => { + self.habit.increment(Local::now().naive_utc().date()); + return EventResult::Consumed(None); + } + Event::Key(Key::Backspace) | Event::Char('p') => { + self.habit.decrement(Local::now().naive_utc().date()); + return EventResult::Consumed(None); + } + _ => return EventResult::Ignored, + } + } +} diff --git a/src/views/mod.rs b/src/views/mod.rs new file mode 100644 index 0000000..e1e8ca5 --- /dev/null +++ b/src/views/mod.rs @@ -0,0 +1,2 @@ +pub mod bitview; +pub mod countview; |