theme dark:light

This commit is contained in:
Harivansh Rathi 2026-03-20 15:37:34 -04:00
parent 7ae7c1ceec
commit 67128fe09b
7 changed files with 190 additions and 40 deletions

View file

@ -64,11 +64,15 @@
theme = mkScript {
name = "theme";
file = ./theme.sh;
runtimeInputs = with pkgs; [coreutils neovim tmux];
runtimeInputs = with pkgs; [coreutils findutils neovim tmux];
replacements = {
"@DEFAULT_MODE@" = theme.defaultMode;
"@STATE_DIR@" = theme.paths.stateDir;
"@STATE_FILE@" = theme.paths.stateFile;
"@GHOSTTY_DIR@" = theme.paths.ghosttyDir;
"@GHOSTTY_CURRENT_FILE@" = theme.paths.ghosttyCurrentFile;
"@GHOSTTY_DARK_FILE@" = "${theme.paths.ghosttyDir}/cozybox-dark";
"@GHOSTTY_LIGHT_FILE@" = "${theme.paths.ghosttyDir}/cozybox-light";
"@TMUX_DIR@" = theme.paths.tmuxDir;
"@TMUX_CURRENT_FILE@" = theme.paths.tmuxCurrentFile;
"@TMUX_DARK_FILE@" = "${tmuxConfigs.dark}";

View file

@ -16,14 +16,20 @@ read_mode() {
link_mode_assets() {
local mode="$1"
local ghostty_target
local tmux_target
local apple_dark_mode
case "$mode" in
dark)
ghostty_target="@GHOSTTY_DARK_FILE@"
tmux_target="@TMUX_DARK_FILE@"
apple_dark_mode=true
;;
light)
ghostty_target="@GHOSTTY_LIGHT_FILE@"
tmux_target="@TMUX_LIGHT_FILE@"
apple_dark_mode=false
;;
*)
echo "invalid mode: $mode" >&2
@ -31,18 +37,40 @@ link_mode_assets() {
;;
esac
mkdir -p "@STATE_DIR@" "@TMUX_DIR@"
mkdir -p "@STATE_DIR@" "@GHOSTTY_DIR@" "@TMUX_DIR@"
printf '%s\n' "$mode" > "@STATE_FILE@"
ln -sfn "$ghostty_target" "@GHOSTTY_CURRENT_FILE@"
ln -sfn "$tmux_target" "@TMUX_CURRENT_FILE@"
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
for socket in /tmp/nvim-*.sock; do
if [[ "$(uname -s)" == "Darwin" ]] && command -v osascript >/dev/null 2>&1; then
osascript -e "tell application \"System Events\" to tell appearance preferences to set dark mode to ${apple_dark_mode}" >/dev/null 2>&1 || true
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-send "<Cmd>ThemeSync $mode<CR>" >/dev/null 2>&1 || true
done
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}"