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