Truncate a string to a maximum length, adding ellipsis if needed.
(s: &str, max_len: usize)
| 772 | |
| 773 | /// Truncate a string to a maximum length, adding ellipsis if needed. |
| 774 | pub(crate) fn truncate_string(s: &str, max_len: usize) -> String { |
| 775 | let char_count = s.chars().count(); |
| 776 | if char_count <= max_len { |
| 777 | s.to_string() |
| 778 | } else if max_len <= 3 { |
| 779 | s.chars().take(max_len).collect() |
| 780 | } else { |
| 781 | let truncated: String = s.chars().take(max_len - 3).collect(); |
| 782 | format!("{}...", truncated) |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | /// Format an author for display. |
| 787 | pub(crate) fn format_author(author: &Author) -> String { |