From df62152260bdd5c39903894095c57c894fd75723 Mon Sep 17 00:00:00 2001 From: Isaiah Odhner Date: Thu, 27 Apr 2023 01:09:45 -0400 Subject: [PATCH] Add tiling mode to terminal wallpaper script --- terminal-wallpaper.sh | 112 ++++++++++++++++++++++++++++++------------ 1 file changed, 80 insertions(+), 32 deletions(-) diff --git a/terminal-wallpaper.sh b/terminal-wallpaper.sh index b6b993c..3d08c20 100644 --- a/terminal-wallpaper.sh +++ b/terminal-wallpaper.sh @@ -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"