Commit f48a3fea authored by John A's avatar John A

Speed up static mode, clean code

Remove unnecessary curly braces, combine escape sequences, fix some
comments
parent de0bdee2
......@@ -362,9 +362,7 @@ init() {
if ((! nfetch)); then
stty -echo # don't echo stdin
printf '%b' '\e[?25l' # hide cursor
printf '%b' '\e[?7l' # disable line wrapping
printf '%b' '\e[2J' # clear screen, move to 0,0
printf '%b' '\e[?25l\e[?7l\e[2J' # hide cursor, disable line wrapping, clear screen and move to 0,0
fi
}
......@@ -384,7 +382,7 @@ branch() {
local dy=0
local chars=()
begin="$(date +%s.%N)" # start timer
((live)) && begin="$(date +%s.%N)" # start timer
branches=$((branches + 1))
......@@ -480,7 +478,6 @@ branch() {
tmpType="shootLeft"
[ $((RANDOM % 2)) -eq 0 ] && tmpType="shootRight"
# secondary shoots alternate from the first
else
case "$tmpType" in
......@@ -505,13 +502,13 @@ branch() {
# choose color
case $type in
trunk|shoot*)
color=${DarkBrown}
[ $(( RANDOM % 4 )) -eq 0 ] && color=${LightBrown}
color=$DarkBrown
[ $(( RANDOM % 4 )) -eq 0 ] && color=$LightBrown
;;
dying) color=${BrownGreen} ;;
dying) color=$BrownGreen ;;
dead) color=${Green} ;;
dead) color=$Green ;;
esac
# choose branch character
......@@ -573,7 +570,7 @@ branch() {
index=$((index+1))
done
# add this line to our list of "edited lines"
# add this line to our list of edited lines
found=0
for item in "${changed[@]}"; do
[ "$item" = "$y" ] && found=1
......@@ -583,13 +580,12 @@ branch() {
# print what we have so far
if ((live)); then
display
end="$(date +%s.%N)" # end timer
# if this step took less than $stepTime, sleep until $stepTime is met
timeLeft="$(printf '%s\n' "($timeStep - ($end - $begin))" | bc -l)"
[ "$(printf '%s\n' "$timeLeft > 0" | bc -l)" -eq 1 ] && sleep "$timeLeft"
((verbose)) && printf '%s\n' "$timeLeft"
if ((live)); then
end="$(date +%s.%N)" # end timer
timeLeft="$(printf '%s\n' "($timeStep - ($end - $begin))" | bc -l)"
[ "$(printf '%s\n' "$timeLeft > 0" | bc -l)" -eq 1 ] && sleep "$timeLeft"
fi
fi
done
}
......@@ -598,14 +594,14 @@ display() {
# parse grid for output
output=""
for (( row=0; row < rows; row++)); do
lineArray=()
# ensure this line actually has characters in it before parsing it
for item in "${changed[@]}"; do
if [ "$row" = "$item" ]; then
for (( col=0; col < cols; col++ )); do
((live)) && printf '%b' '\e[0;0H ' # this prints a space at 0,0 and is necessary
for (( col=0; col <= cols; col++ )); do
((live)) && printf '%b' '\e[0;0H' # moves cursor to 0,0
# grab the character from our grid
char="${grid[$row,$col]}"
lineArray["$col"]="$char"
......@@ -633,9 +629,7 @@ display() {
quit() {
if ((! nfetch)); then
stty echo # echo stdin
printf '%b' '\e[?25h' # show cursor
printf '%b' '\e[?7h' # enable line wrapping
printf '%b\n' "$R" # reset formatting, put cursor on next line
printf '%b\n' '\e[?25h\e[?7h'"${R}" # show cursor, enable line wrapping, reset colors
else
printf '\n' # reset formatting, put cursor on next line
fi
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment