2017-06-26 15:47:12 +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-06-26 15:47:12 +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-02-14 17:21:00 +00:00
|
|
|
use std::io::Write;
|
2017-08-25 13:04:54 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
use filters::filter::Filter;
|
|
|
|
use chrono::NaiveDateTime;
|
2018-10-30 17:40:53 +00:00
|
|
|
use failure::Error;
|
2017-08-25 13:04:54 +00:00
|
|
|
|
|
|
|
use libimagerror::trace::trace_error;
|
|
|
|
use libimagerror::trace::MapErrTrace;
|
|
|
|
use libimagerror::iter::TraceIterator;
|
2018-02-14 17:21:00 +00:00
|
|
|
use libimagerror::io::ToExitCode;
|
2019-02-05 00:37:32 +00:00
|
|
|
use libimagerror::exit::ExitUnwrap;
|
2017-08-25 13:04:54 +00:00
|
|
|
use libimagstore::store::FileLockEntry;
|
2019-06-26 16:45:47 +00:00
|
|
|
use libimagtimetrack::store::TimeTrackStore;
|
2017-08-27 12:56:37 +00:00
|
|
|
use libimagtimetrack::timetracking::TimeTracking;
|
|
|
|
use libimagtimetrack::tag::TimeTrackingTag;
|
|
|
|
use libimagtimetrack::iter::filter::*;
|
2017-08-25 13:04:54 +00:00
|
|
|
|
2017-06-26 15:47:12 +00:00
|
|
|
use libimagrt::runtime::Runtime;
|
|
|
|
|
|
|
|
pub fn year(rt: &Runtime) -> i32 {
|
2017-08-25 13:04:54 +00:00
|
|
|
let cmd = rt.cli().subcommand().1.unwrap(); // checked in main
|
|
|
|
|
|
|
|
let filter = {
|
|
|
|
use chrono::offset::Local;
|
|
|
|
use chrono::naive::NaiveDate;
|
|
|
|
use chrono::Datelike;
|
|
|
|
|
|
|
|
let now = Local::now();
|
|
|
|
|
2018-02-11 18:35:20 +00:00
|
|
|
let start = match cmd
|
|
|
|
.value_of("start")
|
|
|
|
.map(|s| {
|
2018-10-30 17:40:53 +00:00
|
|
|
::chrono::naive::NaiveDateTime::from_str(s).map_err(Error::from)
|
2018-02-11 18:35:20 +00:00
|
|
|
})
|
|
|
|
{
|
2017-08-25 13:04:54 +00:00
|
|
|
None => NaiveDate::from_ymd(now.year(), 1, 1).and_hms(0, 0, 0),
|
|
|
|
Some(Ok(dt)) => dt,
|
|
|
|
Some(Err(e)) => {
|
|
|
|
trace_error(&e);
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-11 18:35:20 +00:00
|
|
|
let end = match cmd
|
|
|
|
.value_of("end")
|
|
|
|
.map(|s| {
|
2018-10-30 17:40:53 +00:00
|
|
|
::chrono::naive::NaiveDateTime::from_str(s).map_err(Error::from)
|
2018-02-11 18:35:20 +00:00
|
|
|
})
|
|
|
|
{
|
2017-08-25 13:04:54 +00:00
|
|
|
None => {
|
|
|
|
NaiveDate::from_ymd(now.year() + 1, 1, 1).and_hms(0, 0, 0)
|
|
|
|
},
|
|
|
|
Some(Ok(dt)) => dt,
|
|
|
|
Some(Err(e)) => {
|
|
|
|
trace_error(&e);
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let tags = cmd
|
|
|
|
.values_of("tags")
|
2019-08-27 08:15:26 +00:00
|
|
|
.map(|ts| ts.map(String::from).map(TimeTrackingTag::from).collect::<Vec<_>>());
|
2017-08-25 13:04:54 +00:00
|
|
|
|
|
|
|
let start_time_filter = has_start_time_where(move |dt: &NaiveDateTime| {
|
|
|
|
start <= *dt
|
|
|
|
});
|
|
|
|
|
|
|
|
let end_time_filter = has_end_time_where(move |dt: &NaiveDateTime| {
|
|
|
|
end >= *dt
|
|
|
|
});
|
|
|
|
|
|
|
|
let tags_filter = move |fle: &FileLockEntry| {
|
|
|
|
match tags {
|
|
|
|
Some(ref tags) => has_one_of_tags(&tags).filter(fle),
|
|
|
|
None => true,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
tags_filter.and(start_time_filter).and(end_time_filter)
|
|
|
|
};
|
|
|
|
|
2018-02-26 21:57:17 +00:00
|
|
|
let mut out = rt.stdout();
|
2017-08-25 13:04:54 +00:00
|
|
|
rt.store()
|
|
|
|
.get_timetrackings()
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-02-11 18:58:53 +00:00
|
|
|
.trace_unwrap()
|
|
|
|
.filter(|e| filter.filter(e))
|
2018-10-30 17:40:53 +00:00
|
|
|
.map(|e| -> Result<_, Error> {
|
2018-02-14 17:21:00 +00:00
|
|
|
debug!("Processing {:?}", e.get_location());
|
2018-02-11 18:58:53 +00:00
|
|
|
|
2018-02-14 17:21:00 +00:00
|
|
|
let tag = e.get_timetrack_tag()?;
|
|
|
|
debug!(" -> tag = {:?}", tag);
|
2018-02-11 18:58:53 +00:00
|
|
|
|
2018-02-14 17:21:00 +00:00
|
|
|
let start = e.get_start_datetime()?;
|
|
|
|
debug!(" -> start = {:?}", start);
|
2018-02-11 18:58:53 +00:00
|
|
|
|
2018-02-14 17:21:00 +00:00
|
|
|
let end = e.get_end_datetime()?;
|
|
|
|
debug!(" -> end = {:?}", end);
|
2018-02-11 18:58:53 +00:00
|
|
|
|
2019-08-27 08:44:03 +00:00
|
|
|
rt.report_touched(e.get_location()).unwrap_or_exit();
|
2018-11-02 10:23:48 +00:00
|
|
|
|
2018-02-14 17:21:00 +00:00
|
|
|
Ok((tag, start, end))
|
|
|
|
})
|
2019-02-05 00:37:32 +00:00
|
|
|
.trace_unwrap_exit()
|
2018-02-14 17:21:00 +00:00
|
|
|
.map(|(tag, start, end)| {
|
|
|
|
match (start, end) {
|
|
|
|
(None, _) => writeln!(out, "{} has no start time.", tag),
|
|
|
|
(Some(s), None) => writeln!(out, "{} | {} - ...", tag, s),
|
|
|
|
(Some(s), Some(e)) => writeln!(out, "{} | {} - {}", tag, s, e),
|
|
|
|
}
|
|
|
|
.to_exit_code()
|
2017-08-25 13:04:54 +00:00
|
|
|
})
|
2018-02-14 17:21:00 +00:00
|
|
|
.collect::<Result<Vec<()>, _>>()
|
2017-08-25 13:04:54 +00:00
|
|
|
.map(|_| 0)
|
2018-02-14 17:21:00 +00:00
|
|
|
.unwrap_or_else(|e| e.code())
|
2017-06-26 15:47:12 +00:00
|
|
|
}
|
|
|
|
|