Skip to content

Instantly share code, notes, and snippets.

@heptal heptal/terminal_gif.md
Last active Dec 20, 2015

Embed
What would you like to do?
GIFs? In MY terminal?!

Video

Seems to run stable for hours!

If you can't see your cursor after killing it, run tput cnorm

Sometimes you may see a seq error if you run it multiple times with different resolutions in the same session. Easier to make a new session, or you can change it unset the variables used initially.

# Play a gif in the terminal indefinitely. Specify the file/url and the horizontal resolution
# Requires the amazing ImageMagick. `(brew|apt-get|yum) install imagemagick`
function playgif ()
{
    f=$(convert $1 -resize $2 -coalesce txt:-|sed -E 's/ //g;s/#.+:([0-9]+).+/_\1/g;s/.+://;s/\(//;s/\)//;s/#.*//;s/255/254/g;s/([0-9]+,[0-9]+,[0-9]+),[0-9]+/\1/;s/_/ /'|tr '\n' 'z');
    w=$(echo ${f::3}|sed -E 's/([0-9]+).*/\1/');
    clear;
    tput civis;
    while true; do
        for i in ${f[@]};
        do
            j=($(echo $i|tr 'z' '\n'|sed -E '/^[0-9]*$/d;s/(.+),(.+),(.+)/\1\/42.5*36+\2\/42.5*6+\3\/42.5+16/'|bc));
            for r in $(seq 0 $w $((${#j[@]}-$w)));
            do
                p=($(for c in $(seq 0 $(($w-1)));do echo " ${j[$(($r+$c))]} ";done));
                IFS='%';
                k=$(echo ${p[@]}|sed -E 's/^/\\e[0m\\e[48;5;/;s/\ /m\ \ \\e[0m\\e[48;5;/g;s/$/m\ \ \\e[0m/');
                echo -e $k;
                unset IFS;
            done;
            tput cup 0 0;
        done;
    done
}
@scarolan

This comment has been minimized.

Copy link

commented Jul 26, 2013

Are there any specific settings required for the terminal for this to work? I tried iTerm and Terminal on OSX but all I get are the raw character codes, instead of colored blocks.

@heptal

This comment has been minimized.

Copy link
Owner Author

commented Aug 3, 2013

Hmm, if 'Report terminal type' (in iTerm) is set to xterm or xterm-256 works for me. It works in Terminal for me as well, and also through ssh. Try running the bash-only version of the still image renderer on console_colors.md and see if you get colors there?

@stecman

This comment has been minimized.

Copy link

commented Dec 17, 2013

This is genius! I had the same issue as @scarolan; replacing \\e with \\033 fixed it for me.

@scarolan

This comment has been minimized.

Copy link

commented Jan 7, 2014

Confirmed @stecman's solution works. It also seems to only work for me in bash, and not zsh, but that might be because I have a heavily tweaked setup with Oh-My-Zsh, powerline, and a patched font.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.