Commit a3fa2d1655
Unregistered key
Layout: unified · split
src/message/printer.rs +30 −1
| @@ -11,6 +11,7 @@ use crate::util::{ | ||
| 11 | 11 | replace_emojis_in_span, |
| 12 | 12 | replace_emojis_in_str, |
| 13 | 13 | space_span, |
| 14 | split_cow, | |
| 14 | 15 | take_width, |
| 15 | 16 | }; |
| 16 | 17 | |
| @@ -176,7 +177,20 @@ impl<'a> TextPrinter<'a> { | ||
| 176 | 177 | } |
| 177 | 178 | |
| 178 | 179 | // Take a leading portion of the text that fits in the line. |
| 179 | let ((s0, w), s1) = take_width(cow, self.remaining()); | |
| 180 | let ((mut s0, mut w), mut s1) = take_width(cow, self.remaining()); | |
| 181 | ||
| 182 | if s0.is_empty() && self.curr_width == 0 { | |
| 183 | // The line is narrower than the next grapheme. Place it on its own line anyway, | |
| 184 | // since otherwise no progress would ever be made. | |
| 185 | let len = UnicodeSegmentation::graphemes(s1.as_ref(), true) | |
| 186 | .next() | |
| 187 | .map_or(0, str::len); | |
| 188 | let (first, rest) = split_cow(s1, len); | |
| 189 | w = UnicodeWidthStr::width(first.as_ref()); | |
| 190 | s0 = first; | |
| 191 | s1 = rest; | |
| 192 | } | |
| 193 | ||
| 180 | 194 | cow = s1; |
| 181 | 195 | |
| 182 | 196 | self.curr_spans.push(Span::styled(s0, style)); |
| @@ -316,4 +330,19 @@ pub mod tests { | ||
| 316 | 330 | assert_eq!(text.lines[0].spans.len(), 1); |
| 317 | 331 | assert_eq!(text.lines[0].spans[0].content, "hello world"); |
| 318 | 332 | } |
| 333 | ||
| 334 | #[test] | |
| 335 | fn test_push_str_wide_char_wider_than_width() { | |
| 336 | let settings = mock_settings(); | |
| 337 | let info = mock_room(); | |
| 338 | let mut printer = TextPrinter::new(1, Style::default(), &settings, &info); | |
| 339 | printer.push_str("a世b", Style::default()); | |
| 340 | let text = printer.finish(); | |
| 341 | let lines: Vec<String> = text | |
| 342 | .lines | |
| 343 | .iter() | |
| 344 | .map(|line| line.spans.iter().map(|span| span.content.as_ref()).collect()) | |
| 345 | .collect(); | |
| 346 | assert_eq!(lines, vec!["a", "世", "b"]); | |
| 347 | } | |
| 319 | 348 | } |