Truncate `s` to at most `max` bytes, appending "..." when it was cut. The cut point walks back to a UTF-8 character boundary: slicing at an arbitrary byte offset panics when the index lands inside a multi-byte character, and recorded text (change messages, provenance commands, file lines) routinely contains em-dashes or other non-ASCII content. Use [`Table::truncate`] instead when the limit is a
(s: &str, max: usize)
| 76 | /// lines) routinely contains em-dashes or other non-ASCII content. Use |
| 77 | /// [`Table::truncate`] instead when the limit is a display width, not bytes. |
| 78 | pub fn truncate_bytes(s: &str, max: usize) -> String { |
| 79 | if s.len() <= max { |
| 80 | return s.to_string(); |
| 81 | } |
| 82 | let mut end = max.saturating_sub(3); |
| 83 | while end > 0 && !s.is_char_boundary(end) { |
| 84 | end -= 1; |
| 85 | } |
| 86 | format!("{}...", &s[..end]) |
| 87 | } |
| 88 | |
| 89 | // Convenience Print Functions |
| 90 |