aboutsummaryrefslogtreecommitdiff
path: root/src/views.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/views.rs')
-rw-r--r--src/views.rs95
1 files changed, 0 insertions, 95 deletions
diff --git a/src/views.rs b/src/views.rs
deleted file mode 100644
index 4f1594a..0000000
--- a/src/views.rs
+++ /dev/null
@@ -1,95 +0,0 @@
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
50 for i in 1..=31 {
51 let day = NaiveDate::from_ymd_opt(year, month, i);
52 let day_style;
53
54 if let Some(d) = day {
55 let day_status = self.habit.get_by_date(d).unwrap_or(&false);
56 let coords = ((i % 7) * 3, i / 7 + 2);
57 let day_chr;
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}