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 | |
parent | 13af935178655b3ea3a8b631931a4bd68934d4af (diff) |
rework views
Diffstat (limited to 'src/views')
-rw-r--r-- | src/views/bitview.rs | 95 | ||||
-rw-r--r-- | src/views/countview.rs | 92 | ||||
-rw-r--r-- | src/views/habitview.rs | 112 | ||||
-rw-r--r-- | src/views/mod.rs | 6 |
4 files changed, 116 insertions, 189 deletions
diff --git a/src/views/bitview.rs b/src/views/bitview.rs deleted file mode 100644 index b31b050..0000000 --- a/src/views/bitview.rs +++ /dev/null | |||
@@ -1,95 +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 BitView { | ||
13 | habit: Habit<bool>, | ||
14 | true_chr: char, | ||
15 | false_chr: char, | ||
16 | future_chr: char, | ||
17 | |||
18 | view_width: u32, | ||
19 | view_height: u32, | ||
20 | // color config | ||
21 | } | ||
22 | |||
23 | impl BitView { | ||
24 | pub fn new(habit: Habit<bool>) -> Self { | ||
25 | return BitView { | ||
26 | habit, | ||
27 | true_chr: '·', | ||
28 | false_chr: '·', | ||
29 | future_chr: '·', | ||
30 | view_width: 21, | ||
31 | view_height: 9, | ||
32 | }; | ||
33 | } | ||
34 | pub fn get_title(&self) -> String { | ||
35 | return self.habit.get_name().to_owned(); | ||
36 | } | ||
37 | } | ||
38 | |||
39 | impl View for BitView { | ||
40 | fn draw(&self, printer: &Printer) { | ||
41 | let now = Local::now(); | ||
42 | let year = now.year(); | ||
43 | let month = now.month(); | ||
44 | |||
45 | let true_style = Style::from(Color::Dark(BaseColor::Cyan)); | ||
46 | let false_style = Style::from(Color::Dark(BaseColor::Magenta)); | ||
47 | let future_style = Style::from(Color::Light(BaseColor::Black)); | ||
48 | |||
49 | for i in 1..=31 { | ||
50 | let day = NaiveDate::from_ymd_opt(year, month, i); | ||
51 | let day_style; | ||
52 | |||
53 | if let Some(d) = day { | ||
54 | let day_status = self.habit.get_by_date(d).unwrap_or(&false); | ||
55 | let coords = ((i % 7) * 3, i / 7 + 2); | ||
56 | let day_chr; | ||
57 | |||
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 | } | ||
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 | } | ||
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 | } | ||
diff --git a/src/views/mod.rs b/src/views/mod.rs index e1e8ca5..1e3732f 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs | |||
@@ -1,2 +1,4 @@ | |||
1 | pub mod bitview; | 1 | // pub mod bitview; |
2 | pub mod countview; | 2 | // pub mod countview; |
3 | |||
4 | pub mod habitview; | ||