rich icon indicating copy to clipboard operation
rich copied to clipboard

[BUG] spinning forever in _split_cells with negative cut

Open gatesn opened this issue 1 year ago • 3 comments

See https://github.com/tconbeer/harlequin/issues/659 Started as of: https://github.com/Textualize/rich/pull/3521 in 13.9.2

Somehow, the _split_cells function is passed a negative value for cut and it sits in an infinite loop.

I don't really understand what this function is doing, but this patch worked well for me!

@@ -122,11 +122,11 @@
             A tuple of two segments.
         """
         text, style, control = segment
         _Segment = Segment
         cell_length = segment.cell_length
-        if cut >= cell_length:
+        if cut < 0 or cut >= cell_length:
             return segment, _Segment("", style, control)

         cell_size = get_character_cell_size

         pos = int((cut / cell_length) * len(text))

gatesn avatar Oct 19 '24 07:10 gatesn

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

github-actions[bot] avatar Oct 19 '24 07:10 github-actions[bot]

That may make the error go away, but it is unlikely to be the fix.

cut shouldn't be negative. I'd need to know what conditions led to split_cells being called with a negative value.

willmcgugan avatar Oct 19 '24 11:10 willmcgugan

So it gets passed down from here: https://github.com/Textualize/textual/blob/6f5eb419a6dd48aa1727677360f67ad405c0bd51/src/textual/widgets/_text_area.py#L1218

Where scroll_x is 0, virtual_width is 0, and gutter_width is 3.

I suppose another place to enforce this could be in the crop function: https://github.com/Textualize/textual/blob/6f5eb419a6dd48aa1727677360f67ad405c0bd51/src/textual/strip.py#L375-L376

Where the start is clamped to zero, but the end isn't clamped to the start?

        start = max(0, start)
+       end = max(start, end)
        end = self.cell_length if end is None else min(self.cell_length, end)

gatesn avatar Oct 19 '24 11:10 gatesn

The issue was fixed in Textual. Thanks for the report.

willmcgugan avatar Oct 24 '24 14:10 willmcgugan

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

github-actions[bot] avatar Oct 24 '24 14:10 github-actions[bot]

Thanks, @willmcgugan !

tconbeer avatar Oct 25 '24 17:10 tconbeer