Protect theme-bg-next from missing background and always start with the first one

This commit is contained in:
David Heinemeier Hansson
2025-07-17 15:34:21 -07:00
parent 296e232151
commit 2696fb39da

View File

@ -8,6 +8,11 @@ CURRENT_BACKGROUND_LINK="$HOME/.config/omarchy/current/background"
mapfile -d '' -t BACKGROUNDS < <(find "$BACKGROUNDS_DIR" -type f -print0 | sort -z)
TOTAL=${#BACKGROUNDS[@]}
if [[ $TOTAL -eq 0 ]]; then
notify-send "No background was found for theme" -t 2000
pkill -x swaybg
setsid swaybg --color '#000000' >/dev/null 2>&2 &
else
# Get current background from symlink
if [[ -L "$CURRENT_BACKGROUND_LINK" ]]; then
CURRENT_BACKGROUND=$(readlink "$CURRENT_BACKGROUND_LINK")
@ -17,7 +22,7 @@ else
fi
# Find current background index
INDEX=0
INDEX=-1
for i in "${!BACKGROUNDS[@]}"; do
if [[ "${BACKGROUNDS[$i]}" == "$CURRENT_BACKGROUND" ]]; then
INDEX=$i
@ -26,8 +31,13 @@ for i in "${!BACKGROUNDS[@]}"; do
done
# Get next background (wrap around)
if [[ $INDEX -eq -1 ]]; then
# Use the first background when no match was found
NEW_BACKGROUND="${BACKGROUNDS[0]}"
else
NEXT_INDEX=$(((INDEX + 1) % TOTAL))
NEW_BACKGROUND="${BACKGROUNDS[$NEXT_INDEX]}"
fi
# Set new background symlink
ln -nsf "$NEW_BACKGROUND" "$CURRENT_BACKGROUND_LINK"
@ -35,3 +45,4 @@ ln -nsf "$NEW_BACKGROUND" "$CURRENT_BACKGROUND_LINK"
# Relaunch swaybg
pkill -x swaybg
setsid swaybg -i "$NEW_BACKGROUND" -m fill >/dev/null 2>&2 &
fi