Add parameter to show done habits

This commit is contained in:
Matthias Beyer 2018-03-22 13:45:16 +01:00
parent e4fc2e8010
commit 00d485c3d3
2 changed files with 50 additions and 9 deletions

View file

@ -229,11 +229,14 @@ fn today(rt: &Runtime, future: bool) {
use libimaghabit::error::ResultExt;
use libimaghabit::error::HabitErrorKind as HEK;
let future = {
let (future, show_done) = {
if !future {
rt.cli().subcommand_matches("today").unwrap().is_present("today-show-future")
let scmd = rt.cli().subcommand_matches("today").unwrap();
let futu = scmd.is_present("today-show-future");
let done = scmd.is_present("today-done");
(futu, done)
} else {
true
(true, rt.cli().subcommand_matches("status").unwrap().is_present("status-done"))
}
};
let today = ::chrono::offset::Local::today().naive_local();
@ -257,6 +260,8 @@ fn today(rt: &Runtime, future: bool) {
.filter(|h| {
let due = h.next_instance_date().map_err_trace_exit_unwrap(1);
// today or in future
debug!("Checking {due:?} == {today:?} or (future = {fut} && {due:?} > {today:?}",
due = due, today = today, fut = future);
due.map(|d| d == today || (future && d > today)).unwrap_or(false)
})
.collect();
@ -266,15 +271,17 @@ fn today(rt: &Runtime, future: bool) {
relevant
};
let any_today_relevant = relevant
let any_today_relevant = show_done || relevant
.iter()
.filter(|h| {
let due = h.next_instance_date().map_err_trace_exit_unwrap(1);
debug!("Checking: {:?} == {:?}", due, today);
due.map(|d| d == today).unwrap_or(false) // relevant today
})
.count() == 0;
.count() != 0;
if any_today_relevant {
debug!("Any today relevant = {}", any_today_relevant);
if !any_today_relevant {
let n = rt
.cli()
.subcommand_matches("today")
@ -294,10 +301,16 @@ fn today(rt: &Runtime, future: bool) {
let date = element.next_instance_date().map_err_trace_exit_unwrap(1);
let name = element.habit_name().map_err_trace_exit_unwrap(1);
if let Some(date) = date { // if there is a date
if let Some(date) = date {
let is_done = element
.instance_exists_for_date(&date)
.map_err_trace_exit_unwrap(1);
if show_done || !is_done {
info!(" * {date}: {name}", date = date, name = name);
}
}
}
} else {
fn lister_fn(h: &FileLockEntry) -> Vec<String> {
debug!("Listing: {:?}", h);
@ -323,7 +336,21 @@ fn today(rt: &Runtime, future: bool) {
table.set_titles(Row::new(header));
let mut empty = true;
for (i, e) in relevant.into_iter().enumerate() {
for (i, e) in relevant.into_iter()
.filter(|habit| {
show_done || {
habit
.next_instance_date()
.map_err_trace_exit_unwrap(1)
.map(|date| {
habit.instance_exists_for_date(&date)
.map_err_trace_exit_unwrap(1)
})
.unwrap_or(false)
}
})
.enumerate()
{
let mut v = vec![format!("{}", i)];
let mut list = lister_fn(&e);
v.append(&mut list);

View file

@ -24,6 +24,13 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.subcommand(SubCommand::with_name("status")
.about("Show the current status. Remind of not-yet-done habits, shows upcoming. Default if no command is passed. Also alias for 'today --future'")
.version("0.1")
.arg(Arg::with_name("status-done")
.long("done")
.short("D")
.multiple(false)
.required(false)
.takes_value(false)
.help("Show entries which are done."))
)
.subcommand(SubCommand::with_name("create")
@ -139,6 +146,13 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.takes_value(true)
.value_name("N")
.help("Show the N next relevant entries. Default = 5"))
.arg(Arg::with_name("today-done")
.long("done")
.short("D")
.multiple(false)
.required(false)
.takes_value(false)
.help("Show entries which are done."))
)
.subcommand(SubCommand::with_name("done")