Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions rich/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ def _split_cells(cls, segment: "Segment", cut: int) -> Tuple["Segment", "Segment

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

while True:
max_iterations = len(text) + 2 # safety guard against infinite loops
for _ in range(max_iterations):
if pos < 0:
pos = 0
if pos > len(text):
pos = len(text)
before = text[:pos]
cell_pos = cell_len(before)
out_by = cell_pos - cut
Expand All @@ -140,12 +145,12 @@ def _split_cells(cls, segment: "Segment", cut: int) -> Tuple["Segment", "Segment
_Segment(before, style, control),
_Segment(text[pos:], style, control),
)
if out_by == -1 and cell_size(text[pos]) == 2:
if out_by == -1 and pos < len(text) and cell_size(text[pos]) == 2:
return (
_Segment(text[:pos] + " ", style, control),
_Segment(" " + text[pos + 1 :], style, control),
)
if out_by == +1 and cell_size(text[pos - 1]) == 2:
if out_by == +1 and pos > 0 and cell_size(text[pos - 1]) == 2:
return (
_Segment(text[: pos - 1] + " ", style, control),
_Segment(" " + text[pos:], style, control),
Expand All @@ -155,6 +160,12 @@ def _split_cells(cls, segment: "Segment", cut: int) -> Tuple["Segment", "Segment
else:
pos -= 1

# Fallback: could not find exact cut position, split at closest
return (
_Segment(text[:pos], style, control),
_Segment(text[pos:], style, control),
)

def split_cells(self, cut: int) -> Tuple["Segment", "Segment"]:
"""Split segment in to two segments at the specified column.

Expand Down