Skip to content

Instantly share code, notes, and snippets.

@epleterte
Last active December 18, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epleterte/5715350 to your computer and use it in GitHub Desktop.
Save epleterte/5715350 to your computer and use it in GitHub Desktop.
Wild asian ping pong in the wild
#!/bin/bash -ue
# ANIMATE them wild asian ping pong players
# chr.bryn@gmail.com
# Q( - _-)_____¦___o__Q(-_ - )
court_length=12
speed="0.3"
colors="false"
clear="false"
function print_usage() {
cat <<EOF
Wild asian ping pong players. Kill with ctrl+c
Usage: ${0} [-h|-l <court length>|-s <speed>]
-c Colors!
-C Clear screen before starting match
-h Haalp
-l Court length. Default: ${court_length}
-s Speed, or how long to sleep between 'frames'. Default: ${speed}
Examples:
${0} -c -l 8 -s 0.1
${0} -C -c -l 50 -s 0.02
EOF
}
while getopts cChl:s: o
do
case $o in
c)
colors="true" ;;
C)
clear="true" ;;
h)
print_usage ; exit ;;
l)
court_length=$OPTARG ;;
s)
speed=$OPTARG ;;
esac
done
shift $(($OPTIND-1))
# XXX: more fancy color stuff
if [ "${colors}" == "true" ]; then
if (which tput >/dev/null); then
ldude="$(tput setaf 4)Q$(tput setaf 3)( - _-)$(tput sgr0)"
rdude="$(tput setaf 6)Q$(tput setaf 3)(-_ - )$(tput sgr0)"
ball="$(tput setaf 2)o$(tput sgr0)"
else
# my android has not tput or whatever dude
green='\e[0;32m'
blue='\e[0;34m'
yellow='\e[0;33m'
cyan='\e[0;36m'
reset='\e[0m'
ldude="${blue}Q${yellow}( - _-)${reset}"
rdude="${cyan}Q${yellow}(-_ - )${reset}"
ball="${green}o${reset}"
fi
else
ldude="Q( - _-)"
rdude="Q(-_ - )"
ball="o"
fi
#court=(_ _ _ _ _ ¦ _ _ _ _ _ _)
declare -a court
[ ${court_length} -lt 2 ] && let court_length=2
net_pos=$((${court_length}/2))
# if even number add one as one is lost to the net
[ $(( ${court_length} % 2 )) -eq 0 ] && let court_length+=1
for (( i=0 ; i < ${court_length};i++)); do court+=(_); done
court[$net_pos]="|"
[ "${clear}" == "true" ] && clear
# disable the cursor
if ( which setterm >/dev/null); then
trap "setterm -cursor on || true" EXIT
setterm -cursor off || true
else
# try using shell escape codes - works just as good as setterm if supported.
trap 'printf "\033[?12l\033[?25h"' EXIT
printf "\033[?25l"
fi
pos=0
#op='+=1'
op='++'
while :; do
acourt=""
#c=(${court[@]})
#c[$pos]="${ball}"
# fix this court generation thingy, it suxx:
#for i in ${c[@]}; do acourt="${acourt}${i}"; done
#for (( i=0 ; i < ${#court[@]} ; i++ )); do a="${court[$i]}" ; [ $i -eq $pos ] && a="${ball}" ; acourt="${acourt}${a}" ; done
# humm - something weird with escape codes. echo -e -n prints fine, printf needs some extra help ... ?
for (( i=0 ; i < ${#court[@]} ; i++ )); do a="${court[$i]}" ; [ $i -eq $pos ] && a="$( printf '%0b%s' ${ball} )" ; acourt="${acourt}${a}" ; done
printf "%0b%s%0b%s%0b%s" "${ldude}" "${acourt}" "${rdude}"
#echo -n -e "${ldude}${acourt}${rdude}"
[ $pos -eq 0 ] && op='+=1'
[ $pos -eq $(($court_length-1)) ] && op='--'
let pos${op}
# flush screen
sleep ${speed} ; printf '\r'
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment