diff --git a/CHANGELOG.md b/CHANGELOG.md index a994765609..1f24a4ea7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ 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] ### Changed @@ -17,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed extraction of recursive exceptions https://github.com/Textualize/rich/pull/3772 - Fixed padding applied to Syntax https://github.com/Textualize/rich/pull/3782 +- Fixed `Panel` title missing the panel background style https://github.com/Textualize/rich/issues/3569 ### Added diff --git a/rich/panel.py b/rich/panel.py index d411e29153..07587e3b33 100644 --- a/rich/panel.py +++ b/rich/panel.py @@ -146,8 +146,7 @@ def __rich_console__( 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 @@ def align_text( 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 @@ def align_text( 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) diff --git a/tests/test_panel.py b/tests/test_panel.py index 65eb9bc0ab..33ce1afa14 100644 --- a/tests/test_panel.py +++ b/tests/test_panel.py @@ -124,6 +124,54 @@ def test_title_text() -> None: assert result == expected +def test_title_text_with_border_color() -> None: + """Regression test for https://github.com/Textualize/rich/issues/2745""" + panel = Panel( + "Hello, World", + border_style="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[34m╭─\x1b[0m\x1b[34m───────────────────\x1b[0m\x1b[31m title \x1b[0m\x1b[34m────────────────────\x1b[0m\x1b[34m─╮\x1b[0m\n\x1b[34m│\x1b[0m Hello, World \x1b[34m│\x1b[0m\n\x1b[34m╰─\x1b[0m\x1b[34m──────────────────\x1b[0m\x1b[1;35m subtitle \x1b[0m\x1b[34m──────────────────\x1b[0m\x1b[34m─╯\x1b[0m\n" + 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: