2. r #@ Reverse character
q #@ Do not print introduction
h #@ Help
v #@ Verbose
)
description="
${progname%-sh} $version (${modified/T/, })
Author: $author
Copyright $copyright
Released under $license; see COPYING for details
${progname%-sh} sends a string of characters flying across the
terminal (or console) window, bouncing off the sides.
OPTIONS:
b #@ Use bold character
c: #@ String character
d: #@ Delay in milliseconds
l: #@ Length of string
r #@ reverse character
q #@ Do not print introduction
h #@ help (-vh for longer help)
v #@ Show status line:
length of delay between iterations
length of string,
string character
print position
"
controls=" CONTROLS:
PageUp - Faster by 1 millisec
PageDown - Slower by 1 millisec
Home - Faster by 10 millisecs
End - Slower by 10 millisecs
The length of the string can be adjusted while running:
INS - Add one character to string
DEL - Remove one character from string
UP, DOWN, LEFT, RIGHT - Change direction
SPACE - Pause; press any key to continue
Character attributes:
b - Toggle bold
r - Toggle reverse
u - Toggle underline
[1-8] - Foreground colour
Other keys:
c - Change character to next character entered
C - Clear screen
d - Prompt for new delay value in milliseconds
h - Show control keys
q - Quit
s - Toggle display of status bar
3. "
press_any_key=$' e[7m Press any key to continue e[0mnn'
verbose=0
IFS=
optstr="${opts[@]}"
IFS=$' tn'
args=( )
####################################################
#$ Functions ###
##################################################
intro()
{
export LESS=eamgRXFj1i
printf "$clearwindow"
printf " %sn" "$description" | ${PAGER:-less}
read -sn1 -ep "$press_any_key"
printf "$clearwindow"
printf "nn %sn" "$controls" | ${PAGER:-less}
read -sn1 -ep $' e[7m Press any key to continue e[0mnn'
[ "$REPLY" = q ] && exit
}
status()
{
local fmt="Delay: %3dms (%.3fs) Length: %3d Character: %ce[K Row: %2d
Column: %3d"
(( status )) || { topline=1; return; }
printat 1 1 $'e[40;37;1m'
printf " $fmt" "$millisecs_delay" "$delay" "$bnum" "$char" "$y" "$x"
printf 'e[0m'
}
set_length()
{
[ $len -ge 0 ] && printat "${y[len]}" "${x[len]}" "$sp"
while [ ${#x[@]} -lt $bnum ]
do
[ $len -lt 0 ] && len=0
x+=( "${x[len]:-1}" )
y+=( "${y[len]:-1}" )
len=$(( ${#x[@]} - 1 )) ## index of last element in arrays
done
while [ ${#x[@]} -gt $bnum ]
do
if [ $len -ge 0 ]
then
unset x[len]
unset y[len]
fi
x=( "${x[@]}" )
y=( "${y[@]}" )
bnum=${#x[@]} ## number of elements in array
len=$(( $bnum - 1 )) ## index of last element in arrays
done
bnum=${#x[@]} ## number of elements in array
len=$(( $bnum - 1 )) ## index of last element in arrays
}
set_delay()
4. {
local arg=$1 n=9
while :
do
case $arg in
"") arg=20; break ;;
-*) arg=0; break ;; ## if less than zero, use 0
.*) arg=${arg#.} ;; ## remove leading period
[!0-9]*) arg=${arg#?} ;;
0?*) arg=${arg#0} ;; ## remove leading zero
0|[1-9]*) break ;; ## clean; continue
esac
done
millisecs_delay=${arg:-20}
if [ $millisecs_delay -le 0 ]
then
millisecs_delay=0
delay=.0001
else
printf -v delay "%d.%03d" "$(( millisecs_delay / 1000 ))" "$
(( millisecs_delay % 1000 ))"
fi
}
get_key() #@ Return ASCII represenation of keypress in variable
{ #@ USAGE: get_key varname
local CR=$'r' LF=$'n' keyvar=${1:-_KEY} tmout=.0001 TMOUT
local ctrl=( '' {A..Z} ) _n
TMOUT=$delay
IFS= read -sn1 -rd '' _key
case $_key in
"") ;;
"$LF") _key=LF ;;
"$ESC")
TMOUT=.01
while read -d '' -sn1 _k
do
_key=$_key$_k
case $_k in
[A-Za-z^~]) TMOUT=0; break ;;
esac
done
TMOUT=$tmout
_esc2key "$_key" _key
;;
*)
if [[ $_key < $ESC ]] ## CTRL-KEY pressed
then
printf -v _n %d "'$_key" ## Convert CTRL-KEY to its ASCII value
_key=^${ctrl[_n]} ## Show ^[X] where X is the letter key
fi
;;
esac
printf -v "$keyvar" %s "$_key"
}
_esc2key()
{
local _ESC2KEY=$1 var=${2:-_ESC2KEY} CSI=$'e[' ESC=$'e'
case $_ESC2KEY in
## Cursor keys
"${CSI}A" | "${CSI}OA" ) _ESC2KEY=UP ;;