diff options
author | Akshay <[email protected]> | 2020-07-16 17:16:26 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-07-16 17:16:26 +0100 |
commit | 7d5e46626110a351104ededeaebdcb9723c7e786 (patch) | |
tree | 8d8aa411e0d70120ba6d8e8010de0aef3ddaec9a /src | |
parent | 10e768b6e6255fda9f6f2a4aaa3c23bb2d829eb1 (diff) |
add track commands
Diffstat (limited to 'src')
-rw-r--r-- | src/app.rs | 33 | ||||
-rw-r--r-- | src/command.rs | 17 |
2 files changed, 40 insertions, 10 deletions
@@ -11,7 +11,7 @@ use cursive::{Printer, Vec2}; | |||
11 | 11 | ||
12 | use chrono::Local; | 12 | use chrono::Local; |
13 | 13 | ||
14 | use crate::habit::{Bit, Count, HabitWrapper, ViewMode}; | 14 | use crate::habit::{Bit, Count, HabitWrapper, TrackEvent, ViewMode}; |
15 | use crate::utils; | 15 | use crate::utils; |
16 | use crate::Command; | 16 | use crate::Command; |
17 | use crate::CONFIGURATION; | 17 | use crate::CONFIGURATION; |
@@ -46,7 +46,7 @@ impl App { | |||
46 | } | 46 | } |
47 | 47 | ||
48 | pub fn delete_by_name(&mut self, name: &str) { | 48 | pub fn delete_by_name(&mut self, name: &str) { |
49 | self.habits.retain(|h| h.get_name() != name); | 49 | self.habits.retain(|h| h.name() != name); |
50 | } | 50 | } |
51 | 51 | ||
52 | pub fn get_mode(&self) -> ViewMode { | 52 | pub fn get_mode(&self) -> ViewMode { |
@@ -184,7 +184,7 @@ impl App { | |||
184 | 184 | ||
185 | // this function does IO | 185 | // this function does IO |
186 | // TODO: convert this into non-blocking async function | 186 | // TODO: convert this into non-blocking async function |
187 | fn save_state(&self) { | 187 | pub fn save_state(&self) { |
188 | let (regular, auto): (Vec<_>, Vec<_>) = self.habits.iter().partition(|&x| !x.is_auto()); | 188 | let (regular, auto): (Vec<_>, Vec<_>) = self.habits.iter().partition(|&x| !x.is_auto()); |
189 | let (regular_f, auto_f) = (utils::habit_file(), utils::auto_habit_file()); | 189 | let (regular_f, auto_f) = (utils::habit_file(), utils::auto_habit_file()); |
190 | 190 | ||
@@ -205,8 +205,7 @@ impl App { | |||
205 | write_to_file(auto, auto_f); | 205 | write_to_file(auto, auto_f); |
206 | } | 206 | } |
207 | 207 | ||
208 | pub fn parse_command(&mut self, input: &str) { | 208 | pub fn parse_command(&mut self, c: Command) { |
209 | let c = Command::from_string(input); | ||
210 | match c { | 209 | match c { |
211 | Command::Add(name, goal, auto) => { | 210 | Command::Add(name, goal, auto) => { |
212 | let kind = if goal == Some(1) { "bit" } else { "count" }; | 211 | let kind = if goal == Some(1) { "bit" } else { "count" }; |
@@ -224,6 +223,24 @@ impl App { | |||
224 | self.delete_by_name(&name); | 223 | self.delete_by_name(&name); |
225 | self.focus = 0; | 224 | self.focus = 0; |
226 | } | 225 | } |
226 | Command::TrackUp(name) => { | ||
227 | let target_habit = self | ||
228 | .habits | ||
229 | .iter_mut() | ||
230 | .find(|x| x.name() == name && x.is_auto()); | ||
231 | if let Some(h) = target_habit { | ||
232 | h.modify(Local::now().naive_utc().date(), TrackEvent::Increment); | ||
233 | } | ||
234 | } | ||
235 | Command::TrackDown(name) => { | ||
236 | let target_habit = self | ||
237 | .habits | ||
238 | .iter_mut() | ||
239 | .find(|x| x.name() == name && x.is_auto()); | ||
240 | if let Some(h) = target_habit { | ||
241 | h.modify(Local::now().naive_utc().date(), TrackEvent::Decrement); | ||
242 | } | ||
243 | } | ||
227 | Command::Quit => self.save_state(), | 244 | Command::Quit => self.save_state(), |
228 | Command::MonthNext => self.sift_forward(), | 245 | Command::MonthNext => self.sift_forward(), |
229 | Command::MonthPrev => self.sift_backward(), | 246 | Command::MonthPrev => self.sift_backward(), |
@@ -253,10 +270,8 @@ impl View for App { | |||
253 | let status = self.status(); | 270 | let status = self.status(); |
254 | printer.print(offset, &status.0); // left status | 271 | printer.print(offset, &status.0); // left status |
255 | 272 | ||
256 | let full = grid_width * (view_width + 2); | 273 | let full = self.max_size().x; |
257 | offset = offset | 274 | offset = offset.map_x(|_| full - status.1.len()); |
258 | .map_x(|_| full - status.1.len()) | ||
259 | .map_y(|_| self.max_size().y - 2); | ||
260 | printer.print(offset, &status.1); // right status | 275 | printer.print(offset, &status.1); // right status |
261 | } | 276 | } |
262 | 277 | ||
diff --git a/src/command.rs b/src/command.rs index ae1b307..79d0fe5 100644 --- a/src/command.rs +++ b/src/command.rs | |||
@@ -11,7 +11,8 @@ pub fn open_command_window(s: &mut Cursive) { | |||
11 | 11 | ||
12 | fn call_on_app(s: &mut Cursive, input: &str) { | 12 | fn call_on_app(s: &mut Cursive, input: &str) { |
13 | s.call_on_name("Main", |view: &mut App| { | 13 | s.call_on_name("Main", |view: &mut App| { |
14 | view.parse_command(input); | 14 | let cmd = Command::from_string(input); |
15 | view.parse_command(cmd); | ||
15 | }); | 16 | }); |
16 | 17 | ||
17 | // special command that requires access to | 18 | // special command that requires access to |
@@ -31,6 +32,8 @@ pub enum Command { | |||
31 | MonthPrev, | 32 | MonthPrev, |
32 | MonthNext, | 33 | MonthNext, |
33 | Delete(String), | 34 | Delete(String), |
35 | TrackUp(String), | ||
36 | TrackDown(String), | ||
34 | Quit, | 37 | Quit, |
35 | Blank, | 38 | Blank, |
36 | } | 39 | } |
@@ -59,6 +62,18 @@ impl Command { | |||
59 | } | 62 | } |
60 | return Command::Delete(args[0].to_string()); | 63 | return Command::Delete(args[0].to_string()); |
61 | } | 64 | } |
65 | "track-up" | "tup" => { | ||
66 | if args.len() < 1 { | ||
67 | return Command::Blank; | ||
68 | } | ||
69 | return Command::TrackUp(args[0].to_string()); | ||
70 | } | ||
71 | "track-down" | "tdown" => { | ||
72 | if args.len() < 1 { | ||
73 | return Command::Blank; | ||
74 | } | ||
75 | return Command::TrackDown(args[0].to_string()); | ||
76 | } | ||
62 | "mprev" | "month-prev" => return Command::MonthPrev, | 77 | "mprev" | "month-prev" => return Command::MonthPrev, |
63 | "mnext" | "month-next" => return Command::MonthNext, | 78 | "mnext" | "month-next" => return Command::MonthNext, |
64 | "q" | "quit" => return Command::Quit, | 79 | "q" | "quit" => return Command::Quit, |