2017-07-20 21:21:57 +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-07-20 21:21:57 +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-01-13 13:31:24 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2017-07-20 21:21:57 +00:00
|
|
|
use filters::filter::Filter;
|
2018-01-13 02:19:41 +00:00
|
|
|
use prettytable::Table;
|
2018-09-30 13:09:47 +00:00
|
|
|
use prettytable::Row;
|
|
|
|
use prettytable::Cell;
|
2018-02-11 20:25:33 +00:00
|
|
|
use kairos::parser::Parsed;
|
|
|
|
use kairos::parser::parse as kairos_parse;
|
|
|
|
use clap::ArgMatches;
|
2018-10-30 17:40:53 +00:00
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::ResultExt;
|
|
|
|
use failure::err_msg;
|
|
|
|
use failure::Error;
|
2017-07-20 21:21:57 +00:00
|
|
|
|
|
|
|
use libimagerror::trace::trace_error;
|
|
|
|
use libimagerror::trace::MapErrTrace;
|
|
|
|
use libimagerror::iter::TraceIterator;
|
2019-02-05 00:37:32 +00:00
|
|
|
use libimagerror::exit::ExitUnwrap;
|
2017-07-20 21:21:57 +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;
|
2017-07-20 21:21:57 +00:00
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
|
|
|
|
|
|
|
pub fn list(rt: &Runtime) -> i32 {
|
|
|
|
let (_, cmd) = rt.cli().subcommand();
|
|
|
|
let cmd = cmd.unwrap(); // checked in main()
|
|
|
|
|
2018-02-11 20:25:33 +00:00
|
|
|
let gettime = |cmd: &ArgMatches, name| {
|
|
|
|
match cmd.value_of(name).map(kairos_parse) {
|
|
|
|
Some(Ok(Parsed::TimeType(tt))) => match tt.calculate() {
|
|
|
|
Ok(tt) => {
|
|
|
|
let dt = tt.get_moment().unwrap_or_else(|| {
|
|
|
|
error!("Failed to get date from '{}'", cmd.value_of(name).unwrap());
|
|
|
|
::std::process::exit(1)
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(dt.clone())
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to calculate date from '{}': {:?}",
|
|
|
|
cmd.value_of(name).unwrap(), e);
|
|
|
|
::std::process::exit(1)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Some(Ok(Parsed::Iterator(_))) => {
|
|
|
|
error!("Expected single point in time, got '{}', which yields a list of dates", cmd.value_of(name).unwrap());
|
|
|
|
::std::process::exit(1)
|
|
|
|
},
|
|
|
|
Some(Err(e)) => {
|
2018-10-30 17:40:53 +00:00
|
|
|
let e = Error::from(e);
|
2018-02-11 20:25:33 +00:00
|
|
|
trace_error(&e);
|
|
|
|
::std::process::exit(1)
|
|
|
|
}
|
|
|
|
None => None,
|
2017-07-20 21:21:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-11 20:25:33 +00:00
|
|
|
let start = gettime(&cmd, "start-time");
|
|
|
|
let end = gettime(&cmd, "end-time");
|
|
|
|
|
2017-07-20 21:21:57 +00:00
|
|
|
let list_not_ended = cmd.is_present("list-not-ended");
|
2019-05-17 20:28:42 +00:00
|
|
|
let show_duration = cmd.is_present("show-duration");
|
2017-07-20 21:21:57 +00:00
|
|
|
|
2019-05-17 20:28:42 +00:00
|
|
|
list_impl(rt, start, end, list_not_ended, show_duration)
|
2018-01-13 13:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list_impl(rt: &Runtime,
|
|
|
|
start: Option<NaiveDateTime>,
|
|
|
|
end: Option<NaiveDateTime>,
|
2019-05-17 20:28:42 +00:00
|
|
|
list_not_ended: bool,
|
|
|
|
show_duration: bool)
|
2018-01-13 13:31:24 +00:00
|
|
|
-> i32
|
|
|
|
{
|
|
|
|
|
2017-07-20 21:21:57 +00:00
|
|
|
let start_time_filter = |timetracking: &FileLockEntry| {
|
|
|
|
start.map(|s| match timetracking.get_start_datetime() {
|
|
|
|
Ok(Some(dt)) => dt >= s,
|
|
|
|
Ok(None) => {
|
|
|
|
warn!("Funny things are happening: Timetracking has no start time");
|
|
|
|
false
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
trace_error(&e);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_or(true)
|
|
|
|
};
|
|
|
|
|
|
|
|
let end_time_filter = |timetracking: &FileLockEntry| {
|
2017-08-30 21:19:47 +00:00
|
|
|
end.map(|s| match timetracking.get_end_datetime() {
|
2017-07-20 21:21:57 +00:00
|
|
|
Ok(Some(dt)) => dt <= s,
|
|
|
|
Ok(None) => list_not_ended,
|
|
|
|
Err(e) => {
|
|
|
|
trace_error(&e);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_or(true)
|
|
|
|
};
|
|
|
|
|
|
|
|
let filter = start_time_filter.and(end_time_filter);
|
|
|
|
|
2018-01-13 02:19:41 +00:00
|
|
|
let mut table = Table::new();
|
2019-05-17 20:28:42 +00:00
|
|
|
let title_row = if !show_duration {
|
|
|
|
Row::new(["Tag", "Start", "End"].iter().map(|s| Cell::new(s)).collect())
|
|
|
|
} else {
|
|
|
|
Row::new(["Tag", "Start", "End", "Duration"].iter().map(|s| Cell::new(s)).collect())
|
|
|
|
};
|
|
|
|
table.set_titles(title_row);
|
2018-01-13 02:19:41 +00:00
|
|
|
|
2019-02-06 00:35:01 +00:00
|
|
|
let mut table_empty = true;
|
|
|
|
|
|
|
|
let table = rt.store()
|
2017-07-20 21:21:57 +00:00
|
|
|
.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))
|
|
|
|
.fold(Ok(table), |acc: Result<_>, e| {
|
|
|
|
acc.and_then(|mut tab: Table| {
|
|
|
|
debug!("Processing {:?}", e.get_location());
|
|
|
|
|
|
|
|
let tag = e.get_timetrack_tag()?;
|
|
|
|
debug!(" -> tag = {:?}", tag);
|
|
|
|
|
|
|
|
let start = e.get_start_datetime()?;
|
|
|
|
debug!(" -> start = {:?}", start);
|
|
|
|
|
|
|
|
let end = e.get_end_datetime()?;
|
|
|
|
debug!(" -> end = {:?}", end);
|
|
|
|
|
|
|
|
let v = match (start, end) {
|
2019-05-17 20:28:42 +00:00
|
|
|
(None, _) => {
|
|
|
|
let mut v = vec![String::from(tag.as_str()), String::from(""), String::from("")];
|
|
|
|
if show_duration {
|
|
|
|
v.push(String::from(""));
|
|
|
|
}
|
|
|
|
v
|
|
|
|
},
|
2018-02-11 18:58:53 +00:00
|
|
|
(Some(s), None) => {
|
2019-05-17 20:28:42 +00:00
|
|
|
let mut v = vec![
|
2018-02-11 18:58:53 +00:00
|
|
|
String::from(tag.as_str()),
|
|
|
|
format!("{}", s),
|
|
|
|
String::from(""),
|
2019-05-17 20:28:42 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if show_duration {
|
|
|
|
v.push(String::from(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
v
|
2018-02-11 18:58:53 +00:00
|
|
|
},
|
|
|
|
(Some(s), Some(e)) => {
|
2019-05-17 20:28:42 +00:00
|
|
|
let mut v = vec![
|
2018-02-11 18:58:53 +00:00
|
|
|
String::from(tag.as_str()),
|
|
|
|
format!("{}", s),
|
|
|
|
format!("{}", e),
|
2019-05-17 20:28:42 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if show_duration {
|
|
|
|
let duration = e - s;
|
|
|
|
let dur = format!("{days} Days, {hours} Hours, {minutes} Minutes, {seconds} Seconds",
|
|
|
|
days = duration.num_days(),
|
|
|
|
hours = duration.num_hours(),
|
|
|
|
minutes = duration.num_minutes(),
|
|
|
|
seconds = duration.num_seconds());
|
|
|
|
|
|
|
|
v.push(dur);
|
|
|
|
}
|
|
|
|
|
|
|
|
v
|
2018-02-11 18:58:53 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let cells : Vec<Cell> = v
|
|
|
|
.into_iter()
|
|
|
|
.map(|s| Cell::new(&s))
|
|
|
|
.collect();
|
|
|
|
tab.add_row(Row::new(cells));
|
|
|
|
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(e.get_location()).unwrap_or_exit();
|
2018-11-02 10:23:48 +00:00
|
|
|
|
2019-02-06 00:35:01 +00:00
|
|
|
table_empty = false;
|
2018-02-11 18:58:53 +00:00
|
|
|
Ok(tab)
|
|
|
|
})
|
2017-07-20 21:21:57 +00:00
|
|
|
})
|
2019-02-06 00:35:01 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
|
|
|
|
|
|
|
if !table_empty {
|
|
|
|
table.print(&mut rt.stdout())
|
|
|
|
.context(err_msg("Failed to print table"))
|
|
|
|
.map_err(Error::from)
|
|
|
|
.map(|_| 0)
|
|
|
|
.map_err_trace()
|
|
|
|
.unwrap_or(1)
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
2017-07-20 21:21:57 +00:00
|
|
|
}
|
|
|
|
|