Adjust a line to a given width (cropping or padding as required). Args: segments (Iterable[Segment]): A list of segments in a single line. length (int): The desired width of the line. style (Style, optional): The style of padding if used (space on the end
(
cls,
line: List["Segment"],
length: int,
style: Optional[Style] = None,
pad: bool = True,
)
| 352 | |
| 353 | @classmethod |
| 354 | def adjust_line_length( |
| 355 | cls, |
| 356 | line: List["Segment"], |
| 357 | length: int, |
| 358 | style: Optional[Style] = None, |
| 359 | pad: bool = True, |
| 360 | ) -> List["Segment"]: |
| 361 | """Adjust a line to a given width (cropping or padding as required). |
| 362 | |
| 363 | Args: |
| 364 | segments (Iterable[Segment]): A list of segments in a single line. |
| 365 | length (int): The desired width of the line. |
| 366 | style (Style, optional): The style of padding if used (space on the end). Defaults to None. |
| 367 | pad (bool, optional): Pad lines with spaces if they are shorter than `length`. Defaults to True. |
| 368 | |
| 369 | Returns: |
| 370 | List[Segment]: A line of segments with the desired length. |
| 371 | """ |
| 372 | line_length = sum(segment.cell_length for segment in line) |
| 373 | new_line: List[Segment] |
| 374 | |
| 375 | if line_length < length: |
| 376 | if pad: |
| 377 | new_line = line + [cls(" " * (length - line_length), style)] |
| 378 | else: |
| 379 | new_line = line[:] |
| 380 | elif line_length > length: |
| 381 | new_line = [] |
| 382 | append = new_line.append |
| 383 | line_length = 0 |
| 384 | for segment in line: |
| 385 | segment_length = segment.cell_length |
| 386 | if line_length + segment_length < length or segment.control: |
| 387 | append(segment) |
| 388 | line_length += segment_length |
| 389 | else: |
| 390 | text, segment_style, _ = segment |
| 391 | text = set_cell_size(text, length - line_length) |
| 392 | append(cls(text, segment_style)) |
| 393 | break |
| 394 | else: |
| 395 | new_line = line[:] |
| 396 | return new_line |
| 397 | |
| 398 | @classmethod |
| 399 | def get_line_length(cls, line: List["Segment"]) -> int: |