Animation inside terminal with escape codes

gnome-terminalterminal

Input file: output of Tower of Hanoi in Brainfuck (some codepoints may not render properly in your browser). The file basically uses escape codes (more specifically ^[[m;nH) to rewrite lines.

Running the command (you may need to do sudo apt-get install pv or equivalent)

cat hanoi.b.out |  pv -l -L 10 -q

gives output like

enter image description here

if the window size is big enough.

If not, the output looks like

enter image description here

where the image starts "scrolling down."

Naturally, this begs the question: why does this (mis-)behaviour happen when the window size is too small?

Best Answer

Not all of the output is cursor-addressing. Some of it is line-feeds, which will (when the cursor happens to be on the bottom row) cause the terminal to scroll up. Here's a visible rendering using unmap of the beginning of the output: look for the \n (newlines are "line-feeds");

\E[H
\E[2J
\E[2;27HTowers of Hanoi in Brainf*ck
\E[3;15HWritten by Clifford Wolf <http://www.clifford.at/bfcpu/>
\E[14;43H-----------------------------------
\E[24;23H-----------------------------------
\E[14;3H-----------------------------------
\E[13;3HxXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
\E[12;5HxXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
\E[11;7HxXXXXXXXXXXXXXXXXXXXXXXXXXx
\E[10;9HxXXXXXXXXXXXXXXXXXXXXXx
\E[9;11HxXXXXXXXXXXXXXXXXXx
\E[8;13HxXXXXXXXXXXXXXx
\E[7;15HxXXXXXXXXXx
\E[6;17HxXXXXXx
\E[5;19HxXx
\E[5;19H   
\E[13;59HxXx
\n
\E[1;1H
\E[6;17H       
\E[23;37HxXXXXXx
\n
\E[1;1H
\E[13;59H   
\E[22;39HxXx
\n
\E[1;1H
\E[7;15H           
\E[13;55HxXXXXXXXXXx

When you use a smaller screen-size, the line-feeds that didn't cause scrolling are more likely to be on the bottom row, so you'll see it scroll up.

Related Question