mirror of
https://github.com/harivansh-afk/nix.git
synced 2026-04-15 06:04:42 +00:00
142 lines
3.8 KiB
Bash
142 lines
3.8 KiB
Bash
usage() {
|
|
echo "usage: theme <dark|light|toggle|current|gen>"
|
|
}
|
|
|
|
read_mode() {
|
|
if [[ -f "@STATE_FILE@" ]]; then
|
|
mode=$(tr -d '[:space:]' < "@STATE_FILE@")
|
|
if [[ "$mode" == "dark" || "$mode" == "light" ]]; then
|
|
echo "$mode"
|
|
return
|
|
fi
|
|
fi
|
|
|
|
echo "@DEFAULT_MODE@"
|
|
}
|
|
|
|
set_wallpaper() {
|
|
if [[ "$(uname -s)" == "Darwin" ]] && command -v osascript >/dev/null 2>&1; then
|
|
if [[ -f "@WALLPAPER_CURRENT_FILE@" ]]; then
|
|
wp_resolved=$(readlink -f "@WALLPAPER_CURRENT_FILE@" 2>/dev/null || echo "@WALLPAPER_CURRENT_FILE@")
|
|
# macOS caches wallpaper data by file path - copy to a unique temp path
|
|
# so macOS is forced to read the new image data
|
|
wp_dir=$(dirname "$wp_resolved")
|
|
wp_tmp="${wp_dir}/.wallpaper-active-$$.jpg"
|
|
rm -f "${wp_dir}"/.wallpaper-active-*.jpg 2>/dev/null || true
|
|
cp "$wp_resolved" "$wp_tmp"
|
|
osascript -e "tell application \"System Events\" to tell every desktop to set picture to \"${wp_tmp}\"" >/dev/null 2>&1 || true
|
|
fi
|
|
fi
|
|
}
|
|
|
|
link_mode_assets() {
|
|
local mode="$1"
|
|
local fzf_target
|
|
local ghostty_target
|
|
local tmux_target
|
|
local apple_dark_mode
|
|
|
|
case "$mode" in
|
|
dark)
|
|
fzf_target="@FZF_DARK_FILE@"
|
|
ghostty_target="@GHOSTTY_DARK_FILE@"
|
|
tmux_target="@TMUX_DARK_FILE@"
|
|
lazygit_target="@LAZYGIT_DARK_FILE@"
|
|
wallpaper="@WALLPAPER_DARK_FILE@"
|
|
apple_dark_mode=true
|
|
;;
|
|
light)
|
|
fzf_target="@FZF_LIGHT_FILE@"
|
|
ghostty_target="@GHOSTTY_LIGHT_FILE@"
|
|
tmux_target="@TMUX_LIGHT_FILE@"
|
|
lazygit_target="@LAZYGIT_LIGHT_FILE@"
|
|
wallpaper="@WALLPAPER_LIGHT_FILE@"
|
|
apple_dark_mode=false
|
|
;;
|
|
*)
|
|
echo "invalid mode: $mode" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
mkdir -p "@STATE_DIR@" "@FZF_DIR@" "@GHOSTTY_DIR@" "@TMUX_DIR@" "@LAZYGIT_DIR@" "@WALLPAPER_DIR@"
|
|
printf '%s\n' "$mode" > "@STATE_FILE@"
|
|
ln -sfn "$fzf_target" "@FZF_CURRENT_FILE@"
|
|
ln -sfn "$ghostty_target" "@GHOSTTY_CURRENT_FILE@"
|
|
ln -sfn "$tmux_target" "@TMUX_CURRENT_FILE@"
|
|
ln -sfn "$lazygit_target" "@LAZYGIT_CURRENT_FILE@"
|
|
|
|
if [[ -f "$wallpaper" ]]; then
|
|
ln -sfn "$wallpaper" "@WALLPAPER_CURRENT_FILE@"
|
|
fi
|
|
|
|
if command -v tmux >/dev/null 2>&1 && tmux start-server >/dev/null 2>&1; then
|
|
tmux source-file "@TMUX_CONFIG@" >/dev/null 2>&1 || true
|
|
fi
|
|
|
|
if [[ "$(uname -s)" == "Darwin" ]] && command -v osascript >/dev/null 2>&1; then
|
|
mkdir -p "@LAZYGIT_DARWIN_DIR@"
|
|
if [[ "$mode" == "dark" ]]; then
|
|
ln -sfn "@LAZYGIT_DARWIN_DARK_FILE@" "@LAZYGIT_DARWIN_FILE@"
|
|
else
|
|
ln -sfn "@LAZYGIT_DARWIN_LIGHT_FILE@" "@LAZYGIT_DARWIN_FILE@"
|
|
fi
|
|
|
|
osascript -e "tell application \"System Events\" to tell appearance preferences to set dark mode to ${apple_dark_mode}" >/dev/null 2>&1 || true
|
|
|
|
set_wallpaper
|
|
|
|
osascript <<'EOF' >/dev/null 2>&1 || true
|
|
tell application "System Events"
|
|
if not (exists process "Ghostty") then
|
|
return
|
|
end if
|
|
|
|
tell process "Ghostty"
|
|
click menu item "Reload Configuration" of menu 1 of menu bar item "Ghostty" of menu bar 1
|
|
end tell
|
|
end tell
|
|
EOF
|
|
fi
|
|
|
|
while IFS= read -r socket; do
|
|
[[ -S "$socket" ]] || continue
|
|
nvim --server "$socket" --remote-expr "execute('ThemeSync $mode')" >/dev/null 2>&1 || true
|
|
done < <(
|
|
{
|
|
find /tmp -maxdepth 1 -type s -name 'nvim-*.sock' 2>/dev/null
|
|
find "${TMPDIR:-/tmp}" -type s -path "*/nvim.${USER}/*/nvim.*" 2>/dev/null
|
|
} | sort -u
|
|
)
|
|
}
|
|
|
|
mode="${1:-current}"
|
|
|
|
case "$mode" in
|
|
dark|light)
|
|
;;
|
|
toggle)
|
|
if [[ "$(read_mode)" == "dark" ]]; then
|
|
mode="light"
|
|
else
|
|
mode="dark"
|
|
fi
|
|
;;
|
|
current)
|
|
read_mode
|
|
exit 0
|
|
;;
|
|
gen)
|
|
wallpaper-gen
|
|
set_wallpaper
|
|
printf 'generated new wallpaper\n'
|
|
exit 0
|
|
;;
|
|
*)
|
|
usage >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
link_mode_assets "$mode"
|
|
printf 'applied %s theme\n' "$mode"
|