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,30 +8,41 @@ CURRENT_BACKGROUND_LINK="$HOME/.config/omarchy/current/background"
mapfile -d '' -t BACKGROUNDS < <(find "$BACKGROUNDS_DIR" -type f -print0 | sort -z) mapfile -d '' -t BACKGROUNDS < <(find "$BACKGROUNDS_DIR" -type f -print0 | sort -z)
TOTAL=${#BACKGROUNDS[@]} TOTAL=${#BACKGROUNDS[@]}
# Get current background from symlink if [[ $TOTAL -eq 0 ]]; then
if [[ -L "$CURRENT_BACKGROUND_LINK" ]]; then notify-send "No background was found for theme" -t 2000
CURRENT_BACKGROUND=$(readlink "$CURRENT_BACKGROUND_LINK") pkill -x swaybg
setsid swaybg --color '#000000' >/dev/null 2>&2 &
else else
# Default to first background if no symlink exists # Get current background from symlink
CURRENT_BACKGROUND="${BACKGROUNDS[0]}" if [[ -L "$CURRENT_BACKGROUND_LINK" ]]; then
fi CURRENT_BACKGROUND=$(readlink "$CURRENT_BACKGROUND_LINK")
else
# Find current background index # Default to first background if no symlink exists
INDEX=0 CURRENT_BACKGROUND="${BACKGROUNDS[0]}"
for i in "${!BACKGROUNDS[@]}"; do
if [[ "${BACKGROUNDS[$i]}" == "$CURRENT_BACKGROUND" ]]; then
INDEX=$i
break
fi fi
done
# Get next background (wrap around) # Find current background index
NEXT_INDEX=$(((INDEX + 1) % TOTAL)) INDEX=-1
NEW_BACKGROUND="${BACKGROUNDS[$NEXT_INDEX]}" for i in "${!BACKGROUNDS[@]}"; do
if [[ "${BACKGROUNDS[$i]}" == "$CURRENT_BACKGROUND" ]]; then
INDEX=$i
break
fi
done
# Set new background symlink # Get next background (wrap around)
ln -nsf "$NEW_BACKGROUND" "$CURRENT_BACKGROUND_LINK" 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
# Relaunch swaybg # Set new background symlink
pkill -x swaybg ln -nsf "$NEW_BACKGROUND" "$CURRENT_BACKGROUND_LINK"
setsid swaybg -i "$NEW_BACKGROUND" -m fill >/dev/null 2>&2 &
# Relaunch swaybg
pkill -x swaybg
setsid swaybg -i "$NEW_BACKGROUND" -m fill >/dev/null 2>&2 &
fi