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