Commit 78d7d616 authored by John A's avatar John A

Improve parsing to increase speed/ reduce CPU use

bonsai.sh now keeps track of the rightmost column containing a
character, so that when parsing each line, it only parses to the last
character- thus saving time and resources.
parent c3f99210
......@@ -308,6 +308,7 @@ setGeometry() {
init() {
IFS=$'\n' # delimit strings by newline
# message processing
if ((flag_m)); then
declare -Ag gridMessage
......@@ -354,7 +355,8 @@ init() {
# fill grid full of spaces
declare -Ag grid
for (( row=0; row < rows; row++ )); do
for (( row=0; row <= rows; row++ )); do
listChanged[$row]=0
for (( col=0; col < cols; col++ )); do
grid[$row,$col]=' '
done
......@@ -568,16 +570,13 @@ branch() {
# add this/these character(s) to our grid
index=0
for char in "${chars[@]}"; do
grid[$y,$((x+index))]="${color}${char}"
index=$((index+1))
done
newX=$((x+index))
grid[$y,$newX]="${color}${char}"
# add this line to our list of edited lines
found=0
for item in "${changed[@]}"; do
[ "$item" = "$y" ] && found=1
# ensure we keep track of last column
[ -n ${listChanged[$y]} ] && [ $newX -gt ${listChanged[$y]} ] && listChanged[$y]=$newX
index=$((index+1))
done
((! found)) && changed+=("$y")
# print what we have so far
if ((live)); then
......@@ -596,23 +595,18 @@ display() {
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' # move cursor to 0,0
# only parse to the last known column with a char in it
for (( col=0; col <= listChanged[$row]; col++ )); do
# grab the character from our grid
char="${grid[$row,$col]}"
lineArray["$col"]="$char"
done
fi
((live)) && printf '%b' '\e[0;0H' # move cursor to 00
# grab the character from our grid
lineArray["$col"]="${grid[$row,$col]}"
done
line="${lineArray[*]}"
line="${lineArray[*]}" # combine array elements into a string
# remove trailing whitespace and reset color
if ((flag_m)) || ((nfetch)); then
line="${line%${line##*[^[:space:]]}}"
line="${line%${line##*[^[:space:]]}}" # remove trailing whitespace and reset color
fi
# add our message unless line is blank
......
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