aboutsummaryrefslogtreecommitdiff
path: root/src/views/bitview.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-02-10 17:15:06 +0000
committerAkshay <[email protected]>2020-02-10 17:15:06 +0000
commitecc2e2b296c4241b56d201012d7a7de5f96007db (patch)
tree48a6381ff9e8041ebc9881cd8ce8037186a8727c /src/views/bitview.rs
parent5019b6f52e0ff6649d318d8e600e1cd52fb01c7f (diff)
factor out count and bit views
Diffstat (limited to 'src/views/bitview.rs')
-rw-r--r--src/views/bitview.rs97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/views/bitview.rs b/src/views/bitview.rs
new file mode 100644
index 0000000..16b3ad8
--- /dev/null
+++ b/src/views/bitview.rs
@@ -0,0 +1,97 @@
1use cursive::direction::Direction;
2use cursive::event::{Event, EventResult, Key};
3use cursive::theme::{BaseColor, Color, Effect, Style};
4use cursive::utils::markup::StyledString;
5use cursive::view::View;
6use cursive::{Printer, Vec2};
7
8use chrono::prelude::*;
9use chrono::{Local, NaiveDate};
10
11use crate::habit::Habit;
12
13pub struct BitView {
14 habit: Habit<bool>,
15 true_chr: char,
16 false_chr: char,
17 future_chr: char,
18
19 view_width: u32,
20 view_height: u32,
21 // color config
22}
23
24impl BitView {
25 pub fn new(habit: Habit<bool>) -> Self {
26 return BitView {
27 habit,
28 true_chr: '·',
29 false_chr: '·',
30 future_chr: '·',
31 view_width: 21,
32 view_height: 9,
33 };
34 }
35 pub fn get_title(&self) -> String {
36 return self.habit.get_name().to_owned();
37 }
38}
39
40impl View for BitView {
41 fn draw(&self, printer: &Printer) {
42 let now = Local::now();
43 let year = now.year();
44 let month = now.month();
45
46 let true_style = Style::from(Color::Dark(BaseColor::Cyan));
47 let false_style = Style::from(Color::Dark(BaseColor::Magenta));
48 let future_style = Style::from(Color::Light(BaseColor::Black));
49 let today_style = Style::from(Color::Dark(BaseColor::White));
50
51 for i in 1..=31 {
52 let day = NaiveDate::from_ymd_opt(year, month, i);
53 let day_style;
54
55 if let Some(d) = day {
56 let day_status = self.habit.get_by_date(d).unwrap_or(&false);
57 let coords = ((i % 7) * 3, i / 7 + 2);
58 let day_chr;
59
60 if d <= now.naive_utc().date() {
61 if *day_status {
62 day_chr = self.true_chr;
63 day_style = true_style;
64 } else {
65 day_chr = self.false_chr;
66 day_style = false_style;
67 }
68 } else {
69 day_chr = self.future_chr;
70 day_style = future_style;
71 }
72
73 printer.with_style(day_style, |p| {
74 p.print(coords, &format!("{:^3}", day_chr));
75 });
76 }
77 }
78 }
79
80 fn required_size(&mut self, _: Vec2) -> Vec2 {
81 (self.view_width, self.view_height).into()
82 }
83
84 fn take_focus(&mut self, _: Direction) -> bool {
85 true
86 }
87
88 fn on_event(&mut self, e: Event) -> EventResult {
89 match e {
90 Event::Key(Key::Enter) => {
91 self.habit.toggle(Local::now().naive_utc().date());
92 return EventResult::Consumed(None);
93 }
94 _ => return EventResult::Ignored,
95 }
96 }
97}