From 7292e1d103151e17cba9c604bf912bf2f34e8bdd Mon Sep 17 00:00:00 2001 From: Abraham Toriz Date: Fri, 16 Jul 2021 17:31:30 -0500 Subject: [PATCH] add the grand total to format::text --- src/commands/display.rs | 5 --- src/formatters/text.rs | 70 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/commands/display.rs b/src/commands/display.rs index 913d184..61b7996 100644 --- a/src/commands/display.rs +++ b/src/commands/display.rs @@ -215,9 +215,4 @@ mod tests { String::new(), ); } - - #[test] - fn displays_grand_total_when_multiple_sheets() { - unimplemented!() - } } diff --git a/src/formatters/text.rs b/src/formatters/text.rs index 2d9ccff..63cbe09 100644 --- a/src/formatters/text.rs +++ b/src/formatters/text.rs @@ -35,8 +35,15 @@ fn format_end(start: NaiveDateTime, end: NaiveDateTime) -> String { /// then by start pub fn print_formatted(entries: Vec, out: &mut W, now: DateTime, ids: bool) -> Result<()> { let grouped_entries = entries.into_iter().group_by(|e| e.sheet.to_string()); + let mut num_sheets = 0; + let mut grand_total = Duration::seconds(0); + + for (i, (key, group)) in grouped_entries.into_iter().enumerate() { + num_sheets += 1; + if i != 0 { + writeln!(out)?; + } - for (key, group) in grouped_entries.into_iter() { writeln!(out, "Timesheet: {}", key)?; // A vector of lines to be printed, with all the components @@ -97,6 +104,23 @@ pub fn print_formatted(entries: Vec, out: &mut W, now: DateTime tabs.separator('-'); tabs.feed(vec!["".into(), "Total".into(), "".into(), format_duration(total)]); + grand_total = grand_total + total; + + out.write_all(tabs.print().as_bytes())?; + } + + if num_sheets > 1 { + let mut tabs = Tabulate::with_columns(vec![ + Col::min_width(3).and_alignment(Right), + Col::min_width(18).and_alignment(Left), + Col::min_width(21).and_alignment(Left), + Col::min_width(8).and_alignment(Right), + Col::min_width(5).max_width(44).and_alignment(Left), + ]); + + tabs.separator('-'); + + tabs.feed(vec!["".into(), "Grand total".into(), "".into(), format_duration(grand_total)]); out.write_all(tabs.print().as_bytes())?; } @@ -302,6 +326,50 @@ mod tests { 2:00:00 -------------------------------------------------------------- Total 2:00:00 +")); + } + + #[test] + fn displays_grand_total_when_multiple_sheets() { + std::env::set_var("TZ", "UTC"); + + let mut output = Vec::new(); + let entries = vec![ + Entry { + id: 1, + sheet: "sheet1".into(), + start: Utc.ymd(2008, 10, 5).and_hms(16, 0, 0), + end: Some(Utc.ymd(2008, 10, 5).and_hms(18, 0, 0)), + note: Some("quiúbole".into()), + }, + Entry { + id: 2, + sheet: "sheet2".into(), + start: Utc.ymd(2008, 10, 5).and_hms(16, 0, 0), + end: Some(Utc.ymd(2008, 10, 5).and_hms(18, 0, 0)), + note: Some("quiúbole".into()), + }, + ]; + + let now = Utc.ymd(2008, 10, 5).and_hms(20, 0, 0); + + print_formatted(entries, &mut output, now, false).unwrap(); + + assert_eq!(PrettyString(&String::from_utf8_lossy(&output)), PrettyString("Timesheet: sheet1 + Day Start End Duration Notes + Sun Oct 05, 2008 16:00:00 - 18:00:00 2:00:00 quiúbole + 2:00:00 +-------------------------------------------------------------- + Total 2:00:00 + +Timesheet: sheet2 + Day Start End Duration Notes + Sun Oct 05, 2008 16:00:00 - 18:00:00 2:00:00 quiúbole + 2:00:00 +-------------------------------------------------------------- + Total 2:00:00 +----------------------------------------------------------- + Grand total 4:00:00 ")); } }