Add a subcommand to list habits due today

This commit is contained in:
Matthias Beyer 2017-12-05 20:34:55 +01:00
parent 2520635cae
commit 5574764d83
2 changed files with 60 additions and 0 deletions

View file

@ -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<String> {
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<String> {
["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<String> {
debug!("Listing: {:?}", h);

View file

@ -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")
)
}