dotfiles/private_dot_config/kitty/private_executable_apply-theme-mode.sh
Anton Volnuhin 1c4e3774cf kitty: put the grok themes in the picker, move the mode store aside
`kitten themes` treats every .conf in <config-dir>/themes as a user
theme, which the mode system's canonical store was sitting in -- so the
picker was offering three bogus entries named light, dark and
no-preference, and had been since that store was introduced.

Move the store to ./theme-modes so ./themes means only what the kitten
means by it, and repoint apply-theme-mode.sh, the kitten shim, the
run_after hook and the test at the new path.

GrokNight and GrokDay move into ./themes with the ## name/author/
license/upstream/blurb header the kitten reads, so they now show up in
`kitten themes` and `--dump-theme` resolves them.

theme-modes/dark.conf carries the #202020 theme that was already on
disk: the file had to be re-added at its new path, so that drift comes
along with it.
2026-08-06 03:24:48 +03:00

88 lines
2.9 KiB
Bash

#!/bin/sh
# Regenerate the *-theme.auto.conf files kitty reads at startup from the
# canonical themes in ./theme-modes, honouring the mode recorded in ./theme-mode:
#
# auto every appearance keeps its own theme, so kitty follows the OS
# light every appearance gets the light theme, so kitty stays light
# dark every appearance gets the dark theme, so kitty stays dark
#
# kitty gives the automatic theme files priority over everything else -- includes
# and even --override -- so pinning a mode means writing the wanted theme into
# whichever file the OS makes kitty pick. That is why the real themes live in
# ./theme-modes and these three files are generated. (./themes is separate:
# that is where the themes kitten looks for user themes.)
set -eu
# Permission bits of a file, or 600 if they cannot be read. GNU stat spells this
# `-c %a` while BSD/macOS spells it `-f %Lp`, and each mis-parses the other's
# form -- GNU treats `-f` as --file-system and prints a block of filesystem info
# before failing -- so try one at a time and only accept an octal answer.
file_mode() {
_m=$(stat -c '%a' "$1" 2>/dev/null) || _m=''
case "$_m" in
[0-7][0-7][0-7] | [0-7][0-7][0-7][0-7]) printf '%s\n' "$_m"; return 0 ;;
esac
_m=$(stat -f '%Lp' "$1" 2>/dev/null) || _m=''
case "$_m" in
[0-7][0-7][0-7] | [0-7][0-7][0-7][0-7]) printf '%s\n' "$_m"; return 0 ;;
esac
printf '600\n'
}
script_dir="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
config_dir=${1:-"$script_dir"}
canonical_dir="$config_dir/theme-modes"
mode_file="$config_dir/theme-mode"
mode=auto
if [ -f "$mode_file" ]; then
mode=$(tr -d ' \t\r\n' < "$mode_file")
fi
case "$mode" in
auto | light | dark) ;;
*) mode=auto ;;
esac
mkdir -p "$canonical_dir"
# Adopt the current automatic themes as canonical when they have not been
# captured yet. Only safe while following the OS: under a pinned mode the
# generated files all hold the pinned theme and would overwrite the others.
if [ "$mode" = auto ]; then
for appearance in dark light no-preference; do
canonical="$canonical_dir/$appearance.conf"
generated="$config_dir/$appearance-theme.auto.conf"
if [ ! -f "$canonical" ] && [ -f "$generated" ]; then
cp "$generated" "$canonical"
fi
done
fi
for appearance in dark light no-preference; do
if [ "$mode" = auto ]; then
source_name=$appearance
else
source_name=$mode
fi
canonical="$canonical_dir/$source_name.conf"
[ -f "$canonical" ] || continue
generated="$config_dir/$appearance-theme.auto.conf"
mode_bits=600
if [ -f "$generated" ]; then
mode_bits=$(file_mode "$generated")
fi
tmp=$(mktemp "$generated.tmp.XXXXXX")
trap 'rm -f "$tmp"' EXIT HUP INT TERM
cat "$canonical" > "$tmp"
chmod "$mode_bits" "$tmp"
mv -f "$tmp" "$generated"
trap - EXIT HUP INT TERM
done
"$script_dir/repair-auto-themes.sh" "$config_dir"