dotfiles/modules/programs/wallpaper/wallpaper.sh

95 lines
1.7 KiB
Bash
Executable file

#!/usr/bin/env bash
WALLPAPERS_PATH="$HOME/.config/wallpapers"
WALLPAPERS=$(find "$WALLPAPERS_PATH" -type f,l)
DEBUG=0
function help() {
echo "Usage:"
echo -e " wallpaper [OPTIONS] [PATH]"
echo ""
echo "Options:"
echo -e " -h, --help \n\t Show this help message and exit"
echo -e " -r, --random \n\t Set a random wallpaper"
echo -e " -s, --set <PATH> \n\t Set a wallpaper"
}
function randomWallpaper() {
echo "$WALLPAPERS" | shuf -n 1
}
function setWallpaperX {
feh --bg-fill "$1"
}
function setWallpaperWayland {
if [[ $DEBUG -eq 1 ]]; then
swaybg --mode fill -i "$1" &
else
swaybg --mode fill -i "$1" 2>/dev/null &
fi
}
function setWallpaper() {
if [[ -z ${WAYLAND_DISPLAY+x} ]]; then
setWallpaperX "$1"
else
setWallpaperWayland "$1"
fi
}
# Parse arguments
# https://stackoverflow.com/a/14203146
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h | --help)
help
exit 0
;;
-r | --random)
WALLPAPER=$(randomWallpaper)
shift # past argument
;;
-s | --set)
WALLPAPER="$2"
shift # past argument
shift # past value
;;
-v | --verbose)
DEBUG=1
shift # past argument
;;
*)
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional arguments
if [[ $# -gt 1 ]]; then
help
exit 1
fi
if [[ $# -eq 1 ]]; then
WALLPAPER="$1"
fi
if [[ -z ${WALLPAPER+x} ]]; then
WALLPAPER=$(echo "$WALLPAPERS" | fzf --preview="viu -sb -h 30 {}" --preview-window=right:70%:wrap)
fi
WALLPAPER=$(realpath "$WALLPAPER")
if [[ ! -e "$WALLPAPER" ]]; then
echo "File not found: $WALLPAPER"
exit 1
fi
echo "Setting wallpaper: '$WALLPAPER'"
setWallpaper "$WALLPAPER"