Commit a3fa2d1655

a3fa2d16557fd8310a78f94acb0de6e02625f98d

parent: 750f118cfc

Unregistered key

Steve Jason <145211128+56steve@users.noreply.github.com> · 2026-11-12 10:09 UTC
committer: <noreply@github.com>

Fix infinite loop when wrapping a wide character in a one column printer (#859)

Layout: unified · split

src/message/printer.rs +30 −1
@@ -11,6 +11,7 @@ use crate::util::{
11 replace_emojis_in_span, 11 replace_emojis_in_span,
12 replace_emojis_in_str, 12 replace_emojis_in_str,
13 space_span, 13 space_span,
14 split_cow,
14 take_width, 15 take_width,
15}; 16};
16 17
@@ -176,7 +177,20 @@ impl<'a> TextPrinter<'a> {
176 } 177 }
177 178
178 // Take a leading portion of the text that fits in the line. 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 cow = s1; 194 cow = s1;
181 195
182 self.curr_spans.push(Span::styled(s0, style)); 196 self.curr_spans.push(Span::styled(s0, style));
@@ -316,4 +330,19 @@ pub mod tests {
316 assert_eq!(text.lines[0].spans.len(), 1); 330 assert_eq!(text.lines[0].spans.len(), 1);
317 assert_eq!(text.lines[0].spans[0].content, "hello world"); 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}