imag/bin/domain/imag-habit/src/main.rs

331 lines
11 KiB
Rust
Raw Normal View History

2017-10-21 12:12:21 +00:00
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// 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
//
#![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;
#[macro_use] extern crate version;
extern crate toml;
extern crate toml_query;
extern crate kairos;
extern crate chrono;
2017-10-21 12:12:21 +00:00
extern crate libimaghabit;
extern crate libimagstore;
extern crate libimagrt;
extern crate libimagerror;
extern crate libimagutil;
2017-12-03 17:46:33 +00:00
extern crate libimagentrylist;
2017-10-21 12:12:21 +00:00
use std::process::exit;
use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup;
use libimagerror::trace::{MapErrTrace, trace_error};
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;
use libimagentrylist::listers::table::TableLister;
use libimagentrylist::lister::Lister;
2017-10-21 12:12:21 +00:00
mod ui;
fn main() {
let rt = generate_runtime_setup("imag-habit",
&version!()[..],
"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),
"today" => today(&rt),
2017-10-21 12:12:21 +00:00
"show" => show(&rt),
_ => {
debug!("Unknown command"); // More error handling
exit(1)
},
}
2017-12-06 19:23:06 +00:00
})
.unwrap_or_else(|| today(&rt));
2017-10-21 12:12:21 +00:00
}
fn create(rt: &Runtime) {
2017-12-03 17:46:11 +00:00
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
let date = match ::kairos::parser::parse(date).map_err_trace_exit_unwrap(1) {
::kairos::parser::Parsed::TimeType(tt) => match tt.get_moment() {
Some(mom) => mom.date(),
None => {
error!("Error: 'date' parameter does not yield a point in time");
exit(1);
},
},
_ => {
error!("Error: 'date' parameter does not yield a point in time");
exit(1);
},
};
let _ = HabitBuilder::default()
.with_name(name)
.with_basedate(date)
.with_recurspec(recu)
.with_comment(comm)
.build(rt.store())
.map_err_trace_exit_unwrap(1);
info!("Ok");
2017-10-21 12:12:21 +00:00
}
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) {
use libimaghabit::error::ResultExt;
use libimaghabit::error::HabitErrorKind as HEK;
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 relevant : Vec<_> = { // scope, to have variable non-mutable in outer scope
let mut 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 || due > today // today or in future
})
.collect();
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!("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 {
TableLister::new(lister_fn)
.with_header(lister_header())
.with_idx(true)
.print_empty(false)
.list(relevant.into_iter())
.map_err_trace_exit_unwrap(1);
}
}
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);
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 due = h.next_instance_date().map_err_trace_exit_unwrap(1);
let due = libimaghabit::util::date_to_string(&due);
2017-12-03 17:46:33 +00:00
let v = vec![name, basedate, recur, comm, due];
debug!(" -> {:?}", v);
v
2017-12-03 17:46:33 +00:00
}
fn lister_header() -> Vec<String> {
["Name", "Basedate", "Recurr", "Comment", "Next Due"].iter().map(|x| String::from(*x)).collect()
2017-12-03 17:46:33 +00:00
}
let iter = 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
},
});
TableLister::new(lister_fn)
.with_header(lister_header())
.with_idx(true)
.print_empty(false)
2017-12-03 17:46:33 +00:00
.list(iter)
.map_err_trace_exit_unwrap(1);
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
fn instance_lister_header() -> Vec<String> {
["Date", "Comment"].iter().map(|x| String::from(*x)).collect()
}
fn instance_lister_fn(i: &FileLockEntry) -> Vec<String> {
use libimaghabit::util::date_to_string;
use libimaghabit::instance::HabitInstance;
let date = date_to_string(&i.get_date().map_err_trace_exit_unwrap(1));
let comm = i.get_comment().map_err_trace_exit_unwrap(1);
vec![date, comm]
}
let _ = 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!("Cannot get habit for {:?} in 'show' subcommand", id);
None
},
Err(e) => {
trace_error(&e);
None
},
})
.filter(|h| h.habit_name().map(|n| name == n).map_err_trace_exit_unwrap(1))
.enumerate()
.map(|(i, habit)| {
let name = habit.habit_name().map_err_trace_exit_unwrap(1);
let basedate = habit.habit_basedate().map_err_trace_exit_unwrap(1);
let recur = habit.habit_recur_spec().map_err_trace_exit_unwrap(1);
let comm = habit.habit_comment().map_err_trace_exit_unwrap(1);
println!("{i} - {name}\nBase : {b},\nRecurrence: {r}\nComment : {c}\n",
i = i,
name = name,
b = basedate,
r = recur,
c = comm);
let instances_iter = habit
.linked_instances()
.map_err_trace_exit_unwrap(1)
.filter_map(|instance_id| {
debug!("Getting: {:?}", instance_id);
rt.store().get(instance_id).map_err_trace_exit_unwrap(1)
});
TableLister::new(instance_lister_fn)
.with_header(instance_lister_header())
.with_idx(true)
.print_empty(false)
2017-12-03 20:18:29 +00:00
.list(instances_iter)
.map_err_trace_exit_unwrap(1);
})
.collect::<Vec<_>>();
2017-10-21 12:12:21 +00:00
}