summaryrefslogtreecommitdiff
path: root/.local/bin
diff options
context:
space:
mode:
authorsinanmohd <sinan@firemail.cc>2023-05-11 11:23:42 +0530
committersinanmohd <sinan@firemail.cc>2023-05-11 11:29:04 +0530
commit1202435746168a2af28437a6152e800498419cb2 (patch)
tree27fb1a799718b4ad4204032d11cc7f5051d9271f /.local/bin
parentb878cc923fe7132f667a73c19e8db628869ceb8d (diff)
cwall: add the ablity to pick random wallpaper from a directory
pass directory as the first argument to cwall
Diffstat (limited to '.local/bin')
-rwxr-xr-x.local/bin/cwall44
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"
;;