diff options
author | Akshay <nerdy@peppe.rs> | 2020-07-23 13:07:35 +0530 |
---|---|---|
committer | Akshay <nerdy@peppe.rs> | 2020-07-23 13:07:35 +0530 |
commit | 537e4f5ebe7404031f240233cbe9807df0d580d9 (patch) | |
tree | 7de9e379217180ccee20f6c74681df36071a893c | |
parent | 59b40932d4602fc7bf84f123930f9a0eb187f4a1 (diff) | |
parent | a0c57162b2026e37220e31a39d821c2a2e31cc51 (diff) |
Merge branch 'master' of https://github.com/yoms/dijo into fix/duplicate-habits
-rw-r--r-- | src/app/impl_self.rs | 19 | ||||
-rw-r--r-- | src/main.rs | 14 |
2 files changed, 32 insertions, 1 deletions
diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs index 1ed19e6..1dfe268 100644 --- a/src/app/impl_self.rs +++ b/src/app/impl_self.rs @@ -33,8 +33,25 @@ impl App { }; } + pub fn list_habit(&self) -> Vec<String> { + let habits_names = self.habits.iter().map(|x| x.name()).collect::<Vec<_>>(); + return habits_names; + } + pub fn add_habit(&mut self, h: Box<dyn HabitWrapper>) { - self.habits.push(h); + if self + .habits + .iter() + .filter(|hab| hab.name() == h.name()) + .count() + > 0 + { + self.message.set_kind(MessageKind::Error); + self.message + .set_message(format!("Habit `{}` allready exist", h.name())) + } else { + self.habits.push(h); + } } pub fn list_habits(&self) -> Vec<String> { diff --git a/src/main.rs b/src/main.rs index d96119e..050a296 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,14 @@ fn main() { .value_name("CMD") .help("run a dijo command"), ) + .arg( + Arg::with_name("list") + .short("l") + .long("list") + .takes_value(false) + .help("list dijo habits") + .conflicts_with("command"), + ) .get_matches(); if let Some(c) = matches.value_of("command") { let command = Command::from_string(c); @@ -49,6 +57,12 @@ fn main() { "Commands other than `track-up` and `track-down` are currently not supported!" ), } + } else if matches.is_present("list") { + let app = App::load_state(); + let _habit_names = app.list_habit(); + for h in _habit_names { + println!("{}", h); + } } else { let mut s = termion().unwrap(); let app = App::load_state(); |