Truncate a string to a maximum length, adding ellipsis if needed.
(s: &str, max_len: usize)
| 793 | |
| 794 | /// Truncate a string to a maximum length, adding ellipsis if needed. |
| 795 | pub(crate) fn truncate_string(s: &str, max_len: usize) -> String { |
| 796 | let char_count = s.chars().count(); |
| 797 | if char_count <= max_len { |
| 798 | s.to_string() |
| 799 | } else if max_len <= 3 { |
| 800 | s.chars().take(max_len).collect() |
| 801 | } else { |
| 802 | let truncated: String = s.chars().take(max_len - 3).collect(); |
| 803 | format!("{}...", truncated) |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | /// Format an author for display. |
| 808 | pub(crate) fn format_author(author: &Author) -> String { |