Skip to content

Commit 3ed86a8

Browse files
fix: importing manim should not trigger pygments.styles.get_all_styles (#3797)
* fix: importing manim should not trigger pygments.styles.get_all_styles Removed the Code.styles_list attribute. Rewrote the documentation to say that a list of all styles can be generated by calling list(pygments.styles.get_all_styles()). The example in the docstring of Code was rewritten to use an explicit code style name. * fix: small change to documentation * Added potential class method to get available code styles. * Adding typehints to newly-added attributes. Co-authored-by: adeshpande <110117391+JasonGrace2282@users.noreply.github.com> * Removing unnecessary lines. Co-authored-by: adeshpande <110117391+JasonGrace2282@users.noreply.github.com> --------- Co-authored-by: adeshpande <110117391+JasonGrace2282@users.noreply.github.com>
1 parent 4641c0a commit 3ed86a8

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

manim/mobject/text/code_mobject.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
from pathlib import Path
1313

1414
import numpy as np
15-
from pygments import highlight
15+
from pygments import highlight, styles
1616
from pygments.formatters.html import HtmlFormatter
1717
from pygments.lexers import get_lexer_by_name, guess_lexer_for_filename
18-
from pygments.styles import get_all_styles
1918

19+
# from pygments.styles import get_all_styles
2020
from manim.constants import *
2121
from manim.mobject.geometry.arc import Dot
2222
from manim.mobject.geometry.polygram import RoundedRectangle
@@ -25,8 +25,6 @@
2525
from manim.mobject.types.vectorized_mobject import VGroup
2626
from manim.utils.color import WHITE
2727

28-
__all__ = ["Code"]
29-
3028

3129
class Code(VGroup):
3230
"""A highlighted source code listing.
@@ -63,7 +61,7 @@ class Code(VGroup):
6361
background_stroke_width=1,
6462
background_stroke_color=WHITE,
6563
insert_line_no=True,
66-
style=Code.styles_list[15],
64+
style="emacs",
6765
background="window",
6866
language="cpp",
6967
)
@@ -127,7 +125,9 @@ def construct(self):
127125
line_no_buff
128126
Defines the spacing between line numbers and displayed code. Defaults to 0.4.
129127
style
130-
Defines the style type of displayed code. You can see possible names of styles in with :attr:`styles_list`. Defaults to ``"vim"``.
128+
Defines the style type of displayed code. To see a list possible
129+
names of styles call :meth:`get_styles_list`.
130+
Defaults to ``"vim"``.
131131
language
132132
Specifies the programming language the given code was written in. If ``None``
133133
(the default), the language will be automatically detected. For the list of
@@ -156,7 +156,7 @@ def construct(self):
156156
# For more information about pygments.lexers visit https://pygments.org/docs/lexers/
157157
# from pygments.lexers import get_all_lexers
158158
# all_lexers = get_all_lexers()
159-
styles_list = list(get_all_styles())
159+
_styles_list_cache: list[str] | None = None
160160
# For more information about pygments.styles visit https://pygments.org/docs/styles/
161161

162162
def __init__(
@@ -288,6 +288,20 @@ def __init__(
288288
)
289289
self.move_to(np.array([0, 0, 0]))
290290

291+
@classmethod
292+
def get_styles_list(cls):
293+
"""Get list of available code styles.
294+
295+
Returns
296+
-------
297+
list[str]
298+
The list of available code styles to use for the ``styles``
299+
argument.
300+
"""
301+
if cls._styles_list_cache is None:
302+
cls._styles_list_cache = list(styles.get_all_styles())
303+
return cls._styles_list_cache
304+
291305
def _ensure_valid_file(self):
292306
"""Function to validate file."""
293307
if self.file_name is None:

0 commit comments

Comments
 (0)