@@ -129,34 +129,31 @@ def _split_cells(cls, segment: "Segment", cut: int) -> Tuple["Segment", "Segment
129129
130130 cell_size = get_character_cell_size
131131
132- pos = int ((cut / cell_length ) * (len (text ))) - 1
133- if pos < 0 :
134- pos = 0
132+ pos = int ((cut / cell_length ) * len (text ))
135133
136- before = text [:pos ]
137- cell_pos = cell_len (before )
138- if cell_pos == cut :
139- return (
140- _Segment (before , style , control ),
141- _Segment (text [pos :], style , control ),
142- )
143- while pos < len (text ):
144- char = text [pos ]
145- pos += 1
146- cell_pos += cell_size (char )
134+ while True :
147135 before = text [:pos ]
148- if cell_pos == cut :
136+ cell_pos = cell_len (before )
137+ out_by = cell_pos - cut
138+ if not out_by :
149139 return (
150140 _Segment (before , style , control ),
151141 _Segment (text [pos :], style , control ),
152142 )
153- if cell_pos > cut :
143+ if out_by == - 1 and cell_size ( text [ pos ]) == 2 :
154144 return (
155- _Segment (before [: pos - 1 ] + " " , style , control ),
145+ _Segment (text [:pos ] + " " , style , control ),
146+ _Segment (" " + text [pos + 1 :], style , control ),
147+ )
148+ if out_by == + 1 and cell_size (text [pos - 1 ]) == 2 :
149+ return (
150+ _Segment (text [: pos - 1 ] + " " , style , control ),
156151 _Segment (" " + text [pos :], style , control ),
157152 )
158-
159- raise AssertionError ("Will never reach here" )
153+ if cell_pos < cut :
154+ pos += 1
155+ else :
156+ pos -= 1
160157
161158 def split_cells (self , cut : int ) -> Tuple ["Segment" , "Segment" ]:
162159 """Split segment in to two segments at the specified column.
0 commit comments