Truncate a string to a maximum length, adding ellipsis if needed.
(s: &str, max_len: usize)
| 816 | |
| 817 | /// Truncate a string to a maximum length, adding ellipsis if needed. |
| 818 | pub(crate) fn truncate_string(s: &str, max_len: usize) -> String { |
| 819 | let char_count = s.chars().count(); |
| 820 | if char_count <= max_len { |
| 821 | s.to_string() |
| 822 | } else if max_len <= 3 { |
| 823 | s.chars().take(max_len).collect() |
| 824 | } else { |
| 825 | let truncated: String = s.chars().take(max_len - 3).collect(); |
| 826 | format!("{}...", truncated) |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | /// Format an author for display. |
| 831 | pub(crate) fn format_author(author: &Author) -> String { |