From Coral Cat, 3 Months ago, written in Plain Text.
  1. #!/usr/bin/env bash
  2. # pipes.sh: Animated pipes terminal screensaver.
  3. #
  4. # This modified version is maintained at:
  5. #
  6. #   https://github.com/pipeseroni/pipes.sh
  7.  
  8. VERSION=1.1.0
  9.  
  10. M=32768
  11. p=1
  12. f=75 s=13 r=2000 t=0
  13. w=80 h=24
  14.  
  15. resize() {
  16.         w=$(tput cols) h=$(tput lines)
  17. }
  18.  
  19. # ab -> idx = a*4 + b
  20. # 0: up, 1: right, 2: down, 3: left
  21. # 00 means going up   , then going up   -> ┃
  22. # 12 means going right, then going down -> ┓
  23. sets=(
  24.     "┃┏ ┓┛━┓  ┗┃┛┗ ┏━"
  25.     "│╭ ╮╯─╮  ╰│╯╰ ╭─"
  26.     "│┌ ┐┘─┐  └│┘└ ┌─"
  27.     "║╔ ╗╝═╗  ╚║╝╚ ╔═"
  28.     "|+ ++-+  +|++ +-"
  29.     "|/ \/-\  \|/\ /-"
  30.     ".. ....  .... .."
  31.     ".o oo.o  o.oo o."
  32.     "-\ /\|/  /-\/ \|"  # railway
  33.     "╿┍ ┑┚╼┒  ┕╽┙┖ ┎╾"  # knobby pipe
  34.     "|p qd-q  b|db p-"
  35. )
  36. v=()
  37. RNDSTART=0
  38. BOLD=1
  39. NOCOLOR=0
  40.  
  41. OPTIND=1
  42. while getopts "p:t:f:s:r:RBChv" arg; do
  43. case $arg in
  44.     p) ((p=(OPTARG>0)?OPTARG:p));;
  45.     t)
  46.         if [[ "$OPTARG" = c???????????????? ]]; then
  47.             V+=(${#sets[@]})
  48.             sets+=("${OPTARG:1}")
  49.         else
  50.             ((OPTARG>=0 && OPTARG<${#sets[@]})) && V+=($OPTARG)
  51.         fi
  52.         ;;
  53.     f) ((f=(OPTARG>19 && OPTARG<101)?OPTARG:f));;
  54.     s) ((s=(OPTARG>4 && OPTARG<16 )?OPTARG:s));;
  55.     r) ((r=(OPTARG>=0)?OPTARG:r));;
  56.     R) RNDSTART=1;;
  57.     B) BOLD=0;;
  58.     C) NOCOLOR=1;;
  59.     h) echo -e "Usage: $(basename $0) [OPTION]..."
  60.         echo -e "Animated pipes terminal screensaver.\n"
  61.         echo -e " -p [1-]\tnumber of pipes (D=1)."
  62.         echo -e " -t [0-$((${#sets[@]} - 1))]\ttype of pipes, can be used more than once (D=0)."
  63.         echo -e " -t c[16 chars]\tcustom type of pipes."
  64.         echo -e " -f [20-100]\tframerate (D=75)."
  65.         echo -e " -s [5-15]\tprobability of a straight fitting (D=13)."
  66.         echo -e " -r LIMIT\treset after x characters, 0 if no limit (D=2000)."
  67.         echo -e " -R \t\trandom starting point."
  68.         echo -e " -B \t\tno bold effect."
  69.         echo -e " -C \t\tno color."
  70.         echo -e " -h\t\thelp (this screen)."
  71.         echo -e " -v\t\tprint version number.\n"
  72.         exit 0;;
  73.     v) echo "$(basename -- "$0") $VERSION"
  74.         exit 0
  75.     esac
  76. done
  77.  
  78. # set default values if not by options
  79. ((${#V[@]})) || V=(0)
  80.  
  81. cleanup() {
  82.     # clear up standard input
  83.     read -t 0.001 && cat </dev/stdin>/dev/null
  84.  
  85.     # terminal has no smcup and rmcup capabilities
  86.     ((FORCE_RESET)) && reset && exit 0
  87.  
  88.     tput rmcup
  89.     tput cnorm
  90.     stty echo
  91.     ((NOCOLOR)) && echo -ne '\e[0m'
  92.     exit 0
  93. }
  94. trap resize SIGWINCH
  95. trap cleanup HUP TERM
  96. trap 'break 2' INT
  97.  
  98. resize
  99.  
  100. for (( i=1; i<=p; i++ )); do
  101.     c[i]=$((i%8)) n[i]=0 l[i]=0
  102.     ((x[i]=RNDSTART==1?RANDOM*w/32768:w/2))
  103.     ((y[i]=RNDSTART==1?RANDOM*h/32768:h/2))
  104.     v[i]=${V[${#V[@]} * RANDOM / M]}
  105. done
  106.  
  107. stty -echo
  108. tput smcup || FORCE_RESET=1
  109. tput civis
  110. tput clear
  111. # any key press exits the loop and this script
  112. while REPLY=; read -t 0.0$((1000/f)) -n 1 2>/dev/null; [[ -z $REPLY ]] ; do
  113.     for (( i=1; i<=p; i++ )); do
  114.         # New position:
  115.         ((${l[i]}%2)) && ((x[i]+=-${l[i]}+2,1)) || ((y[i]+=${l[i]}-1))
  116.  
  117.         # Loop on edges (change color on loop):
  118.         ((${x[i]}>=w||${x[i]}<0||${y[i]}>=h||${y[i]}<0)) && ((c[i]=RANDOM%8, v[i]=V[${#V[@]}*RANDOM/M]))
  119.         ((x[i]=(x[i]+w)%w))
  120.         ((y[i]=(y[i]+h)%h))
  121.  
  122.         # New random direction:
  123.         ((n[i]=RANDOM%s-1))
  124.         ((n[i]=(${n[i]}>1||${n[i]}==0)?${l[i]}:${l[i]}+${n[i]}))
  125.         ((n[i]=(${n[i]}<0)?3:${n[i]}%4))
  126.  
  127.         # Print:
  128.         tput cup ${y[i]} ${x[i]}
  129.         echo -ne "\e[${BOLD}m"
  130.         [[ $NOCOLOR == 0 ]] && echo -ne "\e[3${c[i]}m"
  131.         echo -n "${sets[v[i]]:l[i]*4+n[i]:1}"
  132.         l[i]=${n[i]}
  133.     done
  134.     ((r>0 && t*p>=r)) && tput reset && tput civis && t=0 || ((t++))
  135. done
  136.  
  137. cleanup
captcha