setbg (7795B)
1 #!/bin/sh 2 # Set some variables 3 BACKGROUNDS_LIGHT="/Volumes/HDD/Pictures/Backgrounds/light" 4 BACKGROUNDS_DARK="/Volumes/HDD/Pictures/Backgrounds/dark" 5 BACKGROUNDS_DEST_DIR="$HOME/Pictures/Backgrounds" 6 ORIGINALS_DIR_NAME="originals" # where originals are stored after resizing 7 SCREEN_RESOLUTION='1440/900' 8 SCREEN_ASPECT_RATIO='16:10' # == 1440x900 9 SCREEN_ASPECT_RATIO_FLOAT="$(printf '%s\n' "$SCREEN_RESOLUTION" | bc -l)" 10 11 die() { printf '%s\n' "$1" >&2 && exit 1; } 12 13 checkdeps() { 14 for com in "$@"; do 15 command -v "$com" >/dev/null 2>&1 \ 16 || { printf '%s required but not found.\n' "$com" >&2 && exit 1; } 17 done 18 } 19 checkdeps sxiv wal identify convert 20 21 # Parse command line flags 22 while [ $(($#)) -ne 0 ]; do 23 case "$1" in 24 -d|--dark) 25 dark=1 26 break 27 ;; 28 -l|--light) 29 light=1 30 break 31 ;; 32 -h|--help) 33 echo "Usage:" 34 echo "setbg [-d | -l | -h]" 35 exit 0 36 ;; 37 -*) # unsupported flags 38 echo "Unsupported flag $1" >&2 39 exit 1 40 ;; 41 esac 42 done 43 44 # Set variables/flags for light/dark image 45 if [ -n "$light" ]; then 46 img_dir="$BACKGROUNDS_LIGHT" 47 walflags='-l --saturate 0.8' 48 elif [ -n "$dark" ]; then 49 img_dir="$BACKGROUNDS_DARK" 50 walflags='' 51 else 52 die 'Need to choose light (-l) or dark (-d)' 53 fi 54 55 # Choose an image 56 imgpath="$(find "$img_dir" -type f -maxdepth 1 -exec stat -f "%SB %N" -t '%s' {} \; | sort -rn | cut -d' ' -f2- | sxiv -io | head -n 1)" 57 [ -z "$imgpath" ] && die 'Nothing selected, quitting.' 58 [ -f "$imgpath" ] || die "$imgpath does not exist or is not readable." 59 60 printf 'Using %s\n' "$imgpath" 61 62 # Get just the image name 63 imgname="${imgpath##*/}" 64 65 # Get the format, width, height 66 fwh="$(identify -format "%m:%w:%h" "$imgpath")" 67 printf 'Identified as %s\n' "$fwh" 68 w="$(printf %s "$fwh" | cut -d: -f2)" 69 h="$(printf %s "$fwh" | cut -d: -f3)" 70 71 # Calculate the aspect ratio 72 img_aspect_ratio="$(printf '%s/%s\n' "$w" "$h" | bc -l)" 73 printf 'Aspect ratio %s\n' "$img_aspect_ratio" 74 75 # Set the extension according to the format 76 fmt="$(printf %s "$fwh" | cut -d: -f1)" 77 case "$fmt" in 78 JPEG) imgext='jpg';; 79 PNG) imgext='png';; 80 *) die "Unsupported image extension in $imgpath!" 81 esac 82 83 # Compare two aspect ratios (floats) via bc(1) 84 # will be 1 if equal, 0 if not 85 is_equal_aspect_ratio="$(printf '%s == %s\n' "$img_aspect_ratio" "$SCREEN_ASPECT_RATIO_FLOAT" | bc -l)" 86 87 # If the aspect ratio is different, change the aspect ratio of the image and 88 # back up the original 89 if [ "$is_equal_aspect_ratio" -eq 0 ]; then 90 printf 'Resizing to %s aspect ratio\n' "$SCREEN_ASPECT_RATIO" 91 mkdir -p "$img_dir/$ORIGINALS_DIR_NAME" 2>/dev/null 92 [ -f "$img_dir/$ORIGINALS_DIR_NAME/$imgname" ] \ 93 && rm "$img_dir/$imgname" \ 94 && mv "$img_dir/$ORIGINALS_DIR_NAME/$imgname" "$img_dir/$imgname" 95 96 printf 'convert %s -> %s/%s.%s\n' "$imgpath" "$img_dir" "$imgname" "$$" 97 convert "$imgpath" -resize "$SCREEN_ASPECT_RATIO" "$img_dir/$imgname.$$" 98 printf '%s -> %s/%s/\n' "$imgpath" "$img_dir" "$ORIGINALS_DIR_NAME" 99 mv "$imgpath" "$img_dir/$ORIGINALS_DIR_NAME/" 100 printf '%s/%s.%s -> %s/%s\n' "$img_dir" "$imgname" "$$" "$img_dir" "$imgname" 101 mv "$img_dir/$imgname.$$" "$img_dir/$imgname" 102 touch -r "$img_dir/$ORIGINALS_DIR_NAME/$imgname" "$img_dir/$imgname" # modify file times for proper sorting 103 fi 104 105 # Copy the image to some pre-defined location 106 if [ -n "$light" ]; then 107 targetname="$BACKGROUNDS_DEST_DIR/light.$imgext" 108 else 109 targetname="$BACKGROUNDS_DEST_DIR/dark.$imgext" 110 fi 111 112 mkdir -p "$(dirname "$targetname")" 2>/dev/null 113 printf "copying %s -> %s\n" "$imgpath" "$targetname" 114 cp -f "$imgpath" "$targetname" 115 116 # Set the background 117 wal -c 118 # I want to word split here 119 # shellcheck disable=SC2086 120 wal $walflags -i "$(realpath "$targetname")" 121 122 # If on macOS and using Alacritty, gotta do something extra to set terminal colors. 123 # Generated light and dark color schemes will be under the name 'light' and 'dark' 124 # in the Alacritty config file. 125 # 1. Find the config file. 126 # 2. Remove the existing generated theme. 127 # 3. Write the new colors. 128 os=$(uname -s | tr '[:upper:]' '[:lower:]') 129 case "$os" in 130 darwin*) 131 # Logfile defined probably means we're using alacritty 132 if [ -n "$ALACRITTY_LOG" ]; then 133 # Choose the scheme name we're changing 134 if [ -n "$light" ]; then scheme_name="light" 135 else scheme_name="dark"; fi 136 137 # Wal generates a shell script that defines color0..color15 138 SRC="$HOME"/.cache/wal/colors.sh 139 140 [ -e "$SRC" ] || die "Wal colors not found, exiting script. Have you executed Wal before?" 141 printf "Colors found, source ready.\n" 142 143 READLINK=$( command -v greadlink || command -v readlink ) 144 145 # Get config file 146 # Default config path in Mac systems 147 DEFAULT_MACOS_CONFIG="$HOME"/.config/alacritty/alacritty.yml 148 [ -e "$DEFAULT_MACOS_CONFIG" ] || die "Alacritty config not found, exiting script." 149 CFG="$DEFAULT_MACOS_CONFIG" 150 [ -L "$DEFAULT_MACOS_CONFIG" ] && { 151 printf "Following symlink to config...\n" 152 CFG=$($READLINK -f "$DEFAULT_MACOS_CONFIG") 153 } 154 [ -f "$CFG" ] || die "$CFG does not exist or not readable." 155 156 # Get hex colors from Wal cache 157 # No need for shellcheck to check this, it comes from pywal 158 # shellcheck disable=SC1090 159 . "$SRC" 160 161 # Create temp file for sed results 162 tempfile=$(mktemp) 163 trap 'rm $tempfile' INT TERM EXIT 164 165 # Delete existing color declarations generated by this script 166 # That means, everything from '$scheme_name: &${scheme_name}' (indendented by 4 spaces) to the next potential scheme definition (indented by 4 spaces), 167 # deleting everything that starts with 8 spaces (the next level of indentation). 168 # Yes, this is currently very dependant on the file's indentation. Which is bad. 169 # If the scheme definition exists, delete it 170 if grep -q "$scheme_name: &${scheme_name}" "$CFG"; then 171 sed <"$CFG" "/^\( \{4\}\)${scheme_name}: &${scheme_name}/,/^ \{4\}[a-z]*: &[a-z]*\$/ { /^ \{8,\}/d; }" > "$tempfile" \ 172 && cat "$tempfile" > "$CFG" 173 # If it doesn't: 174 else 175 printf "There's no existing 'generated' colors, adding...\n"; 176 # If a 'schemes' declaration exists, place this scheme declaration under it 177 if grep -q "schemes:" "$CFG"; then 178 sed '/schemes:/r /dev/stdin' "$CFG" > "$tempfile" <<EOF 179 $scheme_name: &$scheme_name 180 EOF 181 cat "$tempfile" > "$CFG" 182 # If not, create the full schemes declaration 183 else 184 cat "$CFG" - >"$tempfile" <<EOF 185 schemes: 186 $scheme_name: &$scheme_name 187 EOF 188 cat "$tempfile" > "$CFG" 189 fi 190 fi 191 192 # Write new color definitions 193 # We know $colorX is unset, we set it by sourcing pywal results above, so no need to shellcheck 194 # shellcheck disable=SC2154 195 { sed "/^ \{4\}$scheme_name/ r /dev/stdin" "$CFG" > "$tempfile" <<EOF 196 primary: 197 background: '$color0' 198 foreground: '$color7' 199 cursor: 200 text: '$color0' 201 cursor: '$color7' 202 normal: 203 black: '$color0' 204 red: '$color1' 205 green: '$color2' 206 yellow: '$color3' 207 blue: '$color4' 208 magenta: '$color5' 209 cyan: '$color6' 210 white: '$color7' 211 bright: 212 black: '$color8' 213 red: '$color9' 214 green: '$color10' 215 yellow: '$color11' 216 blue: '$color12' 217 magenta: '$color13' 218 cyan: '$color14' 219 white: '$color15' 220 EOF 221 } && sed "/^colors: /c\\ 222 colors: *${scheme_name}" "$tempfile" > "$CFG" \ 223 && rm "$tempfile" 224 trap - INT TERM EXIT 225 printf "'%s' exported to '%s'\n" "$SRC" "$CFG" 226 fi 227 ;; 228 esac