nix/scripts/theme
Barrett Ruth feaeff3464 cleanup
2026-02-09 13:59:37 -05:00

134 lines
3.9 KiB
Bash
Executable file

#!/bin/sh
require() {
for cmd in "$@"; do
command -v "$cmd" >/dev/null 2>&1 || {
echo "theme: missing dependency: $cmd" >&2
exit 1
}
done
}
themes="daylight
midnight"
as_list="$(printf "%s\n" "$themes" | awk 'NF{printf "\"%s\",", $0}' | sed 's/,$//')"
case "$(uname)" in
Linux)
if [ -n "$1" ]; then
theme="$1"
else
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
require rofi
theme="$(printf "%s\n" "$themes" | rofi -dmenu -p 'theme')"
else
require dmenu
theme="$(printf "%s\n" "$themes" | dmenu -p 'select theme: ')"
fi
fi
;;
Darwin)
if [ -n "$1" ]; then
theme="$1"
else
theme="$(
osascript <<EOF
set theList to {$as_list}
set chosenTheme to (choose from list theList ¬
with title "Theme" ¬
with prompt "Pick a theme" ¬
OK button name "Apply" ¬
cancel button name "Cancel" ¬
without empty selection allowed)
if chosenTheme is false then
return ""
else
return item 1 of chosenTheme
end if
EOF
)"
fi
;;
*)
exit 1
;;
esac
[ -z "$theme" ] && exit 1
echo "$themes" | grep -Fxq "$theme" || exit 1
case "$(uname)" in
Linux)
if command -v gsettings >/dev/null 2>&1; then
case "$theme" in
midnight)
gsettings set org.gnome.desktop.interface color-scheme 'prefer-dark'
;;
daylight)
gsettings set org.gnome.desktop.interface color-scheme 'prefer-light'
;;
*) ;;
esac
fi
[ "$XDG_SESSION_TYPE" = "wayland" ] && {
if [ "$XDG_CURRENT_DESKTOP" = 'Hyprland' ]; then
current_theme=$(readlink "$XDG_CONFIG_HOME/hypr/theme.conf" 2>/dev/null | awk -F/ '{print $NF}' | sed 's/\.conf$//')
if [ "$current_theme" != "$theme" ]; then
ln -sf "$XDG_CONFIG_HOME/hypr/themes/$theme.conf" "$XDG_CONFIG_HOME/hypr/theme.conf"
ln -sf "$XDG_CONFIG_HOME/waybar/themes/$theme.css" "$XDG_CONFIG_HOME/waybar/themes/theme.css"
hyprctl reload
pkill -USR1 waybar
pkill -USR1 waybar
fi
elif [ "$XDG_CURRENT_DESKTOP" = 'sway' ]; then
current_theme=$(readlink "$XDG_CONFIG_HOME/sway/themes/theme" 2>/dev/null | sed 's|.*/||')
if [ "$current_theme" != "$theme" ]; then
test -f "$XDG_CONFIG_HOME/sway/themes/theme" && ln -sf "$XDG_CONFIG_HOME/sway/themes/$theme" "$XDG_CONFIG_HOME/sway/themes/theme"
swaymsg reload
fi
fi
}
test -d "$XDG_CONFIG_HOME/rofi/themes" && ln -sf "$XDG_CONFIG_HOME/rofi/themes/$theme.rasi" "$XDG_CONFIG_HOME/rofi/themes/theme.rasi"
;;
Darwin)
case "$theme" in
daylight)
osascript -e 'tell app "System Events" to tell appearance preferences to set dark mode to false' 2>/dev/null || true
;;
midnight)
osascript -e 'tell app "System Events" to tell appearance preferences to set dark mode to true' 2>/dev/null || true
;;
esac
;;
esac
test -d "$XDG_CONFIG_HOME/fzf/themes" && ln -sf "$XDG_CONFIG_HOME/fzf/themes/$theme" "$XDG_CONFIG_HOME/fzf/themes/theme"
test -d "$XDG_CONFIG_HOME/rg/themes" && ln -sf "$XDG_CONFIG_HOME/rg/themes/$theme" "$XDG_CONFIG_HOME/rg/themes/theme"
test -d "$XDG_CONFIG_HOME/sioyek/themes" && ln -sf "$XDG_CONFIG_HOME/sioyek/themes/$theme.config" "$XDG_CONFIG_HOME/sioyek/themes/theme.config"
test -d "$XDG_CONFIG_HOME/task/themes" && ln -sf "$XDG_CONFIG_HOME/task/themes/$theme.theme" "$XDG_CONFIG_HOME/task/themes/theme.theme"
if command -v claude >/dev/null 2>&1; then
CLAUDE_CONFIG="${CLAUDE_CONFIG_DIR:-$HOME}/.claude.json"
claude_theme='light'
case "$theme" in
daylight)
claude_theme='light'
;;
midnight)
claude_theme='dark'
;;
esac
test -f "$CLAUDE_CONFIG" && jq ".theme=\"$claude_theme\"" "$CLAUDE_CONFIG" >"$CLAUDE_CONFIG.tmp" && mv "$CLAUDE_CONFIG.tmp" "$CLAUDE_CONFIG"
fi
test -f ~/.zshenv && sed -i "s|^\(export THEME=\).*|\1$theme|" ~/.zshenv
[ -n "$TMUX" ] && tmux setenv -g THEME "$theme"
for socket in /tmp/nvim-*.sock; do
test -S "$socket" && nvim --server "$socket" --remote-expr "luaeval('require(\"config.fzf_reload\").reload()')" 2>/dev/null || true
done