A line in repr output.
| 492 | |
| 493 | @dataclass |
| 494 | class _Line: |
| 495 | """A line in repr output.""" |
| 496 | |
| 497 | parent: Optional["_Line"] = None |
| 498 | is_root: bool = False |
| 499 | node: Optional[Node] = None |
| 500 | text: str = "" |
| 501 | suffix: str = "" |
| 502 | whitespace: str = "" |
| 503 | expanded: bool = False |
| 504 | last: bool = False |
| 505 | |
| 506 | @property |
| 507 | def expandable(self) -> bool: |
| 508 | """Check if the line may be expanded.""" |
| 509 | return bool(self.node is not None and self.node.children) |
| 510 | |
| 511 | def check_length(self, max_length: int) -> bool: |
| 512 | """Check this line fits within a given number of cells.""" |
| 513 | start_length = ( |
| 514 | len(self.whitespace) + cell_len(self.text) + cell_len(self.suffix) |
| 515 | ) |
| 516 | assert self.node is not None |
| 517 | return self.node.check_length(start_length, max_length) |
| 518 | |
| 519 | def expand(self, indent_size: int) -> Iterable["_Line"]: |
| 520 | """Expand this line by adding children on their own line.""" |
| 521 | node = self.node |
| 522 | assert node is not None |
| 523 | whitespace = self.whitespace |
| 524 | assert node.children |
| 525 | if node.key_repr: |
| 526 | new_line = yield _Line( |
| 527 | text=f"{node.key_repr}{node.key_separator}{node.open_brace}", |
| 528 | whitespace=whitespace, |
| 529 | ) |
| 530 | else: |
| 531 | new_line = yield _Line(text=node.open_brace, whitespace=whitespace) |
| 532 | child_whitespace = self.whitespace + " " * indent_size |
| 533 | tuple_of_one = node.is_tuple and len(node.children) == 1 |
| 534 | for last, child in loop_last(node.children): |
| 535 | separator = "," if tuple_of_one else node.separator |
| 536 | line = _Line( |
| 537 | parent=new_line, |
| 538 | node=child, |
| 539 | whitespace=child_whitespace, |
| 540 | suffix=separator, |
| 541 | last=last and not tuple_of_one, |
| 542 | ) |
| 543 | yield line |
| 544 | |
| 545 | yield _Line( |
| 546 | text=node.close_brace, |
| 547 | whitespace=whitespace, |
| 548 | suffix=self.suffix, |
| 549 | last=self.last, |
| 550 | ) |
| 551 |