Use variable to prevent hook redefinition #2

This commit is contained in:
Ajeet D'Souza 2021-03-06 17:04:22 +05:30
parent 1789a70b55
commit 0bd11821f8
5 changed files with 60 additions and 72 deletions

View File

@ -47,16 +47,14 @@ function __zoxide_hook() {
{%- endmatch %} {%- endmatch %}
# Initialize hook. # Initialize hook.
if [ "${__zoxide_hooked}" != '1' ]; then
__zoxide_hooked='1'
{%- if hook == Hook::None %} {%- if hook == Hook::None %}
{{ NOT_CONFIGURED }} {{ NOT_CONFIGURED }}
{%- else %} {%- else %}
if [ "${__zoxide_hooked}" != '1' ]; then
PROMPT_COMMAND="${PROMPT_COMMAND:+${PROMPT_COMMAND};}__zoxide_hook" PROMPT_COMMAND="${PROMPT_COMMAND:+${PROMPT_COMMAND};}__zoxide_hook"
__zoxide_hooked='1'
fi
{%- endif %} {%- endif %}
fi
{{ SECTION }} {{ SECTION }}
# When using zoxide with --no-aliases, alias these internal functions as # When using zoxide with --no-aliases, alias these internal functions as
@ -72,7 +70,7 @@ function __zoxide_z() {
__zoxide_cd "${OLDPWD}" __zoxide_cd "${OLDPWD}"
else else
# shellcheck disable=SC2016 # shellcheck disable=SC2016
\builtin echo 'zoxide: $OLDPWD is not set' \builtin printf 'zoxide: $OLDPWD is not set\n'
return 1 return 1
fi fi
elif [ "$#" -eq 1 ] && [ -d "$1" ]; then elif [ "$#" -eq 1 ] && [ -d "$1" ]; then

View File

@ -30,19 +30,19 @@ end
# #
# Initialize hook to add new entries to the database. # Initialize hook to add new entries to the database.
if test "$__zoxide_hooked" != '1'
set __zoxide_hooked '1'
{%- match hook %} {%- match hook %}
{%- when Hook::None %} {%- when Hook::None %}
function __zoxide_hook function __zoxide_hook
{%- when Hook::Prompt %} {%- when Hook::Prompt %}
function __zoxide_hook --on-event fish_prompt function __zoxide_hook --on-event fish_prompt
{%- when Hook::Pwd %} {%- when Hook::Pwd %}
function __zoxide_hook --on-variable PWD function __zoxide_hook --on-variable PWD
{%- endmatch %} {%- endmatch %}
command zoxide add (__zoxide_pwd) command zoxide add (__zoxide_pwd)
end end
end
{{ SECTION }} {{ SECTION }}
# When using zoxide with --no-aliases, alias these internal functions as # When using zoxide with --no-aliases, alias these internal functions as

View File

@ -1,13 +1,6 @@
{%- let SECTION = "# =============================================================================\n#" -%} {%- let SECTION = "# =============================================================================\n#" -%}
{%- let NOT_CONFIGURED = "# -- not configured --" -%} {%- 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 }} {{ SECTION }}
# Utility functions for zoxide. # Utility functions for zoxide.
# #
@ -47,20 +40,19 @@ __zoxide_hook() {
{%- endmatch %} {%- endmatch %}
# Initialize hook. # Initialize hook.
if [ "${__zoxide_hooked}" != '1' ]; then
__zoxide_hooked='1'
{%- match hook %} {%- match hook %}
{%- when Hook::None %} {%- when Hook::None %}
{{ NOT_CONFIGURED }} {{ NOT_CONFIGURED }}
{%- when Hook::Prompt %} {%- when Hook::Prompt %}
if [ "${__zoxide_hooked}" != '1' ]; then
PS1="${PS1}\$(__zoxide_hook)" PS1="${PS1}\$(__zoxide_hook)"
__zoxide_hooked='1'
fi
{%- when Hook::Pwd %} {%- when Hook::Pwd %}
{{ NOT_CONFIGURED }} \printf "%s\n%s\n" \
"zoxide: PWD hooks are not supported on POSIX shells." \
" Use '--hook prompt' when initializing zoxide."
{%- endmatch %} {%- endmatch %}
fi
{{ SECTION }} {{ SECTION }}
# When using zoxide with --no-aliases, alias these internal functions as # When using zoxide with --no-aliases, alias these internal functions as
@ -76,7 +68,7 @@ __zoxide_z() {
__zoxide_cd "${OLDPWD}" __zoxide_cd "${OLDPWD}"
else else
# shellcheck disable=SC2016 # shellcheck disable=SC2016
\echo 'zoxide: $OLDPWD is not set' \printf 'zoxide: $OLDPWD is not set'
return 1 return 1
fi fi
elif [ "$#" -eq 1 ] && [ -d "$1" ]; then elif [ "$#" -eq 1 ] && [ -d "$1" ]; then

View File

@ -28,29 +28,29 @@ function __zoxide_hook {
} }
# Initialize hook. # Initialize hook.
if ($__zoxide_hooked -ne '1') {
$__zoxide_hooked = '1'
{%- match hook %} {%- match hook %}
{%- when Hook::None %} {%- when Hook::None %}
{{ NOT_CONFIGURED }} {{ NOT_CONFIGURED }}
{%- when Hook::Prompt %} {%- when Hook::Prompt %}
$PreZoxidePrompt = $function:prompt $__zoxide_prompt_old = $function:prompt
function prompt { function prompt {
$null = __zoxide_hook $null = __zoxide_hook
& $PreZoxidePrompt & $__zoxide_prompt_old
} }
{%- when Hook::Pwd %} {%- when Hook::Pwd %}
if ($PSVersionTable.PSVersion.Major -ge 6) { if ($PSVersionTable.PSVersion.Major -ge 6) {
$ExecutionContext.InvokeCommand.LocationChangedAction = { $ExecutionContext.InvokeCommand.LocationChangedAction = {
$null = __zoxide_hook $null = __zoxide_hook
} }
} else { } else {
Write-Error "` Write-Error ("`n" +
zoxide: PWD hooks are not supported below PowerShell 6. "zoxide: PWD hooks are not supported below PowerShell 6.`n" +
Use '--hook prompt' when initializing zoxide." " Use '--hook prompt' when initializing zoxide.")
} }
{%- endmatch %} {%- endmatch %}
}
{{ SECTION }} {{ SECTION }}
# When using zoxide with --no-aliases, alias these internal functions as # When using zoxide with --no-aliases, alias these internal functions as

View File

@ -29,19 +29,17 @@ function __zoxide_hook() {
} }
# Initialize hook. # Initialize hook.
if [ "${__zoxide_hooked}" != '1' ]; then
__zoxide_hooked='1'
{%- match hook %} {%- match hook %}
{%- when Hook::None %} {%- when Hook::None %}
{{ NOT_CONFIGURED }} {{ NOT_CONFIGURED }}
{%- when Hook::Prompt %} {%- when Hook::Prompt %}
[[ -n "${precmd_functions[(r)__zoxide_hook]}" ]] || {
precmd_functions+=(__zoxide_hook) precmd_functions+=(__zoxide_hook)
}
{%- when Hook::Pwd %} {%- when Hook::Pwd %}
chpwd_functions=(${chpwd_functions[@]} "__zoxide_hook") chpwd_functions=(${chpwd_functions[@]} "__zoxide_hook")
{%- endmatch %} {%- endmatch %}
fi
{{ SECTION }} {{ SECTION }}
# When using zoxide with --no-aliases, alias these internal functions as # When using zoxide with --no-aliases, alias these internal functions as
@ -56,7 +54,7 @@ function __zoxide_z() {
if [ -n "$OLDPWD" ]; then if [ -n "$OLDPWD" ]; then
__zoxide_cd "$OLDPWD" __zoxide_cd "$OLDPWD"
else else
\builtin echo 'zoxide: \$OLDPWD is not set' \builtin printf 'zoxide: $OLDPWD is not set'
return 1 return 1
fi fi
elif [ "$#" -eq 1 ] && [ -d "$1" ]; then elif [ "$#" -eq 1 ] && [ -d "$1" ]; then