From 5574764d83128b9a35537c37764f2274ad7f6848 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 5 Dec 2017 20:34:55 +0100 Subject: [PATCH] Add a subcommand to list habits due today --- bin/domain/imag-habit/src/main.rs | 55 +++++++++++++++++++++++++++++++ bin/domain/imag-habit/src/ui.rs | 5 +++ 2 files changed, 60 insertions(+) diff --git a/bin/domain/imag-habit/src/main.rs b/bin/domain/imag-habit/src/main.rs index cefb4613..60911c56 100644 --- a/bin/domain/imag-habit/src/main.rs +++ b/bin/domain/imag-habit/src/main.rs @@ -38,6 +38,7 @@ extern crate clap; extern crate toml; extern crate toml_query; extern crate kairos; +extern crate chrono; extern crate libimaghabit; extern crate libimagstore; @@ -75,6 +76,7 @@ fn main() { "create" => create(&rt), "delete" => delete(&rt), "list" => list(&rt), + "today" => today(&rt), "show" => show(&rt), _ => { debug!("Unknown command"); // More error handling @@ -120,6 +122,59 @@ fn delete(rt: &Runtime) { unimplemented!() } +// Almost the same as `list()` but with other lister functions and an additional filter for only +// listing entries which are due today. +fn today(rt: &Runtime) { + fn lister_fn(h: &FileLockEntry) -> Vec { + debug!("Listing: {:?}", h); + let name = h.habit_name().map_err_trace_exit_unwrap(1); + let basedate = h.habit_basedate().map_err_trace_exit_unwrap(1); + let recur = h.habit_recur_spec().map_err_trace_exit_unwrap(1); + let comm = h.habit_comment().map_err_trace_exit_unwrap(1); + + let v = vec![name, basedate, recur, comm]; + debug!(" -> {:?}", v); + v + } + + fn lister_header() -> Vec { + ["Name", "Basedate", "Recurr", "Comment"].iter().map(|x| String::from(*x)).collect() + } + + let today = ::chrono::offset::Local::today().naive_local(); + + let today_relevant : Vec<_> = rt + .store() + .all_habit_templates() + .map_err_trace_exit_unwrap(1) + .filter_map(|id| match rt.store().get(id.clone()) { + Ok(Some(h)) => Some(h), + Ok(None) => { + error!("No habit found for {:?}", id); + None + }, + Err(e) => { + trace_error(&e); + None + }, + }) + .filter(|h| { + let due = h.next_instance_date().map_err_trace_exit_unwrap(1); + due == today + }) + .collect(); + + if today_relevant.is_empty() { + info!("No Habits due today."); + } else { + TableLister::new(lister_fn) + .with_header(lister_header()) + .with_idx(true) + .list(today_relevant.into_iter()) + .map_err_trace_exit_unwrap(1); + } +} + fn list(rt: &Runtime) { fn lister_fn(h: &FileLockEntry) -> Vec { debug!("Listing: {:?}", h); diff --git a/bin/domain/imag-habit/src/ui.rs b/bin/domain/imag-habit/src/ui.rs index 3b2569df..f72e67f3 100644 --- a/bin/domain/imag-habit/src/ui.rs +++ b/bin/domain/imag-habit/src/ui.rs @@ -107,4 +107,9 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .value_name("NAME") .help("Name of the habit to show")) ) + + .subcommand(SubCommand::with_name("today") + .about("List habits which are due today") + .version("0.1") + ) }