2017-10-21 12:12:21 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-10-21 12:12:21 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2018-09-27 06:13:58 +00:00
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
2017-10-21 12:12:21 +00:00
|
|
|
#![deny(
|
|
|
|
non_camel_case_types,
|
|
|
|
non_snake_case,
|
|
|
|
path_statements,
|
|
|
|
trivial_numeric_casts,
|
|
|
|
unstable_features,
|
|
|
|
unused_allocation,
|
|
|
|
unused_import_braces,
|
|
|
|
unused_imports,
|
|
|
|
unused_must_use,
|
|
|
|
unused_mut,
|
|
|
|
unused_qualifications,
|
|
|
|
while_true,
|
|
|
|
)]
|
|
|
|
|
|
|
|
extern crate clap;
|
|
|
|
#[macro_use] extern crate log;
|
|
|
|
extern crate toml;
|
|
|
|
extern crate toml_query;
|
|
|
|
extern crate kairos;
|
2017-12-05 19:34:55 +00:00
|
|
|
extern crate chrono;
|
2018-02-20 12:16:31 +00:00
|
|
|
extern crate prettytable;
|
2018-10-30 17:40:53 +00:00
|
|
|
#[macro_use] extern crate failure;
|
2017-10-21 12:12:21 +00:00
|
|
|
|
|
|
|
extern crate libimaghabit;
|
|
|
|
extern crate libimagstore;
|
2018-01-31 10:09:00 +00:00
|
|
|
#[macro_use] extern crate libimagrt;
|
2017-10-21 12:12:21 +00:00
|
|
|
extern crate libimagerror;
|
|
|
|
extern crate libimagutil;
|
2017-12-08 13:34:14 +00:00
|
|
|
extern crate libimaginteraction;
|
2017-10-21 12:12:21 +00:00
|
|
|
|
2018-02-14 17:29:46 +00:00
|
|
|
use std::io::Write;
|
2017-10-21 12:12:21 +00:00
|
|
|
use std::process::exit;
|
|
|
|
|
2018-02-20 12:16:31 +00:00
|
|
|
use prettytable::Table;
|
2018-09-30 13:09:47 +00:00
|
|
|
use prettytable::Cell;
|
|
|
|
use prettytable::Row;
|
2018-10-30 17:40:53 +00:00
|
|
|
use failure::Error;
|
2018-02-20 12:16:31 +00:00
|
|
|
|
2017-10-21 12:12:21 +00:00
|
|
|
use libimagrt::runtime::Runtime;
|
|
|
|
use libimagrt::setup::generate_runtime_setup;
|
|
|
|
use libimagerror::trace::{MapErrTrace, trace_error};
|
2018-05-01 15:42:47 +00:00
|
|
|
use libimagerror::iter::TraceIterator;
|
2018-02-14 17:29:46 +00:00
|
|
|
use libimagerror::exit::ExitUnwrap;
|
|
|
|
use libimagerror::io::ToExitCode;
|
2017-10-21 12:12:21 +00:00
|
|
|
use libimaghabit::store::HabitStore;
|
|
|
|
use libimaghabit::habit::builder::HabitBuilder;
|
|
|
|
use libimaghabit::habit::HabitTemplate;
|
2017-12-03 17:46:33 +00:00
|
|
|
use libimagstore::store::FileLockEntry;
|
2017-12-19 17:41:54 +00:00
|
|
|
use libimagstore::store::Store;
|
|
|
|
use libimagstore::storeid::StoreId;
|
2017-12-08 13:34:14 +00:00
|
|
|
use libimaginteraction::ask::ask_bool;
|
2018-11-04 21:36:35 +00:00
|
|
|
use libimagutil::debug_result::DebugResult;
|
2017-10-21 12:12:21 +00:00
|
|
|
|
|
|
|
mod ui;
|
|
|
|
|
|
|
|
fn main() {
|
2018-01-31 10:09:00 +00:00
|
|
|
let version = make_imag_version!();
|
2017-10-21 12:12:21 +00:00
|
|
|
let rt = generate_runtime_setup("imag-habit",
|
2018-01-31 10:09:00 +00:00
|
|
|
&version,
|
2017-10-21 12:12:21 +00:00
|
|
|
"Habit tracking tool",
|
|
|
|
ui::build_ui);
|
|
|
|
|
|
|
|
|
2017-12-06 19:23:06 +00:00
|
|
|
let _ = rt
|
|
|
|
.cli()
|
2017-10-21 12:12:21 +00:00
|
|
|
.subcommand_name()
|
|
|
|
.map(|name| {
|
|
|
|
debug!("Call {}", name);
|
|
|
|
match name {
|
|
|
|
"create" => create(&rt),
|
|
|
|
"delete" => delete(&rt),
|
|
|
|
"list" => list(&rt),
|
2017-12-19 18:10:36 +00:00
|
|
|
"today" => today(&rt, false),
|
|
|
|
"status" => today(&rt, true),
|
2017-10-21 12:12:21 +00:00
|
|
|
"show" => show(&rt),
|
2017-12-19 17:41:54 +00:00
|
|
|
"done" => done(&rt),
|
2018-04-04 15:01:02 +00:00
|
|
|
other => {
|
|
|
|
debug!("Unknown command");
|
|
|
|
let _ = rt.handle_unknown_subcommand("imag-habit", other, rt.cli())
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-04-04 15:01:02 +00:00
|
|
|
.code()
|
|
|
|
.map(::std::process::exit);
|
2017-10-21 12:12:21 +00:00
|
|
|
},
|
|
|
|
}
|
2017-12-06 19:23:06 +00:00
|
|
|
})
|
2017-12-19 18:10:36 +00:00
|
|
|
.unwrap_or_else(|| today(&rt, true));
|
2017-10-21 12:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn create(rt: &Runtime) {
|
2017-12-22 12:34:59 +00:00
|
|
|
use kairos::parser::parse as kairos_parse;
|
|
|
|
use kairos::parser::Parsed;
|
|
|
|
let scmd = rt.cli().subcommand_matches("create").unwrap(); // safe by call from main()
|
|
|
|
let name = scmd.value_of("create-name").map(String::from).unwrap(); // safe by clap
|
|
|
|
let recu = scmd.value_of("create-date-recurr-spec").map(String::from).unwrap(); // safe by clap
|
|
|
|
let comm = scmd.value_of("create-comment").map(String::from).unwrap(); // safe by clap
|
|
|
|
let date = scmd.value_of("create-date").unwrap(); // safe by clap
|
|
|
|
|
2019-02-05 00:37:32 +00:00
|
|
|
let parsedate = |d, pname| match kairos_parse(d).map_err_trace_exit_unwrap() {
|
2018-11-04 21:36:35 +00:00
|
|
|
Parsed::TimeType(tt) => tt.calculate()
|
|
|
|
.map_dbg(|y| format!("TimeType yielded: '{:?}'", y))
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-11-04 21:36:35 +00:00
|
|
|
.get_moment()
|
|
|
|
.ok_or_else(|| {
|
|
|
|
error!("Error: '{}' parameter does not yield a point in time", pname);
|
|
|
|
exit(1)
|
|
|
|
})
|
|
|
|
.unwrap() // safe by above
|
|
|
|
.date(),
|
2017-12-03 17:46:11 +00:00
|
|
|
_ => {
|
2017-12-22 12:34:59 +00:00
|
|
|
error!("Error: '{}' parameter does not yield a point in time", pname);
|
2017-12-03 17:46:11 +00:00
|
|
|
exit(1);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-02-08 13:23:04 +00:00
|
|
|
debug!("Building habit: name = {name}, basedate = {date}, recurr = {recu}, comment = {comm}",
|
|
|
|
name = name,
|
|
|
|
date = date,
|
|
|
|
recu = recu,
|
|
|
|
comm = comm);
|
|
|
|
|
2017-12-22 12:34:59 +00:00
|
|
|
let hb = HabitBuilder::default()
|
2017-12-03 17:46:11 +00:00
|
|
|
.with_name(name)
|
2017-12-22 12:34:59 +00:00
|
|
|
.with_basedate(parsedate(date, "date"))
|
2017-12-03 17:46:11 +00:00
|
|
|
.with_recurspec(recu)
|
2017-12-22 12:34:59 +00:00
|
|
|
.with_comment(comm);
|
|
|
|
|
|
|
|
let hb = if let Some(until) = scmd.value_of("create-until") {
|
|
|
|
hb.with_until(parsedate(until, "until"))
|
|
|
|
} else {
|
|
|
|
hb
|
|
|
|
};
|
2017-12-03 17:46:11 +00:00
|
|
|
|
2018-02-08 13:23:04 +00:00
|
|
|
debug!("Builder = {:?}", hb);
|
|
|
|
|
2019-02-05 00:37:32 +00:00
|
|
|
let fle = hb.build(rt.store()).map_err_trace_exit_unwrap();
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(fle.get_location()).unwrap_or_exit();
|
2017-10-21 12:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn delete(rt: &Runtime) {
|
2017-12-08 13:34:14 +00:00
|
|
|
use libimaghabit::instance::HabitInstance;
|
|
|
|
|
|
|
|
let scmd = rt.cli().subcommand_matches("delete").unwrap(); // safe by call from main()
|
|
|
|
let name = scmd.value_of("delete-name").map(String::from).unwrap(); // safe by clap
|
|
|
|
let yes = scmd.is_present("delete-yes");
|
|
|
|
let delete_instances = scmd.is_present("delete-instances");
|
|
|
|
|
2018-11-01 19:33:24 +00:00
|
|
|
let mut input = rt.stdin().unwrap_or_else(|| {
|
|
|
|
error!("No input stream. Cannot ask for permission");
|
|
|
|
exit(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut output = rt.stdout();
|
|
|
|
|
2017-12-08 13:34:14 +00:00
|
|
|
let _ = rt
|
|
|
|
.store()
|
|
|
|
.all_habit_templates()
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
|
|
|
.trace_unwrap_exit()
|
|
|
|
.map(|sid| (sid.clone(), rt.store().get(sid).map_err_trace_exit_unwrap())) // get the FileLockEntry
|
2017-12-08 13:34:14 +00:00
|
|
|
.filter(|&(_, ref habit)| match habit { // filter for name of habit == name we look for
|
2019-02-05 00:37:32 +00:00
|
|
|
&Some(ref h) => h.habit_name().map_err_trace_exit_unwrap() == name,
|
2017-12-08 13:34:14 +00:00
|
|
|
&None => false,
|
|
|
|
})
|
|
|
|
.filter_map(|(a, o)| o.map(|x| (a, x))) // map: (a, Option<b>) -> Option<(a, b)> -> (a, b)
|
|
|
|
.map(|(sid, fle)| {
|
|
|
|
if delete_instances {
|
|
|
|
|
|
|
|
// if this does not succeed, we did something terribly wrong
|
2019-02-05 00:37:32 +00:00
|
|
|
let t_name = fle.habit_name().map_err_trace_exit_unwrap();
|
2017-12-08 13:34:14 +00:00
|
|
|
assert_eq!(t_name, name);
|
|
|
|
|
2019-02-05 00:37:32 +00:00
|
|
|
let get_instance = |iid| rt.store().get(iid).map_err_trace_exit_unwrap();
|
|
|
|
let has_template_name = |i: &FileLockEntry| t_name == i.get_template_name().map_err_trace_exit_unwrap();
|
2017-12-08 13:34:14 +00:00
|
|
|
let instance_location = |i: FileLockEntry| i.get_location().clone();
|
|
|
|
let delete_instance_by_id = |id| {
|
2019-02-05 00:37:32 +00:00
|
|
|
let do_delete = |id| rt.store().delete(id).map_err_trace_exit_unwrap();
|
2017-12-08 13:34:14 +00:00
|
|
|
if !yes {
|
|
|
|
let q = format!("Really delete {}", id);
|
2018-11-01 19:33:24 +00:00
|
|
|
if ask_bool(&q, Some(false), &mut input, &mut output)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-11-01 19:33:24 +00:00
|
|
|
{
|
2017-12-08 13:34:14 +00:00
|
|
|
let _ = do_delete(id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let _ = do_delete(id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-26 22:43:24 +00:00
|
|
|
let _ = fle
|
2017-12-08 13:34:14 +00:00
|
|
|
.linked_instances()
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
|
|
|
.trace_unwrap_exit()
|
2017-12-08 13:34:14 +00:00
|
|
|
.filter_map(get_instance)
|
|
|
|
.filter(has_template_name)
|
|
|
|
.map(instance_location)
|
|
|
|
.map(delete_instance_by_id)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(fle);
|
|
|
|
|
2019-02-05 00:37:32 +00:00
|
|
|
let do_delete_template = |sid| rt.store().delete(sid).map_err_trace_exit_unwrap();
|
2017-12-08 13:34:14 +00:00
|
|
|
if !yes {
|
|
|
|
let q = format!("Really delete template {}", sid);
|
2018-11-01 19:33:24 +00:00
|
|
|
if ask_bool(&q, Some(false), &mut input, &mut output)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-11-01 19:33:24 +00:00
|
|
|
{
|
2017-12-08 13:34:14 +00:00
|
|
|
let _ = do_delete_template(sid);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let _ = do_delete_template(sid);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
info!("Done");
|
2017-10-21 12:12:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 19:34:55 +00:00
|
|
|
// Almost the same as `list()` but with other lister functions and an additional filter for only
|
|
|
|
// listing entries which are due today.
|
2017-12-19 18:10:36 +00:00
|
|
|
//
|
|
|
|
// if `future` is false, the `rt.cli()` will be checked or a subcommand "today" and the related
|
|
|
|
// future flag. If it is true, the check will not be performed and it is assumed that `--future`
|
|
|
|
// was passed.
|
|
|
|
fn today(rt: &Runtime, future: bool) {
|
2018-10-30 17:40:53 +00:00
|
|
|
use failure::ResultExt;
|
2017-12-06 19:21:20 +00:00
|
|
|
|
2018-03-22 12:45:16 +00:00
|
|
|
let (future, show_done) = {
|
2017-12-19 18:10:36 +00:00
|
|
|
if !future {
|
2018-03-22 12:45:16 +00:00
|
|
|
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)
|
2017-12-19 18:10:36 +00:00
|
|
|
} else {
|
2019-02-06 00:54:47 +00:00
|
|
|
if let Some(status) = rt.cli().subcommand_matches("status") {
|
|
|
|
(true, status.is_present("status-done"))
|
|
|
|
} else {
|
|
|
|
(true, false)
|
|
|
|
}
|
2017-12-19 18:10:36 +00:00
|
|
|
}
|
|
|
|
};
|
2017-12-05 19:34:55 +00:00
|
|
|
let today = ::chrono::offset::Local::today().naive_local();
|
|
|
|
|
2017-12-06 19:21:20 +00:00
|
|
|
let relevant : Vec<_> = { // scope, to have variable non-mutable in outer scope
|
|
|
|
let mut relevant : Vec<_> = rt
|
|
|
|
.store()
|
|
|
|
.all_habit_templates()
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
|
|
|
.trace_unwrap_exit()
|
2017-12-06 19:21:20 +00:00
|
|
|
.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| {
|
2019-02-05 00:37:32 +00:00
|
|
|
let due = h.next_instance_date().map_err_trace_exit_unwrap();
|
2017-12-22 11:26:29 +00:00
|
|
|
// today or in future
|
2018-03-22 12:45:16 +00:00
|
|
|
debug!("Checking {due:?} == {today:?} or (future = {fut} && {due:?} > {today:?}",
|
|
|
|
due = due, today = today, fut = future);
|
2017-12-22 11:26:29 +00:00
|
|
|
due.map(|d| d == today || (future && d > today)).unwrap_or(false)
|
2017-12-06 19:21:20 +00:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2017-12-22 11:26:29 +00:00
|
|
|
// unwrap is safe because we filtered above
|
2019-02-05 00:37:32 +00:00
|
|
|
relevant.sort_by_key(|h| h.next_instance_date().map_err_trace_exit_unwrap().unwrap());
|
2017-12-06 19:21:20 +00:00
|
|
|
relevant
|
|
|
|
};
|
|
|
|
|
2018-03-22 12:45:16 +00:00
|
|
|
let any_today_relevant = show_done || relevant
|
2017-12-06 19:21:20 +00:00
|
|
|
.iter()
|
2017-12-05 19:34:55 +00:00
|
|
|
.filter(|h| {
|
2019-02-05 00:37:32 +00:00
|
|
|
let due = h.next_instance_date().map_err_trace_exit_unwrap();
|
2018-03-22 12:45:16 +00:00
|
|
|
debug!("Checking: {:?} == {:?}", due, today);
|
2017-12-22 11:26:29 +00:00
|
|
|
due.map(|d| d == today).unwrap_or(false) // relevant today
|
2017-12-05 19:34:55 +00:00
|
|
|
})
|
2018-03-22 12:45:16 +00:00
|
|
|
.count() != 0;
|
2017-12-06 19:21:20 +00:00
|
|
|
|
2018-03-22 12:45:16 +00:00
|
|
|
debug!("Any today relevant = {}", any_today_relevant);
|
2019-02-10 21:46:08 +00:00
|
|
|
debug!("relevant = {:?}", relevant);
|
|
|
|
|
2018-03-22 12:45:16 +00:00
|
|
|
if !any_today_relevant {
|
2017-12-06 19:21:20 +00:00
|
|
|
let n = rt
|
|
|
|
.cli()
|
|
|
|
.subcommand_matches("today")
|
|
|
|
.and_then(|am| {
|
|
|
|
am.value_of("today-show-next-n")
|
|
|
|
.map(|x| {
|
|
|
|
x.parse::<usize>()
|
2018-10-30 17:40:53 +00:00
|
|
|
.context(format_err!("Cannot parse String '{}' to integer", x))
|
|
|
|
.map_err(Error::from)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2017-12-06 19:21:20 +00:00
|
|
|
})
|
|
|
|
}).unwrap_or(5);
|
2017-12-05 19:34:55 +00:00
|
|
|
|
|
|
|
info!("No Habits due today.");
|
2017-12-06 19:21:20 +00:00
|
|
|
info!("Upcoming:");
|
|
|
|
// list `n` which are relevant in the future.
|
2018-11-08 15:08:46 +00:00
|
|
|
relevant.iter().take(n).for_each(|element| {
|
2019-02-05 00:37:32 +00:00
|
|
|
let date = element.next_instance_date().map_err_trace_exit_unwrap();
|
|
|
|
let name = element.habit_name().map_err_trace_exit_unwrap();
|
2017-12-06 19:21:20 +00:00
|
|
|
|
2018-03-22 12:45:16 +00:00
|
|
|
if let Some(date) = date {
|
|
|
|
let is_done = element
|
|
|
|
.instance_exists_for_date(&date)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2018-03-22 12:45:16 +00:00
|
|
|
|
|
|
|
if show_done || !is_done {
|
|
|
|
info!(" * {date}: {name}", date = date, name = name);
|
|
|
|
}
|
2017-12-22 11:26:29 +00:00
|
|
|
}
|
2018-11-08 15:08:46 +00:00
|
|
|
});
|
2017-12-05 19:34:55 +00:00
|
|
|
} else {
|
2017-12-22 11:26:29 +00:00
|
|
|
fn lister_fn(h: &FileLockEntry) -> Vec<String> {
|
|
|
|
debug!("Listing: {:?}", h);
|
2019-02-05 00:37:32 +00:00
|
|
|
let name = h.habit_name().map_err_trace_exit_unwrap();
|
|
|
|
let basedate = h.habit_basedate().map_err_trace_exit_unwrap();
|
|
|
|
let recur = h.habit_recur_spec().map_err_trace_exit_unwrap();
|
|
|
|
let due = h.next_instance_date().map_err_trace_exit_unwrap()
|
2017-12-22 11:26:29 +00:00
|
|
|
.map(date_to_string_helper)
|
|
|
|
.unwrap_or_else(|| String::from("<finished>"));
|
2019-02-05 00:37:32 +00:00
|
|
|
let comm = h.habit_comment().map_err_trace_exit_unwrap();
|
2017-12-22 11:26:29 +00:00
|
|
|
|
|
|
|
let v = vec![name, basedate, recur, due, comm];
|
|
|
|
debug!(" -> {:?}", v);
|
|
|
|
v
|
|
|
|
}
|
|
|
|
|
2018-02-20 12:16:31 +00:00
|
|
|
let header = ["#", "Name", "Basedate", "Recurr", "Next Due", "Comment"]
|
|
|
|
.iter()
|
|
|
|
.map(|s| Cell::new(s))
|
|
|
|
.collect::<Vec<Cell>>();
|
|
|
|
|
|
|
|
let mut table = Table::new();
|
|
|
|
table.set_titles(Row::new(header));
|
|
|
|
|
|
|
|
let mut empty = true;
|
2018-11-08 15:08:46 +00:00
|
|
|
relevant
|
|
|
|
.into_iter()
|
|
|
|
.filter(|habit| show_done || {
|
2019-02-10 21:46:59 +00:00
|
|
|
let instance_exists = habit
|
2018-11-08 15:08:46 +00:00
|
|
|
.next_instance_date()
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-11-08 15:08:46 +00:00
|
|
|
.map(|date| {
|
2019-02-10 21:46:08 +00:00
|
|
|
let instance_exists = habit
|
|
|
|
.instance_exists_for_date(&date)
|
|
|
|
.map_err_trace_exit_unwrap();
|
|
|
|
|
|
|
|
debug!("instance exists for {:?} for {:?} = {:?}",
|
|
|
|
habit.get_location().local_display_string(),
|
|
|
|
date,
|
|
|
|
instance_exists);
|
|
|
|
|
|
|
|
instance_exists
|
2018-11-08 15:08:46 +00:00
|
|
|
})
|
2019-02-10 21:46:59 +00:00
|
|
|
.unwrap_or(false);
|
|
|
|
|
|
|
|
!instance_exists
|
2018-03-22 12:45:16 +00:00
|
|
|
})
|
|
|
|
.enumerate()
|
2018-11-08 15:08:46 +00:00
|
|
|
.for_each(|(i, e)| {
|
|
|
|
let mut v = vec![format!("{}", i)];
|
|
|
|
let mut list = lister_fn(&e);
|
2018-11-01 17:07:57 +00:00
|
|
|
|
2018-11-08 15:08:46 +00:00
|
|
|
{
|
|
|
|
let _ = rt
|
|
|
|
.report_touched(e.get_location())
|
2019-02-05 00:37:32 +00:00
|
|
|
.unwrap_or_exit();
|
2018-11-08 15:08:46 +00:00
|
|
|
}
|
2018-11-01 17:07:57 +00:00
|
|
|
|
2018-11-08 15:08:46 +00:00
|
|
|
v.append(&mut list);
|
|
|
|
table.add_row(v.iter().map(|s| Cell::new(s)).collect());
|
|
|
|
empty = false;
|
|
|
|
});
|
2017-12-22 11:26:29 +00:00
|
|
|
|
2018-02-20 12:16:31 +00:00
|
|
|
if !empty {
|
2018-02-26 21:57:17 +00:00
|
|
|
let _ = table.print(&mut rt.stdout()).to_exit_code().unwrap_or_exit();
|
2018-02-20 12:16:31 +00:00
|
|
|
}
|
2017-12-05 19:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-21 12:12:21 +00:00
|
|
|
fn list(rt: &Runtime) {
|
2017-12-03 17:46:33 +00:00
|
|
|
fn lister_fn(h: &FileLockEntry) -> Vec<String> {
|
|
|
|
debug!("Listing: {:?}", h);
|
2019-02-05 00:37:32 +00:00
|
|
|
let name = h.habit_name().map_err_trace_exit_unwrap();
|
|
|
|
let basedate = h.habit_basedate().map_err_trace_exit_unwrap();
|
|
|
|
let recur = h.habit_recur_spec().map_err_trace_exit_unwrap();
|
|
|
|
let comm = h.habit_comment().map_err_trace_exit_unwrap();
|
2019-06-30 09:09:18 +00:00
|
|
|
let (due, done) = if let Some(date) = h.next_instance_date().map_err_trace_exit_unwrap() {
|
|
|
|
let done = h.instance_exists_for_date(&date)
|
|
|
|
.map(|b| if b { "x" } else { "" })
|
|
|
|
.map(String::from)
|
|
|
|
.map_err_trace_exit_unwrap();
|
|
|
|
(date_to_string_helper(date), done)
|
|
|
|
} else {
|
|
|
|
// "finished" as in "the habit is closed"
|
|
|
|
(String::from("<finished>"), String::from(""))
|
|
|
|
};
|
2017-12-03 17:46:33 +00:00
|
|
|
|
2019-06-30 09:09:18 +00:00
|
|
|
let v = vec![name, basedate, recur, comm, due, done];
|
2017-12-05 19:28:33 +00:00
|
|
|
debug!(" -> {:?}", v);
|
|
|
|
v
|
2017-12-03 17:46:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 09:09:18 +00:00
|
|
|
let header = ["#", "Name", "Basedate", "Recurr", "Comment", "Next Due", "Done"]
|
2018-02-20 12:16:31 +00:00
|
|
|
.iter()
|
|
|
|
.map(|s| Cell::new(s))
|
|
|
|
.collect::<Vec<Cell>>();
|
2017-12-03 17:46:33 +00:00
|
|
|
|
2018-02-20 12:16:31 +00:00
|
|
|
let mut empty = true;
|
|
|
|
let mut table = Table::new();
|
|
|
|
table.set_titles(Row::new(header));
|
|
|
|
|
|
|
|
let _ = rt
|
2017-12-03 17:46:33 +00:00
|
|
|
.store()
|
|
|
|
.all_habit_templates()
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
|
|
|
.trace_unwrap_exit()
|
2017-12-03 17:46:33 +00:00
|
|
|
.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
|
|
|
|
},
|
2018-02-20 12:16:31 +00:00
|
|
|
})
|
|
|
|
.enumerate()
|
|
|
|
.for_each(|(i, e)| {
|
|
|
|
let mut v = vec![format!("{}", i)];
|
|
|
|
let mut list = lister_fn(&e);
|
2018-11-01 17:07:57 +00:00
|
|
|
|
|
|
|
{
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(e.get_location()).unwrap_or_exit();
|
2018-11-01 17:07:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-20 12:16:31 +00:00
|
|
|
v.append(&mut list);
|
|
|
|
table.add_row(v.iter().map(|s| Cell::new(s)).collect());
|
|
|
|
empty = false;
|
2017-12-03 17:46:33 +00:00
|
|
|
});
|
|
|
|
|
2018-02-20 12:16:31 +00:00
|
|
|
if !empty {
|
2018-02-26 21:57:17 +00:00
|
|
|
let _ = table.print(&mut rt.stdout()).to_exit_code().unwrap_or_exit();
|
2018-02-20 12:16:31 +00:00
|
|
|
}
|
2017-10-21 12:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn show(rt: &Runtime) {
|
2017-12-03 20:18:29 +00:00
|
|
|
let scmd = rt.cli().subcommand_matches("show").unwrap(); // safe by call from main()
|
|
|
|
let name = scmd
|
|
|
|
.value_of("show-name")
|
|
|
|
.map(String::from)
|
|
|
|
.unwrap(); // safe by clap
|
|
|
|
|
2019-06-30 09:31:56 +00:00
|
|
|
fn instance_lister_fn(rt: &Runtime, i: &FileLockEntry) -> Vec<String> {
|
2018-01-31 21:04:54 +00:00
|
|
|
use libimagutil::date::date_to_string;
|
2017-12-03 20:18:29 +00:00
|
|
|
use libimaghabit::instance::HabitInstance;
|
|
|
|
|
2019-02-05 00:37:32 +00:00
|
|
|
let date = date_to_string(&i.get_date().map_err_trace_exit_unwrap());
|
2019-06-30 09:31:56 +00:00
|
|
|
let comm = i.get_comment(rt.store()).map_err_trace_exit_unwrap();
|
2017-12-03 20:18:29 +00:00
|
|
|
|
|
|
|
vec![date, comm]
|
|
|
|
}
|
|
|
|
|
2018-02-20 12:16:31 +00:00
|
|
|
let header = ["#", "Date", "Comment"]
|
|
|
|
.iter()
|
|
|
|
.map(|s| Cell::new(s))
|
|
|
|
.collect::<Vec<Cell>>();
|
|
|
|
|
|
|
|
let mut table = Table::new();
|
|
|
|
table.set_titles(Row::new(header));
|
2017-12-03 20:18:29 +00:00
|
|
|
|
|
|
|
let _ = rt
|
|
|
|
.store()
|
|
|
|
.all_habit_templates()
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
|
|
|
.trace_unwrap_exit()
|
2017-12-19 17:41:54 +00:00
|
|
|
.filter_map(|id| get_from_store(rt.store(), id))
|
2019-02-05 00:37:32 +00:00
|
|
|
.filter(|h| h.habit_name().map(|n| name == n).map_err_trace_exit_unwrap())
|
2017-12-03 20:18:29 +00:00
|
|
|
.enumerate()
|
|
|
|
.map(|(i, habit)| {
|
2019-02-05 00:37:32 +00:00
|
|
|
let name = habit.habit_name().map_err_trace_exit_unwrap();
|
|
|
|
let basedate = habit.habit_basedate().map_err_trace_exit_unwrap();
|
|
|
|
let recur = habit.habit_recur_spec().map_err_trace_exit_unwrap();
|
|
|
|
let comm = habit.habit_comment().map_err_trace_exit_unwrap();
|
2017-12-03 20:18:29 +00:00
|
|
|
|
2018-02-26 21:57:17 +00:00
|
|
|
let _ = writeln!(rt.stdout(),
|
2018-02-14 17:29:46 +00:00
|
|
|
"{i} - {name}\nBase : {b},\nRecurrence: {r}\nComment : {c}\n",
|
2017-12-03 20:18:29 +00:00
|
|
|
i = i,
|
|
|
|
name = name,
|
|
|
|
b = basedate,
|
|
|
|
r = recur,
|
2018-02-14 17:29:46 +00:00
|
|
|
c = comm)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
2017-12-03 20:18:29 +00:00
|
|
|
|
2018-02-20 12:16:31 +00:00
|
|
|
let mut empty = true;
|
2019-06-30 09:31:56 +00:00
|
|
|
let iter = habit
|
2017-12-03 20:18:29 +00:00
|
|
|
.linked_instances()
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
|
|
|
.trace_unwrap_exit()
|
2017-12-03 20:18:29 +00:00
|
|
|
.filter_map(|instance_id| {
|
|
|
|
debug!("Getting: {:?}", instance_id);
|
2019-02-05 00:37:32 +00:00
|
|
|
rt.store().get(instance_id).map_err_trace_exit_unwrap()
|
2018-02-20 12:16:31 +00:00
|
|
|
})
|
2019-06-30 09:31:56 +00:00
|
|
|
.enumerate();
|
|
|
|
|
|
|
|
// We need to drop here because we iterate over instances and in the
|
|
|
|
// instance_lister_fn() we call instance.get_comment(), which internally tries to
|
|
|
|
// Store::get() the template object.
|
|
|
|
// This would fail because the template is already borrowed.
|
|
|
|
drop(habit);
|
|
|
|
|
|
|
|
iter.for_each(|(i, e)| {
|
2018-02-20 12:16:31 +00:00
|
|
|
let mut v = vec![format!("{}", i)];
|
2019-06-30 09:31:56 +00:00
|
|
|
let mut instances = instance_lister_fn(&rt, &e);
|
2018-11-01 17:07:57 +00:00
|
|
|
|
|
|
|
{
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(e.get_location()).unwrap_or_exit();
|
2018-11-01 17:07:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-20 12:16:31 +00:00
|
|
|
v.append(&mut instances);
|
|
|
|
table.add_row(v.iter().map(|s| Cell::new(s)).collect());
|
|
|
|
empty = false;
|
2017-12-03 20:18:29 +00:00
|
|
|
});
|
|
|
|
|
2018-02-20 12:16:31 +00:00
|
|
|
if !empty {
|
2018-02-26 21:57:17 +00:00
|
|
|
let _ = table.print(&mut rt.stdout()).to_exit_code().unwrap_or_exit();
|
2018-02-20 12:16:31 +00:00
|
|
|
}
|
2017-12-03 20:18:29 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2017-10-21 12:12:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 17:41:54 +00:00
|
|
|
fn done(rt: &Runtime) {
|
|
|
|
let scmd = rt.cli().subcommand_matches("done").unwrap(); // safe by call from main()
|
|
|
|
let names : Vec<_> = scmd.values_of("done-name").unwrap().map(String::from).collect();
|
|
|
|
|
|
|
|
let today = ::chrono::offset::Local::today().naive_local();
|
|
|
|
|
|
|
|
let relevant : Vec<_> = { // scope, to have variable non-mutable in outer scope
|
|
|
|
let mut relevant : Vec<_> = rt
|
|
|
|
.store()
|
|
|
|
.all_habit_templates()
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
|
|
|
.trace_unwrap_exit()
|
2017-12-19 17:41:54 +00:00
|
|
|
.filter_map(|id| get_from_store(rt.store(), id))
|
|
|
|
.filter(|h| {
|
2019-02-05 00:37:32 +00:00
|
|
|
let due = h.next_instance_date().map_err_trace_exit_unwrap();
|
2017-12-22 11:26:29 +00:00
|
|
|
due.map(|d| (d == today || d < today) || scmd.is_present("allow-future"))
|
|
|
|
.unwrap_or(false)
|
2017-12-19 17:41:54 +00:00
|
|
|
})
|
|
|
|
.filter(|h| {
|
2019-02-05 00:37:32 +00:00
|
|
|
names.contains(&h.habit_name().map_err_trace_exit_unwrap())
|
2017-12-19 17:41:54 +00:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2017-12-22 11:26:29 +00:00
|
|
|
// unwrap is safe because we filtered above
|
2019-02-05 00:37:32 +00:00
|
|
|
relevant.sort_by_key(|h| h.next_instance_date().map_err_trace_exit_unwrap().unwrap());
|
2017-12-19 17:41:54 +00:00
|
|
|
relevant
|
|
|
|
};
|
|
|
|
|
2018-03-22 20:25:09 +00:00
|
|
|
for mut r in relevant {
|
2019-02-05 00:37:32 +00:00
|
|
|
let next_instance_name = r.habit_name().map_err_trace_exit_unwrap();
|
|
|
|
let next_instance_date = r.next_instance_date().map_err_trace_exit_unwrap();
|
2017-12-22 11:26:29 +00:00
|
|
|
if let Some(next) = next_instance_date {
|
|
|
|
debug!("Creating new instance on {:?}", next);
|
|
|
|
r.create_instance_with_date(rt.store(), &next)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2017-12-19 17:41:54 +00:00
|
|
|
|
2017-12-22 11:26:29 +00:00
|
|
|
info!("Done on {date}: {name}",
|
2018-01-31 21:04:54 +00:00
|
|
|
date = libimagutil::date::date_to_string(&next),
|
2017-12-22 11:26:29 +00:00
|
|
|
name = next_instance_name);
|
|
|
|
} else {
|
|
|
|
info!("Ignoring: {}, because there is no due date (the habit is finised)",
|
|
|
|
next_instance_name);
|
|
|
|
}
|
2017-12-19 17:41:54 +00:00
|
|
|
|
2018-11-01 17:07:57 +00:00
|
|
|
{
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(r.get_location()).unwrap_or_exit();
|
2018-11-01 17:07:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 17:41:54 +00:00
|
|
|
}
|
|
|
|
info!("Done.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function for `Iterator::filter_map()`ing `all_habit_templates()` and `Store::get` them.
|
|
|
|
fn get_from_store<'a>(store: &'a Store, id: StoreId) -> Option<FileLockEntry<'a>> {
|
|
|
|
match store.get(id.clone()) {
|
|
|
|
Ok(Some(h)) => Some(h),
|
|
|
|
Ok(None) => {
|
|
|
|
error!("No habit found for {:?}", id);
|
|
|
|
None
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
trace_error(&e);
|
|
|
|
None
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 11:26:29 +00:00
|
|
|
|
|
|
|
fn date_to_string_helper(d: chrono::NaiveDate) -> String {
|
2018-01-31 21:04:54 +00:00
|
|
|
libimagutil::date::date_to_string(&d)
|
2017-12-22 11:26:29 +00:00
|
|
|
}
|
|
|
|
|