Skip to content

Commit

Permalink
fix infinite loop in cropping
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Oct 22, 2024
1 parent e3b1948 commit 55f267d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/textual/strip.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,13 @@ def crop(self, start: int, end: int | None = None) -> Strip:
Returns:
A new Strip.
"""

start = max(0, start)
end = self.cell_length if end is None else min(self.cell_length, end)
if start == 0 and end == self.cell_length:
return self
if end <= start:
return Strip([], 0)
cache_key = (start, end)
cached = self._crop_cache.get(cache_key)
if cached is not None:
Expand Down
4 changes: 1 addition & 3 deletions src/textual/widgets/_text_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -1236,9 +1236,7 @@ def render_line(self, y: int) -> Strip:

# Crop the line to show only the visible part (some may be scrolled out of view)
if not self.soft_wrap:
text_strip = text_strip.crop(
scroll_x, scroll_x + virtual_width - gutter_width
)
text_strip = text_strip.crop(scroll_x, scroll_x + virtual_width)

# Stylize the line the cursor is currently on.
if cursor_row == line_index:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_strip.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_crop():

assert Strip([Segment("foo")]).crop(1, 3) == Strip([Segment("oo")])
assert Strip([Segment("foo")]).crop(1, 2) == Strip([Segment("o")])
assert Strip([Segment("foo")]).crop(1, 1) == Strip([Segment("")])
assert Strip([Segment("foo")]).crop(1, 1) == Strip([])

assert Strip([Segment("foo💩"), Segment("b💩ar"), Segment("ba💩z")]).crop(
1, 6
Expand Down

0 comments on commit 55f267d

Please sign in to comment.