Description
Description of proposed feature
Currently, the preview command is fixed, e.g. xdg-open
on Linux or similar open
variant on other operating systems.
Even if these commands often can be configured to invoke a particular program, this is still limiting and assumes a GUI.
Having a preview_command
on the config object (or maybe even a command line option for easier discoverability) that is invoked if the configuration option is set would help making it easier to integrate manim
in more workflows.
How can the new feature be used?
-
In my case, I'd like to invoke the
timg
program that directly renders videos in the terminal.
xdg-open
could be configured to use that, but sincexdg-open
is assuming a GUI program to launch, it generates some blinking wait mouse-cursor as it awaits some GUI program to show up which never happens. Also it outputs some log messages that mess with the output on the terminal. So ideally, I'd like to directly invoketimg
.
With my little patch below, this is how that looks like (and it is actually pretty awesome to render things directly in the shell)
-
Or consider another use-case, in which a user would like to invoke a shell-script with the preview content to do something special with it, like displaying on a different screen etc.
Additional comments
There is another thing that should probably be done: running the preview command in the foreground with sp.run(commands)
, not sp.Popen(commands)
. The reason why this is beneficial is that if the invoked program outputs something to the shell, it is not disturbed with any text output coming later, e.g. the shell prompt returning or other log messages.
The reason why this can be safely done without impacting the current functionality: the programs otherwise being invoked (xdg-open
, open
, etc.) already go in the background, so there would be no change to that behavior.
Putting it all together, this is how I have currently patched two lines of my local copy of manim
to do what I need to invoke timg
that it can output on the terminal without disturbance of other programs messing with the terminal.
Here the program is hardcoded, but of course that should be be invoking config["preview_command"]
in a proper implementation.
--- a/manim/utils/file_ops.py
+++ b/manim/utils/file_ops.py
@@ -192,7 +192,7 @@ def open_file(file_path, in_browser=False):
os.startfile(file_path if not in_browser else file_path.parent)
else:
if current_os == "Linux":
- commands = ["xdg-open"]
+ commands = ["timg"] # This should be configurable
file_path = file_path if not in_browser else file_path.parent
elif current_os.startswith("CYGWIN"):
commands = ["cygstart"]
@@ -205,7 +205,7 @@ def open_file(file_path, in_browser=False):
else:
raise OSError("Unable to identify your operating system...")
commands.append(file_path)
- sp.Popen(commands)
+ sp.run(commands) # Running in foreground prevents messing with output
def open_media_file(file_writer: SceneFileWriter) -> None:
If this would be a configuration option, then the part of the code would probably be something like
if config["preview_command"]:
#...
elif current_os == "Linux":
# ...
Final thoughts
The suggestions (provide a command that is configurable, and run that command in the foreground instead of putting it in the background with Popen()) will be entirely backward compatible but will make it possible to give users a way to expand their workflows.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status