perf(spectrum): build the FG and BG tables without a 256-iteration loop

Fill the colour tables with a brace expansion zipped against the code
list instead of looping over the 256 codes with two subscript
assignments each. The resulting arrays are byte-identical, and the
escape character is written as $'\e' instead of a literal control byte.

Measured on macOS arm64, zsh 5.9: 0.85 ms -> 0.35 ms per interactive
start.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Robby Russell
2026-09-06 08:14:53 -07:00
co-authored by Claude Fable 5.1
parent 426650fbe8
commit 5af2aadee2
+9 -4
View File
@@ -14,10 +14,15 @@ FX=(
reverse "%{%}" no-reverse "%{%}"
)
for color in {000..255}; do
FG[$color]="%{[38;5;${color}m%}"
BG[$color]="%{[48;5;${color}m%}"
done
# Build the tables with array operations instead of a 256-iteration loop
() {
local -a codes fg bg
codes=({000..255})
fg=( "%{"$'\e'"[38;5;"${^codes}"m%}" )
bg=( "%{"$'\e'"[48;5;"${^codes}"m%}" )
FG=( ${codes:^fg} )
BG=( ${codes:^bg} )
}
# Show all 256 colors with color number
function spectrum_ls() {