From b4a3c2e8bb9f9e8d2658cf3f76c188ad5d15738a Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Sat, 28 Jan 2023 17:23:52 +0100 Subject: [PATCH] fix for render width --- src/textual/render.py | 18 +- src/textual/widget.py | 4 +- .../__snapshots__/test_snapshots.ambr | 159 ++++++++++++++++++ tests/snapshot_tests/test_snapshots.py | 5 + 4 files changed, 183 insertions(+), 3 deletions(-) diff --git a/src/textual/render.py b/src/textual/render.py index 611ee4420..6d5dbc326 100644 --- a/src/textual/render.py +++ b/src/textual/render.py @@ -2,13 +2,20 @@ from rich.console import Console, RenderableType from rich.protocol import rich_cast -def measure(console: Console, renderable: RenderableType, default: int) -> int: +def measure( + console: Console, + renderable: RenderableType, + default: int, + *, + container_width: int | None = None +) -> int: """Measure a rich renderable. Args: console: A console object. renderable: Rich renderable. default: Default width to use if renderable does not expose dimensions. + container_width: Width of container or None to use console width. Returns: Width in cells @@ -17,6 +24,13 @@ def measure(console: Console, renderable: RenderableType, default: int) -> int: renderable = rich_cast(renderable) get_console_width = getattr(renderable, "__rich_measure__", None) if get_console_width is not None: - render_width = get_console_width(console, console.options).maximum + render_width = get_console_width( + console, + ( + console.options + if container_width is None + else console.options.update_width(container_width) + ), + ).maximum width = max(0, render_width) return width diff --git a/src/textual/widget.py b/src/textual/widget.py index ca34cd05b..a71889031 100644 --- a/src/textual/widget.py +++ b/src/textual/widget.py @@ -795,7 +795,9 @@ class Widget(DOMNode): console = self.app.console renderable = self._render() - width = measure(console, renderable, container.width) + width = measure( + console, renderable, container.width, container_width=container.width + ) if self.expand: width = max(container.width, width) if self.shrink: diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr index 52b617982..df2a02df9 100644 --- a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr +++ b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr @@ -12380,6 +12380,165 @@ ''' # --- +# name: test_label_wrap + ''' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + LabelWrap + + + + + + + + + + + + + + + + Apple Banana Cherry Mango Fig Guava Pineapple:Dragon Unicorn Centaur Phoenix Ch + + + Apple Banana Cherry Mango Fig Guava Pineapple:Dragon Unicorn Centaur Phoenix  + Chimera Castle + + + ╭────────────────────────────────────────────────────────────────────────────╮ + Apple Banana Cherry Mango Fig Guava Pineapple:Dragon Unicorn Centaur  + Phoenix Chimera Castle + ╰────────────────────────────────────────────────────────────────────────────╯ + + + + + + + + + + + + ''' +# --- # name: test_layers ''' diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index 868bd91aa..7374899cf 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -193,3 +193,8 @@ def test_demo(snap_compare): press=["down", "down", "down", "_", "_", "_"], terminal_size=(100, 30), ) + + +def test_label_wrap(snap_compare): + """Test Label wrapping with a Panel""" + assert snap_compare("snapshot_apps/label_wrap.py")