summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvan Martin <evan.martin@gmail.com>2023-10-10 10:44:10 -0700
committerEvan Martin <evan.martin@gmail.com>2023-10-10 10:44:10 -0700
commitb1102a68f87cb2a43b086708de45f7760e722ac3 (patch)
treeba317ece4a8f476f98958682b8b5c9fe74f62eea
parentfd9ee6c9e8be3f723d8f0e88fcf3e064188437c6 (diff)
downloadn2-b1102a68f87cb2a43b086708de45f7760e722ac3.tar.gz
add a test for utf8 truncation
(Followup to #86)
-rw-r--r--src/progress.rs22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/progress.rs b/src/progress.rs
index 111daa7..2fa53ff 100644
--- a/src/progress.rs
+++ b/src/progress.rs
@@ -317,12 +317,7 @@ impl FancyState {
lines += 1;
if let Some(line) = &task.last_line {
let max_len = max_cols - 2;
- let substring = if line.len() >= max_len {
- &line[..char_boundary(line, max_len)]
- } else {
- line
- };
- println!(" {}", substring);
+ println!(" {}", truncate(line, max_len));
lines += 1;
}
}
@@ -356,14 +351,14 @@ fn task_message(message: &str, seconds: usize, max_cols: usize) -> String {
out
}
-fn char_boundary(s: &str, mut max: usize) -> usize {
+fn truncate(s: &str, mut max: usize) -> &str {
if max >= s.len() {
- return max;
+ return s;
}
while !s.is_char_boundary(max) {
max -= 1;
}
- max
+ &s[..max]
}
/// Render a StateCounts as an ASCII progress bar.
@@ -448,4 +443,13 @@ mod tests {
assert_eq!(task_message("building foo.o", 5, 80), "building foo.o (5s)");
assert_eq!(task_message("building foo.o", 5, 10), "bu... (5s)");
}
+
+ #[test]
+ fn truncate_utf8() {
+ let text = "utf8 progress bar: ━━━━━━━━━━━━";
+ for len in 10..text.len() {
+ // test passes if this doesn't panic
+ truncate(text, len);
+ }
+ }
}