chore(zshrc): improve cached-eval usage

This commit is contained in:
2025-07-07 23:10:25 +01:00
parent ed14e9a73d
commit d2b5cd53b0
2 changed files with 29 additions and 10 deletions

25
zshenv
View File

@@ -76,6 +76,20 @@ command-path() {
echo "${commands[$1]}" echo "${commands[$1]}"
} }
# mise-which is a wrapper around the `mise which` command. If mise is not
# available, or `mise which` fails to find the command, it falls back to the
# `command-path` function.
#
# Primarily used before mise is initialized in an interactive shell, where
# regular path lookup would return the shimmed version of the command, but you
# actually need the absolute path to the command.
#
# Arguments:
# $1 - cmd: The command to find the path to.
mise-which() {
command-exists mise && mise which "$1" 2>/dev/null || command-path "$1"
}
source-if-exists() { source-if-exists() {
if [ -f "$1" ]; then if [ -f "$1" ]; then
source "$1" source "$1"
@@ -137,6 +151,17 @@ cached-eval() {
shift 1 shift 1
local script="$@" local script="$@"
# If given source file is empty, silently return 0. This allows us to call
# cached-eval with dynamic command path lookup, without having to wrap it in a
# if statement that checks if the command exists.
if [[ -z "$source_file" ]]; then
return 0
fi
if [[ -z "$script" ]]; then
echo "cached-eval: No script provided for: $source_file" >&2
return 1
fi
if [[ ! -f "$source_file" ]]; then if [[ ! -f "$source_file" ]]; then
echo "cached-eval: Source file not found: $source_file" >&2 echo "cached-eval: Source file not found: $source_file" >&2

14
zshrc
View File

@@ -61,13 +61,7 @@ path_prepend "$MISE_HOME/shims"
# If available, make sure to load direnv shell hook before mise. # If available, make sure to load direnv shell hook before mise.
if command-exists direnv; then if command-exists direnv; then
if command-exists mise; then cached-eval "$(mise-which direnv)" direnv hook zsh
# If mise is available, use it to find the absolute path to direnv.
cached-eval "$(mise which direnv)" direnv hook zsh
else
# Otherwise, find it via PATH, which is likely to be mise's shim.
cached-eval "$(command-path direnv)" direnv hook zsh
fi
fi fi
# ============================================================================== # ==============================================================================
@@ -181,7 +175,7 @@ if command-exists fzf; then
--walker-skip .git,node_modules,.terraform,target --walker-skip .git,node_modules,.terraform,target
--preview 'tree -C {}'" --preview 'tree -C {}'"
cached-eval "$(command-path fzf)" fzf --zsh cached-eval "$(mise-which fzf)" fzf --zsh
zstyle ':completion:*' menu no zstyle ':completion:*' menu no
zstyle ':completion:*' special-dirs true zstyle ':completion:*' special-dirs true
@@ -271,8 +265,8 @@ if ! command-exists starship && [ -f "$MISE_INSTALL_PATH" ]; then
fi fi
if command-exists starship; then if command-exists starship; then
cached-eval "$(command-path starship)" starship init zsh --print-full-init cached-eval "$(mise-which starship)" starship init zsh --print-full-init
setup-completions starship "$(command-path starship)" starship completions zsh setup-completions starship "$(mise-which starship)" starship completions zsh
else else
echo "WARN: starship not found, skipping prompt setup" >&2 echo "WARN: starship not found, skipping prompt setup" >&2
echo " install with: mise use -g starship" >&2 echo " install with: mise use -g starship" >&2