From b7041c51f2cf55f8c26bd852162776aed2111de5 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 10 Feb 2020 21:34:34 +0530 Subject: simpler view styling logic --- src/views.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/views.rs b/src/views.rs index 5b9ec20..4f1594a 100644 --- a/src/views.rs +++ b/src/views.rs @@ -1,5 +1,7 @@ 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}; @@ -23,9 +25,9 @@ impl BitView { pub fn new(habit: Habit) -> Self { return BitView { habit, - true_chr: 'x', - false_chr: 'o', - future_chr: '.', + true_chr: '·', + false_chr: '·', + future_chr: '·', view_width: 21, view_height: 9, }; @@ -41,27 +43,40 @@ impl View for BitView { 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 { - printer.print(coords, &format!("{:^3}", self.true_chr)) + day_chr = self.true_chr; + day_style = true_style; } else { - printer.print(coords, &format!("{:^3}", self.false_chr)) + day_chr = self.false_chr; + day_style = false_style; } } else { - printer.print(coords, &format!("{:^3}", self.future_chr)) + 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 { - (20, 9).into() + (self.view_width, self.view_height).into() } fn take_focus(&mut self, _: Direction) -> bool { -- cgit v1.2.3