fix background style with soft wrap

This commit is contained in:
Will McGugan 2026-01-23 15:04:35 +00:00
parent 05ff970926
commit 39ee57dfe7
2 changed files with 23 additions and 5 deletions

View File

@ -1723,12 +1723,13 @@ class Console:
for renderable in renderables:
extend(render(renderable, render_options))
else:
render_style = self.get_style(style)
new_line = Segment.line()
for renderable in renderables:
extend(
Segment.apply_style(
render(renderable, render_options), self.get_style(style)
)
)
for line in Segment.split_lines(render(renderable, render_options)):
extend(Segment.apply_style(line, render_style))
new_segments.append(new_line)
if new_line_start:
if (
len("".join(segment.text for segment in new_segments).splitlines())

View File

@ -1110,3 +1110,20 @@ def test_soft_wrap() -> None:
print(repr(output))
expected = "\x1b[37;44m Hello World \x1b[0m\n"
assert output == expected
def test_soft_wrap_styled() -> None:
"""Regression test for https://github.com/Textualize/rich/issues/3838
If soft_wrap is True and a style is set, we don't want to style the new lines.
"""
console = Console(color_system="standard", width=80, force_terminal=True)
with console.capture() as capture:
console.print("soft wrap is on", style="blue on white", soft_wrap=True)
console.print("Next line")
output = capture.get()
print(repr(output))
# Background is reset before \n
expected = "\x1b[34;47msoft wrap is on\x1b[0m\nNext line\n"
assert output == expected