Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: importing manim should not trigger pygments.styles.get_all_styles #3797

Merged
28 changes: 21 additions & 7 deletions manim/mobject/text/code_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
from pathlib import Path

import numpy as np
from pygments import highlight
from pygments import highlight, styles
from pygments.formatters.html import HtmlFormatter
from pygments.lexers import get_lexer_by_name, guess_lexer_for_filename
from pygments.styles import get_all_styles

# from pygments.styles import get_all_styles
from manim.constants import *
from manim.mobject.geometry.arc import Dot
from manim.mobject.geometry.polygram import RoundedRectangle
Expand All @@ -25,8 +25,6 @@
from manim.mobject.types.vectorized_mobject import VGroup
from manim.utils.color import WHITE

__all__ = ["Code"]

JasonGrace2282 marked this conversation as resolved.
Show resolved Hide resolved

class Code(VGroup):
"""A highlighted source code listing.
Expand Down Expand Up @@ -63,7 +61,7 @@ class Code(VGroup):
background_stroke_width=1,
background_stroke_color=WHITE,
insert_line_no=True,
style=Code.styles_list[15],
style="emacs",
background="window",
language="cpp",
)
Expand Down Expand Up @@ -127,7 +125,9 @@ def construct(self):
line_no_buff
Defines the spacing between line numbers and displayed code. Defaults to 0.4.
style
Defines the style type of displayed code. You can see possible names of styles in with :attr:`styles_list`. Defaults to ``"vim"``.
Defines the style type of displayed code. To see a list possible
names of styles call :meth:`get_styles_list`.
Defaults to ``"vim"``.
language
Specifies the programming language the given code was written in. If ``None``
(the default), the language will be automatically detected. For the list of
Expand Down Expand Up @@ -156,7 +156,7 @@ def construct(self):
# For more information about pygments.lexers visit https://pygments.org/docs/lexers/
# from pygments.lexers import get_all_lexers
# all_lexers = get_all_lexers()
styles_list = list(get_all_styles())
_styles_list_cache: list[str] | None = None
# For more information about pygments.styles visit https://pygments.org/docs/styles/

def __init__(
Expand Down Expand Up @@ -288,6 +288,20 @@ def __init__(
)
self.move_to(np.array([0, 0, 0]))

@classmethod
def get_styles_list(cls):
"""Get list of available code styles.

Returns
-------
list[str]
The list of available code styles to use for the ``styles``
argument.
"""
if cls._styles_list_cache is None:
cls._styles_list_cache = list(styles.get_all_styles())
return cls._styles_list_cache

def _ensure_valid_file(self):
"""Function to validate file."""
if self.file_name is None:
Expand Down
Loading