hypr/.scripts/random_swww.sh

124 lines
6.1 KiB
Bash
Executable file

#!/usr/bin/env bash
# ╭───────────────╮
# │ SWWW-settings │
# ╰───────────────╯
export SWWW_TRANSITION_FPS=60
export SWWW_TRANSITION_STEP=60
export SWWW_TRANSITION=center
# ╭───────────────────────────────────────────────────────────────────────────╮
# │ Set default excluded directories if SWWW_EXCLUDED_DIRS is not already set │
# │ Dirs to exclude: Anime, Colorful, Gaming, Gifs, Gruvbox, Logos, Muted, │
# │ Nordic, Windoof │
# ╰───────────────────────────────────────────────────────────────────────────╯
: "${SWWW_EXCLUDED_DIRS:=Anime,Colorful,Gaming,Gifs,Logos,Nordic,Windoof}"
_swww() {
if [ -z "$1" ]; then
# ─< random wallpaper selection >─────────────────────────────────────────────────────────
local wall_dir="$HOME/.wallpaper"
# ─< Create an array of directories to exclude >──────────────────────────────────────────
IFS=',' read -ra EXCLUDED_DIRS <<<"$SWWW_EXCLUDED_DIRS"
# # ─< Build the find command with exclusions >─────────────────────────────────────────────
# local find_cmd="find \"$wall_dir\" -type f \( -iname \"*.png\" -o -iname \"*.jpg\" -o -iname \"*.jpeg\" -o -iname \"*.gif\" \)"
#
# # Add directory exclusions
# for dir in "${EXCLUDED_DIRS[@]}"; do
# find_cmd+=" -not \( -path \"$wall_dir/$dir/*\" -prune \)"
# done
#
# # Add final exclusions
# find_cmd+=" -not -path \"*/\.git/*\""
#
# # ─< Select a random picture >────────────────────────────────────────────────────────────
# local pic=$(eval "$find_cmd" | shuf -n 1)
# Alternative approach using shopt
shopt -s globstar nullglob
local pics=()
for pic in "$wall_dir"/**/*.{png,jpg,jpeg,gif}; do
# Skip excluded directories
for dir in "${EXCLUDED_DIRS[@]}"; do
[[ $pic == "$wall_dir/$dir/"* ]] && continue 2
done
pics+=("$pic")
done
local pic=$(printf "%s\n" "${pics[@]}" | shuf -n 1)
# Check if we found any picture
# notify-send "pic" "$pic"
if [ -z "$pic" ]; then
notify-send "ERROR" "No wallpapers found in $wall_dir (excluding: $SWWW_EXCLUDED_DIRS)"
return 1
fi
# swww img "$pic" --transition-fps "$SWWW_TRANSITION_FPS" --transition-step "$SWWW_TRANSITION_STEP" --transition-type "$SWWW_TRANSITION"
# ─< checking for swww >──────────────────────────────────────────────────────────────────
if ! command -v swww >/dev/null 2>&1; then
notify-send "ERROR" "swww was not found on the system"
return 1
fi
# ─< Check if swww daemon is running, start if not >─────────────────────────────────────
if ! pgrep -x "swww-daemon" >/dev/null; then
notify-send "SWWW" "swww not running - starting it now"
swww-daemon
sleep 1 # Give the daemon a second to start
if ! pgrep -x "swww" >/dev/null; then
notify-send "ERROR" "seems like swww didn't start as expected"
return 0
fi
notify-send "SWWW" "started successfully"
fi
# ─< checking the wall_dir variable >─────────────────────────────────────────────────────
# if [[ ! -d "$wall_dir" ]]; then
# notify-send "ERROR" "$wall_dir path not found"
# return 1
# fi
# ─< executing swww with the random $pic >────────────────────────────────────────────────
if swww img "$pic" --transition-fps "$SWWW_TRANSITION_FPS" --transition-step "$SWWW_TRANSITION_STEP" --transition-type "$SWWW_TRANSITION"; then
notify-send "󰸉 " "Changed wallpaper to ${pic##*/}"
notify-send " " "Not sourcing from: $SWWW_EXCLUDED_DIRS"
else
notify-send "ERROR" "Failed to set wallpaper: $pic"
return 1
fi
else
if ! command -v swww >/dev/null 2>&1; then
notify-send "ERROR" "swww was not found on the system"
return 1
fi
# ─< Check if swww daemon is running, start if not >─────────────────────────────────────
if ! pgrep -x "swww-daemon" >/dev/null; then
notify-send "SWWW" "swww not running - starting it now"
swww-daemon
sleep 1 # Give the daemon a second to start
if ! pgrep -x "swww-daemon" >/dev/null; then
notify-send "ERROR" "seems like swww didn't start as expected"
return 1
fi
notify-send "SWWW" "started successfully"
fi
# Check if file exists
if [[ ! -f "$1" ]]; then
notify-send "ERROR" "File not found: $1"
return 1
fi
# ─< executing swww with the random $pic >────────────────────────────────────────────────
if swww img "$1" --transition-fps "$SWWW_TRANSITION_FPS" --transition-step "$SWWW_TRANSITION_STEP" --transition-type "$SWWW_TRANSITION"; then
notify-send "󰸉 " "Changed wallpaper to ${1##*/}"
else
notify-send "ERROR" "Failed to set wallpaper: $1"
return 1
fi
fi
}
_swww "$@"