From ba2806e7f472d6c8e0118ce143562594be7f747b Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 23 Jan 2026 13:30:56 +0000 Subject: [PATCH 1/4] don't strip whitespace when soft_wrap is True --- rich/text.py | 10 +++++----- tests/test_text.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/rich/text.py b/rich/text.py index b5b114b42e..9ac3ec2521 100644 --- a/rich/text.py +++ b/rich/text.py @@ -691,7 +691,6 @@ def __rich_console__( ) -> Iterable[Segment]: tab_size: int = console.tab_size if self.tab_size is None else self.tab_size justify = self.justify or options.justify or DEFAULT_JUSTIFY - overflow = self.overflow or options.overflow or DEFAULT_OVERFLOW lines = self.wrap( @@ -1232,10 +1231,11 @@ def wrap( if "\t" in line: line.expand_tabs(tab_size) if no_wrap: - new_lines = Lines([line]) - else: - offsets = divide_line(str(line), width, fold=wrap_overflow == "fold") - new_lines = line.divide(offsets) + lines.append(line) + continue + + offsets = divide_line(str(line), width, fold=wrap_overflow == "fold") + new_lines = line.divide(offsets) for line in new_lines: line.rstrip_end(width) if wrap_justify: diff --git a/tests/test_text.py b/tests/test_text.py index 93a98d7c0a..b528608ad7 100644 --- a/tests/test_text.py +++ b/tests/test_text.py @@ -1092,3 +1092,21 @@ def test_append_loop_regression() -> None: b = Text("two", "blue") b.append_text(b) assert b.plain == "twotwo" + + +def test_soft_wrap() -> None: + """Regression test for https://github.com/Textualize/rich/issues/3841 + + Soft wrap should not strip trailing whitespace. + + """ + console = Console(color_system="standard", width=80, force_terminal=True) + text = Text(" Hello World ", style="white on blue") + + with console.capture() as capture: + console.print(text, soft_wrap=True) + + output = capture.get() + print(repr(output)) + expected = "\x1b[37;44m Hello World \x1b[0m\n" + assert output == expected From c2514fb22c1bb3287a79b035c1b17b16183482b4 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 23 Jan 2026 13:31:59 +0000 Subject: [PATCH 2/4] typo --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7b0398864..6e200399dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,7 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Fixed exception when callling `inspect` on objects with unusual `__qualname__` attribute https://github.com/Textualize/rich/pull/3894 +- Fixed exception when calling `inspect` on objects with unusual `__qualname__` attribute https://github.com/Textualize/rich/pull/3894 ## [14.1.0] - 2025-06-25 From b9040928de20454342b58e9ecaabeba092b74626 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 23 Jan 2026 13:37:35 +0000 Subject: [PATCH 3/4] changelog --- CHANGELOG.md | 1 + rich/text.py | 16 +++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e200399dc..57db5c696d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - IPython now respects when a `Console` instance is passed to `pretty.install` https://github.com/Textualize/rich/pull/3915 - Fixed extraneous blank line on non-interactive disabled `Progress` https://github.com/Textualize/rich/pull/3905 - Fixed extra padding on first cell in columns https://github.com/Textualize/rich/pull/3935 +- Fixed trailing whitespace removed when soft_wrap=True ### Added diff --git a/rich/text.py b/rich/text.py index 9ac3ec2521..60109dd9e8 100644 --- a/rich/text.py +++ b/rich/text.py @@ -1231,13 +1231,15 @@ def wrap( if "\t" in line: line.expand_tabs(tab_size) if no_wrap: - lines.append(line) - continue - - offsets = divide_line(str(line), width, fold=wrap_overflow == "fold") - new_lines = line.divide(offsets) - for line in new_lines: - line.rstrip_end(width) + if overflow == "ignore": + lines.append(line) + continue + new_lines = Lines([line]) + else: + offsets = divide_line(str(line), width, fold=wrap_overflow == "fold") + new_lines = line.divide(offsets) + for line in new_lines: + line.rstrip_end(width) if wrap_justify: new_lines.justify( console, width, justify=wrap_justify, overflow=wrap_overflow From 4d0cbb74a8cc5c7dd9af3056499a43efbd13355a Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 23 Jan 2026 13:38:07 +0000 Subject: [PATCH 4/4] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57db5c696d..a3b06bdaef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - IPython now respects when a `Console` instance is passed to `pretty.install` https://github.com/Textualize/rich/pull/3915 - Fixed extraneous blank line on non-interactive disabled `Progress` https://github.com/Textualize/rich/pull/3905 - Fixed extra padding on first cell in columns https://github.com/Textualize/rich/pull/3935 -- Fixed trailing whitespace removed when soft_wrap=True +- Fixed trailing whitespace removed when soft_wrap=True https://github.com/Textualize/rich/pull/3937 ### Added