Split text by cell position. If the cell position falls within a double width character, it is converted to two spaces. Args: text: Text to split. cell_position Offset in cells. unicode_version: Unicode version, `"auto"` to auto detect, `"latest"` for the latest uni
(
text: str, cell_position: int, unicode_version: str = "auto"
)
| 233 | |
| 234 | |
| 235 | def _split_text( |
| 236 | text: str, cell_position: int, unicode_version: str = "auto" |
| 237 | ) -> tuple[str, str]: |
| 238 | """Split text by cell position. |
| 239 | |
| 240 | If the cell position falls within a double width character, it is converted to two spaces. |
| 241 | |
| 242 | Args: |
| 243 | text: Text to split. |
| 244 | cell_position Offset in cells. |
| 245 | unicode_version: Unicode version, `"auto"` to auto detect, `"latest"` for the latest unicode version. |
| 246 | |
| 247 | Returns: |
| 248 | Tuple to two split strings. |
| 249 | """ |
| 250 | if cell_position <= 0: |
| 251 | return "", text |
| 252 | |
| 253 | spans, cell_length = split_graphemes(text, unicode_version) |
| 254 | |
| 255 | # Guess initial offset |
| 256 | offset = int((cell_position / cell_length) * len(spans)) |
| 257 | left_size = sum(map(_span_get_cell_len, spans[:offset])) |
| 258 | |
| 259 | while True: |
| 260 | if left_size == cell_position: |
| 261 | if offset >= len(spans): |
| 262 | return text, "" |
| 263 | split_index = spans[offset][0] |
| 264 | return text[:split_index], text[split_index:] |
| 265 | if left_size < cell_position: |
| 266 | start, end, cell_size = spans[offset] |
| 267 | if left_size + cell_size > cell_position: |
| 268 | return text[:start] + " ", " " + text[end:] |
| 269 | offset += 1 |
| 270 | left_size += cell_size |
| 271 | else: # left_size > cell_position |
| 272 | start, end, cell_size = spans[offset - 1] |
| 273 | if left_size - cell_size < cell_position: |
| 274 | return text[:start] + " ", " " + text[end:] |
| 275 | offset -= 1 |
| 276 | left_size -= cell_size |
| 277 | |
| 278 | |
| 279 | def split_text( |
no test coverage detected