From dc21f39d18a79415689c8038f7af772dc30c1aa2 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Sun, 7 Mar 2021 21:36:23 +0100 Subject: Update chwp util --- .local/bin/chwp | 53 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) (limited to '.local') diff --git a/.local/bin/chwp b/.local/bin/chwp index 0bec61b..5dacc07 100755 --- a/.local/bin/chwp +++ b/.local/bin/chwp @@ -11,7 +11,9 @@ main() directory=${CHWP_BASEDIR:-$HOME/pictures/wallpapers} size=${CHWP_SIZE:-1920x1080} timeout=${1:-0} - walldir=$directory/$size + walldir="$directory/$size" + + log "main" "Checking $walldir for wallpapers" # Fail early if there's no wallpaper directory if [ ! -d "$walldir" ] @@ -22,11 +24,16 @@ main() CACHEFILE="$(mktemp)" + log "main" "Using $CACHEFILE as CACHEFILE" + find "${walldir}" -type f > "$CACHEFILE" + log main "Found $(wc -l "$CACHEFILE" | awk '{ print $1 }') wallpapers" + # If no timeout was set, just change it once if [ "$timeout" -eq 0 ] then + log main "No timeout set, only updating once" set_papes exit fi @@ -41,37 +48,67 @@ main() set_papes() { + log "set_papes" "Gathering wallpapers" + pape="$(make_pape)" + log set_papes "Setting wallpaper to $pape" + feh --bg-fill "$pape" + + log set_papes "Cleaning up temporary wallpaper image" + rm -f -- "$pape" } make_pape() { + log "make_pape" "Creating wallpaper" + output="$(mktemp)" imagelist="$(mktemp)" monitorlist="$(mktemp)" - xrandr -q | grep -F ' connected ' > "$monitorlist" + if [ -z "$CHWP_MONITORS" ] + then + log "make_pape" "Trying to determine screens" + + xrandr -q \ + | grep -F ' connected ' \ + | grep -Eo '[[:digit:]]+x[[:digit:]]+' \ + > "$monitorlist" + else + log "make_pape" "Using \$CHWP_MONITORS for monitorlist" + + printf "%s" "$CHWP_MONITORS" \ + | tr " " "\n" \ + | grep -v "^$" \ + > "$monitorlist" + fi - for resolution in $CHWP_MONITORS + log "make_pape" "Found $(wc -l "$monitorlist" | awk '{ print $1 }') screen(s)" + + while read -r resolution do + log "make_pape" "Adding wallpaper for $resolution sized screen" + current_pape=$(get_pape "$resolution") if [ "$current_pape" = "" ] then - printf "No wallpapers for %s\n" "$resolution" >&2 + log "make_pape" "No wallpapers for $resolution" current_pape="$(mktemp --tmpdir XXXXX.png)" convert -size "$resolution" canvas:gray "$current_pape" + else + log "make_pape" "Using $current_pape" fi printf '"%s"\n' "$current_pape" >> "$imagelist" unset resolution unset current_pape - done + done < "$monitorlist" # shellcheck disable=SC2046 # shellcheck disable=SC2086 @@ -86,10 +123,16 @@ get_pape() { if [ ! -d "$directory/$1" ] then + log "get_pape" "$directory/$1 does not exist" return 1 fi printf "%s\n" "$(find "$directory/$1" -type f | shuf | head -n 1)" } +log() +{ + printf "[%s] %s: %s\n" "$(date +%FT%T)" "$1" "$2" >&2 +} + main "$@" -- cgit v1.1