From 54cb7991be526e2b8cd45e0a5b7cdb4a6d2e310b Mon Sep 17 00:00:00 2001 From: Ulyssa Date: Wed, 14 Aug 2024 20:48:04 -0700 Subject: [PATCH] Fix underflow panics when using `TextPrinter::push_span_nobreak` (#322) --- src/message/printer.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/message/printer.rs b/src/message/printer.rs index e981609..12d03be 100644 --- a/src/message/printer.rs +++ b/src/message/printer.rs @@ -94,7 +94,7 @@ impl<'a> TextPrinter<'a> { } fn remaining(&self) -> usize { - self.width - self.curr_width + self.width.saturating_sub(self.curr_width) } /// If there is any text on the current line, start a new one. @@ -276,3 +276,18 @@ impl<'a> TextPrinter<'a> { self.text } } + +#[cfg(test)] +pub mod tests { + use super::*; + + #[test] + fn test_push_nobreak() { + let mut printer = TextPrinter::new(5, Style::default(), false, false); + printer.push_span_nobreak("hello world".into()); + let text = printer.finish(); + assert_eq!(text.lines.len(), 1); + assert_eq!(text.lines[0].spans.len(), 1); + assert_eq!(text.lines[0].spans[0].content, "hello world"); + } +}