aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-07-04 15:07:02 +0100
committerAkshay <[email protected]>2020-07-04 15:07:02 +0100
commitb1b8369fe5621ff5b2222977bb357bec15a911e2 (patch)
tree3c4668178489dd5c47b0cdb9ca7450f5b1eabe3d /src
parent1807562540c6c3c2529d88f8d81b7ceaf82b6fbb (diff)
new 'delete' command
Diffstat (limited to 'src')
-rw-r--r--src/app.rs36
-rw-r--r--src/command.rs10
-rw-r--r--src/habit.rs4
3 files changed, 45 insertions, 5 deletions
diff --git a/src/app.rs b/src/app.rs
index 4ea342f..7495dd8 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -58,12 +58,23 @@ impl App {
58 self.habits.push(h); 58 self.habits.push(h);
59 } 59 }
60 60
61 pub fn delete_by_name(&mut self, name: &str) {
62 self.habits.retain(|h| h.get_name() != name);
63 }
64
61 pub fn set_mode(&mut self, set_mode: ViewMode) { 65 pub fn set_mode(&mut self, set_mode: ViewMode) {
62 if set_mode != self.view_mode { 66 if set_mode != self.view_mode {
63 self.view_mode = set_mode; 67 self.view_mode = set_mode;
64 } 68 }
65 } 69 }
66 70
71 pub fn set_view_month_offset(&mut self, offset: u32) {
72 self.view_month_offset = offset;
73 for v in self.habits.iter_mut() {
74 v.set_view_month_offset(offset);
75 }
76 }
77
67 pub fn sift_backward(&mut self) { 78 pub fn sift_backward(&mut self) {
68 self.view_month_offset += 1; 79 self.view_month_offset += 1;
69 for v in self.habits.iter_mut() { 80 for v in self.habits.iter_mut() {
@@ -175,6 +186,10 @@ impl App {
175 self.add_habit(Box::new(Bit::new(name))); 186 self.add_habit(Box::new(Bit::new(name)));
176 } 187 }
177 } 188 }
189 Command::Delete(name) => {
190 self.delete_by_name(&name);
191 self.focus = 0;
192 }
178 Command::MonthNext => self.sift_forward(), 193 Command::MonthNext => self.sift_forward(),
179 Command::MonthPrev => self.sift_backward(), 194 Command::MonthPrev => self.sift_backward(),
180 _ => { 195 _ => {
@@ -255,8 +270,11 @@ impl View for App {
255 return EventResult::Consumed(None); 270 return EventResult::Consumed(None);
256 } 271 }
257 Event::Char('d') => { 272 Event::Char('d') => {
273 if self.habits.is_empty() {
274 return EventResult::Consumed(None);
275 }
258 self.habits.remove(self.focus); 276 self.habits.remove(self.focus);
259 self.focus = 0; 277 self.focus = self.focus.checked_sub(1).unwrap_or(0);
260 return EventResult::Consumed(None); 278 return EventResult::Consumed(None);
261 } 279 }
262 Event::Char('w') => { 280 Event::Char('w') => {
@@ -266,9 +284,13 @@ impl View for App {
266 return EventResult::Consumed(None); 284 return EventResult::Consumed(None);
267 } 285 }
268 Event::Char('q') => { 286 Event::Char('q') => {
269 self.save_state(); 287 // self.save_state();
270 return EventResult::with_cb(|s| s.quit()); 288 return EventResult::with_cb(|s| s.quit());
271 } 289 }
290
291 /* We want sifting to be an app level function,
292 * that later trickles down into each habit
293 * */
272 Event::CtrlChar('f') => { 294 Event::CtrlChar('f') => {
273 self.sift_forward(); 295 self.sift_forward();
274 return EventResult::Consumed(None); 296 return EventResult::Consumed(None);
@@ -277,7 +299,15 @@ impl View for App {
277 self.sift_backward(); 299 self.sift_backward();
278 return EventResult::Consumed(None); 300 return EventResult::Consumed(None);
279 } 301 }
280 _ => self.habits[self.focus].on_event(e), 302
303 /* Every keybind that is not caught by App trickle
304 * s down to the focused Habit We sift back to today
305 * before performing any action, "refocusing" the cursor
306 * */
307 _ => {
308 self.set_view_month_offset(0);
309 self.habits[self.focus].on_event(e)
310 }
281 } 311 }
282 } 312 }
283} 313}
diff --git a/src/command.rs b/src/command.rs
index c4f20fc..f94eab8 100644
--- a/src/command.rs
+++ b/src/command.rs
@@ -17,10 +17,10 @@ fn call_on_app(s: &mut Cursive, input: &str) {
17} 17}
18 18
19pub enum Command { 19pub enum Command {
20 Add(String, String, Option<u32>), 20 Add(String, String, Option<u32>), // habit name, habit type, optional goal
21 MonthPrev, 21 MonthPrev,
22 MonthNext, 22 MonthNext,
23 Delete, 23 Delete(String),
24 Blank, 24 Blank,
25} 25}
26 26
@@ -45,6 +45,12 @@ impl Command {
45 goal, 45 goal,
46 ); 46 );
47 } 47 }
48 "delete" | "d" => {
49 if args.len() < 1 {
50 return Command::Blank;
51 }
52 return Command::Delete(args[0].to_string());
53 }
48 "mprev" | "month-prev" => return Command::MonthPrev, 54 "mprev" | "month-prev" => return Command::MonthPrev,
49 "mnext" | "month-next" => return Command::MonthNext, 55 "mnext" | "month-next" => return Command::MonthNext,
50 _ => return Command::Blank, 56 _ => return Command::Blank,
diff --git a/src/habit.rs b/src/habit.rs
index 9872bbc..48dd363 100644
--- a/src/habit.rs
+++ b/src/habit.rs
@@ -66,6 +66,7 @@ pub trait HabitWrapper: erased_serde::Serialize {
66 fn take_focus(&mut self, _: Direction) -> bool; 66 fn take_focus(&mut self, _: Direction) -> bool;
67 fn set_view_month_offset(&mut self, offset: u32); 67 fn set_view_month_offset(&mut self, offset: u32);
68 fn view_month_offset(&self) -> u32; 68 fn view_month_offset(&self) -> u32;
69 fn get_name(&self) -> String;
69} 70}
70 71
71macro_rules! auto_habit_impl { 72macro_rules! auto_habit_impl {
@@ -99,6 +100,9 @@ macro_rules! auto_habit_impl {
99 fn view_month_offset(&self) -> u32 { 100 fn view_month_offset(&self) -> u32 {
100 Habit::view_month_offset(self) 101 Habit::view_month_offset(self)
101 } 102 }
103 fn get_name(&self) -> String {
104 Habit::name(self)
105 }
102 } 106 }
103 }; 107 };
104} 108}