#!/bin/sh # Regenerate the *-theme.auto.conf files kitty reads at startup from the # canonical themes in ./themes, 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 # ./themes and these three files are generated. 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/themes" 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"