Wednesday, December 26, 2012, 12:39
My favorite twitter feed at the moment is climagic. He posts an assortment of great little scripts and other cli tips and tweaks at least a few times a week. One of the neatest things things he posted in a long while is his bash ascii snow simulator.The code for it is below:
c=$(tput cols);clear;while :;do echo $(tput lines) $c $(($RANDOM%$c));sleep 0.1;done|gawk '{a[$3]=0;for(x in a){o=a[x];a[x]=a[x]+1;printf"\033[%s;%sH ",o,x;printf"\033[%s;%sH*\033[0;0H",a[x],x;}}'
I've modified it a very little bit and made a rainy windsheild simulator as well:
c=$(tput cols);clear;while :;do echo $(tput lines) $c $(($RANDOM%$c));sleep 0.1;done|gawk '{a[$3]=0;for(x in a){o=a[x];a[x]=a[x+1]+1;printf"\033[%s;%sH ",o,x;printf"\033[%s;%sH.\033[0;0H",a[x],x;}}'
The most interesting part of these to me is how few characters it takes to generate something so visually interesting. These scripts can be shortened a bit if you are running them from the command line by removing the c variable for columns, but when running from a script this variable isn't passed so I've left it in.
Particularly fun has been running the snow simulator on an old-style television with a Raspberry Pi with the font size way way up.