From 706b4d1a4193a07abf1b110330191771741829c6 Mon Sep 17 00:00:00 2001 From: Eddie Ruiz Date: Fri, 7 Jun 2024 00:06:42 -0400 Subject: [PATCH 1/5] 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. --- manim/mobject/text/code_mobject.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/manim/mobject/text/code_mobject.py b/manim/mobject/text/code_mobject.py index b7aa6a7980..4b5858631e 100644 --- a/manim/mobject/text/code_mobject.py +++ b/manim/mobject/text/code_mobject.py @@ -15,8 +15,8 @@ from pygments import highlight 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 @@ -25,8 +25,6 @@ from manim.mobject.types.vectorized_mobject import VGroup from manim.utils.color import WHITE -__all__ = ["Code"] - class Code(VGroup): """A highlighted source code listing. @@ -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="solarized-dark", background="window", language="cpp", ) @@ -127,7 +125,8 @@ 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. You can see possible names of + styles by calling `list(pygments.styles.get_all_styles())`. 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 @@ -156,7 +155,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 = list(get_all_styles()) # For more information about pygments.styles visit https://pygments.org/docs/styles/ def __init__( From b831003f72f839346ffcddf04ca860d312e888fa Mon Sep 17 00:00:00 2001 From: Eddie Ruiz Date: Fri, 7 Jun 2024 00:43:47 -0400 Subject: [PATCH 2/5] fix: small change to documentation --- manim/mobject/text/code_mobject.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/manim/mobject/text/code_mobject.py b/manim/mobject/text/code_mobject.py index 4b5858631e..cb76fe7474 100644 --- a/manim/mobject/text/code_mobject.py +++ b/manim/mobject/text/code_mobject.py @@ -125,8 +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 by calling `list(pygments.styles.get_all_styles())`. Defaults to ``"vim"``. + Defines the style type of displayed code. To see a list possible + names of styles call ``list(pygments.styles.get_all_styles())``. + 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 From c79afaaedae9a8ab8a9ac76c18756236f68d56f4 Mon Sep 17 00:00:00 2001 From: Eddie Ruiz Date: Sat, 8 Jun 2024 13:20:36 -0400 Subject: [PATCH 3/5] Added potential class method to get available code styles. --- manim/mobject/text/code_mobject.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/manim/mobject/text/code_mobject.py b/manim/mobject/text/code_mobject.py index cb76fe7474..c78b6dc17f 100644 --- a/manim/mobject/text/code_mobject.py +++ b/manim/mobject/text/code_mobject.py @@ -12,7 +12,7 @@ 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 @@ -61,7 +61,7 @@ class Code(VGroup): background_stroke_width=1, background_stroke_color=WHITE, insert_line_no=True, - style="solarized-dark", + style="emacs", background="window", language="cpp", ) @@ -126,7 +126,7 @@ def construct(self): Defines the spacing between line numbers and displayed code. Defaults to 0.4. style Defines the style type of displayed code. To see a list possible - names of styles call ``list(pygments.styles.get_all_styles())``. + names of styles call :meth:`get_styles_list`. Defaults to ``"vim"``. language Specifies the programming language the given code was written in. If ``None`` @@ -157,6 +157,7 @@ def construct(self): # from pygments.lexers import get_all_lexers # all_lexers = get_all_lexers() # styles_list = list(get_all_styles()) + _styles_list_cache = None # For more information about pygments.styles visit https://pygments.org/docs/styles/ def __init__( @@ -288,6 +289,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: From 07292985ae36d26f567414ca37c05ad797a54019 Mon Sep 17 00:00:00 2001 From: Eddie Date: Tue, 11 Jun 2024 00:43:37 -0400 Subject: [PATCH 4/5] Adding typehints to newly-added attributes. Co-authored-by: adeshpande <110117391+JasonGrace2282@users.noreply.github.com> --- manim/mobject/text/code_mobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/text/code_mobject.py b/manim/mobject/text/code_mobject.py index c78b6dc17f..4736ec6c44 100644 --- a/manim/mobject/text/code_mobject.py +++ b/manim/mobject/text/code_mobject.py @@ -157,7 +157,7 @@ def construct(self): # from pygments.lexers import get_all_lexers # all_lexers = get_all_lexers() # styles_list = list(get_all_styles()) - _styles_list_cache = None + _styles_list_cache: list[str] | None = None # For more information about pygments.styles visit https://pygments.org/docs/styles/ def __init__( From e36a036aa2f6c477addcc923fd035701ebf3c2a7 Mon Sep 17 00:00:00 2001 From: Eddie Date: Tue, 11 Jun 2024 00:44:14 -0400 Subject: [PATCH 5/5] Removing unnecessary lines. Co-authored-by: adeshpande <110117391+JasonGrace2282@users.noreply.github.com> --- manim/mobject/text/code_mobject.py | 1 - 1 file changed, 1 deletion(-) diff --git a/manim/mobject/text/code_mobject.py b/manim/mobject/text/code_mobject.py index 4736ec6c44..999ab3c90e 100644 --- a/manim/mobject/text/code_mobject.py +++ b/manim/mobject/text/code_mobject.py @@ -156,7 +156,6 @@ 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/