Truncate a string to a maximum length, adding ellipsis if needed.
(s: &str, max_len: usize)
| 782 | |
| 783 | /// Truncate a string to a maximum length, adding ellipsis if needed. |
| 784 | pub(crate) fn truncate_string(s: &str, max_len: usize) -> String { |
| 785 | let char_count = s.chars().count(); |
| 786 | if char_count <= max_len { |
| 787 | s.to_string() |
| 788 | } else if max_len <= 3 { |
| 789 | s.chars().take(max_len).collect() |
| 790 | } else { |
| 791 | let truncated: String = s.chars().take(max_len - 3).collect(); |
| 792 | format!("{}...", truncated) |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | /// Format an author for display. |
| 797 | pub(crate) fn format_author(author: &Author) -> String { |