diff options
Diffstat (limited to 'src/views/habitview.rs')
-rw-r--r-- | src/views/habitview.rs | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/src/views/habitview.rs b/src/views/habitview.rs new file mode 100644 index 0000000..3ae0c90 --- /dev/null +++ b/src/views/habitview.rs | |||
@@ -0,0 +1,112 @@ | |||
1 | use cursive::direction::Direction; | ||
2 | use cursive::event::{Event, EventResult, Key}; | ||
3 | use cursive::theme::{BaseColor, Color, 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::{Habit, HabitTrait, HabitType, TrackEvent}; | ||
11 | |||
12 | pub struct HabitView { | ||
13 | habit: Habit, | ||
14 | // characters to use | ||
15 | true_chr: char, | ||
16 | false_chr: char, | ||
17 | future_chr: char, | ||
18 | // view dimensions | ||
19 | view_width: u32, | ||
20 | view_height: u32, | ||
21 | // color config | ||
22 | reached_color: Color, | ||
23 | todo_color: Color, | ||
24 | future_color: Color, | ||
25 | } | ||
26 | |||
27 | impl HabitView { | ||
28 | pub fn new(habit: Habit) -> Self { | ||
29 | return HabitView { | ||
30 | habit, | ||
31 | true_chr: '·', | ||
32 | false_chr: '·', | ||
33 | future_chr: '·', | ||
34 | view_width: 21, | ||
35 | view_height: 9, | ||
36 | reached_color: Color::Dark(BaseColor::Cyan), | ||
37 | todo_color: Color::Dark(BaseColor::Magenta), | ||
38 | future_color: Color::Light(BaseColor::Black), | ||
39 | }; | ||
40 | } | ||
41 | pub fn get_title(&self) -> String { | ||
42 | return self.habit.get_name().to_owned(); | ||
43 | } | ||
44 | } | ||
45 | |||
46 | impl View for HabitView { | ||
47 | fn draw(&self, printer: &Printer) { | ||
48 | let now = Local::now(); | ||
49 | let year = now.year(); | ||
50 | let month = now.month(); | ||
51 | |||
52 | let goal_reached_style = Style::from(self.reached_color); | ||
53 | let todo_style = Style::from(self.todo_color); | ||
54 | let future_style = Style::from(self.future_color); | ||
55 | |||
56 | for i in 1..=31 { | ||
57 | let day = NaiveDate::from_ymd_opt(year, month, i); | ||
58 | let mut day_style; | ||
59 | |||
60 | if let Some(d) = day { | ||
61 | if self.habit.reached_goal(d) { | ||
62 | day_style = goal_reached_style; | ||
63 | } else { | ||
64 | day_style = todo_style; | ||
65 | } | ||
66 | let coords = ((i % 7) * 3, i / 7 + 2); | ||
67 | let day_chr: Box<dyn std::fmt::Display> = match self.habit.get_by_date(d) { | ||
68 | Some(val) => match val { | ||
69 | HabitType::Bit(b) => { | ||
70 | if *b { | ||
71 | Box::new(self.true_chr) | ||
72 | } else { | ||
73 | Box::new(self.false_chr) | ||
74 | } | ||
75 | } | ||
76 | HabitType::Count(c) => Box::new(c.to_string()), | ||
77 | }, | ||
78 | None => { | ||
79 | day_style = future_style; | ||
80 | Box::new(self.future_chr) | ||
81 | } | ||
82 | }; | ||
83 | printer.with_style(day_style, |p| { | ||
84 | p.print(coords, &format!("{:^3}", day_chr)); | ||
85 | }); | ||
86 | } | ||
87 | } | ||
88 | } | ||
89 | |||
90 | fn required_size(&mut self, _: Vec2) -> Vec2 { | ||
91 | (self.view_width, self.view_height).into() | ||
92 | } | ||
93 | |||
94 | fn take_focus(&mut self, _: Direction) -> bool { | ||
95 | true | ||
96 | } | ||
97 | |||
98 | fn on_event(&mut self, e: Event) -> EventResult { | ||
99 | let now = Local::now().naive_utc().date(); | ||
100 | match e { | ||
101 | Event::Key(Key::Enter) | Event::Char('n') => { | ||
102 | self.habit.modify(now, TrackEvent::Increment); | ||
103 | return EventResult::Consumed(None); | ||
104 | } | ||
105 | Event::Key(Key::Backspace) | Event::Char('p') => { | ||
106 | self.habit.modify(now, TrackEvent::Decrement); | ||
107 | return EventResult::Consumed(None); | ||
108 | } | ||
109 | _ => return EventResult::Ignored, | ||
110 | } | ||
111 | } | ||
112 | } | ||