From 2696fb39dab52372f078823bcb99ecbf4f613bb7 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 17 Jul 2025 15:34:21 -0700 Subject: [PATCH] Protect theme-bg-next from missing background and always start with the first one --- bin/omarchy-theme-bg-next | 55 +++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/bin/omarchy-theme-bg-next b/bin/omarchy-theme-bg-next index cec7c31..fa99ccf 100755 --- a/bin/omarchy-theme-bg-next +++ b/bin/omarchy-theme-bg-next @@ -8,30 +8,41 @@ CURRENT_BACKGROUND_LINK="$HOME/.config/omarchy/current/background" mapfile -d '' -t BACKGROUNDS < <(find "$BACKGROUNDS_DIR" -type f -print0 | sort -z) TOTAL=${#BACKGROUNDS[@]} -# Get current background from symlink -if [[ -L "$CURRENT_BACKGROUND_LINK" ]]; then - CURRENT_BACKGROUND=$(readlink "$CURRENT_BACKGROUND_LINK") +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 - # Default to first background if no symlink exists - CURRENT_BACKGROUND="${BACKGROUNDS[0]}" -fi - -# Find current background index -INDEX=0 -for i in "${!BACKGROUNDS[@]}"; do - if [[ "${BACKGROUNDS[$i]}" == "$CURRENT_BACKGROUND" ]]; then - INDEX=$i - break + # Get current background from symlink + if [[ -L "$CURRENT_BACKGROUND_LINK" ]]; then + CURRENT_BACKGROUND=$(readlink "$CURRENT_BACKGROUND_LINK") + else + # Default to first background if no symlink exists + CURRENT_BACKGROUND="${BACKGROUNDS[0]}" fi -done -# Get next background (wrap around) -NEXT_INDEX=$(((INDEX + 1) % TOTAL)) -NEW_BACKGROUND="${BACKGROUNDS[$NEXT_INDEX]}" + # Find current background index + INDEX=-1 + for i in "${!BACKGROUNDS[@]}"; do + if [[ "${BACKGROUNDS[$i]}" == "$CURRENT_BACKGROUND" ]]; then + INDEX=$i + break + fi + done -# Set new background symlink -ln -nsf "$NEW_BACKGROUND" "$CURRENT_BACKGROUND_LINK" + # 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 -# Relaunch swaybg -pkill -x swaybg -setsid swaybg -i "$NEW_BACKGROUND" -m fill >/dev/null 2>&2 & + # Set new background symlink + ln -nsf "$NEW_BACKGROUND" "$CURRENT_BACKGROUND_LINK" + + # Relaunch swaybg + pkill -x swaybg + setsid swaybg -i "$NEW_BACKGROUND" -m fill >/dev/null 2>&2 & +fi