Syntax theme to use standard colors.
| 181 | |
| 182 | |
| 183 | class ANSISyntaxTheme(SyntaxTheme): |
| 184 | """Syntax theme to use standard colors.""" |
| 185 | |
| 186 | def __init__(self, style_map: Dict[TokenType, Style]) -> None: |
| 187 | self.style_map = style_map |
| 188 | self._missing_style = Style.null() |
| 189 | self._background_style = Style.null() |
| 190 | self._style_cache: Dict[TokenType, Style] = {} |
| 191 | |
| 192 | def get_style_for_token(self, token_type: TokenType) -> Style: |
| 193 | """Look up style in the style map.""" |
| 194 | try: |
| 195 | return self._style_cache[token_type] |
| 196 | except KeyError: |
| 197 | # Styles form a hierarchy |
| 198 | # We need to go from most to least specific |
| 199 | # e.g. ("foo", "bar", "baz") to ("foo", "bar") to ("foo",) |
| 200 | get_style = self.style_map.get |
| 201 | token = tuple(token_type) |
| 202 | style = self._missing_style |
| 203 | while token: |
| 204 | _style = get_style(token) |
| 205 | if _style is not None: |
| 206 | style = _style |
| 207 | break |
| 208 | token = token[:-1] |
| 209 | self._style_cache[token_type] = style |
| 210 | return style |
| 211 | |
| 212 | def get_background_style(self) -> Style: |
| 213 | return self._background_style |
| 214 | |
| 215 | |
| 216 | SyntaxPosition = Tuple[int, int] |
no outgoing calls