respect text.style in panels

This commit is contained in:
Will McGugan 2024-09-30 19:48:50 +01:00
parent 7022e20224
commit 508c0086f8
3 changed files with 33 additions and 8 deletions

View File

@ -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 = (

View File

@ -173,7 +173,7 @@ class Text(JupyterMixin):
return self.plain
def __repr__(self) -> str:
return f"<text {self.plain!r} {self._spans!r}>"
return f"<text {self.plain!r} {self._spans!r} {self.style!r}>"
def __add__(self, other: Any) -> "Text":
if isinstance(other, (str, Text)):

View File

@ -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: