From 7ce781ea37eb37ed52b70a5d59d9d5bfedb414ae Mon Sep 17 00:00:00 2001 From: Robby Russell Date: Fri, 18 Sep 2026 02:13:45 -0700 Subject: [PATCH] perf(appearance): cache the diff and ls capability probes (#14067) --- lib/theme-and-appearance.zsh | 131 ++++++++++++++++++++--------------- 1 file changed, 77 insertions(+), 54 deletions(-) diff --git a/lib/theme-and-appearance.zsh b/lib/theme-and-appearance.zsh index 5cfa2e631..a5037e335 100644 --- a/lib/theme-and-appearance.zsh +++ b/lib/theme-and-appearance.zsh @@ -13,69 +13,92 @@ ZSH_THEME_RUBY_PROMPT_PREFIX="(" 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 __omz_test_cmd_args { + # Usage: __omz_test_cmd_args cmd args... + # Runs `cmd args... /dev/null` and remembers whether it succeeded, so a + # command needing two operands passes the first one itself: + # e.g. __omz_test_cmd_args gls --color -> gls --color /dev/null + # __omz_test_cmd_args diff --color /dev/null -> diff --color /dev/null /dev/null + 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 -if command diff --color /dev/null{,} &>/dev/null; then +if __omz_test_cmd_args diff --color /dev/null; then function diff { command diff --color "$@" } fi -# Don't set ls coloring if disabled -[[ "$DISABLE_LS_COLORS" != true ]] || return 0 +# 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 + [[ "$DISABLE_LS_COLORS" != true ]] || return 0 -# Default coloring for BSD-based ls -export LSCOLORS="Gxfxcxdxbxegedabagacad" + # Default coloring for BSD-based ls + export LSCOLORS="Gxfxcxdxbxegedabagacad" -# Default coloring for GNU-based ls -if [[ -z "$LS_COLORS" ]]; then - # Define LS_COLORS via dircolors if available. Otherwise, set a default - # equivalent to LSCOLORS (generated via https://geoff.greer.fm/lscolors) - if (( $+commands[dircolors] )); then - [[ -f "$HOME/.dircolors" ]] \ - && source <(dircolors -b "$HOME/.dircolors") \ - || source <(dircolors -b) - else - export LS_COLORS="di=1;36:ln=35:so=32:pi=33:ex=31:bd=34;46:cd=34;43:su=30;41:sg=30;46:tw=30;42:ow=30;43" + # Default coloring for GNU-based ls + if [[ -z "$LS_COLORS" ]]; then + # Define LS_COLORS via dircolors if available. Otherwise, set a default + # equivalent to LSCOLORS (generated via https://geoff.greer.fm/lscolors) + if (( $+commands[dircolors] )); then + [[ -f "$HOME/.dircolors" ]] \ + && source <(dircolors -b "$HOME/.dircolors") \ + || source <(dircolors -b) + else + export LS_COLORS="di=1;36:ln=35:so=32:pi=33:ex=31:bd=34;46:cd=34;43:su=30;41:sg=30;46:tw=30;42:ow=30;43" + 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 + case "$OSTYPE" in + netbsd*) + # 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 + __omz_test_cmd_args gls --color && alias ls='gls --color=tty' + ;; + openbsd*) + # On OpenBSD, `gls` (ls from GNU coreutils) and `colorls` (ls from base, + # with color and multibyte support) are available from ports. + # `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`. + __omz_test_cmd_args gls --color && alias ls='gls --color=tty' + __omz_test_cmd_args colorls -G && alias ls='colorls -G' + ;; + (darwin|freebsd)*) + # This alias works by default just using $LSCOLORS + __omz_test_cmd_args ls -G && alias ls='ls -G' + # Only use GNU ls if installed and there are user defaults for $LS_COLORS, + # as the default coloring scheme is not very pretty + zstyle -t ':omz:lib:theme-and-appearance' gnu-ls \ + && __omz_test_cmd_args gls --color \ + && alias ls='gls --color=tty' + ;; + *) + if __omz_test_cmd_args ls --color; then + alias ls='ls --color=tty' + elif __omz_test_cmd_args ls -G; then + alias ls='ls -G' + fi + ;; + esac } -# Find the option for using colors in ls, depending on the version -case "$OSTYPE" in - netbsd*) - # 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 - test-ls-args gls --color && alias ls='gls --color=tty' - ;; - openbsd*) - # On OpenBSD, `gls` (ls from GNU coreutils) and `colorls` (ls from base, - # with color and multibyte support) are available from ports. - # `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`. - test-ls-args gls --color && alias ls='gls --color=tty' - test-ls-args colorls -G && alias ls='colorls -G' - ;; - (darwin|freebsd)*) - # This alias works by default just using $LSCOLORS - test-ls-args ls -G && alias ls='ls -G' - # Only use GNU ls if installed and there are user defaults for $LS_COLORS, - # as the default coloring scheme is not very pretty - zstyle -t ':omz:lib:theme-and-appearance' gnu-ls \ - && test-ls-args gls --color \ - && alias ls='gls --color=tty' - ;; - *) - if test-ls-args ls --color; then - alias ls='ls --color=tty' - elif test-ls-args ls -G; then - alias ls='ls -G' - fi - ;; -esac - -unfunction test-ls-args +unfunction __omz_test_cmd_args +unset __omz_probes __omz_probe_cache __omz_probe_cached