From aedff3ffa7259b7daefa4d1ec96e75015edd4e96 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 15 Feb 2020 18:21:09 +0530 Subject: rework views --- src/views/bitview.rs | 95 ----------------------------------------- src/views/countview.rs | 92 ---------------------------------------- src/views/habitview.rs | 112 +++++++++++++++++++++++++++++++++++++++++++++++++ src/views/mod.rs | 6 ++- 4 files changed, 116 insertions(+), 189 deletions(-) delete mode 100644 src/views/bitview.rs delete mode 100644 src/views/countview.rs create mode 100644 src/views/habitview.rs diff --git a/src/views/bitview.rs b/src/views/bitview.rs deleted file mode 100644 index b31b050..0000000 --- a/src/views/bitview.rs +++ /dev/null @@ -1,95 +0,0 @@ -use cursive::direction::Direction; -use cursive::event::{Event, EventResult, Key}; -use cursive::theme::{BaseColor, Color, Style}; -use cursive::view::View; -use cursive::{Printer, Vec2}; - -use chrono::prelude::*; -use chrono::{Local, NaiveDate}; - -use crate::habit::Habit; - -pub struct BitView { - habit: Habit, - true_chr: char, - false_chr: char, - future_chr: char, - - view_width: u32, - view_height: u32, - // color config -} - -impl BitView { - pub fn new(habit: Habit) -> Self { - return BitView { - habit, - true_chr: '·', - false_chr: '·', - future_chr: '·', - view_width: 21, - view_height: 9, - }; - } - pub fn get_title(&self) -> String { - return self.habit.get_name().to_owned(); - } -} - -impl View for BitView { - fn draw(&self, printer: &Printer) { - let now = Local::now(); - let year = now.year(); - let month = now.month(); - - 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)); - - for i in 1..=31 { - let day = NaiveDate::from_ymd_opt(year, month, i); - let day_style; - - if let Some(d) = day { - 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; - day_style = true_style; - } else { - day_chr = self.false_chr; - day_style = false_style; - } - } else { - day_chr = self.future_chr; - day_style = future_style; - } - - printer.with_style(day_style, |p| { - p.print(coords, &format!("{:^3}", day_chr)); - }); - } - } - } - - 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) => { - self.habit.toggle(Local::now().naive_utc().date()); - return EventResult::Consumed(None); - } - _ => return EventResult::Ignored, - } - } -} diff --git a/src/views/countview.rs b/src/views/countview.rs deleted file mode 100644 index d906acb..0000000 --- a/src/views/countview.rs +++ /dev/null @@ -1,92 +0,0 @@ -use cursive::direction::Direction; -use cursive::event::{Event, EventResult, Key}; -use cursive::theme::{BaseColor, Color, Style}; -use cursive::view::View; -use cursive::{Printer, Vec2}; - -use chrono::prelude::*; -use chrono::{Local, NaiveDate}; - -use crate::habit::Habit; - -pub struct CountView { - habit: Habit, - future_chr: char, - - view_width: u32, - view_height: u32, - // color config -} - -impl CountView { - pub fn new(habit: Habit) -> 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/habitview.rs b/src/views/habitview.rs new file mode 100644 index 0000000..3ae0c90 --- /dev/null +++ b/src/views/habitview.rs @@ -0,0 +1,112 @@ +use cursive::direction::Direction; +use cursive::event::{Event, EventResult, Key}; +use cursive::theme::{BaseColor, Color, Style}; +use cursive::view::View; +use cursive::{Printer, Vec2}; + +use chrono::prelude::*; +use chrono::{Local, NaiveDate}; + +use crate::habit::{Habit, HabitTrait, HabitType, TrackEvent}; + +pub struct HabitView { + habit: Habit, + // characters to use + true_chr: char, + false_chr: char, + future_chr: char, + // view dimensions + view_width: u32, + view_height: u32, + // color config + reached_color: Color, + todo_color: Color, + future_color: Color, +} + +impl HabitView { + pub fn new(habit: Habit) -> Self { + return HabitView { + habit, + true_chr: '·', + false_chr: '·', + future_chr: '·', + view_width: 21, + view_height: 9, + reached_color: Color::Dark(BaseColor::Cyan), + todo_color: Color::Dark(BaseColor::Magenta), + future_color: Color::Light(BaseColor::Black), + }; + } + pub fn get_title(&self) -> String { + return self.habit.get_name().to_owned(); + } +} + +impl View for HabitView { + fn draw(&self, printer: &Printer) { + let now = Local::now(); + let year = now.year(); + let month = now.month(); + + let goal_reached_style = Style::from(self.reached_color); + let todo_style = Style::from(self.todo_color); + let future_style = Style::from(self.future_color); + + for i in 1..=31 { + let day = NaiveDate::from_ymd_opt(year, month, i); + let mut day_style; + + if let Some(d) = day { + if self.habit.reached_goal(d) { + day_style = goal_reached_style; + } else { + day_style = todo_style; + } + let coords = ((i % 7) * 3, i / 7 + 2); + let day_chr: Box = match self.habit.get_by_date(d) { + Some(val) => match val { + HabitType::Bit(b) => { + if *b { + Box::new(self.true_chr) + } else { + Box::new(self.false_chr) + } + } + HabitType::Count(c) => Box::new(c.to_string()), + }, + None => { + day_style = future_style; + Box::new(self.future_chr) + } + }; + printer.with_style(day_style, |p| { + p.print(coords, &format!("{:^3}", day_chr)); + }); + } + } + } + + 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 { + let now = Local::now().naive_utc().date(); + match e { + Event::Key(Key::Enter) | Event::Char('n') => { + self.habit.modify(now, TrackEvent::Increment); + return EventResult::Consumed(None); + } + Event::Key(Key::Backspace) | Event::Char('p') => { + self.habit.modify(now, TrackEvent::Decrement); + return EventResult::Consumed(None); + } + _ => return EventResult::Ignored, + } + } +} diff --git a/src/views/mod.rs b/src/views/mod.rs index e1e8ca5..1e3732f 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs @@ -1,2 +1,4 @@ -pub mod bitview; -pub mod countview; +// pub mod bitview; +// pub mod countview; + +pub mod habitview; -- cgit v1.2.3