From f5fc8197ffd0cdc17432e4f8c0cc9368ccef692b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Wed, 5 Feb 2025 02:02:50 +0100 Subject: [PATCH] fix: avoid extra line break on whitespace only lines when wrapping paragraphs (#1636) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- ratatui-widgets/src/paragraph.rs | 28 ++++++++++++++++++++++++++++ ratatui-widgets/src/reflow.rs | 1 + 2 files changed, 29 insertions(+) diff --git a/ratatui-widgets/src/paragraph.rs b/ratatui-widgets/src/paragraph.rs index d697b76c..b0f049c9 100644 --- a/ratatui-widgets/src/paragraph.rs +++ b/ratatui-widgets/src/paragraph.rs @@ -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( + ¶graph, + &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."; diff --git a/ratatui-widgets/src/reflow.rs b/ratatui-widgets/src/reflow.rs index 4aefa9e0..46a5f8c3 100644 --- a/ratatui-widgets/src/reflow.rs +++ b/ratatui-widgets/src/reflow.rs @@ -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![]); }