Skip to content

Instantly share code, notes, and snippets.

@gary23w
Created November 16, 2023 00:54
Show Gist options
  • Save gary23w/1a1a6a2a52229c68f17a260a492b0378 to your computer and use it in GitHub Desktop.
Save gary23w/1a1a6a2a52229c68f17a260a492b0378 to your computer and use it in GitHub Desktop.
matrix-style-cascading-screensaver-in-bash
#!/bin/bash
# free time plus a need to build a screensaver in bash, enjoy the cascade.
# ps. the key to this is the "eval" function.
# - gary
# clear the screen and hide the cursor
echo -e "\033[2J\033[?25l"
# get the number of lines and columns of the terminal
R=$(tput lines)
C=$(tput cols)
R=$((R - 1)) # decrease R by 1 for zero-based indexing
# dame to display
name="Gary"
name_length=${#name}
while true; do
(
# shorthand for echo and sleep
e='echo -e'
s='sleep'
j=$((RANDOM % C))
d=$((RANDOM % R))
# display characters
for i in $(eval echo {1..$R}); do
if ((RANDOM % 10 == 0)); then
#c=${name:$((RANDOM % name_length)):1}
c=$name
else
c=$(printf '\\\\0%o' $((RANDOM % 57 + 33)))
fi
# print the character in green then in white at each position(matrix style)
eval $e "\"\033[$((i-1));${j}H\033[32m$c\033[$i;${j}H\033[37m$c\""
eval $s 0.1
if [ $i -ge $d ]; then
eval $e "\"\033[$((i-d));${j}H \""
fi
done
for i in $(eval echo {$((i-d))..$R}); do
eval $e "\"\033[$i;${j}f \""
eval $s 0.1
done
) &
sleep 0.05
done
# does this even work?
trap 'echo -e "\033[?25h"; clear; exit' SIGINT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment