Add duration listing in imag-timetrack

This patch adds the -d / --show-duration feature to the "list"
subcommand, which makes imag-timetrack list the duration of the
timetracking in the output.

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-05-17 22:28:42 +02:00
parent fad684daf2
commit 3884d7319c
3 changed files with 49 additions and 9 deletions

View file

@ -78,14 +78,16 @@ pub fn list(rt: &Runtime) -> i32 {
let end = gettime(&cmd, "end-time"); let end = gettime(&cmd, "end-time");
let list_not_ended = cmd.is_present("list-not-ended"); let list_not_ended = cmd.is_present("list-not-ended");
let show_duration = cmd.is_present("show-duration");
list_impl(rt, start, end, list_not_ended) list_impl(rt, start, end, list_not_ended, show_duration)
} }
pub fn list_impl(rt: &Runtime, pub fn list_impl(rt: &Runtime,
start: Option<NaiveDateTime>, start: Option<NaiveDateTime>,
end: Option<NaiveDateTime>, end: Option<NaiveDateTime>,
list_not_ended: bool) list_not_ended: bool,
show_duration: bool)
-> i32 -> i32
{ {
@ -119,7 +121,12 @@ pub fn list_impl(rt: &Runtime,
let filter = start_time_filter.and(end_time_filter); let filter = start_time_filter.and(end_time_filter);
let mut table = Table::new(); let mut table = Table::new();
table.set_titles(Row::new(["Tag", "Start", "End"].into_iter().map(|s| Cell::new(s)).collect())); 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);
let mut table_empty = true; let mut table_empty = true;
@ -142,20 +149,45 @@ pub fn list_impl(rt: &Runtime,
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
let v = match (start, end) { let v = match (start, end) {
(None, _) => vec![String::from(tag.as_str()), String::from(""), String::from("")], (None, _) => {
let mut v = vec![String::from(tag.as_str()), String::from(""), String::from("")];
if show_duration {
v.push(String::from(""));
}
v
},
(Some(s), None) => { (Some(s), None) => {
vec![ let mut v = vec![
String::from(tag.as_str()), String::from(tag.as_str()),
format!("{}", s), format!("{}", s),
String::from(""), String::from(""),
] ];
if show_duration {
v.push(String::from(""));
}
v
}, },
(Some(s), Some(e)) => { (Some(s), Some(e)) => {
vec![ let mut v = vec![
String::from(tag.as_str()), String::from(tag.as_str()),
format!("{}", s), format!("{}", s),
format!("{}", e), format!("{}", e),
] ];
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
}, },
}; };

View file

@ -107,7 +107,7 @@ fn main() {
} else { } else {
let start = ::chrono::offset::Local::today().naive_local().and_hms(0, 0, 0); let start = ::chrono::offset::Local::today().naive_local().and_hms(0, 0, 0);
let end = ::chrono::offset::Local::today().naive_local().and_hms(23, 59, 59); let end = ::chrono::offset::Local::today().naive_local().and_hms(23, 59, 59);
list_impl(&rt, Some(start), Some(end), false) list_impl(&rt, Some(start), Some(end), false, false)
}; };
::std::process::exit(retval); ::std::process::exit(retval);

View file

@ -46,6 +46,14 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.multiple(false) .multiple(false)
.required(false) .required(false)
.help("List not yet ended timetrackings even if after 'end-time'")) .help("List not yet ended timetrackings even if after 'end-time'"))
.arg(Arg::with_name("show-duration")
.short("d")
.long("show-duration")
.takes_value(false)
.multiple(false)
.required(false)
.help("Show durations of ended timetrackings"))
) )
.subcommand(SubCommand::with_name("start") .subcommand(SubCommand::with_name("start")