summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Hormiere <guillaume.hormiere@delair.aero>2020-07-22 00:38:01 +0200
committerGuillaume Hormiere <guillaume.hormiere@delair.aero>2020-07-22 00:38:01 +0200
commit9dfe454cd8bc816522446e7e3b9f45630c886112 (patch)
tree4b31fbdd318ba58f55b2723fca55dbc4ca10e6a5
parent9102dc72be19f87ec9e2893a35f5020957135c1c (diff)
Add list command for shell script purpose
Usage dijo -l for printing the habit names list Add check on habit add to avoid duplicate habits
-rw-r--r--src/app/impl_self.rs21
-rw-r--r--src/main.rs14
2 files changed, 34 insertions, 1 deletions
diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs
index 95f1871..38162ee 100644
--- a/src/app/impl_self.rs
+++ b/src/app/impl_self.rs
@@ -33,8 +33,27 @@ impl App {
};
}
+ pub fn list_habit(&self) -> Vec<String> {
+ let mut habits_names: Vec<String> = vec![];
+ for h in self.habits.iter() {
+ habits_names.push(h.name())
+ }
+ 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_message(format!("Habit `{}` allready exist", h.name()))
+ } else {
+ self.habits.push(h);
+ }
}
pub fn delete_by_name(&mut self, name: &str) {
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();