aboutsummaryrefslogtreecommitdiff
path: root/src/views
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-03-11 12:52:07 +0000
committerAkshay <[email protected]>2020-03-11 12:52:07 +0000
commit49057cfdc6ff69114dbc5190d3ad70e26d74143c (patch)
treefc3e72b6ee74174c3c0c67d89cf700cedc68ccb2 /src/views
parent7c49624423d279ddba887a01a41a5134a1fb7b00 (diff)
flattened views module
Diffstat (limited to 'src/views')
-rw-r--r--src/views/habitview.rs137
-rw-r--r--src/views/mod.rs4
2 files changed, 0 insertions, 141 deletions
diff --git a/src/views/habitview.rs b/src/views/habitview.rs
deleted file mode 100644
index c9ca505..0000000
--- a/src/views/habitview.rs
+++ /dev/null
@@ -1,137 +0,0 @@
1use cursive::direction::Direction;
2use cursive::event::{Event, EventResult, Key};
3use cursive::theme::{BaseColor, Color, Style};
4use cursive::view::View;
5use cursive::{Printer, Vec2};
6
7use chrono::prelude::*;
8use chrono::{Local, NaiveDate};
9
10use crate::habit::{Habit, HabitTrait, HabitType, TrackEvent};
11
12use serde::Serialize;
13
14pub struct HabitView {
15 habit: Habit,
16 // characters to use
17 true_chr: char,
18 false_chr: char,
19 future_chr: char,
20 // view dimensions
21 view_width: u32,
22 view_height: u32,
23 // color config
24 reached_color: Color,
25 todo_color: Color,
26 future_color: Color,
27}
28
29impl HabitView {
30 pub fn new(habit: Habit) -> Self {
31 return HabitView {
32 habit,
33 true_chr: '·',
34 false_chr: '·',
35 future_chr: '·',
36 view_width: 25,
37 view_height: 8,
38 reached_color: Color::Dark(BaseColor::Cyan),
39 todo_color: Color::Dark(BaseColor::Magenta),
40 future_color: Color::Light(BaseColor::Black),
41 };
42 }
43 pub fn get_name(&self) -> String {
44 return self.habit.get_name().to_owned();
45 }
46 pub fn get_size(&self) -> Vec2 {
47 (self.view_width, self.view_height).into()
48 }
49 pub fn remaining(&self) -> u32 {
50 self.habit.remaining(Local::now().naive_utc().date())
51 }
52 pub fn total(&self) -> u32 {
53 self.habit.total()
54 }
55}
56
57impl View for HabitView {
58 fn draw(&self, printer: &Printer) {
59 let now = Local::now();
60 let year = now.year();
61 let month = now.month();
62
63 let goal_reached_style = Style::from(self.reached_color);
64 let todo_style = Style::from(self.todo_color);
65 let future_style = Style::from(self.future_color);
66
67 printer.with_style(
68 if !printer.focused {
69 future_style
70 } else {
71 goal_reached_style
72 },
73 |p| {
74 p.print(
75 (0, 0),
76 &format!("{:width$}", self.get_name(), width = self.get_size().x),
77 )
78 },
79 );
80
81 for i in 1..=31 {
82 let day = NaiveDate::from_ymd_opt(year, month, i);
83 let mut day_style;
84
85 if let Some(d) = day {
86 if self.habit.reached_goal(d) {
87 day_style = goal_reached_style;
88 } else {
89 day_style = todo_style;
90 }
91 let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into();
92 let day_chr: Box<dyn std::fmt::Display> = match self.habit.get_by_date(d) {
93 Some(val) => match val {
94 HabitType::Bit(b) => {
95 if *b {
96 Box::new(self.true_chr)
97 } else {
98 Box::new(self.false_chr)
99 }
100 }
101 HabitType::Count(c) => Box::new(c.to_string()),
102 },
103 None => {
104 day_style = future_style;
105 Box::new(self.future_chr)
106 }
107 };
108 printer.with_style(day_style, |p| {
109 p.print(coords, &format!("{:^3}", day_chr));
110 });
111 }
112 }
113 }
114
115 fn required_size(&mut self, _: Vec2) -> Vec2 {
116 (self.view_width, self.view_height).into()
117 }
118
119 fn take_focus(&mut self, _: Direction) -> bool {
120 true
121 }
122
123 fn on_event(&mut self, e: Event) -> EventResult {
124 let now = Local::now().naive_utc().date();
125 match e {
126 Event::Key(Key::Enter) | Event::Char('n') => {
127 self.habit.modify(now, TrackEvent::Increment);
128 return EventResult::Consumed(None);
129 }
130 Event::Key(Key::Backspace) | Event::Char('p') => {
131 self.habit.modify(now, TrackEvent::Decrement);
132 return EventResult::Consumed(None);
133 }
134 _ => return EventResult::Ignored,
135 }
136 }
137}
diff --git a/src/views/mod.rs b/src/views/mod.rs
deleted file mode 100644
index 1e3732f..0000000
--- a/src/views/mod.rs
+++ /dev/null
@@ -1,4 +0,0 @@
1// pub mod bitview;
2// pub mod countview;
3
4pub mod habitview;