Add tiling mode to terminal wallpaper script

This commit is contained in:
Isaiah Odhner 2023-04-27 01:09:45 -04:00
parent 6e7b04da52
commit df62152260

View File

@ -5,42 +5,90 @@
clear
TERMINAL_WALLPAPER=/home/io/Projects/textual-paint/samples/galaxies.ans
# Draw terminal background (basic)
# cat $TERMINAL_WALLPAPER
TERMINAL_WALLPAPER_MODE=TILE
# Split lines of terminal background into array
IFS=$'\n' read -d '' -r -a lines < $TERMINAL_WALLPAPER
# Draw terminal background according to mode
if [[ $TERMINAL_WALLPAPER_MODE == "TOP_LEFT" ]]; then
cat $TERMINAL_WALLPAPER
elif [[ $TERMINAL_WALLPAPER_MODE == "CENTER" ]]; then
# Measure image size
image_height=${#lines[@]}
image_width=0
for (( y=0; y<$image_height; y++ )); do
line=${lines[$y]}
# Have to strip ANSI escape sequences from line length
line_length=${#line}
filtered_line=$(sed -r "s/\x1B\[[0-9;]*[mK]//g" <<< "$line")
line_width=${#filtered_line}
# Split lines of terminal background into array
IFS=$'\n' read -d '' -r -a lines < $TERMINAL_WALLPAPER
# echo "y=$y line : $line line_length=$line_length"
# echo "y=$y filtered line: $filtered_line line_width=$line_width"
# Measure image size
image_height=${#lines[@]}
image_width=0
for (( y=0; y<$image_height; y++ )); do
line=${lines[$y]}
# Have to strip ANSI escape sequences from line length
line_length=${#line}
filtered_line=$(sed -r "s/\x1B\[[0-9;]*[mK]//g" <<< "$line")
line_width=${#filtered_line}
if [[ $line_width -gt $image_width ]]; then
image_width=$line_width
fi
done
# Draw background centered
terminal_width=$(tput cols)
terminal_height=$(tput lines)
offset_x=$(( ($terminal_width - $image_width) / 2 ))
offset_y=$(( ($terminal_height - $image_height) / 2 ))
# Ensure offset is positive
if [[ $offset_x -lt 0 ]]; then offset_x=0; fi
if [[ $offset_y -lt 0 ]]; then offset_y=0; fi
# Draw image
for (( i=0; i<$image_height; i++ )); do
tput cup $(( $offset_y + $i )) $offset_x
echo -e "${lines[$i]}"
done
# echo "y=$y line : $line line_length=$line_length"
# echo "y=$y filtered line: $filtered_line line_width=$line_width"
if [[ $line_width -gt $image_width ]]; then
image_width=$line_width
fi
done
# Calculate offset
terminal_width=$(tput cols)
terminal_height=$(tput lines)
offset_x=$(( ($terminal_width - $image_width) / 2 ))
offset_y=$(( ($terminal_height - $image_height) / 2 ))
# Ensure offset is positive
if [[ $offset_x -lt 0 ]]; then offset_x=0; fi
if [[ $offset_y -lt 0 ]]; then offset_y=0; fi
# Draw image
for (( i=0; i<$image_height; i++ )); do
tput cup $(( $offset_y + $i )) $offset_x
echo -e "${lines[$i]}"
done
elif [[ $TERMINAL_WALLPAPER_MODE == "TILE" ]]; then
# Split lines of terminal background into array
IFS=$'\n' read -d '' -r -a lines < $TERMINAL_WALLPAPER
# Measure image size
image_height=${#lines[@]}
image_width=0
for (( y=0; y<$image_height; y++ )); do
line=${lines[$y]}
# Have to strip ANSI escape sequences from line length
line_length=${#line}
filtered_line=$(sed -r "s/\x1B\[[0-9;]*[mK]//g" <<< "$line")
line_width=${#filtered_line}
# echo "y=$y line : $line line_length=$line_length"
# echo "y=$y filtered line: $filtered_line line_width=$line_width"
if [[ $line_width -gt $image_width ]]; then
image_width=$line_width
fi
done
# Draw tiled image
tput rmam
setterm -linewrap off
terminal_width=$(tput cols)
terminal_height=$(tput lines)
for (( y=0; y<$terminal_height; y+=$image_height )); do
for (( x=0; x<$terminal_width; x+=$image_width )); do
for (( i=0; i<$image_height; i++ )); do
# Don't scroll past bottom of terminal
if [[ $(( $y + $i + 1 )) -ge $terminal_height ]]; then
break
fi
# Draw line
tput cup $(( $y + $i )) $x
echo -e "${lines[$i]}"
done
done
done
tput smam
setterm -linewrap on
fi
# Reset cursor position
echo -e "\033[0;0H"