Let "today" command list upcoming habits
This commit is contained in:
parent
5574764d83
commit
0fa1ae28f8
2 changed files with 63 additions and 19 deletions
|
@ -125,6 +125,9 @@ fn delete(rt: &Runtime) {
|
||||||
// Almost the same as `list()` but with other lister functions and an additional filter for only
|
// Almost the same as `list()` but with other lister functions and an additional filter for only
|
||||||
// listing entries which are due today.
|
// listing entries which are due today.
|
||||||
fn today(rt: &Runtime) {
|
fn today(rt: &Runtime) {
|
||||||
|
use libimaghabit::error::ResultExt;
|
||||||
|
use libimaghabit::error::HabitErrorKind as HEK;
|
||||||
|
|
||||||
fn lister_fn(h: &FileLockEntry) -> Vec<String> {
|
fn lister_fn(h: &FileLockEntry) -> Vec<String> {
|
||||||
debug!("Listing: {:?}", h);
|
debug!("Listing: {:?}", h);
|
||||||
let name = h.habit_name().map_err_trace_exit_unwrap(1);
|
let name = h.habit_name().map_err_trace_exit_unwrap(1);
|
||||||
|
@ -143,7 +146,8 @@ fn today(rt: &Runtime) {
|
||||||
|
|
||||||
let today = ::chrono::offset::Local::today().naive_local();
|
let today = ::chrono::offset::Local::today().naive_local();
|
||||||
|
|
||||||
let today_relevant : Vec<_> = rt
|
let relevant : Vec<_> = { // scope, to have variable non-mutable in outer scope
|
||||||
|
let mut relevant : Vec<_> = rt
|
||||||
.store()
|
.store()
|
||||||
.all_habit_templates()
|
.all_habit_templates()
|
||||||
.map_err_trace_exit_unwrap(1)
|
.map_err_trace_exit_unwrap(1)
|
||||||
|
@ -160,17 +164,49 @@ fn today(rt: &Runtime) {
|
||||||
})
|
})
|
||||||
.filter(|h| {
|
.filter(|h| {
|
||||||
let due = h.next_instance_date().map_err_trace_exit_unwrap(1);
|
let due = h.next_instance_date().map_err_trace_exit_unwrap(1);
|
||||||
due == today
|
due == today || due > today // today or in future
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if today_relevant.is_empty() {
|
relevant.sort_by_key(|h| h.next_instance_date().map_err_trace_exit_unwrap(1));
|
||||||
|
relevant
|
||||||
|
};
|
||||||
|
|
||||||
|
let any_today_relevant = relevant
|
||||||
|
.iter()
|
||||||
|
.filter(|h| {
|
||||||
|
let due = h.next_instance_date().map_err_trace_exit_unwrap(1);
|
||||||
|
due == today // relevant today
|
||||||
|
})
|
||||||
|
.count() == 0;
|
||||||
|
|
||||||
|
if any_today_relevant {
|
||||||
|
let n = rt
|
||||||
|
.cli()
|
||||||
|
.subcommand_matches("today")
|
||||||
|
.and_then(|am| {
|
||||||
|
am.value_of("today-show-next-n")
|
||||||
|
.map(|x| {
|
||||||
|
x.parse::<usize>()
|
||||||
|
.chain_err(|| HEK::from(format!("Cannot parse String '{}' to integer", x)))
|
||||||
|
.map_err_trace_exit_unwrap(1)
|
||||||
|
})
|
||||||
|
}).unwrap_or(5);
|
||||||
|
|
||||||
info!("No Habits due today.");
|
info!("No Habits due today.");
|
||||||
|
info!("Upcoming:");
|
||||||
|
// list `n` which are relevant in the future.
|
||||||
|
for element in relevant.iter().take(n) {
|
||||||
|
let date = element.next_instance_date().map_err_trace_exit_unwrap(1);
|
||||||
|
let name = element.habit_name().map_err_trace_exit_unwrap(1);
|
||||||
|
|
||||||
|
info!(" * {date}: {name}", date = date, name = name);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
TableLister::new(lister_fn)
|
TableLister::new(lister_fn)
|
||||||
.with_header(lister_header())
|
.with_header(lister_header())
|
||||||
.with_idx(true)
|
.with_idx(true)
|
||||||
.list(today_relevant.into_iter())
|
.list(relevant.into_iter())
|
||||||
.map_err_trace_exit_unwrap(1);
|
.map_err_trace_exit_unwrap(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,5 +111,13 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
||||||
.subcommand(SubCommand::with_name("today")
|
.subcommand(SubCommand::with_name("today")
|
||||||
.about("List habits which are due today")
|
.about("List habits which are due today")
|
||||||
.version("0.1")
|
.version("0.1")
|
||||||
|
.arg(Arg::with_name("today-show-next-n")
|
||||||
|
.long("show")
|
||||||
|
.short("s")
|
||||||
|
.multiple(false)
|
||||||
|
.required(false)
|
||||||
|
.takes_value(true)
|
||||||
|
.value_name("N")
|
||||||
|
.help("Show the N next relevant entries. Default = 5"))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue