fish: add theme command to toggle kitty light/dark

`theme` switches kitty between the existing dark-theme.auto.conf and
light-theme.auto.conf via `kitty @ set-colors --all --configured`, so it
stays in sync with whatever `kitten themes --light/--dark` selects.
Subcommands: light, dark, auto (hand control back to the OS appearance
via load-config), status.

Tide is resynced by calling __tide_apply_terminal_appearance directly,
so the prompt recolors even if kitty sends no palette-change
notification for a dynamic color change. conf.d/kitty_theme.fish tracks
$fish_terminal_color_theme into $kitty_theme_mode so the toggle still
reads the real state after kitty follows an OS appearance change.

light-theme.auto.conf had no tab colors, which left the dark tab bar on
a light background after switching; add a matching tab style block.
This commit is contained in:
Anton Volnuhin 2026-08-06 01:15:47 +03:00
parent 0defab964d
commit 384bfe57b3
4 changed files with 129 additions and 0 deletions

View File

@ -20,4 +20,12 @@ color7 #bbbbbb
color15 #ffffff
selection_foreground #2a2b33
# START_AUTOGENERATED_TAB_STYLE
# Feel free to update these colors manually and remove these comments.
active_tab_foreground #f8f8f8
active_tab_background #2f5af3
inactive_tab_foreground #2f5af3
inactive_tab_background #f8f8f8
# END_AUTOGENERATED_TAB_STYLE
macos_titlebar_color background

View File

@ -0,0 +1,16 @@
status is-interactive
or exit
# Keep $kitty_theme_mode tracking what the terminal actually reports, so that
# `theme` toggles from the real state even after kitty follows an OS appearance
# change on its own. fish populates $fish_terminal_color_theme at the first
# prompt and again whenever the terminal signals a palette change.
function __kitty_theme_track --on-variable fish_terminal_color_theme
contains -- "$fish_terminal_color_theme" light dark
or return
test "$kitty_theme_mode" = "$fish_terminal_color_theme"
and return
set --universal kitty_theme_mode $fish_terminal_color_theme
end

View File

@ -0,0 +1,14 @@
function __kitty_theme_current --description 'Report the color mode kitty is currently using'
# $kitty_theme_mode is kept in sync with the terminal by conf.d/kitty_theme.fish
# and by `theme` itself; the rest are fallbacks for shells that have not yet
# seen an interactive prompt.
if contains -- "$kitty_theme_mode" light dark
echo $kitty_theme_mode
else if contains -- "$fish_terminal_color_theme" light dark
echo $fish_terminal_color_theme
else if test "$(defaults read -g AppleInterfaceStyle 2>/dev/null)" = Dark
echo dark
else
echo light
end
end

View File

@ -0,0 +1,91 @@
function theme --description 'Switch kitty between its light and dark color themes'
set -l config_dir $HOME/.config/kitty
set -q KITTY_CONFIG_DIRECTORY
and set config_dir $KITTY_CONFIG_DIRECTORY
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 switch to light' \
' theme dark switch to dark' \
' theme auto follow the macOS appearance again' \
' theme status print the current mode'
return 0
case status
__kitty_theme_current
return 0
case toggle
if test (__kitty_theme_current) = dark
set target light
else
set target dark
end
case auto light dark
# handled below
case '*'
echo "theme: unknown mode '$target' (expected light, dark, auto or status)" >&2
return 2
end
if not set -q KITTY_WINDOW_ID
echo "theme: not running inside kitty" >&2
return 1
end
if test "$target" = auto
# Re-reading the config restores kitty's own OS-appearance handling,
# which a manual override suppresses until the OS appearance changes.
kitty @ load-config
or return $status
if test "$(defaults read -g AppleInterfaceStyle 2>/dev/null)" = Dark
set target dark
else
set target light
end
else
set -l theme_file $config_dir/$target-theme.auto.conf
if not test -r $theme_file
echo "theme: cannot read $theme_file" >&2
return 1
end
# Only pass through settings whose value is a hex color; kitty's
# set-colors rejects everything else these files may contain.
set -l colors (awk 'NF == 2 && $1 !~ /^#/ && $2 ~ /^#[0-9a-fA-F]+$/ { print $1 "=" $2 }' $theme_file)
if test (count $colors) -eq 0
echo "theme: no colors found in $theme_file" >&2
return 1
end
kitty @ set-colors --all --configured $colors
or return $status
end
set --universal kitty_theme_mode $target
if functions -q __tide_apply_terminal_appearance
__tide_apply_terminal_appearance $target
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
echo $target
end