add the grand total to format::text

This commit is contained in:
Abraham Toriz 2021-07-16 17:31:30 -05:00
parent 6c532c73c4
commit 7292e1d103
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
2 changed files with 69 additions and 6 deletions

View File

@ -215,9 +215,4 @@ mod tests {
String::new(),
);
}
#[test]
fn displays_grand_total_when_multiple_sheets() {
unimplemented!()
}
}

View File

@ -35,8 +35,15 @@ fn format_end(start: NaiveDateTime, end: NaiveDateTime) -> String {
/// then by start
pub fn print_formatted<W: Write>(entries: Vec<Entry>, out: &mut W, now: DateTime<Utc>, 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<W: Write>(entries: Vec<Entry>, 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
"));
}
}