aboutsummaryrefslogtreecommitdiff
path: root/src/views
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-02-10 17:15:06 +0000
committerAkshay <[email protected]>2020-02-10 17:15:06 +0000
commitecc2e2b296c4241b56d201012d7a7de5f96007db (patch)
tree48a6381ff9e8041ebc9881cd8ce8037186a8727c /src/views
parent5019b6f52e0ff6649d318d8e600e1cd52fb01c7f (diff)
factor out count and bit views
Diffstat (limited to 'src/views')
-rw-r--r--src/views/bitview.rs97
-rw-r--r--src/views/countview.rs93
-rw-r--r--src/views/mod.rs2
3 files changed, 192 insertions, 0 deletions
diff --git a/src/views/bitview.rs b/src/views/bitview.rs
new file mode 100644
index 0000000..16b3ad8
--- /dev/null
+++ b/src/views/bitview.rs
@@ -0,0 +1,97 @@
1use cursive::direction::Direction;
2use cursive::event::{Event, EventResult, Key};
3use cursive::theme::{BaseColor, Color, Effect, Style};
4use cursive::utils::markup::StyledString;
5use cursive::view::View;
6use cursive::{Printer, Vec2};
7
8use chrono::prelude::*;
9use chrono::{Local, NaiveDate};
10
11use crate::habit::Habit;
12
13pub struct BitView {
14 habit: Habit<bool>,
15 true_chr: char,
16 false_chr: char,
17 future_chr: char,
18
19 view_width: u32,
20 view_height: u32,
21 // color config
22}
23
24impl BitView {
25 pub fn new(habit: Habit<bool>) -> Self {
26 return BitView {
27 habit,
28 true_chr: '·',
29 false_chr: '·',
30 future_chr: '·',
31 view_width: 21,
32 view_height: 9,
33 };
34 }
35 pub fn get_title(&self) -> String {
36 return self.habit.get_name().to_owned();
37 }
38}
39
40impl View for BitView {
41 fn draw(&self, printer: &Printer) {
42 let now = Local::now();
43 let year = now.year();
44 let month = now.month();
45
46 let true_style = Style::from(Color::Dark(BaseColor::Cyan));
47 let false_style = Style::from(Color::Dark(BaseColor::Magenta));
48 let future_style = Style::from(Color::Light(BaseColor::Black));
49 let today_style = Style::from(Color::Dark(BaseColor::White));
50
51 for i in 1..=31 {
52 let day = NaiveDate::from_ymd_opt(year, month, i);
53 let day_style;
54
55 if let Some(d) = day {
56 let day_status = self.habit.get_by_date(d).unwrap_or(&false);
57 let coords = ((i % 7) * 3, i / 7 + 2);
58 let day_chr;
59
60 if d <= now.naive_utc().date() {
61 if *day_status {
62 day_chr = self.true_chr;
63 day_style = true_style;
64 } else {
65 day_chr = self.false_chr;
66 day_style = false_style;
67 }
68 } else {
69 day_chr = self.future_chr;
70 day_style = future_style;
71 }
72
73 printer.with_style(day_style, |p| {
74 p.print(coords, &format!("{:^3}", day_chr));
75 });
76 }
77 }
78 }
79
80 fn required_size(&mut self, _: Vec2) -> Vec2 {
81 (self.view_width, self.view_height).into()
82 }
83
84 fn take_focus(&mut self, _: Direction) -> bool {
85 true
86 }
87
88 fn on_event(&mut self, e: Event) -> EventResult {
89 match e {
90 Event::Key(Key::Enter) => {
91 self.habit.toggle(Local::now().naive_utc().date());
92 return EventResult::Consumed(None);
93 }
94 _ => return EventResult::Ignored,
95 }
96 }
97}
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 @@
1use cursive::direction::Direction;
2use cursive::event::{Event, EventResult, Key};
3use cursive::theme::{BaseColor, Color, Effect, Style};
4use cursive::utils::markup::StyledString;
5use cursive::view::View;
6use cursive::{Printer, Vec2};
7
8use chrono::prelude::*;
9use chrono::{Local, NaiveDate};
10
11use crate::habit::Habit;
12
13pub struct CountView {
14 habit: Habit<u32>,
15 future_chr: char,
16
17 view_width: u32,
18 view_height: u32,
19 // color config
20}
21
22impl 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
36impl 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}
diff --git a/src/views/mod.rs b/src/views/mod.rs
new file mode 100644
index 0000000..e1e8ca5
--- /dev/null
+++ b/src/views/mod.rs
@@ -0,0 +1,2 @@
1pub mod bitview;
2pub mod countview;