Merge pull request #1686 from Textualize/fix-renderable-width

fix for render width
This commit is contained in:
Will McGugan
2023-01-30 13:06:16 +01:00
committed by GitHub
5 changed files with 184 additions and 3 deletions

View File

@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Programmatically setting `overflow_x`/`overflow_y` refreshes the layout correctly https://github.com/Textualize/textual/issues/1616
- Fixed double-paste into `Input` https://github.com/Textualize/textual/issues/1657
- Added a workaround for an apparent Windows Terminal paste issue https://github.com/Textualize/textual/issues/1661
- Fixes issue with renderable width calculation https://github.com/Textualize/textual/issues/1685
## [0.10.1] - 2023-01-20

View File

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

View File

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

File diff suppressed because one or more lines are too long

View File

@@ -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")