dotfiles/private_dot_config/private_fish/functions/theme.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

89 lines
2.8 KiB
Fish

function theme --description 'Switch kitty between its light and dark color themes'
set -l config_dir (__kitty_config_dir)
if test (count $argv) -gt 1
echo "theme: expected at most one argument" >&2
return 2
end
set -l target $argv[1]
test -z "$target"
and set target toggle
switch $target
case -h --help help
printf '%s\n' \
'Usage: theme [light|dark|auto|status]' \
'' \
' theme toggle between light and dark' \
' theme light pin kitty to light' \
' theme dark pin kitty to dark' \
' theme auto follow the macOS appearance again' \
' theme status print the current mode' \
'' \
'The mode is remembered in '(__kitty_config_dir)'/theme-mode and' \
'applies to new kitty instances too.'
return 0
case status
set -l setting (__kitty_theme_mode_setting)
if test "$setting" = auto
printf '%s (auto, following macOS)\n' (__kitty_theme_current)
else
printf '%s (pinned)\n' $setting
end
return 0
case toggle
if test (__kitty_theme_current) = dark
set target light
else
set target dark
end
# Nothing to pin when the result already matches macOS -- record
# `auto` so kitty keeps following the OS from here on.
test "$target" = (__kitty_macos_appearance)
and set target auto
case auto light dark
# handled below
case '*'
echo "theme: unknown mode '$target' (expected light, dark, auto or status)" >&2
return 2
end
set -l apply "$HOME/.config/kitty/apply-theme-mode.sh"
if not test -x $apply
echo "theme: missing $apply" >&2
return 1
end
echo $target >$config_dir/theme-mode
or return $status
$apply "$config_dir"
or return $status
# Reloading is what makes kitty pick up the regenerated theme files.
if set -q KITTY_WINDOW_ID
kitty @ load-config
or return $status
end
set -l effective (__kitty_theme_current)
if functions -q __tide_apply_terminal_appearance
__tide_apply_terminal_appearance $effective
set -l apply_status $status
if test $apply_status -eq 0
functions -q _tide_cache_variables
and _tide_cache_variables
functions -q _tide_sub_reload
and tide reload
end
end
if test "$target" = auto
printf '%s (auto, following macOS)\n' $effective
else
printf '%s (pinned)\n' $effective
end
end