comparison binpipes.sh @ 4815:b4c2f1d0035b

<GeekDude> mv pipes.sh bin\\pipes.sh
author HackBot
date Fri, 12 Sep 2014 13:25:37 +0000
parents pipes.sh@b2595fb2d737
children
comparison
equal deleted inserted replaced
4814:d684ab3dcd67 4815:b4c2f1d0035b
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/livibetter/pipes.sh
7
8 VERSION=0.1.1
9
10 M=32768
11 p=1
12 f=75 s=13 r=2000 t=0
13 w=$(tput cols) h=$(tput lines)
14 # ab -> idx = a*4 + b
15 # 0: up, 1: right, 2: down, 3: left
16 # 00 means going up , then going up -> ┃
17 # 12 means going right, then going down -> ┓
18 sets=(
19 "┃┏ ┓┛━┓ ┗┃┛┗ ┏━"
20 "│╭ ╮╯─╮ ╰│╯╰ ╭─"
21 "│┌ ┐┘─┐ └│┘└ ┌─"
22 "║╔ ╗╝═╗ ╚║╝╚ ╔═"
23 "|+ ++-+ +|++ +-"
24 "|/ \/-\ \|/\ /-"
25 )
26 v=()
27 RNDSTART=0
28 NOCOLOR=0
29
30 OPTIND=1
31 while getopts "p:t:f:s:r:RChv" arg; do
32 case $arg in
33 p) ((p=(OPTARG>0)?OPTARG:p));;
34 t) ((OPTARG>=0 && OPTARG<${#sets[@]})) && V+=($OPTARG);;
35 f) ((f=(OPTARG>19 && OPTARG<101)?OPTARG:f));;
36 s) ((s=(OPTARG>4 && OPTARG<16 )?OPTARG:s));;
37 r) ((r=(OPTARG>=0)?OPTARG:r));;
38 R) RNDSTART=1;;
39 C) NOCOLOR=1;;
40 h) echo -e "Usage: $(basename $0) [OPTION]..."
41 echo -e "Animated pipes terminal screensaver.\n"
42 echo -e " -p [1-]\tnumber of pipes (D=1)."
43 echo -e " -t [0-$((${#sets[@]} - 1))]\ttype of pipes, can be used more than once (D=0)."
44 echo -e " -f [20-100]\tframerate (D=75)."
45 echo -e " -s [5-15]\tprobability of a straight fitting (D=13)."
46 echo -e " -r LIMIT\treset after x characters, 0 if no limit (D=2000)."
47 echo -e " -R \t\trandom starting point."
48 echo -e " -C \t\tno color."
49 echo -e " -h\t\thelp (this screen)."
50 echo -e " -v\t\tprint version number.\n"
51 exit 0;;
52 v) echo "$(basename -- "$0") $VERSION"
53 exit 0
54 esac
55 done
56
57 # set default values if not by options
58 ((${#V[@]})) || V=(0)
59
60 # Attempt to workaround for Bash versions < 4, such as 3.2 on Mac:
61 # https://gist.github.com/livibetter/4689307/#comment-892368
62 # Untested--in conduction of using shebang `env bash`--should fall back to
63 # `sleep`
64 printf -v SLEEP "read -t0.0$((1000/f)) -n 1"
65 if $SLEEP &>/dev/null; (($? != 142)); then
66 printf -v SLEEP "sleep 0.0$((1000/f))"
67 fi
68
69 cleanup() {
70 # clear up standard input
71 read -t 0 && cat </dev/stdin>/dev/null
72
73 # terminal has no smcup and rmcup capabilities
74 ((FORCE_RESET)) && reset && exit 0
75
76 tput rmcup
77 tput cnorm
78 stty echo
79 ((NOCOLOR)) && echo -ne '\e[0m'
80 exit 0
81 }
82 trap cleanup HUP TERM
83 trap 'break 2' INT
84
85 for (( i=1; i<=p; i++ )); do
86 c[i]=$((i%8)) n[i]=0 l[i]=0
87 ((x[i]=RNDSTART==1?RANDOM*w/32768:w/2))
88 ((y[i]=RNDSTART==1?RANDOM*h/32768:h/2))
89 v[i]=${V[${#V[@]} * RANDOM / M]}
90 done
91
92 stty -echo
93 tput smcup || FORCE_RESET=1
94 tput civis
95 tput clear
96 # any key press exits the loop and this script
97 while REPLY=; $SLEEP; [[ -z $REPLY ]] ; do
98 for (( i=1; i<=p; i++ )); do
99 # New position:
100 ((${l[i]}%2)) && ((x[i]+=-${l[i]}+2,1)) || ((y[i]+=${l[i]}-1))
101
102 # Loop on edges (change color on loop):
103 ((${x[i]}>w||${x[i]}<0||${y[i]}>h||${y[i]}<0)) && ((c[i]=RANDOM%8, v[i]=V[${#V[@]}*RANDOM/M]))
104 ((x[i]=(x[i]+w)%w))
105 ((y[i]=(y[i]+h)%h))
106
107 # New random direction:
108 ((n[i]=RANDOM%s-1))
109 ((n[i]=(${n[i]}>1||${n[i]}==0)?${l[i]}:${l[i]}+${n[i]}))
110 ((n[i]=(${n[i]}<0)?3:${n[i]}%4))
111
112 # Print:
113 tput cup ${y[i]} ${x[i]}
114 [[ $NOCOLOR == 0 ]] && echo -ne "\033[1;3${c[i]}m"
115 echo -n "${sets[v[i]]:l[i]*4+n[i]:1}"
116 l[i]=${n[i]}
117 done
118 ((r>0 && t*p>=r)) && tput reset && tput civis && t=0 || ((t++))
119 done
120
121 cleanup