diff --git a/src/formatters/text.rs b/src/formatters/text.rs index ef197b9..223e009 100644 --- a/src/formatters/text.rs +++ b/src/formatters/text.rs @@ -98,7 +98,14 @@ pub fn print_formatted(entries: Vec, out: &mut W, now: DateTime // When array_map is stabilized this can be shortened let lengths = lines .iter() - .map(|[i, d, s, e, du, n]| [i.len(), d.len(), s.len(), e.len(), du.len(), n.len()]) + .map(|[i, d, s, e, du, n]| [ + i.chars().count(), + d.chars().count(), + s.chars().count(), + e.chars().count(), + du.chars().count(), + n.chars().count(), + ]) .reduce(|[a, b, c, d, e, f], [g, h, i, j, k, l]| { [a.max(g), b.max(h), c.max(i), d.max(j), e.max(k), f.max(l)] }).unwrap(); @@ -125,7 +132,7 @@ pub fn print_formatted(entries: Vec, out: &mut W, now: DateTime lpad(&duration, 8.max(lengths[4])), ); - let space_left = term_width.saturating_sub(first_line.len() + 1).max(40); + let space_left = term_width.saturating_sub(first_line.chars().count() + 1).max(40); let note_lines = constrained_lines(¬e, space_left); for (i, note_line) in note_lines.into_iter().enumerate() { @@ -136,11 +143,13 @@ pub fn print_formatted(entries: Vec, out: &mut W, now: DateTime writeln!(out, "{}", first_line)?; } } else { - writeln!(out, "{} {}", " ".repeat(first_line.len()), note_line)?; + writeln!(out, "{} {}", " ".repeat(first_line.chars().count()), note_line)?; } - if note_line.len() > max_note_length { - max_note_length = note_line.len(); + let chars = note_line.chars().count(); + + if chars > max_note_length { + max_note_length = chars; } } } @@ -379,6 +388,32 @@ mod tests { 2:00:00 ------------------------------------------------------------------- Total 2:00:00 +")); + } + + #[test] + fn note_with_accents() { + let mut output = Vec::new(); + let entries = vec![ + Entry { + id: 1, + sheet: "default".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, 100).unwrap(); + + assert_eq!(PrettyString(&String::from_utf8_lossy(&output)), PrettyString("Timesheet: default + 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 ")); } }