← oracle_full

rich_3521

resolved RESOLVED UNSUBMITTED PASS · None tool calls · 0 s · Textualize/rich

Task input

Segment.split_cells fix

Fixes https://github.com/Textualize/textual/issues/5090

Tool calls (0)

#ToolArgumentsResult
No trace captured.

Patch

--- a/rich/segment.py
+++ b/rich/segment.py
@@ -129,34 +129,31 @@ def _split_cells(cls, segment: "Segment", cut: int) -> Tuple["Segment", "Segment
 
         cell_size = get_character_cell_size
 
-        pos = int((cut / cell_length) * (len(text))) - 1
-        if pos < 0:
-            pos = 0
+        pos = int((cut / cell_length) * len(text))
 
-        before = text[:pos]
-        cell_pos = cell_len(before)
-        if cell_pos == cut:
-            return (
-                _Segment(before, style, control),
-                _Segment(text[pos:], style, control),
-            )
-        while pos < len(text):
-            char = text[pos]
-            pos += 1
-            cell_pos += cell_size(char)
+        while True:
             before = text[:pos]
-            if cell_pos == cut:
+            cell_pos = cell_len(before)
+            out_by = cell_pos - cut
+            if not out_by:
                 return (
                     _Segment(before, style, control),
                     _Segment(text[pos:], style, control),
                 )
-            if cell_pos > cut:
+            if out_by == -1 and cell_size(text[pos]) == 2:
                 return (
-                    _Segment(before[: pos - 1] + " ", style, control),
+                    _Segment(text[:pos] + " ", style, control),
+                    _Segment(" " + text[pos + 1 :], style, control),
+                )
+            if out_by == +1 and cell_size(text[pos - 1]) == 2:
+                return (
+                    _Segment(text[: pos - 1] + " ", style, control),
                     _Segment(" " + text[pos:], style, control),
                 )
-
-        raise AssertionError("Will never reach here")
+            if cell_pos < cut:
+                pos += 1
+            else:
+                pos -= 1
 
     def split_cells(self, cut: int) -> Tuple["Segment", "Segment"]:
         """Split segment in to two segments at the specified column.

Test output

show
...........................................................              [100%]
59 passed in 0.14s