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