fix: avoid extra line break on whitespace only lines when wrapping paragraphs (#1636)

Currently whitespace only lines produces an extra line break when
trimming is disabled, because both the trimmed as well as the
non-trimmed line get inserted. Fix this by only inserting the
non-trimmed one.

Co-authored-by: Björn Steinbrink <b.steinbrink@demv.de>
This commit is contained in:
Björn Steinbrink 2025-02-05 02:02:50 +01:00 committed by GitHub
parent 9f399ac7a6
commit f5fc8197ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 0 deletions

View File

@ -778,6 +778,34 @@ mod tests {
);
}
#[test]
fn test_render_wrapped_paragraph_with_whitespace_only_line() {
let text: Text = ["A", " ", "B", " a", "C"].into_iter().map(Line::from).collect();
let paragraph = Paragraph::new(text.clone()).wrap(Wrap { trim: false });
let trimmed_paragraph = Paragraph::new(text).wrap(Wrap { trim: true });
test_case(
&paragraph,
&Buffer::with_lines([
"A",
" ",
"B",
" a",
"C",
]),
);
test_case(
&trimmed_paragraph,
&Buffer::with_lines([
"A",
"",
"B",
"a",
"C",
]),
);
}
#[test]
fn test_render_paragraph_with_line_truncation() {
let text = "This is a long line of text that should be truncated.";

View File

@ -167,6 +167,7 @@ where
if pending_line.is_empty()
&& self.pending_word.is_empty()
&& !self.pending_whitespace.is_empty()
&& self.trim
{
self.wrapped_lines.push_back(vec![]);
}