Clean up semantic theme compatibility

This commit is contained in:
Ryan Hughes
2026-06-02 19:35:07 -04:00
parent 89bfa841c4
commit 4087be7f79
4 changed files with 86 additions and 78 deletions
+56 -22
View File
@@ -290,6 +290,30 @@ add_gradient_function_values() {
done
}
alias_theme_color() {
local key="$1"
local fallback="$2"
[[ ${THEME_COLORS[$key]} ]] || THEME_COLORS[$key]="${THEME_COLORS[$fallback]}"
}
resolve_theme_mode() {
local bg_hex lum
[[ ${THEME_COLORS[mode]} ]] || THEME_COLORS[mode]="${THEME_COLORS[theme_type]}"
[[ ${THEME_COLORS[mode]} ]] && return
if [[ -f $NEXT_THEME_DIR/light.mode ]]; then
THEME_COLORS[mode]="light"
elif [[ ${THEME_COLORS[background]} =~ ^#[0-9A-Fa-f]{6}$ ]]; then
bg_hex="${THEME_COLORS[background]#\#}"
lum=$(( $(printf "%d" "0x${bg_hex:0:2}") + $(printf "%d" "0x${bg_hex:2:2}") + $(printf "%d" "0x${bg_hex:4:2}") ))
(( lum > 382 )) && THEME_COLORS[mode]="light" || THEME_COLORS[mode]="dark"
else
THEME_COLORS[mode]="dark"
fi
}
# Only generate dynamic templates for themes with a colors.toml definition
if [[ -f $COLORS_FILE ]]; then
sed_script=$(mktemp)
@@ -308,14 +332,23 @@ if [[ -f $COLORS_FILE ]]; then
# Legacy compatibility: map ANSI color0..color15 and raw bg/fg to semantic names
declare -A legacy_alias=(
[bg]=background [fg]=foreground
[red]=color1 [green]=color2 [yellow]=color3
[blue]=color4 [purple]=color5 [cyan]=color6
[bright_red]=color9 [bright_green]=color10 [bright_yellow]=color11
[bright_blue]=color12 [bright_purple]=color13 [bright_cyan]=color14
[bg]=background
[fg]=foreground
[red]=color1
[green]=color2
[yellow]=color3
[blue]=color4
[purple]=color5
[cyan]=color6
[bright_red]=color9
[bright_green]=color10
[bright_yellow]=color11
[bright_blue]=color12
[bright_purple]=color13
[bright_cyan]=color14
)
for key in "${!legacy_alias[@]}"; do
[[ ${THEME_COLORS[$key]} ]] || THEME_COLORS[$key]="${THEME_COLORS[${legacy_alias[$key]}]}"
alias_theme_color "$key" "${legacy_alias[$key]}"
done
[[ ${THEME_COLORS[light_fg]} ]] || THEME_COLORS[light_fg]="${THEME_COLORS[color7]:-${THEME_COLORS[foreground]}}"
@@ -340,15 +373,25 @@ if [[ -f $COLORS_FILE ]]; then
# Keep semantic themes compatible with user templates that still reference
# the legacy ANSI placeholders directly.
declare -A ansi_alias=(
[color0]=bg [color1]=red [color2]=green
[color3]=yellow [color4]=blue [color5]=purple
[color6]=cyan [color7]=fg [color8]=muted
[color9]=bright_red [color10]=bright_green [color11]=bright_yellow
[color12]=bright_blue [color13]=bright_purple [color14]=bright_cyan
[color0]=bg
[color1]=red
[color2]=green
[color3]=yellow
[color4]=blue
[color5]=purple
[color6]=cyan
[color7]=fg
[color8]=muted
[color9]=bright_red
[color10]=bright_green
[color11]=bright_yellow
[color12]=bright_blue
[color13]=bright_purple
[color14]=bright_cyan
[color15]=bright_fg
)
for key in "${!ansi_alias[@]}"; do
[[ ${THEME_COLORS[$key]} ]] || THEME_COLORS[$key]="${THEME_COLORS[${ansi_alias[$key]}]}"
alias_theme_color "$key" "${ansi_alias[$key]}"
done
# Resolve theme mode (dark|light) with this precedence:
@@ -356,16 +399,7 @@ if [[ -f $COLORS_FILE ]]; then
# 2. legacy `light.mode` file shipped beside the theme
# 3. background luminance auto-detect
# `theme_type` is kept as an alias so existing templates ({{ theme_type }}) keep working.
[[ ${THEME_COLORS[mode]} ]] || THEME_COLORS[mode]="${THEME_COLORS[theme_type]}"
if [[ ! ${THEME_COLORS[mode]} ]]; then
if [[ -f $NEXT_THEME_DIR/light.mode ]]; then
THEME_COLORS[mode]="light"
else
bg_hex="${THEME_COLORS[background]#\#}"
lum=$(( $(printf "%d" "0x${bg_hex:0:2}") + $(printf "%d" "0x${bg_hex:2:2}") + $(printf "%d" "0x${bg_hex:4:2}") ))
[[ $lum -gt 382 ]] && THEME_COLORS[mode]="light" || THEME_COLORS[mode]="dark"
fi
fi
resolve_theme_mode
THEME_COLORS[theme_type]="${THEME_COLORS[mode]}"
for key in "${!THEME_COLORS[@]}"; do