| 2 | use unicode_width::UnicodeWidthStr; |
| 3 | |
| 4 | pub fn truncate(text: &str, max_width: usize) -> String { |
| 5 | let width = UnicodeWidthStr::width(text); |
| 6 | if width <= max_width { |
| 7 | return text.to_string(); |
| 8 | } |
| 9 | |
| 10 | let mut result = String::new(); |
| 11 | let mut current_width = 0; |
| 12 | |
| 13 | for ch in text.chars() { |
| 14 | let ch_str = ch.to_string(); |
| 15 | let ch_width = UnicodeWidthStr::width(ch_str.as_str()); |
| 16 | if current_width + ch_width + 3 > max_width { |
| 17 | break; |
| 18 | } |
| 19 | result.push(ch); |
| 20 | current_width += ch_width; |
| 21 | } |
| 22 | |
| 23 | result.push_str("..."); |
| 24 | result |
| 25 | } |
| 26 | |
| 27 | pub fn pad_left(text: &str, width: usize) -> String { |
| 28 | let text_width = text.width(); |