MCPcopy
hub / github.com/Textualize/rich / PygmentsSyntaxTheme

Class PygmentsSyntaxTheme

rich/syntax.py:141–180  ·  view source on GitHub ↗

Syntax theme that delegates to Pygments theme.

Source from the content-addressed store, hash-verified

139
140
141class PygmentsSyntaxTheme(SyntaxTheme):
142 """Syntax theme that delegates to Pygments theme."""
143
144 def __init__(self, theme: Union[str, Type[PygmentsStyle]]) -> None:
145 self._style_cache: Dict[TokenType, Style] = {}
146 if isinstance(theme, str):
147 try:
148 self._pygments_style_class = get_style_by_name(theme)
149 except ClassNotFound:
150 self._pygments_style_class = get_style_by_name("default")
151 else:
152 self._pygments_style_class = theme
153
154 self._background_color = self._pygments_style_class.background_color
155 self._background_style = Style(bgcolor=self._background_color)
156
157 def get_style_for_token(self, token_type: TokenType) -> Style:
158 """Get a style from a Pygments class."""
159 try:
160 return self._style_cache[token_type]
161 except KeyError:
162 try:
163 pygments_style = self._pygments_style_class.style_for_token(token_type)
164 except KeyError:
165 style = Style.null()
166 else:
167 color = pygments_style["color"]
168 bgcolor = pygments_style["bgcolor"]
169 style = Style(
170 color="#" + color if color else "#000000",
171 bgcolor="#" + bgcolor if bgcolor else self._background_color,
172 bold=pygments_style["bold"],
173 italic=pygments_style["italic"],
174 underline=pygments_style["underline"],
175 )
176 self._style_cache[token_type] = style
177 return style
178
179 def get_background_style(self) -> Style:
180 return self._background_style
181
182
183class ANSISyntaxTheme(SyntaxTheme):

Callers 5

test_get_line_color_noneFunction · 0.90
test_get_style_for_tokenFunction · 0.90
get_themeMethod · 0.85

Calls

no outgoing calls

Tested by 4

test_get_line_color_noneFunction · 0.72
test_get_style_for_tokenFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…