dotfiles/private_dot_config/private_fish/functions/private_kitten.fish
Anton Volnuhin addce489b4 kitty: remember the light/dark mode across restarts
kitty gives *-theme.auto.conf priority over everything -- includes and
even --override -- and has no option to stop following the OS. So the
only way to start in a chosen mode is to control what those files
contain. Make them generated:

  themes/{light,dark,no-preference}.conf  canonical, the real themes
  theme-mode                              auto | light | dark
  apply-theme-mode.sh                      writes the three .auto.conf

Under `auto` each appearance keeps its own theme, so kitty follows the
OS as before. Pinned, all three get the pinned theme, so kitty starts in
it whatever the OS says and a config reload no longer resets it.

`theme` now records the mode and reloads instead of calling set-colors,
which is why the setting survives restarts. A bare `theme` records
`auto` rather than pinning when the toggle lands on the macOS
appearance, so nothing is pinned unless it actually differs.

The kitten shim hashes the automatic theme files before running `kitten
themes` and copies whichever changed into themes/, so picking a new
light theme while pinned to dark is kept and applied on the next switch.
Covered by tests/test_theme_mode.sh.

conf.d/kitty_theme.fish is gone: theme-mode is now the source of truth,
so the universal-variable tracker it maintained is redundant.
2026-08-06 01:49:45 +03:00

61 lines
1.9 KiB
Fish

function kitten --description 'Run kitten and restore Kitty theme overrides'
set -l config_dir "$HOME/.config/kitty"
if set -q KITTY_CONFIG_DIRECTORY
set config_dir "$KITTY_CONFIG_DIRECTORY"
end
set -l is_themes false
if test (count $argv) -gt 0; and test "$argv[1]" = themes
set is_themes true
end
# The automatic theme files are generated from $config_dir/themes, so record
# their state first: whichever one the kitten rewrites is the theme just
# chosen for that appearance, and has to be captured before the mode is
# re-applied over the top of it.
set -l appearances dark light no-preference
set -l before
if test $is_themes = true
for appearance in $appearances
set -l file "$config_dir/$appearance-theme.auto.conf"
if test -r "$file"
set -a before (command shasum <"$file" | string split -f1 ' ')
else
set -a before missing
end
end
end
command kitten $argv
set -l kitten_status $status
if test $kitten_status -eq 0
and test $is_themes = true
for index in (seq (count $appearances))
set -l appearance $appearances[$index]
set -l file "$config_dir/$appearance-theme.auto.conf"
test -r "$file"
or continue
set -l now (command shasum <"$file" | string split -f1 ' ')
test "$now" = "$before[$index]"
and continue
command mkdir -p "$config_dir/themes"
or return $status
command cp "$file" "$config_dir/themes/$appearance.conf"
or return $status
end
"$HOME/.config/kitty/apply-theme-mode.sh" "$config_dir"
or return $status
if set -q KITTY_WINDOW_ID
command kitty @ load-config
or return $status
end
end
return $kitten_status
end