Skip to content

Commit 55f267d

Browse files
committed
fix infinite loop in cropping
1 parent e3b1948 commit 55f267d

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

src/textual/strip.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,13 @@ def crop(self, start: int, end: int | None = None) -> Strip:
372372
Returns:
373373
A new Strip.
374374
"""
375+
375376
start = max(0, start)
376377
end = self.cell_length if end is None else min(self.cell_length, end)
377378
if start == 0 and end == self.cell_length:
378379
return self
380+
if end <= start:
381+
return Strip([], 0)
379382
cache_key = (start, end)
380383
cached = self._crop_cache.get(cache_key)
381384
if cached is not None:

src/textual/widgets/_text_area.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,9 +1236,7 @@ def render_line(self, y: int) -> Strip:
12361236

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

12431241
# Stylize the line the cursor is currently on.
12441242
if cursor_row == line_index:

tests/test_strip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def test_crop():
131131

132132
assert Strip([Segment("foo")]).crop(1, 3) == Strip([Segment("oo")])
133133
assert Strip([Segment("foo")]).crop(1, 2) == Strip([Segment("o")])
134-
assert Strip([Segment("foo")]).crop(1, 1) == Strip([Segment("")])
134+
assert Strip([Segment("foo")]).crop(1, 1) == Strip([])
135135

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

0 commit comments

Comments
 (0)