There are various ways to create animated GIFs for terminal output. Three methods that can be used on Linux (in this case Ubuntu) are described below.
This is probably the easiest way. The disadvantage of this method is that when I tried to record an animation of a progress bar created with the Python Rich library, it didn't show up.
Asciinema can be be installed with pip install asciinema
Asciicast2gif is more complicated and is best run from a Docker container.
Creating an alias in .bashrc makes it easy to use.
# alias for asciinema rendering (requires Docker to be installed and user in docker group)
alias asciicast2gif='docker run --rm -v $PWD:/data asciinema/asciicast2gif'
The commands are:
asciinema rec my_file.cast
asciicast2gif my_file.cast my_file.gif
The my_file.cast
file is JSON and can be modified relatively easily to tweak timesteps or modify content.
There are options for setting width, height, colour theme etc.
Byzanz is a screen recorder that can output to gif, while gifsicle is a tool for editing gifs. The advantage of this method is that it records exactly what plays on the screen just as you see it. The downside is that getting the exact timing and area of the screen to record correct can be a bit fiddly.
sudo apt install byzanz gifsicle
You can make Byzanz execute a command as it begins, but I found it easier to send it to the background and start the code myself. The following command records an 8 second gif of the lower left portion of the screen after a 3 second delay.
byzanz-record -d 8 --delay 3 -x 0 -y 804 -w 730 -h 272 my_file.gif &
If the gif ends up being longer that what you are demonstrating, you can trim the end with gifsicle.
The following command takes the first 11 frames from my_file.gif
and saves them as my_file_trimmed.gif
with an output quality of 3.
gifsicle my_file.gif '#0-11' -O3 -o my_file_trimmed.gif
Note that when I tried to trim frames from the start of the gif the output was corrupted. I think this is because some subsequent frames only contain the changes, rather than the whole image each time.
There is another tool called terminalizer
that can also be used.
I tried it before but found it tricky to install.
Here is a gist with instructions for installing terminalizer that may be helpful.