From 397c65c1b7f36035f5de843092a34f978d3e5ce5 Mon Sep 17 00:00:00 2001 From: PowerUser64 Date: Mon, 21 Oct 2024 02:07:15 -0700 Subject: [PATCH] zsh: don't auto load working dir if it has direnv files Speeds up prompt loading in some cases by preventing loading unneeded folders with direnv --- .zshrc | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/.zshrc b/.zshrc index e4db3a7..3f6ead9 100644 --- a/.zshrc +++ b/.zshrc @@ -245,6 +245,8 @@ load_working_dir() { fi } +alias lwd=load_working_dir + SWD_INITIAL_WORKING_DIR="$(pwd)" save_working_dir() { if [ "$(pwd)" != "$SWD_INITIAL_WORKING_DIR" ]; then @@ -255,8 +257,28 @@ save_working_dir() { autoload -U add-zsh-hook add-zsh-hook chpwd save_working_dir -# shellchek disable=SC2164 -load_working_dir && cd - > /dev/null || true +# Don't load last working directory if it's a direnv location +skip_load=false +if [ -e "${XDG_DATA_HOME:-"$HOME/.local/share"}"/direnv/allow ]; then + # Avoid direnv slowing down previous working directory + working_path="$(cat "$WORKING_DIR_SAVE_FILE" 2> /dev/null || echo -n '/')" + if cat "${XDG_DATA_HOME:-"$HOME/.local/share"}"/direnv/allow/* | + grep -Po '^.*/' | + sed 's_/$__g' | + grep -qFf - <(echo "$working_path") + then + skip_load=true + fi +fi + +# if we made it to the root directory, then we can assume it's alright to load +if [ "$skip_load" = false ]; then + # echo "Loading path" + # shellchek disable=SC2164 + load_working_dir && cd - > /dev/null || true +else + echo "LWD: Skipped loading working dir because of direnv" +fi # Case insensitive globbing (fix for termux) [ -n "$TERMUX_VERSION" ] && { setopt nocaseglob; zstyle ':completion:*' path-completion true; }