A stack of styles.
| 763 | |
| 764 | |
| 765 | class StyleStack: |
| 766 | """A stack of styles.""" |
| 767 | |
| 768 | __slots__ = ["_stack"] |
| 769 | |
| 770 | def __init__(self, default_style: "Style") -> None: |
| 771 | self._stack: List[Style] = [default_style] |
| 772 | |
| 773 | def __repr__(self) -> str: |
| 774 | return f"<stylestack {self._stack!r}>" |
| 775 | |
| 776 | @property |
| 777 | def current(self) -> Style: |
| 778 | """Get the Style at the top of the stack.""" |
| 779 | return self._stack[-1] |
| 780 | |
| 781 | def push(self, style: Style) -> None: |
| 782 | """Push a new style on to the stack. |
| 783 | |
| 784 | Args: |
| 785 | style (Style): New style to combine with current style. |
| 786 | """ |
| 787 | self._stack.append(self._stack[-1] + style) |
| 788 | |
| 789 | def pop(self) -> Style: |
| 790 | """Pop last style and discard. |
| 791 | |
| 792 | Returns: |
| 793 | Style: New current style (also available as stack.current) |
| 794 | """ |
| 795 | self._stack.pop() |
| 796 | return self._stack[-1] |
no outgoing calls