diff options
Diffstat (limited to 'src/views.rs')
-rw-r--r-- | src/views.rs | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/src/views.rs b/src/views.rs new file mode 100644 index 0000000..ebb83b8 --- /dev/null +++ b/src/views.rs | |||
@@ -0,0 +1,125 @@ | |||
1 | use cursive::direction::Direction; | ||
2 | use cursive::event::{Event, EventResult, Key}; | ||
3 | use cursive::theme::Style; | ||
4 | use cursive::view::View; | ||
5 | use cursive::{Printer, Vec2}; | ||
6 | |||
7 | use chrono::prelude::*; | ||
8 | use chrono::{Local, NaiveDate}; | ||
9 | |||
10 | use crate::habit::{Bit, Count, Habit, TrackEvent}; | ||
11 | use crate::CONFIGURATION; | ||
12 | |||
13 | pub trait ShadowView { | ||
14 | fn draw(&self, printer: &Printer); | ||
15 | fn required_size(&mut self, _: Vec2) -> Vec2; | ||
16 | fn take_focus(&mut self, _: Direction) -> bool; | ||
17 | fn on_event(&mut self, e: Event) -> EventResult; | ||
18 | } | ||
19 | |||
20 | // the only way to not rewrite each View implementation for trait | ||
21 | // objects of Habit is to rewrite the View trait itself. | ||
22 | impl<T> ShadowView for T | ||
23 | where | ||
24 | T: Habit, | ||
25 | T::HabitType: std::fmt::Display, | ||
26 | { | ||
27 | fn draw(&self, printer: &Printer) { | ||
28 | let now = Local::now(); | ||
29 | let year = now.year(); | ||
30 | let month = now.month(); | ||
31 | |||
32 | let goal_reached_style = Style::from(CONFIGURATION.reached_color); | ||
33 | let todo_style = Style::from(CONFIGURATION.todo_color); | ||
34 | let future_style = Style::from(CONFIGURATION.future_color); | ||
35 | |||
36 | printer.with_style( | ||
37 | if !printer.focused { | ||
38 | future_style | ||
39 | } else { | ||
40 | goal_reached_style | ||
41 | }, | ||
42 | |p| { | ||
43 | p.print( | ||
44 | (0, 0), | ||
45 | &format!("{:width$}", self.name(), width = CONFIGURATION.view_width), | ||
46 | ) | ||
47 | }, | ||
48 | ); | ||
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 | if self.reached_goal(d) { | ||
56 | day_style = goal_reached_style; | ||
57 | } else { | ||
58 | day_style = todo_style; | ||
59 | } | ||
60 | let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into(); | ||
61 | if let Some(c) = self.get_by_date(d) { | ||
62 | printer.with_style(day_style, |p| { | ||
63 | p.print(coords, &format!("{:^3}", c)); | ||
64 | }); | ||
65 | } else { | ||
66 | printer.with_style(future_style, |p| { | ||
67 | p.print(coords, &format!("{:^3}", CONFIGURATION.future_chr)); | ||
68 | }); | ||
69 | } | ||
70 | //printer.with_style(day_style, |p| { | ||
71 | // p.print(coords, &format!("{:^3}", c)); | ||
72 | // } else { | ||
73 | // p.print(coords, &format!("{:^3}", CONFIGURATION.future_chr)); | ||
74 | // } | ||
75 | //}); | ||
76 | } | ||
77 | } | ||
78 | } | ||
79 | fn required_size(&mut self, _: Vec2) -> Vec2 { | ||
80 | (25, 6).into() | ||
81 | } | ||
82 | |||
83 | fn take_focus(&mut self, _: Direction) -> bool { | ||
84 | true | ||
85 | } | ||
86 | |||
87 | fn on_event(&mut self, e: Event) -> EventResult { | ||
88 | let now = Local::now().naive_utc().date(); | ||
89 | match e { | ||
90 | Event::Key(Key::Enter) | Event::Char('n') => { | ||
91 | self.modify(now, TrackEvent::Increment); | ||
92 | return EventResult::Consumed(None); | ||
93 | } | ||
94 | Event::Key(Key::Backspace) | Event::Char('p') => { | ||
95 | self.modify(now, TrackEvent::Decrement); | ||
96 | return EventResult::Consumed(None); | ||
97 | } | ||
98 | _ => return EventResult::Ignored, | ||
99 | } | ||
100 | } | ||
101 | } | ||
102 | |||
103 | macro_rules! auto_view_impl { | ||
104 | ($struct_name:ident) => { | ||
105 | impl View for $struct_name { | ||
106 | fn draw(&self, printer: &Printer) { | ||
107 | ShadowView::draw(self, printer); | ||
108 | } | ||
109 | fn required_size(&mut self, x: Vec2) -> Vec2 { | ||
110 | ShadowView::required_size(self, x) | ||
111 | } | ||
112 | |||
113 | fn take_focus(&mut self, d: Direction) -> bool { | ||
114 | ShadowView::take_focus(self, d) | ||
115 | } | ||
116 | |||
117 | fn on_event(&mut self, e: Event) -> EventResult { | ||
118 | ShadowView::on_event(self, e) | ||
119 | } | ||
120 | } | ||
121 | }; | ||
122 | } | ||
123 | |||
124 | auto_view_impl!(Count); | ||
125 | auto_view_impl!(Bit); | ||