`chezmoi apply` on Linux printed "could not regenerate kitty theme files" every run. GNU stat reads -f as --file-system, so `stat -f '%Lp' file` prints a block of filesystem info to stdout and exits non-zero. The `|| stat -c '%a'` fallback then appended the real answer to that block, and chmod rejected the result, failing the script under set -e. Replace the chain with a file_mode() helper in both scripts: try the GNU form first, since BSD rejects -c cleanly without printing anything, and accept the answer only if it is octal. Covered by a new case in tests/test_theme_mode.sh using a stat stub that reproduces GNU's behaviour; it fails against the old chain.
87 lines
2.8 KiB
Bash
87 lines
2.8 KiB
Bash
#!/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"
|