Truncate a string to a maximum length, adding ellipsis if needed.
(s: &str, max_len: usize)
| 758 | |
| 759 | /// Truncate a string to a maximum length, adding ellipsis if needed. |
| 760 | pub(crate) fn truncate_string(s: &str, max_len: usize) -> String { |
| 761 | let char_count = s.chars().count(); |
| 762 | if char_count <= max_len { |
| 763 | s.to_string() |
| 764 | } else if max_len <= 3 { |
| 765 | s.chars().take(max_len).collect() |
| 766 | } else { |
| 767 | let truncated: String = s.chars().take(max_len - 3).collect(); |
| 768 | format!("{}...", truncated) |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | /// Format an author for display. |
| 773 | pub(crate) fn format_author(author: &Author) -> String { |