mirror of
https://github.com/robbyrussell/oh-my-zsh.git
synced 2026-09-21 18:46:06 +02:00
perf(appearance): cache the diff and ls capability probes (#14067)
This commit is contained in:
@@ -13,13 +13,40 @@ 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
|
||||
|
||||
# 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
|
||||
|
||||
@@ -39,43 +66,39 @@ if [[ -z "$LS_COLORS" ]]; then
|
||||
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
|
||||
test-ls-args gls --color && alias ls='gls --color=tty'
|
||||
__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`.
|
||||
test-ls-args gls --color && alias ls='gls --color=tty'
|
||||
test-ls-args colorls -G && alias ls='colorls -G'
|
||||
__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
|
||||
test-ls-args ls -G && alias ls='ls -G'
|
||||
__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 \
|
||||
&& test-ls-args gls --color \
|
||||
&& __omz_test_cmd_args gls --color \
|
||||
&& alias ls='gls --color=tty'
|
||||
;;
|
||||
*)
|
||||
if test-ls-args ls --color; then
|
||||
if __omz_test_cmd_args ls --color; then
|
||||
alias ls='ls --color=tty'
|
||||
elif test-ls-args ls -G; then
|
||||
elif __omz_test_cmd_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
|
||||
|
||||
Reference in New Issue
Block a user