2023-05-07 13:53:31 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
WALLPAPERS_PATH="$HOME/.config/wallpapers"
|
|
|
|
WALLPAPERS=$(find "$WALLPAPERS_PATH" -type f,l)
|
2023-09-22 10:14:12 +02:00
|
|
|
DEBUG=0
|
2023-05-07 13:53:31 +02:00
|
|
|
|
|
|
|
function help() {
|
2023-05-08 12:54:21 +02:00
|
|
|
echo "Usage:"
|
2023-05-11 17:33:14 +02:00
|
|
|
echo -e " wallpaper [OPTIONS] [PATH]"
|
2023-05-07 13:53:31 +02:00
|
|
|
echo ""
|
|
|
|
echo "Options:"
|
2023-05-08 12:54:21 +02:00
|
|
|
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"
|
2023-05-07 13:53:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function randomWallpaper() {
|
2023-06-08 11:38:08 +02:00
|
|
|
echo "$WALLPAPERS" | shuf -n 1
|
|
|
|
}
|
|
|
|
|
|
|
|
function setWallpaperX {
|
|
|
|
feh --bg-fill "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
function setWallpaperWayland {
|
2023-09-22 10:14:12 +02:00
|
|
|
if [[ $DEBUG -eq 1 ]]; then
|
|
|
|
swaybg --mode fill -i "$1" &
|
|
|
|
else
|
|
|
|
swaybg --mode fill -i "$1" 2>/dev/null &
|
|
|
|
fi
|
2023-05-07 13:53:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setWallpaper() {
|
2023-06-08 11:38:08 +02:00
|
|
|
if [[ -z ${WAYLAND_DISPLAY+x} ]]; then
|
|
|
|
setWallpaperX "$1"
|
|
|
|
else
|
|
|
|
setWallpaperWayland "$1"
|
|
|
|
fi
|
2023-05-07 13:53:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# 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
|
|
|
|
;;
|
2023-09-22 10:14:12 +02:00
|
|
|
-v | --verbose)
|
|
|
|
DEBUG=1
|
|
|
|
shift # past argument
|
|
|
|
;;
|
2023-05-07 13:53:31 +02:00
|
|
|
*)
|
|
|
|
POSITIONAL+=("$1") # save it in an array for later
|
|
|
|
shift # past argument
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
set -- "${POSITIONAL[@]}" # restore positional arguments
|
|
|
|
|
2023-05-08 12:54:21 +02:00
|
|
|
if [[ $# -gt 1 ]]; then
|
2023-05-11 17:33:14 +02:00
|
|
|
help
|
|
|
|
exit 1
|
2023-05-08 12:54:21 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $# -eq 1 ]]; then
|
2023-05-11 17:33:14 +02:00
|
|
|
WALLPAPER="$1"
|
2023-05-08 12:54:21 +02:00
|
|
|
fi
|
|
|
|
|
2023-05-07 13:53:31 +02:00
|
|
|
if [[ -z ${WALLPAPER+x} ]]; then
|
2023-05-08 12:54:21 +02:00
|
|
|
WALLPAPER=$(echo "$WALLPAPERS" | fzf --preview="viu -sb -h 30 {}" --preview-window=right:70%:wrap)
|
2023-05-07 13:53:31 +02:00
|
|
|
fi
|
|
|
|
|
2023-05-08 12:54:21 +02:00
|
|
|
WALLPAPER=$(realpath "$WALLPAPER")
|
|
|
|
|
|
|
|
if [[ ! -e "$WALLPAPER" ]]; then
|
2023-05-07 13:53:31 +02:00
|
|
|
echo "File not found: $WALLPAPER"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-05-08 12:54:21 +02:00
|
|
|
echo "Setting wallpaper: '$WALLPAPER'"
|
2023-05-07 13:53:31 +02:00
|
|
|
setWallpaper "$WALLPAPER"
|