Skip to content

Commit

Permalink
Fix underflow panics when using TextPrinter::push_span_nobreak (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulyssa authored Aug 15, 2024
1 parent c94d7d0 commit 54cb799
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/message/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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");
}
}

0 comments on commit 54cb799

Please sign in to comment.