Skip to content

Commit

Permalink
Fix issue where height - totalvpad < 0 would cause render_lines crash
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed May 3, 2022
1 parent 0e8df8c commit 49d4c92
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
15 changes: 14 additions & 1 deletion rich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,11 @@ def render_lines(
_rendered = self.render(renderable, render_options)
if style:
_rendered = Segment.apply_style(_rendered, style)

render_height = render_options.height
if render_height is not None:
render_height = max(0, render_height)

lines = list(
islice(
Segment.split_and_crop_lines(
Expand All @@ -1340,7 +1345,7 @@ def render_lines(
pad=pad,
),
None,
render_options.height,
render_height,
)
)
if render_options.height is not None:
Expand Down Expand Up @@ -2460,3 +2465,11 @@ def _svg_hash(svg_main_code: str) -> str:
},
}
)
from rich.console import Console
from rich.padding import Padding

console = Console()
options = console.options.update_height(1)

padding = Padding("hello", pad=(1, 1, 1, 1))
console.render_lines(padding, options=options)
16 changes: 16 additions & 0 deletions tests/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)
from rich.control import Control
from rich.measure import measure_renderables
from rich.padding import Padding
from rich.pager import SystemPager
from rich.panel import Panel
from rich.region import Region
Expand Down Expand Up @@ -988,3 +989,18 @@ def __rich_console__(self, console, options):
expected = "╭──────────────────╮\n│ ╭──────────────╮ │\n│ │ foo │ │\n│ ╰──────────────╯ │\n│ ╭──────────────╮ │\n│ │ bar │ │\n│ ╰──────────────╯ │\n│ │\n│ │\n│ │\n│ │\n╰──────────────────╯\n"

assert result == expected


def test_render_lines_height_minus_vertical_pad_is_negative():
# https://github.com/Textualize/textual/issues/389
console = Console(
force_terminal=True,
color_system="truecolor",
width=20,
height=40,
legacy_windows=False,
)
options = console.options.update_height(1)

# Ensuring that no exception is raised...
console.render_lines(Padding("hello", pad=(1, 0)), options=options)

0 comments on commit 49d4c92

Please sign in to comment.