diff --git a/rich/panel.py b/rich/panel.py index 77fdc6a4..8cfa6f4a 100644 --- a/rich/panel.py +++ b/rich/panel.py @@ -175,6 +175,9 @@ class Panel(JupyterMixin): text = text.copy() text.truncate(width) excess_space = width - cell_len(text.plain) + if text.style: + text.stylize(console.get_style(text.style)) + if excess_space: if align == "left": return Text.assemble( @@ -203,8 +206,6 @@ class Panel(JupyterMixin): title_text = self._title if title_text is not None: - if title_text.style is not None: - title_text.stylize_before(title_text.style) title_text.stylize_before(partial_border_style) child_width = ( diff --git a/rich/text.py b/rich/text.py index 7d9c9413..91f30f96 100644 --- a/rich/text.py +++ b/rich/text.py @@ -173,7 +173,7 @@ class Text(JupyterMixin): return self.plain def __repr__(self) -> str: - return f"" + return f"" def __add__(self, other: Any) -> "Text": if isinstance(other, (str, Text)): diff --git a/tests/test_panel.py b/tests/test_panel.py index 5ae3babe..2a1286a4 100644 --- a/tests/test_panel.py +++ b/tests/test_panel.py @@ -6,6 +6,7 @@ from rich.console import Console from rich.panel import Panel from rich.segment import Segment from rich.style import Style +from rich.text import Text tests = [ Panel("Hello, World", padding=0), @@ -31,15 +32,17 @@ expected = [ def render(panel, width=50) -> str: console = Console(file=io.StringIO(), width=50, legacy_windows=False) console.print(panel) - return console.file.getvalue() + result = console.file.getvalue() + print(result) + return result @pytest.mark.parametrize("panel,expected", zip(tests, expected)) -def test_render_panel(panel, expected): +def test_render_panel(panel, expected) -> None: assert render(panel) == expected -def test_console_width(): +def test_console_width() -> None: console = Console(file=io.StringIO(), width=50, legacy_windows=False) panel = Panel("Hello, World", expand=False) min_width, max_width = panel.__rich_measure__(console, console.options) @@ -47,7 +50,7 @@ def test_console_width(): assert max_width == 16 -def test_fixed_width(): +def test_fixed_width() -> None: console = Console(file=io.StringIO(), width=50, legacy_windows=False) panel = Panel("Hello World", width=20) min_width, max_width = panel.__rich_measure__(console, console.options) @@ -55,7 +58,7 @@ def test_fixed_width(): assert max_width == 20 -def test_render_size(): +def test_render_size() -> None: console = Console(width=63, height=46, legacy_windows=False) options = console.options.update_dimensions(80, 4) lines = console.render_lines(Panel("foo", title="Hello"), options=options) @@ -99,6 +102,27 @@ def test_render_size(): assert lines == expected +def test_title_text() -> None: + panel = Panel( + "Hello, World", + title=Text("title", style="red"), + subtitle=Text("subtitle", style="magenta bold"), + ) + console = Console( + file=io.StringIO(), + width=50, + legacy_windows=False, + force_terminal=True, + color_system="truecolor", + ) + console.print(panel) + + result = console.file.getvalue() + print(repr(result)) + expected = "╭───────────────────────────────────\x1b[31m title \x1b[0m────────────────────────────────────╮\n│ Hello, World │\n╰──────────────────────────────────\x1b[1;35m subtitle \x1b[0m──────────────────────────────────╯\n" + assert result == expected + + if __name__ == "__main__": expected = [] for panel in tests: