diff options
author | Akshay <[email protected]> | 2020-07-12 08:03:58 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-07-12 08:03:58 +0100 |
commit | e670dab4826006dda1bccdd05d99a61b4fe2b900 (patch) | |
tree | c4dc48c77b3ac708303e032c08f62cfc4448147f /src/views.rs | |
parent | bc81cf4d3f0847bdf64974ad91f60c293b4f6a77 (diff) |
add new mode: week view mode
accessible via 'v' (single habit week mode toggle) and 'V' (bulk week
mode) from any mode, press 'esc' to go back to day mode (this is
probably the 'normal' mode of dijo)
Diffstat (limited to 'src/views.rs')
-rw-r--r-- | src/views.rs | 85 |
1 files changed, 56 insertions, 29 deletions
diff --git a/src/views.rs b/src/views.rs index 3e623a9..f5ba01b 100644 --- a/src/views.rs +++ b/src/views.rs | |||
@@ -7,7 +7,7 @@ use cursive::{Printer, Vec2}; | |||
7 | use chrono::prelude::*; | 7 | use chrono::prelude::*; |
8 | use chrono::{Duration, Local, NaiveDate}; | 8 | use chrono::{Duration, Local, NaiveDate}; |
9 | 9 | ||
10 | use crate::habit::{Bit, Count, Habit, TrackEvent}; | 10 | use crate::habit::{Bit, Count, Habit, TrackEvent, ViewMode}; |
11 | 11 | ||
12 | use crate::CONFIGURATION; | 12 | use crate::CONFIGURATION; |
13 | 13 | ||
@@ -63,37 +63,64 @@ where | |||
63 | }, | 63 | }, |
64 | ); | 64 | ); |
65 | 65 | ||
66 | // fn draw_day(&self, p: &Printer) { }; | 66 | let draw_month = |printer: &Printer| { |
67 | // fn draw_month(&self, p: &Printer) {}; | 67 | let days = (1..31) |
68 | 68 | .map(|i| NaiveDate::from_ymd_opt(year, month, i)) | |
69 | // match self.view_mode() { | 69 | .flatten() // dates 28-31 may not exist, ignore them if they don't |
70 | // ViewMode::Day => draw_day(self, p), | 70 | .collect::<Vec<_>>(); |
71 | // ViewMode::Month => | 71 | for (week, line_nr) in days.chunks(7).zip(2..) { |
72 | // } | 72 | let weekly_goal = self.goal() * week.len() as u32; |
73 | 73 | let is_this_week = week.contains(&Local::now().naive_utc().date()); | |
74 | //let draw_day = |printer: &Printer| { | 74 | let remaining = week.iter().map(|&i| self.remaining(i)).sum::<u32>(); |
75 | let mut i = 1; | 75 | let completions = weekly_goal - remaining; |
76 | while let Some(d) = NaiveDate::from_ymd_opt(year, month, i) { | 76 | let full = CONFIGURATION.view_width - 8; |
77 | let day_style; | 77 | let bars_to_fill = (completions * full as u32) / weekly_goal; |
78 | if self.reached_goal(d) { | 78 | let percentage = (completions as f64 * 100f64) / weekly_goal as f64; |
79 | day_style = goal_reached_style; | ||
80 | } else { | ||
81 | day_style = todo_style; | ||
82 | } | ||
83 | let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into(); | ||
84 | if let Some(c) = self.get_by_date(d) { | ||
85 | printer.with_style(day_style, |p| { | ||
86 | p.print(coords, &format!("{:^3}", c)); | ||
87 | }); | ||
88 | } else { | ||
89 | printer.with_style(future_style, |p| { | 79 | printer.with_style(future_style, |p| { |
90 | p.print(coords, &format!("{:^3}", CONFIGURATION.future_chr)); | 80 | p.print((4, line_nr), &"―".repeat(full)); |
81 | }); | ||
82 | printer.with_style(goal_reached_style, |p| { | ||
83 | p.print((4, line_nr), &"―".repeat(bars_to_fill as usize)); | ||
91 | }); | 84 | }); |
85 | printer.with_style( | ||
86 | if is_this_week { | ||
87 | Style::none() | ||
88 | } else { | ||
89 | future_style | ||
90 | }, | ||
91 | |p| { | ||
92 | p.print((0, line_nr), &format!("{:3.0}% ", percentage)); | ||
93 | }, | ||
94 | ); | ||
92 | } | 95 | } |
93 | i += 1; | 96 | }; |
94 | } | 97 | let draw_day = |printer: &Printer| { |
95 | //}; | 98 | let mut i = 1; |
96 | //draw_day(printer); | 99 | while let Some(d) = NaiveDate::from_ymd_opt(year, month, i) { |
100 | let day_style; | ||
101 | if self.reached_goal(d) { | ||
102 | day_style = goal_reached_style; | ||
103 | } else { | ||
104 | day_style = todo_style; | ||
105 | } | ||
106 | let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into(); | ||
107 | if let Some(c) = self.get_by_date(d) { | ||
108 | printer.with_style(day_style, |p| { | ||
109 | p.print(coords, &format!("{:^3}", c)); | ||
110 | }); | ||
111 | } else { | ||
112 | printer.with_style(future_style, |p| { | ||
113 | p.print(coords, &format!("{:^3}", CONFIGURATION.future_chr)); | ||
114 | }); | ||
115 | } | ||
116 | i += 1; | ||
117 | } | ||
118 | }; | ||
119 | match self.view_mode() { | ||
120 | ViewMode::Day => draw_day(printer), | ||
121 | ViewMode::Month => draw_month(printer), | ||
122 | _ => draw_day(printer), | ||
123 | }; | ||
97 | } | 124 | } |
98 | 125 | ||
99 | fn required_size(&mut self, _: Vec2) -> Vec2 { | 126 | fn required_size(&mut self, _: Vec2) -> Vec2 { |