From 0bd11821f85c02812745292efac6f0e63d71a778 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Sat, 6 Mar 2021 17:04:22 +0530 Subject: [PATCH] Use variable to prevent hook redefinition #2 --- templates/bash.txt | 14 ++++++-------- templates/fish.txt | 24 ++++++++++++------------ templates/posix.txt | 30 +++++++++++------------------- templates/powershell.txt | 40 ++++++++++++++++++++-------------------- templates/zsh.txt | 24 +++++++++++------------- 5 files changed, 60 insertions(+), 72 deletions(-) diff --git a/templates/bash.txt b/templates/bash.txt index c3b33d1..b3d0e54 100644 --- a/templates/bash.txt +++ b/templates/bash.txt @@ -47,17 +47,15 @@ function __zoxide_hook() { {%- endmatch %} # Initialize hook. -{%- if hook == Hook::None %} -{{ NOT_CONFIGURED }} - -{%- else %} if [ "${__zoxide_hooked}" != '1' ]; then - PROMPT_COMMAND="${PROMPT_COMMAND:+${PROMPT_COMMAND};}__zoxide_hook" __zoxide_hooked='1' + {%- if hook == Hook::None %} + {{ NOT_CONFIGURED }} + {%- else %} + PROMPT_COMMAND="${PROMPT_COMMAND:+${PROMPT_COMMAND};}__zoxide_hook" + {%- endif %} fi -{%- endif %} - {{ SECTION }} # When using zoxide with --no-aliases, alias these internal functions as # desired. @@ -72,7 +70,7 @@ function __zoxide_z() { __zoxide_cd "${OLDPWD}" else # shellcheck disable=SC2016 - \builtin echo 'zoxide: $OLDPWD is not set' + \builtin printf 'zoxide: $OLDPWD is not set\n' return 1 fi elif [ "$#" -eq 1 ] && [ -d "$1" ]; then diff --git a/templates/fish.txt b/templates/fish.txt index fb854d0..f3f5bd6 100644 --- a/templates/fish.txt +++ b/templates/fish.txt @@ -30,18 +30,18 @@ end # # Initialize hook to add new entries to the database. -{%- match hook %} -{%- when Hook::None %} -function __zoxide_hook - -{%- when Hook::Prompt %} -function __zoxide_hook --on-event fish_prompt - -{%- when Hook::Pwd %} -function __zoxide_hook --on-variable PWD - -{%- endmatch %} - command zoxide add (__zoxide_pwd) +if test "$__zoxide_hooked" != '1' + set __zoxide_hooked '1' + {%- match hook %} + {%- when Hook::None %} + function __zoxide_hook + {%- when Hook::Prompt %} + function __zoxide_hook --on-event fish_prompt + {%- when Hook::Pwd %} + function __zoxide_hook --on-variable PWD + {%- endmatch %} + command zoxide add (__zoxide_pwd) + end end {{ SECTION }} diff --git a/templates/posix.txt b/templates/posix.txt index e516037..3842113 100644 --- a/templates/posix.txt +++ b/templates/posix.txt @@ -1,13 +1,6 @@ {%- let SECTION = "# =============================================================================\n#" -%} {%- let NOT_CONFIGURED = "# -- not configured --" -%} -{%- if hook == Hook::Pwd -%} -\echo "\ -zoxide: PWD hooks are not supported on POSIX shells. - Use '--hook prompt' when initializing zoxide." - -{% endif -%} - {{ SECTION }} # Utility functions for zoxide. # @@ -47,21 +40,20 @@ __zoxide_hook() { {%- endmatch %} # Initialize hook. -{%- match hook %} -{%- when Hook::None %} -{{ NOT_CONFIGURED }} - -{%- when Hook::Prompt %} if [ "${__zoxide_hooked}" != '1' ]; then - PS1="${PS1}\$(__zoxide_hook)" __zoxide_hooked='1' + {%- match hook %} + {%- when Hook::None %} + {{ NOT_CONFIGURED }} + {%- when Hook::Prompt %} + PS1="${PS1}\$(__zoxide_hook)" + {%- when Hook::Pwd %} + \printf "%s\n%s\n" \ + "zoxide: PWD hooks are not supported on POSIX shells." \ + " Use '--hook prompt' when initializing zoxide." + {%- endmatch %} fi -{%- when Hook::Pwd %} -{{ NOT_CONFIGURED }} - -{%- endmatch %} - {{ SECTION }} # When using zoxide with --no-aliases, alias these internal functions as # desired. @@ -76,7 +68,7 @@ __zoxide_z() { __zoxide_cd "${OLDPWD}" else # shellcheck disable=SC2016 - \echo 'zoxide: $OLDPWD is not set' + \printf 'zoxide: $OLDPWD is not set' return 1 fi elif [ "$#" -eq 1 ] && [ -d "$1" ]; then diff --git a/templates/powershell.txt b/templates/powershell.txt index 8cf85db..10a6af2 100644 --- a/templates/powershell.txt +++ b/templates/powershell.txt @@ -28,30 +28,30 @@ function __zoxide_hook { } # Initialize hook. -{%- match hook %} -{%- when Hook::None %} -{{ NOT_CONFIGURED }} - -{%- when Hook::Prompt %} -$PreZoxidePrompt = $function:prompt -function prompt { - $null = __zoxide_hook - & $PreZoxidePrompt -} - -{%- when Hook::Pwd %} -if ($PSVersionTable.PSVersion.Major -ge 6) { - $ExecutionContext.InvokeCommand.LocationChangedAction = { +if ($__zoxide_hooked -ne '1') { + $__zoxide_hooked = '1' + {%- match hook %} + {%- when Hook::None %} + {{ NOT_CONFIGURED }} + {%- when Hook::Prompt %} + $__zoxide_prompt_old = $function:prompt + function prompt { $null = __zoxide_hook + & $__zoxide_prompt_old } -} else { - Write-Error "` -zoxide: PWD hooks are not supported below PowerShell 6. - Use '--hook prompt' when initializing zoxide." + {%- when Hook::Pwd %} + if ($PSVersionTable.PSVersion.Major -ge 6) { + $ExecutionContext.InvokeCommand.LocationChangedAction = { + $null = __zoxide_hook + } + } else { + Write-Error ("`n" + + "zoxide: PWD hooks are not supported below PowerShell 6.`n" + + " Use '--hook prompt' when initializing zoxide.") + } + {%- endmatch %} } -{%- endmatch %} - {{ SECTION }} # When using zoxide with --no-aliases, alias these internal functions as # desired. diff --git a/templates/zsh.txt b/templates/zsh.txt index 9d6a6a8..7735bd0 100644 --- a/templates/zsh.txt +++ b/templates/zsh.txt @@ -29,19 +29,17 @@ function __zoxide_hook() { } # Initialize hook. -{%- match hook %} -{%- when Hook::None %} -{{ NOT_CONFIGURED }} - -{%- when Hook::Prompt %} -[[ -n "${precmd_functions[(r)__zoxide_hook]}" ]] || { +if [ "${__zoxide_hooked}" != '1' ]; then + __zoxide_hooked='1' + {%- match hook %} + {%- when Hook::None %} + {{ NOT_CONFIGURED }} + {%- when Hook::Prompt %} precmd_functions+=(__zoxide_hook) -} - -{%- when Hook::Pwd %} -chpwd_functions=(${chpwd_functions[@]} "__zoxide_hook") - -{%- endmatch %} + {%- when Hook::Pwd %} + chpwd_functions=(${chpwd_functions[@]} "__zoxide_hook") + {%- endmatch %} +fi {{ SECTION }} # When using zoxide with --no-aliases, alias these internal functions as @@ -56,7 +54,7 @@ function __zoxide_z() { if [ -n "$OLDPWD" ]; then __zoxide_cd "$OLDPWD" else - \builtin echo 'zoxide: \$OLDPWD is not set' + \builtin printf 'zoxide: $OLDPWD is not set' return 1 fi elif [ "$#" -eq 1 ] && [ -d "$1" ]; then