Commit 54cb7991be

54cb7991be526e2b8cd45e0a5b7cdb4a6d2e310b

parent: c94d7d0ad7

Unregistered key

Ulyssa <git@ulyssa.dev> · 2024-08-15 03:48 UTC
committer: <noreply@github.com>

Fix underflow panics when using `TextPrinter::push_span_nobreak` (#322)

Layout: unified · split

src/message/printer.rs +16 −1
@@ -94,7 +94,7 @@ impl<'a> TextPrinter<'a> {
94 } 94 }
95 95
96 fn remaining(&self) -> usize { 96 fn remaining(&self) -> usize {
97 self.width - self.curr_width 97 self.width.saturating_sub(self.curr_width)
98 } 98 }
99 99
100 /// If there is any text on the current line, start a new one. 100 /// If there is any text on the current line, start a new one.
@@ -276,3 +276,18 @@ impl<'a> TextPrinter<'a> {
276 self.text 276 self.text
277 } 277 }
278} 278}
279
280#[cfg(test)]
281pub mod tests {
282 use super::*;
283
284 #[test]
285 fn test_push_nobreak() {
286 let mut printer = TextPrinter::new(5, Style::default(), false, false);
287 printer.push_span_nobreak("hello world".into());
288 let text = printer.finish();
289 assert_eq!(text.lines.len(), 1);
290 assert_eq!(text.lines[0].spans.len(), 1);
291 assert_eq!(text.lines[0].spans[0].content, "hello world");
292 }
293}