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