Truncate a string to a maximum length, adding ellipsis if needed.
(s: &str, max_len: usize)
| 518 | |
| 519 | /// Truncate a string to a maximum length, adding ellipsis if needed. |
| 520 | pub(crate) fn truncate_string(s: &str, max_len: usize) -> String { |
| 521 | let char_count = s.chars().count(); |
| 522 | if char_count <= max_len { |
| 523 | s.to_string() |
| 524 | } else if max_len <= 3 { |
| 525 | s.chars().take(max_len).collect() |
| 526 | } else { |
| 527 | let truncated: String = s.chars().take(max_len - 3).collect(); |
| 528 | format!("{}...", truncated) |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /// Format an author for display. |
| 533 | pub(crate) fn format_author(author: &Author) -> String { |