From 0e4edfdd79d30ed799a69b7483fa7037bd15593b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B9=A4=E7=BF=94=E4=B8=87=E9=87=8C?= Date: Fri, 28 Jan 2022 00:16:19 +0800 Subject: [PATCH] improve config helper (#1721) --- manimlib/utils/init_config.py | 152 ++++++++++++++++++++++++++-------- 1 file changed, 119 insertions(+), 33 deletions(-) diff --git a/manimlib/utils/init_config.py b/manimlib/utils/init_config.py index db33cd70..9fcdd07e 100644 --- a/manimlib/utils/init_config.py +++ b/manimlib/utils/init_config.py @@ -1,5 +1,28 @@ -import yaml import os +import yaml +import inspect +import importlib + +from rich import box +from rich.rule import Rule +from rich.table import Table +from rich.console import Console +from rich.prompt import Prompt, Confirm + + +def get_manim_dir(): + manimlib_module = importlib.import_module("manimlib") + manimlib_dir = os.path.dirname(inspect.getabsfile(manimlib_module)) + return os.path.abspath(os.path.join(manimlib_dir, "..")) + + +def remove_empty_value(dictionary): + for key in list(dictionary.keys()): + if dictionary[key] == "": + dictionary.pop(key) + elif isinstance(dictionary[key], dict): + remove_empty_value(dictionary[key]) + def init_customization(): configuration = { @@ -24,6 +47,7 @@ def init_customization(): }, "window_position": "UR", "window_monitor": 0, + "full_screen": False, "break_into_partial_movies": False, "camera_qualities": { "low": { @@ -46,41 +70,103 @@ def init_customization(): } } - print("Initialize configuration") - scope = input(" Please select the scope of the configuration [global/local]: ") - if scope == "global": - from manimlib.config import get_manim_dir - file_name = os.path.join(get_manim_dir(), "manimlib", "default_config.yml") - else: - file_name = os.path.join(os.getcwd(), "custom_config.yml") + console = Console() + console.print(Rule("[bold]Configuration Guide[/bold]")) + # print("Initialize configuration") + try: + scope = Prompt.ask( + " Select the scope of the configuration", + choices=["global", "local"], + default="local" + ) - print("\n directories:") - configuration["directories"]["output"] = input(" [1/8] Where should manim output video and image files place: ") - configuration["directories"]["raster_images"] = input(" [2/8] Which folder should manim find raster images (.jpg .png .gif) in (optional): ") - configuration["directories"]["vector_images"] = input(" [3/8] Which folder should manim find vector images (.svg .xdv) in (optional): ") - configuration["directories"]["sounds"] = input(" [4/8] Which folder should manim find sound files (.mp3 .wav) in (optional): ") - configuration["directories"]["temporary_storage"] = input(" [5/8] Which folder should manim storage temporary files: ") + console.print("[bold]Directories:[/bold]") + dir_config = configuration["directories"] + dir_config["output"] = Prompt.ask( + " Where should manim [bold]output[/bold] video and image files place [prompt.default](optional, default is none)", + default="", + show_default=False + ) + dir_config["raster_images"] = Prompt.ask( + " Which folder should manim find [bold]raster images[/bold] (.jpg .png .gif) in " + "[prompt.default](optional, default is none)", + default="", + show_default=False + ) + dir_config["vector_images"] = Prompt.ask( + " Which folder should manim find [bold]vector images[/bold] (.svg .xdv) in " + "[prompt.default](optional, default is none)", + default="", + show_default=False + ) + dir_config["sounds"] = Prompt.ask( + " Which folder should manim find [bold]sound files[/bold] (.mp3 .wav) in " + "[prompt.default](optional, default is none)", + default="", + show_default=False + ) + dir_config["temporary_storage"] = Prompt.ask( + " Which folder should manim storage [bold]temporary files[/bold] " + "[prompt.default](recommended, use system temporary folder by default)", + default="", + show_default=False + ) - print("\n tex:") - tex = input(" [6/8] Which executable file to use to compile [latex/xelatex]: ") - if tex == "latex": - configuration["tex"]["executable"] = "latex" - configuration["tex"]["template_file"] = "tex_template.tex" - configuration["tex"]["intermediate_filetype"] = "dvi" - else: - configuration["tex"]["executable"] = "xelatex -no-pdf" - configuration["tex"]["template_file"] = "ctex_template.tex" - configuration["tex"]["intermediate_filetype"] = "xdv" + console.print("[bold]LaTeX:[/bold]") + tex_config = configuration["tex"] + tex = Prompt.ask( + " Select an executable program to use to compile a LaTeX source file", + choices=["latex", "xelatex"], + default="latex" + ) + if tex == "latex": + tex_config["executable"] = "latex" + tex_config["template_file"] = "tex_template.tex" + tex_config["intermediate_filetype"] = "dvi" + else: + tex_config["executable"] = "xelatex -no-pdf" + tex_config["template_file"] = "ctex_template.tex" + tex_config["intermediate_filetype"] = "xdv" + + console.print("[bold]Styles:[/bold]") + configuration["style"]["background_color"] = Prompt.ask( + " Which [bold]background color[/bold] do you want [italic](hex code)", + default="#333333" + ) - print("\n style:") - configuration["style"]["background_color"] = input(" [7/8] Which background color do you want (hex code): ") + console.print("[bold]Camera qualities:[/bold]") + table = Table( + "low", "medium", "high", "ultra_high", + title="Four defined qualities", + box=box.ROUNDED + ) + table.add_row("480p15", "720p30", "1080p60", "2160p60") + console.print(table) + configuration["camera_qualities"]["default_quality"] = Prompt.ask( + " Which one to choose as the default rendering quality", + choices=["low", "medium", "high", "ultra_high"], + default="high" + ) - print("\n camera_qualities:") - print(" Four defined qualities: low: 480p15 medium: 720p30 high: 1080p60 ultra_high: 2160p60") - configuration["camera_qualities"]["default_quality"] = input(" [8/8] Which one to choose as the default rendering quality [low/medium/high/ultra_high]: ") + write_to_file = Confirm.ask( + "\n[bold]Are you sure to write these configs to file?[/bold]", + default=True + ) + if not write_to_file: + raise KeyboardInterrupt - with open(file_name, 'w', encoding="utf_8") as file: - yaml.dump(configuration, file) + global_file_name = os.path.join(get_manim_dir(), "manimlib", "default_config.yml") + if scope == "global": + file_name = global_file_name + else: + if os.path.exists(global_file_name): + remove_empty_value(configuration) + file_name = os.path.join(os.getcwd(), "custom_config.yml") + with open(file_name, "w", encoding="utf-8") as f: + yaml.dump(configuration, f) + + console.print(f"\n:rocket: You have successfully set up a {scope} configuration file!") + console.print(f"You can manually modify it in: [cyan]`{file_name}`[/cyan]") - print(f"\nYou have set up a {scope} configuration file") - print(f"You can manually modify it again in: {file_name}\n") + except KeyboardInterrupt: + console.print("\n[green]Exit configuration guide[/green]")