Split text into lines such that each line fits within the available (cell) width. Args: text: The text to fold such that it fits in the given width. width: The width available (number of cells). Returns: A list of strings such that each string in the list has cell w
(text: str, width: int, unicode_version: str = "auto")
| 324 | |
| 325 | |
| 326 | def chop_cells(text: str, width: int, unicode_version: str = "auto") -> list[str]: |
| 327 | """Split text into lines such that each line fits within the available (cell) width. |
| 328 | |
| 329 | Args: |
| 330 | text: The text to fold such that it fits in the given width. |
| 331 | width: The width available (number of cells). |
| 332 | |
| 333 | Returns: |
| 334 | A list of strings such that each string in the list has cell width |
| 335 | less than or equal to the available width. |
| 336 | """ |
| 337 | if _is_single_cell_widths(text): |
| 338 | return [text[index : index + width] for index in range(0, len(text), width)] |
| 339 | spans, _ = split_graphemes(text, unicode_version) |
| 340 | line_size = 0 # Size of line in cells |
| 341 | lines: list[str] = [] |
| 342 | line_offset = 0 # Offset (in codepoints) of start of line |
| 343 | for start, end, cell_size in spans: |
| 344 | if line_size + cell_size > width: |
| 345 | lines.append(text[line_offset:start]) |
| 346 | line_offset = start |
| 347 | line_size = 0 |
| 348 | line_size += cell_size |
| 349 | if line_size: |
| 350 | lines.append(text[line_offset:]) |
| 351 | |
| 352 | return lines |