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

Class PygmentsSyntaxTheme

rich/syntax.py:141–180  ·  rich/syntax.py::PygmentsSyntaxTheme

Syntax theme that delegates to Pygments theme.

Source from the content-addressed store, hash-verified

139
140
141class PygmentsSyntaxTheme(SyntaxTheme):
142 class="st">""class="st">"Syntax theme that delegates to Pygments theme."class="st">""
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(class="st">"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 class="st">""class="st">"Get a style from a Pygments class."class="st">""
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[class="st">"color"]
168 bgcolor = pygments_style[class="st">"bgcolor"]
169 style = Style(
170 color=class="st">"class="cm">#" + color if color else class="st">"#000000",
171 bgcolor=class="st">"class="cm">#" + bgcolor if bgcolor else self._background_color,
172 bold=pygments_style[class="st">"bold"],
173 italic=pygments_style[class="st">"italic"],
174 underline=pygments_style[class="st">"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