Skip to content

Instantly share code, notes, and snippets.

@akinomyoga
Last active February 20, 2022 01:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akinomyoga/fb8fa72b8016b624227e66fb9b39fdc9 to your computer and use it in GitHub Desktop.
Save akinomyoga/fb8fa72b8016b624227e66fb9b39fdc9 to your computer and use it in GitHub Desktop.
Shell Animation using sleep
# function sleep for bash-4.0+/zsh
##
## 関数 sleep time
## スリープする。
##
## @param[in] time 待ち時間。単位は秒。小数も可能。
##
## 例: $ sleep 0.1
##
## 注意: ファイルディスクリプタ 9 を別の用途に使うと駄目
##
function sleep { local REPLY=; ! read -u 9 -t "$1"; }
if [[ $OSTYPE == cygwin* ]]; then
# Cygwin work around
exec 9< <(
[[ $- == *i* ]] && trap -- '' INT QUIT
while :; do command sleep 2147483647; done
)
if [[ $BASH_VERSION ]]; then
function sleep {
local s="${1%%.*}"
if ((s>0)); then
! read -u 9 -t "$1" s
else
! read -t "$1" s < /dev/tcp/0.0.0.0/80
fi
}
fi
else
rm -f ~/.sleep.tmp
mkfifo ~/.sleep.tmp
exec 9<> ~/.sleep.tmp
rm -f ~/.sleep.tmp
fi
#!/bin/bash
source sleep.sh
[[ $ZSH_VERSION ]] && setopt KSH_ARRAYS
: ${COLUMNS:=$(tput cols)} ${LINES:=$(tput lines)}
((xN=${COLUMNS:-80}-1,yN=${LINES:-24}-1,mfp=((xN+yN)/15+3)*2))
printf '\e[?47h\e7\e[m'
trap -- "printf '\e[m\e8\e[?47l'" 0
[[ $ZSH_VERSION ]] && trap -- exit INT QUIT
let color=196 'colors[cN++]=color+='{6,-36,1,-6,36,-1}{,,,,}
movechar=CBDA
expr_move=(x++ y++ x-- y--)
expr_iswall=(x==xN-1 y==yN-1 x==0 y==0)
while :; do
((x=RANDOM%xN,y=RANDOM%yN,d=0,c=0))
printf '\e[H\e[2J\e[%d;%dH' $((y+1)) $((x+1))
for ((i=0;i<xN*yN;i++)); do
((((a=RANDOM%mfp)<2||expr_iswall[d])&&(
d=(d+1+(a&1)*2)%4,
expr_iswall[d]&&(d=(d+2)%4)),
expr_move[d]))
printf "\e[${movechar:$d:1}\e[48;5;${colors[c++/5%cN]}m \e[D"
sleep 0.01
done
printf "\e[H\e[${yN}B\r\e[m"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment