fix(theme): surface partial-apply failures instead of swallowing them

nomarchy-theme-set chains six optional "tell each app the theme changed"
steps. Each used `command -v X && X || true`, which collapsed two very
different outcomes into the same silent path:

  - X isn't installed -> skip (correct, expected, fine)
  - X exists but returned non-zero -> skip (wrong - user just got a
    half-applied theme with zero feedback about which app didn't refresh)

Replaced the inline guards with a small helper that distinguishes
absent from failed and accumulates real failures into a list. At the
end of the run, if anything failed, we notify-send a single message
naming the apps that didn't refresh ("Did not refresh: Waybar, btop")
and echo the same to stderr. The theme apply itself still completes -
we don't abort the chain on one failure - so the user gets the partial
benefit AND the diagnostic.

Same pattern as the waybar SIGUSR2 fix (386da51): make the hot path
loud about real problems while staying quiet about expected
no-installed states.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Bernardo Magri
2026-04-30 20:08:39 +01:00
parent 5c5b377bd6
commit 5fc9f5ee34

View File

@@ -51,25 +51,55 @@ nomarchy-env-update
nomarchy-theme-set-templates
# Run the chain of "tell each app the theme changed" steps. Each step is
# optional — the corresponding helper might not be installed, the service
# might not be running, the user might not use Obsidian. Skipping is fine;
# what we don't want is a step that EXISTS, fails for a real reason, and
# leaves the user with a half-applied theme + no idea why. Collect every
# real failure into _theme_set_fails and surface them in one notify-send
# at the end so the user knows exactly what didn't refresh.
_theme_set_fails=()
_theme_set_try() {
local label="$1"; shift
# First arg after label is the command to test for existence.
# `systemctl` always exists on a NixOS system, so we let it through
# and rely on its own exit code to decide skip vs fail.
if [[ "$1" != "systemctl" ]] && ! command -v "$1" >/dev/null 2>&1; then
return 0 # not installed → silently skip
fi
if ! "$@" >/dev/null 2>&1; then
_theme_set_fails+=("$label")
fi
}
# Walker reads its CSS via @import of ~/.config/nomarchy/current/theme/apps/walker/style.css,
# and waybar's shared fallback style does the same with waybar.css. HM's sd-switch
# only restarts services whose unit *definition* changed, so when only the imported
# file's contents change, neither gets reloaded. Restart them explicitly.
command -v nomarchy-restart-walker >/dev/null 2>&1 && nomarchy-restart-walker || true
command -v nomarchy-restart-waybar >/dev/null 2>&1 && nomarchy-restart-waybar || true
_theme_set_try "Walker" nomarchy-restart-walker
_theme_set_try "Waybar" nomarchy-restart-waybar
# Hot-reload long-running TUIs / agents that read their colors from
# the active-theme symlink and would otherwise stay on the old palette
# until the user restarts them by hand.
command -v nomarchy-restart-btop >/dev/null 2>&1 && nomarchy-restart-btop || true
command -v nomarchy-restart-opencode >/dev/null 2>&1 && nomarchy-restart-opencode || true
_theme_set_try "btop" nomarchy-restart-btop
_theme_set_try "opencode" nomarchy-restart-opencode
# Sync palette into Obsidian vaults (no-op when the user has no Obsidian
# config or no obsidian.css template in the active theme).
command -v nomarchy-theme-set-obsidian >/dev/null 2>&1 && nomarchy-theme-set-obsidian || true
_theme_set_try "Obsidian" nomarchy-theme-set-obsidian
# Reload the wallpaper — its ExecStart path is stable (~/.config/nomarchy/current/background)
# so sd-switch does not detect a unit change when only the symlink target moves.
systemctl --user restart nomarchy-wallpaper.service 2>/dev/null || true
_theme_set_try "wallpaper" systemctl --user restart nomarchy-wallpaper.service
if (( ${#_theme_set_fails[@]} > 0 )); then
if command -v notify-send >/dev/null 2>&1; then
notify-send -u normal "Theme applied with warnings" \
"Did not refresh: $(IFS=', '; echo "${_theme_set_fails[*]}")"
fi
echo "Warning: failed to refresh: ${_theme_set_fails[*]}" >&2
fi
nomarchy-hook theme-set "$THEME_NAME"