#!/bin/sh set -eu # Settings the generated theme files must carry whatever theme was saved into # them. kitty applies -theme.auto.conf after -- and with priority # over -- kitty.conf, so a theme naming one of these wins and there is nowhere # else left to say otherwise: # # macos_titlebar_color background titlebar follows the theme's background # tab_bar_background none tab bar uses the terminal background, # kitty's default and what kitty.conf asks # for; Catppuccin Latte and others pin a # contrasting grey, which leaves the tab # bar a visibly different colour from the # window it sits above # # The canonical copies in ./theme-modes stay exactly as the themes kitten wrote # them; only these generated files are normalised. # 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' } # The theme with those settings stripped and re-appended in a fixed form. # Trailing blank lines go too, so running this over its own output reproduces it # byte for byte and repeated repairs cannot make the file grow. normalised() { awk ' $1 == "macos_titlebar_color" || $1 == "tab_bar_background" { next } { line[++n] = $0 } END { while (n > 0 && line[n] ~ /^[ \t]*$/) n-- for (i = 1; i <= n; i++) print line[i] } ' "$1" printf '\nmacos_titlebar_color background\ntab_bar_background none\n' } config_dir=${1:-"$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"} for appearance in dark light no-preference; do config_file="$config_dir/$appearance-theme.auto.conf" [ -f "$config_file" ] || continue tmp=$(mktemp "$config_file.tmp.XXXXXX") trap 'rm -f "$tmp"' EXIT HUP INT TERM normalised "$config_file" > "$tmp" if cmp -s "$tmp" "$config_file"; then rm -f "$tmp" else chmod "$(file_mode "$config_file")" "$tmp" mv -f "$tmp" "$config_file" fi trap - EXIT HUP INT TERM done