Skip to content

Commit

Permalink
Merge pull request #20 from parkercoates/reduced-randomness
Browse files Browse the repository at this point in the history
* Randomize starting positions and colors
* Add -c for specifying colors
* Add -K to keep pipes color and type after crossing edges.
  • Loading branch information
livibetter authored Feb 12, 2018
2 parents 9b7d30c + 40033f4 commit 1cef29e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
30 changes: 30 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,31 @@ Custom pipe, for example: ``-t cMAYFORCEBWITHYOU``.
.. figure:: doc/pipes.tc.png

``-c [0-7]``
------------

Color of pipes, can be used more than once (D=1 2 3 4 5 6 7 0).

Numbers are terminal color numbers. Traditionally typical values are:

+---+------------+
| 0 | background |
+---+------------+
| 1 | red |
+---+------------+
| 2 | green |
+---+------------+
| 3 | yellow |
+---+------------+
| 4 | blue |
+---+------------+
| 5 | magenta |
+---+------------+
| 6 | cyan |
+---+------------+
| 7 | foreground |
+---+------------+

``-f [20-100]``
---------------

Expand Down Expand Up @@ -176,6 +201,11 @@ No color.
.. figure:: doc/pipes.C.png

``-K``
------

Pipes keep their color and type when hitting the screen edge.

``-h``
------

Expand Down
29 changes: 22 additions & 7 deletions pipes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ v=()
RNDSTART=0
BOLD=1
NOCOLOR=0
KEEPCOLORANDTYPE=0

OPTIND=1
while getopts "p:t:f:s:r:RBChv" arg; do
while getopts "p:t:c:f:s:r:RBCKhv" arg; do
case $arg in
p) ((p=(OPTARG>0)?OPTARG:p));;
t)
Expand All @@ -49,23 +50,27 @@ case $arg in
((OPTARG>=0 && OPTARG<${#sets[@]})) && V+=($OPTARG)
fi
;;
c) [[ $OPTARG =~ ^[0-7]$ ]] && C+=($OPTARG);;
f) ((f=(OPTARG>19 && OPTARG<101)?OPTARG:f));;
s) ((s=(OPTARG>4 && OPTARG<16 )?OPTARG:s));;
r) ((r=(OPTARG>=0)?OPTARG:r));;
R) RNDSTART=1;;
B) BOLD=0;;
C) NOCOLOR=1;;
K) KEEPCOLORANDTYPE=1;;
h) echo -e "Usage: $(basename $0) [OPTION]..."
echo -e "Animated pipes terminal screensaver.\n"
echo -e " -p [1-]\tnumber of pipes (D=1)."
echo -e " -t [0-$((${#sets[@]} - 1))]\ttype of pipes, can be used more than once (D=0)."
echo -e " -c [0-7]\tcolor of pipes, can be used more than once (D=1 2 3 4 5 6 7 0)."
echo -e " -t c[16 chars]\tcustom type of pipes."
echo -e " -f [20-100]\tframerate (D=75)."
echo -e " -s [5-15]\tprobability of a straight fitting (D=13)."
echo -e " -r LIMIT\treset after x characters, 0 if no limit (D=2000)."
echo -e " -R \t\trandom starting point."
echo -e " -B \t\tno bold effect."
echo -e " -C \t\tno color."
echo -e " -K \t\tpipes keep their color and type when hitting the screen edge."
echo -e " -h\t\thelp (this screen)."
echo -e " -v\t\tprint version number.\n"
exit 0;;
Expand All @@ -76,6 +81,10 @@ done

# set default values if not by options
((${#V[@]})) || V=(0)
VN=${#V[@]}
((${#C[@]})) || C=(1 2 3 4 5 6 7 0)
CN=${#C[@]}


cleanup() {
# clear up standard input
Expand All @@ -96,11 +105,17 @@ trap 'break 2' INT

resize

ci=$((KEEPCOLORANDTYPE?0:CN*RANDOM/M))
vi=$((KEEPCOLORANDTYPE?0:VN*RANDOM/M))
for (( i=1; i<=p; i++ )); do
c[i]=$((i%8)) n[i]=0 l[i]=0
((x[i]=RNDSTART==1?RANDOM*w/32768:w/2))
((y[i]=RNDSTART==1?RANDOM*h/32768:h/2))
v[i]=${V[${#V[@]} * RANDOM / M]}
n[i]=0
l[i]=0
((x[i]=RNDSTART==1?w*RANDOM/M:w/2))
((y[i]=RNDSTART==1?h*RANDOM/M:h/2))
c[i]=${C[ci]}
((ci=(ci+1)%CN))
v[i]=${V[vi]}
((vi=(vi+1)%VN))
done

stty -echo
Expand All @@ -114,12 +129,12 @@ while REPLY=; read -t 0.0$((1000/f)) -n 1 2>/dev/null; [[ -z $REPLY ]] ; do
((${l[i]}%2)) && ((x[i]+=-${l[i]}+2,1)) || ((y[i]+=${l[i]}-1))

# Loop on edges (change color on loop):
((${x[i]}>=w||${x[i]}<0||${y[i]}>=h||${y[i]}<0)) && ((c[i]=RANDOM%8, v[i]=V[${#V[@]}*RANDOM/M]))
((!KEEPCOLORANDTYPE&&(${x[i]}>=w||${x[i]}<0||${y[i]}>=h||${y[i]}<0))) && ((c[i]=C[$CN*RANDOM/M], v[i]=V[$VN*RANDOM/M]))
((x[i]=(x[i]+w)%w))
((y[i]=(y[i]+h)%h))

# New random direction:
((n[i]=RANDOM%s-1))
((n[i]=s*RANDOM/M-1))
((n[i]=(${n[i]}>1||${n[i]}==0)?${l[i]}:${l[i]}+${n[i]}))
((n[i]=(${n[i]}<0)?3:${n[i]}%4))

Expand Down

0 comments on commit 1cef29e

Please sign in to comment.