perf(appearance): cache the diff and ls capability probes

`diff --color` and `ls -G` (or `--color`, `gls`, `colorls` depending on
the OS) were probed with a fork each on every startup. Remember the
results in $ZSH_CACHE_DIR for a day, the same way lib/grep.zsh does.

The ls section is wrapped in an anonymous function so that the early
return for DISABLE_LS_COLORS still runs the cleanup at the end of the
file. Behaviour is otherwise unchanged.

Measured on macOS arm64, zsh 5.9: 5.7 ms -> 0.7 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:05:29 -07:00
co-authored by Claude Fable 5.1
parent 426650fbe8
commit ded814e0ac
+36 -15
View File
@@ -13,13 +13,38 @@ ZSH_THEME_RUBY_PROMPT_PREFIX="("
ZSH_THEME_RUBY_PROMPT_SUFFIX=")" ZSH_THEME_RUBY_PROMPT_SUFFIX=")"
# The command probes below fork a process each, and their result doesn't change
# from one shell to the next, so they're cached for a day (like lib/grep.zsh).
typeset -A __omz_probes
__omz_probe_cache="$ZSH_CACHE_DIR/appearance-probes"
__omz_probe_cached=("$__omz_probe_cache"(Nm-1))
[[ -z "$__omz_probe_cached" ]] || source "$__omz_probe_cache"
function test-cmd-args {
# Usage: test-cmd-args cmd args...
# e.g. test-cmd-args gls --color
# Runs `cmd args... /dev/null` and remembers whether it succeeded
local key="$*"
if (( ! ${+__omz_probes[$key]} )); then
command "$@" /dev/null &>/dev/null
__omz_probes[$key]=$?
if [[ -w "$ZSH_CACHE_DIR" ]]; then
print -r -- "__omz_probes=(" "${(@qqkv)__omz_probes}" ")" >| "$__omz_probe_cache"
fi
fi
return $__omz_probes[$key]
}
# Use diff --color if available # Use diff --color if available
if command diff --color /dev/null{,} &>/dev/null; then if test-cmd-args diff --color /dev/null; then
function diff { function diff {
command diff --color "$@" command diff --color "$@"
} }
fi fi
# Set up ls coloring. Done in a function so the early return below still
# reaches the cleanup at the end of this file.
() {
# Don't set ls coloring if disabled # Don't set ls coloring if disabled
[[ "$DISABLE_LS_COLORS" != true ]] || return 0 [[ "$DISABLE_LS_COLORS" != true ]] || return 0
@@ -39,43 +64,39 @@ if [[ -z "$LS_COLORS" ]]; then
fi fi
fi fi
function test-ls-args {
# Usage: test-ls-args cmd args...
# e.g. test-ls-args gls --color
command "$@" /dev/null &>/dev/null
}
# Find the option for using colors in ls, depending on the version # Find the option for using colors in ls, depending on the version
case "$OSTYPE" in case "$OSTYPE" in
netbsd*) netbsd*)
# On NetBSD, test if `gls` (GNU ls) is installed (this one supports colors); # On NetBSD, test if `gls` (GNU ls) is installed (this one supports colors);
# otherwise, leave ls as is, because NetBSD's ls doesn't support -G # otherwise, leave ls as is, because NetBSD's ls doesn't support -G
test-ls-args gls --color && alias ls='gls --color=tty' test-cmd-args gls --color && alias ls='gls --color=tty'
;; ;;
openbsd*) openbsd*)
# On OpenBSD, `gls` (ls from GNU coreutils) and `colorls` (ls from base, # On OpenBSD, `gls` (ls from GNU coreutils) and `colorls` (ls from base,
# with color and multibyte support) are available from ports. # with color and multibyte support) are available from ports.
# `colorls` will be installed on purpose and can't be pulled in by installing # `colorls` will be installed on purpose and can't be pulled in by installing
# coreutils (which might be installed for ), so prefer it to `gls`. # coreutils (which might be installed for ), so prefer it to `gls`.
test-ls-args gls --color && alias ls='gls --color=tty' test-cmd-args gls --color && alias ls='gls --color=tty'
test-ls-args colorls -G && alias ls='colorls -G' test-cmd-args colorls -G && alias ls='colorls -G'
;; ;;
(darwin|freebsd)*) (darwin|freebsd)*)
# This alias works by default just using $LSCOLORS # This alias works by default just using $LSCOLORS
test-ls-args ls -G && alias ls='ls -G' test-cmd-args ls -G && alias ls='ls -G'
# Only use GNU ls if installed and there are user defaults for $LS_COLORS, # Only use GNU ls if installed and there are user defaults for $LS_COLORS,
# as the default coloring scheme is not very pretty # as the default coloring scheme is not very pretty
zstyle -t ':omz:lib:theme-and-appearance' gnu-ls \ zstyle -t ':omz:lib:theme-and-appearance' gnu-ls \
&& test-ls-args gls --color \ && test-cmd-args gls --color \
&& alias ls='gls --color=tty' && alias ls='gls --color=tty'
;; ;;
*) *)
if test-ls-args ls --color; then if test-cmd-args ls --color; then
alias ls='ls --color=tty' alias ls='ls --color=tty'
elif test-ls-args ls -G; then elif test-cmd-args ls -G; then
alias ls='ls -G' alias ls='ls -G'
fi fi
;; ;;
esac esac
}
unfunction test-ls-args unfunction test-cmd-args
unset __omz_probes __omz_probe_cache __omz_probe_cached