diff options
Diffstat (limited to '.local')
-rwxr-xr-x | .local/bin/cwall | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/.local/bin/cwall b/.local/bin/cwall index 49c11de..58a5f7e 100755 --- a/.local/bin/cwall +++ b/.local/bin/cwall @@ -13,7 +13,7 @@ die() usage() { cat <<- EOF - Usage: ${0##*/} [wallpaper.img] + Usage: ${0##*/} [wallpaper.img|directory] A helper script for swaybg EOF @@ -32,6 +32,41 @@ dep_check() unset dep } +randimg() +{ + # usage: randbg dir + : "${1:?}" + maxattempts=10 + attempt=0 + + img= + while :; do + img="$(find "$1" \ + -name "*.webp" \ + -o -name "*.jpeg" \ + -o -name "*.jpg" \ + -o -name "*.png" \ + -o -name "*.heif" \ + -o -name "*.heic" \ + -o -name "*.avif" | shuf -n1)" + + [ -z "$img" ] && + break; + + # make sure the file is an actual image + case "$(file --brief --dereference --mime-type "$img")" in + "image/"*) + echo "$img" + break; + ;; + *) + attempt=$((attempt + 1)) + [ "$attempt" -gt "$maxattempts" ] && + break; + esac + done +} + updatewall() { : "${1:?}" @@ -58,6 +93,13 @@ main() "image/"*) updatewall "$1" ;; + "inode/directory") + img=$(randimg "$1") + [ -z "$img" ] && + die "no image file in directory" + + updatewall "$img" + ;; *) die "invalid image file" ;; |