Scripting for Fun (6)

5 Name: Nameless : 2026/02/06 04:03

Using bash, fill the screen with rainbow-colored blocks.
This uses tput to do the lifting instead of manually blasting control codes out.
You've seen the elegance of elisp, now get ready for the ugliness of bash.

#!/bin/bash
#usage: rainbow.sh [fillrate]
#fillrate defaults to 2 (higher is faster)
#tries to be resize-aware but isn't great at it lol
#the resize can happen at exactly the wrong time, filling the whole screen with one color

check_key() {
read -t 0.03125 -N 1 -r -s
}

all_done() {
tput setab 0
tput clear
exit 0
}

emit_color() {
x=$(($RANDOM%width))
y=$(($RANDOM%height))
tput setab $(($RANDOM%8))
tput cup $y $x
echo -n " "
tput setab 0
}

trap 'all_done' SIGINT

check_key
while [ "$REPLY" = "" ]
do
width=`tput cols`
height=`tput lines`
for (( ii = 0 ; ii < ${1:-4} ; ii++ ))
do
emit_color
done
check_key
done
all_done
Name: E-Mail:
Leave these fields empty (spam trap):