A stack of themes. Args: theme (Theme): A theme instance
| 79 | |
| 80 | |
| 81 | class ThemeStack: |
| 82 | class="st">"""A stack of themes. |
| 83 | |
| 84 | Args: |
| 85 | theme (Theme): A theme instance |
| 86 | class="st">""" |
| 87 | |
| 88 | def __init__(self, theme: Theme) -> None: |
| 89 | self._entries: List[Dict[str, Style]] = [theme.styles] |
| 90 | self.get = self._entries[-1].get |
| 91 | |
| 92 | def push_theme(self, theme: Theme, inherit: bool = True) -> None: |
| 93 | class="st">"""Push a theme on the top of the stack. |
| 94 | |
| 95 | Args: |
| 96 | theme (Theme): A Theme instance. |
| 97 | inherit (boolean, optional): Inherit styles from current top of stack. |
| 98 | class="st">""" |
| 99 | styles: Dict[str, Style] |
| 100 | styles = ( |
| 101 | {**self._entries[-1], **theme.styles} if inherit else theme.styles.copy() |
| 102 | ) |
| 103 | self._entries.append(styles) |
| 104 | self.get = self._entries[-1].get |
| 105 | |
| 106 | def pop_theme(self) -> None: |
| 107 | class="st">""class="st">"Pop (and discard) the top-most theme."class="st">"" |
| 108 | if len(self._entries) == 1: |
| 109 | raise ThemeStackError(class="st">"Unable to pop base theme") |
| 110 | self._entries.pop() |
| 111 | self.get = self._entries[-1].get |
| 112 | |
| 113 | |
| 114 | if __name__ == class="st">"__main__": class="cm"># pragma: no cover |
no outgoing calls