fix(panel): fix title missing panel background

Fix `Panel` title missing the panel background style.

This really just reverts the change in 7a38204. There's a history of
issues related to the panel title styles, so I was careful to run all
the examples in those issues to ensure there wasn't any regression.

Fixes #3569
This commit is contained in:
TomJGooding 2025-05-01 21:49:07 +01:00
parent 69e1618f1f
commit 30e5ed61a6
3 changed files with 33 additions and 4 deletions

View File

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased
### Fixed
- Fixed `Panel` title missing the panel background style https://github.com/Textualize/rich/issues/3569
## [14.0.0] - 2025-03-30
### Added

View File

@ -146,8 +146,7 @@ class Panel(JupyterMixin):
Padding(self.renderable, _padding) if any(_padding) else self.renderable
)
style = console.get_style(self.style)
partial_border_style = console.get_style(self.border_style)
border_style = style + partial_border_style
border_style = style + console.get_style(self.border_style)
width = (
options.max_width
if self.width is None
@ -206,7 +205,7 @@ class Panel(JupyterMixin):
title_text = self._title
if title_text is not None:
title_text.stylize_before(partial_border_style)
title_text.stylize_before(border_style)
child_width = (
width - 2
@ -255,7 +254,7 @@ class Panel(JupyterMixin):
subtitle_text = self._subtitle
if subtitle_text is not None:
subtitle_text.stylize_before(partial_border_style)
subtitle_text.stylize_before(border_style)
if subtitle_text is None or width <= 4:
yield Segment(box.get_bottom([width - 2]), border_style)

View File

@ -148,6 +148,30 @@ def test_title_text_with_border_color() -> None:
assert result == expected
def test_title_text_with_panel_background() -> None:
"""Regression test for https://github.com/Textualize/rich/issues/3569"""
panel = Panel(
"Hello, World",
style="on blue",
title=Text("title", style="red"),
subtitle=Text("subtitle", style="magenta bold"),
)
console = Console(
file=io.StringIO(),
width=50,
height=20,
legacy_windows=False,
force_terminal=True,
color_system="truecolor",
)
console.print(panel)
result = console.file.getvalue()
print(repr(result))
expected = "\x1b[44m╭─\x1b[0m\x1b[44m───────────────────\x1b[0m\x1b[31;44m title \x1b[0m\x1b[44m────────────────────\x1b[0m\x1b[44m─╮\x1b[0m\n\x1b[44m│\x1b[0m\x1b[44m \x1b[0m\x1b[44mHello, World\x1b[0m\x1b[44m \x1b[0m\x1b[44m \x1b[0m\x1b[44m│\x1b[0m\n\x1b[44m╰─\x1b[0m\x1b[44m──────────────────\x1b[0m\x1b[1;35;44m subtitle \x1b[0m\x1b[44m──────────────────\x1b[0m\x1b[44m─╯\x1b[0m\n"
assert result == expected
if __name__ == "__main__":
expected = []
for panel in tests: