summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAkshay <nerdy@peppe.rs>2020-07-04 19:37:02 +0530
committerAkshay <nerdy@peppe.rs>2020-07-04 19:37:02 +0530
commitb1b8369fe5621ff5b2222977bb357bec15a911e2 (patch)
tree3c4668178489dd5c47b0cdb9ca7450f5b1eabe3d
parent1807562540c6c3c2529d88f8d81b7ceaf82b6fbb (diff)
new 'delete' command
-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 {
self.habits.push(h);
}
+ pub fn delete_by_name(&mut self, name: &str) {
+ self.habits.retain(|h| h.get_name() != name);
+ }
+
pub fn set_mode(&mut self, set_mode: ViewMode) {
if set_mode != self.view_mode {
self.view_mode = set_mode;
}
}
+ pub fn set_view_month_offset(&mut self, offset: u32) {
+ self.view_month_offset = offset;
+ for v in self.habits.iter_mut() {
+ v.set_view_month_offset(offset);
+ }
+ }
+
pub fn sift_backward(&mut self) {
self.view_month_offset += 1;
for v in self.habits.iter_mut() {
@@ -175,6 +186,10 @@ impl App {
self.add_habit(Box::new(Bit::new(name)));
}
}
+ Command::Delete(name) => {
+ self.delete_by_name(&name);
+ self.focus = 0;
+ }
Command::MonthNext => self.sift_forward(),
Command::MonthPrev => self.sift_backward(),
_ => {
@@ -255,8 +270,11 @@ impl View for App {
return EventResult::Consumed(None);
}
Event::Char('d') => {
+ if self.habits.is_empty() {
+ return EventResult::Consumed(None);
+ }
self.habits.remove(self.focus);
- self.focus = 0;
+ self.focus = self.focus.checked_sub(1).unwrap_or(0);
return EventResult::Consumed(None);
}
Event::Char('w') => {
@@ -266,9 +284,13 @@ impl View for App {
return EventResult::Consumed(None);
}
Event::Char('q') => {
- self.save_state();
+ // self.save_state();
return EventResult::with_cb(|s| s.quit());
}
+
+ /* We want sifting to be an app level function,
+ * that later trickles down into each habit
+ * */
Event::CtrlChar('f') => {
self.sift_forward();
return EventResult::Consumed(None);
@@ -277,7 +299,15 @@ impl View for App {
self.sift_backward();
return EventResult::Consumed(None);
}
- _ => self.habits[self.focus].on_event(e),
+
+ /* Every keybind that is not caught by App trickle
+ * s down to the focused Habit We sift back to today
+ * before performing any action, "refocusing" the cursor
+ * */
+ _ => {
+ self.set_view_month_offset(0);
+ self.habits[self.focus].on_event(e)
+ }
}
}
}
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) {
}
pub enum Command {
- Add(String, String, Option<u32>),
+ Add(String, String, Option<u32>), // habit name, habit type, optional goal
MonthPrev,
MonthNext,
- Delete,
+ Delete(String),
Blank,
}
@@ -45,6 +45,12 @@ impl Command {
goal,
);
}
+ "delete" | "d" => {
+ if args.len() < 1 {
+ return Command::Blank;
+ }
+ return Command::Delete(args[0].to_string());
+ }
"mprev" | "month-prev" => return Command::MonthPrev,
"mnext" | "month-next" => return Command::MonthNext,
_ => 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 {
fn take_focus(&mut self, _: Direction) -> bool;
fn set_view_month_offset(&mut self, offset: u32);
fn view_month_offset(&self) -> u32;
+ fn get_name(&self) -> String;
}
macro_rules! auto_habit_impl {
@@ -99,6 +100,9 @@ macro_rules! auto_habit_impl {
fn view_month_offset(&self) -> u32 {
Habit::view_month_offset(self)
}
+ fn get_name(&self) -> String {
+ Habit::name(self)
+ }
}
};
}