ids can be show

This commit is contained in:
Abraham Toriz 2021-06-25 14:05:27 -05:00
parent caf7f900f8
commit 2ebe0a40a4
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
2 changed files with 39 additions and 8 deletions

View File

@ -76,6 +76,6 @@ impl<'a> Command<'a> for DisplayCommand {
}
};
args.format.print_formatted(sheets_to_display, out, Utc::now(), Local.offset_from_utc_datetime(&Utc::now().naive_utc()))
args.format.print_formatted(sheets_to_display, out, Utc::now(), Local.offset_from_utc_datetime(&Utc::now().naive_utc()), args.ids)
}
}

View File

@ -58,9 +58,9 @@ impl Formatter {
/// to the local timezone is given in `offset` to prevent this function from
/// using a secondary effect to retrieve the time and conver dates. This
/// also makes it easier to test.
pub fn print_formatted<W: Write, O: Offset>(&self, entries: Vec<Entry>, out: &mut W, now: DateTime<Utc>, offset: O) -> error::Result<()> {
pub fn print_formatted<W: Write, O: Offset>(&self, entries: Vec<Entry>, out: &mut W, now: DateTime<Utc>, offset: O, ids: bool) -> error::Result<()> {
match &self {
Formatter::Text => self.print_formatted_text(entries, out, now, offset)?,
Formatter::Text => self.print_formatted_text(entries, out, now, offset, ids)?,
Formatter::Custom(name) => {
}
}
@ -70,7 +70,7 @@ impl Formatter {
/// Print in the default text format. Assume entries are sorted by sheet and
/// then by start
fn print_formatted_text<W: Write, O: Offset>(&self, entries: Vec<Entry>, out: &mut W, now: DateTime<Utc>, offset: O) -> error::Result<()> {
fn print_formatted_text<W: Write, O: Offset>(&self, entries: Vec<Entry>, out: &mut W, now: DateTime<Utc>, offset: O, ids: bool) -> error::Result<()> {
let grouped_entries = entries.into_iter().group_by(|e| e.sheet.to_string());
// Build a timezone based on the offset given to this function. This
@ -91,7 +91,7 @@ impl Formatter {
for (date, entries) in entries_by_date.into_iter() {
let mut daily = Duration::seconds(0);
for (i,entry) in entries.into_iter().enumerate() {
for (i, entry) in entries.into_iter().enumerate() {
let start = format_start(fixed_offset.from_utc_datetime(&entry.start.naive_utc()).time());
let end = entry.end.map(|t| {
format_end(
@ -103,12 +103,14 @@ impl Formatter {
daily = daily + duration;
let duration = format_duration(duration);
let id = if ids { entry.id.to_string() } else { "".into() };
if i == 0 {
let date = date.format("%a %b %d, %Y").to_string();
lines.push(["".into(), date, start, end, duration, entry.note]);
lines.push([id, date, start, end, duration, entry.note]);
} else {
lines.push(["".into(), "".into(), start, end, duration, entry.note]);
lines.push([id, "".into(), start, end, duration, entry.note]);
}
}
@ -128,7 +130,7 @@ impl Formatter {
writeln!(out,
"{} {} {} {} {} {}",
lpad(" ", 3.max(lengths[0])),
if ids { lpad("ID", 3.max(lengths[0])) } else { lpad(" ", 3.max(lengths[0])) },
rpad("Day", 18),
rpad("Start", 10),
rpad("End", 10.max(lengths[3])),
@ -276,6 +278,35 @@ mod tests {
2:00:00
----------------------------------------------------------
Total 52:00:00
"));
}
#[test]
fn test_text_output_with_ids() {
let formatter = Formatter::Text;
let mut output = Vec::new();
let entries = vec![
Entry::new_sample(1, Utc.ymd(2008, 10, 3).and_hms(12, 0, 0), Some(Utc.ymd(2008, 10, 3).and_hms(14, 0, 0))),
Entry::new_sample(2, Utc.ymd(2008, 10, 3).and_hms(16, 0, 0), Some(Utc.ymd(2008, 10, 3).and_hms(18, 0, 0))),
Entry::new_sample(3, Utc.ymd(2008, 10, 5).and_hms(16, 0, 0), Some(Utc.ymd(2008, 10, 5).and_hms(18, 0, 0))),
Entry::new_sample(4, Utc.ymd(2008, 10, 5).and_hms(18, 0, 0), None),
];
let now = Utc.ymd(2008, 10, 5).and_hms(20, 0, 0);
let offset = Utc;
formatter.print_formatted(entries, &mut output, now, offset, true).unwrap();
assert_eq!(PrettyString(&String::from_utf8_lossy(&output)), PrettyString("Timesheet: default
ID Day Start End Duration Notes
1 Fri Oct 03, 2008 12:00:00 - 14:00:00 2:00:00 entry 1
2 16:00:00 - 18:00:00 2:00:00 entry 2
4:00:00
3 Sun Oct 05, 2008 16:00:00 - 18:00:00 2:00:00 entry 3
4 18:00:00 - 2:00:00 entry 4
4:00:00
---------------------------------------------------------
Total 8:00:00
"));
}
}