This commit is contained in:
Isaiah Odhner 2023-04-27 00:57:37 -04:00
parent 4332ec1d58
commit 6e7b04da52

View File

@ -11,55 +11,35 @@ TERMINAL_WALLPAPER=/home/io/Projects/textual-paint/samples/galaxies.ans
# Split lines of terminal background into array # Split lines of terminal background into array
IFS=$'\n' read -d '' -r -a lines < $TERMINAL_WALLPAPER IFS=$'\n' read -d '' -r -a lines < $TERMINAL_WALLPAPER
# # Strips ANSI codes from text
# # 1: The text
# # >: The ANSI stripped text
# function strip_ansi() {
# shopt -s extglob # function uses extended globbing
# printf %s "${1//$'\e'\[*([0-9;])m/}"
# }
# Measure image size # Measure image size
image_height=${#lines[@]} image_height=${#lines[@]}
image_width=0 image_width=0
for (( y=0; y<$image_height; y++ )); do for (( y=0; y<$image_height; y++ )); do
line=${lines[$y]} line=${lines[$y]}
# Have to strip ANSI escape sequences from line length # Have to strip ANSI escape sequences from line length
# line_width=50
line_length=${#line} line_length=${#line}
# line_width=$(echo $line | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | wc -c)
filtered_line=$(sed -r "s/\x1B\[[0-9;]*[mK]//g" <<< "$line") filtered_line=$(sed -r "s/\x1B\[[0-9;]*[mK]//g" <<< "$line")
# line_width=$(echo "$filtered_line" | wc -c)
# line_width=$(strip_ansi "$line" | wc -c)
line_width=${#filtered_line} line_width=${#filtered_line}
# echo "y=$y line : $line line_length=$line_length line_width=$line_width" # echo "y=$y line : $line line_length=$line_length"
# echo "y=$y filtered line: $filtered_line" # echo "y=$y filtered line: $filtered_line line_width=$line_width"
# echo "stripped: $(strip_ansi "$line") $(strip_ansi "$line" | wc -c)"
if [[ $line_width -gt $image_width ]]; then if [[ $line_width -gt $image_width ]]; then
image_width=$line_width image_width=$line_width
fi fi
done done
# echo "image_width: $image_width"
# echo "image_height: $image_height"
# Draw background centered # Draw background centered
terminal_width=$(tput cols) terminal_width=$(tput cols)
terminal_height=$(tput lines) terminal_height=$(tput lines)
offset_x=$(( ($terminal_width - $image_width) / 2 )) offset_x=$(( ($terminal_width - $image_width) / 2 ))
offset_y=$(( ($terminal_height - $image_height) / 2 )) offset_y=$(( ($terminal_height - $image_height) / 2 ))
# echo "terminal_width: $terminal_width terminal_height: $terminal_height offset_x: $offset_x offset_y: $offset_y" # Ensure offset is positive
# ensure offset is positive if [[ $offset_x -lt 0 ]]; then offset_x=0; fi
if [[ $offset_x -lt 0 ]]; then if [[ $offset_y -lt 0 ]]; then offset_y=0; fi
offset_x=0 # Draw image
fi
if [[ $offset_y -lt 0 ]]; then
offset_y=0
fi
for (( i=0; i<$image_height; i++ )); do for (( i=0; i<$image_height; i++ )); do
tput cup $(( $offset_y + $i )) $offset_x
tput cup $(( $offset_y + $i )) $offset_x echo -e "${lines[$i]}"
echo -e "${lines[$i]}"
done done
# Reset cursor position # Reset cursor position