@@ -129,34 +129,31 @@ def _split_cells(cls, segment: "Segment", cut: int) -> Tuple["Segment", "Segment
129
129
130
130
cell_size = get_character_cell_size
131
131
132
- pos = int ((cut / cell_length ) * (len (text ))) - 1
133
- if pos < 0 :
134
- pos = 0
132
+ pos = int ((cut / cell_length ) * len (text ))
135
133
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 :
147
135
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 :
149
139
return (
150
140
_Segment (before , style , control ),
151
141
_Segment (text [pos :], style , control ),
152
142
)
153
- if cell_pos > cut :
143
+ if out_by == - 1 and cell_size (text [pos ]) == 2 :
144
+ return (
145
+ _Segment (before [:pos ] + " " , style , control ),
146
+ _Segment (" " + text [pos + 1 :], style , control ),
147
+ )
148
+ if out_by == + 1 and cell_size (text [pos ]) == 2 :
154
149
return (
155
150
_Segment (before [: pos - 1 ] + " " , style , control ),
156
151
_Segment (" " + text [pos :], style , control ),
157
152
)
158
-
159
- raise AssertionError ("Will never reach here" )
153
+ if cell_pos < cut :
154
+ pos += 1
155
+ else :
156
+ pos -= 1
160
157
161
158
def split_cells (self , cut : int ) -> Tuple ["Segment" , "Segment" ]:
162
159
"""Split segment in to two segments at the specified column.
0 commit comments