aboutsummaryrefslogtreecommitdiff
path: root/src
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
parent5019b6f52e0ff6649d318d8e600e1cd52fb01c7f (diff)
factor out count and bit views
Diffstat (limited to 'src')
-rw-r--r--src/views/bitview.rs (renamed from src/views.rs)2
-rw-r--r--src/views/countview.rs93
-rw-r--r--src/views/mod.rs2
3 files changed, 97 insertions, 0 deletions
diff --git a/src/views.rs b/src/views/bitview.rs
index 4f1594a..16b3ad8 100644
--- a/src/views.rs
+++ b/src/views/bitview.rs
@@ -46,6 +46,7 @@ impl View for BitView {
46 let true_style = Style::from(Color::Dark(BaseColor::Cyan)); 46 let true_style = Style::from(Color::Dark(BaseColor::Cyan));
47 let false_style = Style::from(Color::Dark(BaseColor::Magenta)); 47 let false_style = Style::from(Color::Dark(BaseColor::Magenta));
48 let future_style = Style::from(Color::Light(BaseColor::Black)); 48 let future_style = Style::from(Color::Light(BaseColor::Black));
49 let today_style = Style::from(Color::Dark(BaseColor::White));
49 50
50 for i in 1..=31 { 51 for i in 1..=31 {
51 let day = NaiveDate::from_ymd_opt(year, month, i); 52 let day = NaiveDate::from_ymd_opt(year, month, i);
@@ -55,6 +56,7 @@ impl View for BitView {
55 let day_status = self.habit.get_by_date(d).unwrap_or(&false); 56 let day_status = self.habit.get_by_date(d).unwrap_or(&false);
56 let coords = ((i % 7) * 3, i / 7 + 2); 57 let coords = ((i % 7) * 3, i / 7 + 2);
57 let day_chr; 58 let day_chr;
59
58 if d <= now.naive_utc().date() { 60 if d <= now.naive_utc().date() {
59 if *day_status { 61 if *day_status {
60 day_chr = self.true_chr; 62 day_chr = self.true_chr;
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;