From 1202435746168a2af28437a6152e800498419cb2 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Thu, 11 May 2023 11:23:42 +0530 Subject: cwall: add the ablity to pick random wallpaper from a directory pass directory as the first argument to cwall --- .local/bin/cwall | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to '.local/bin') 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" ;; -- cgit v1.2.3