Use variable to prevent hook redefinition (#154)

This commit is contained in:
Ajeet D'Souza 2021-03-06 18:25:13 +05:30 committed by GitHub
parent 22d19a74b2
commit bc81154c5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 92 deletions

View File

@ -47,16 +47,14 @@ function __zoxide_hook() {
{%- endmatch %} {%- endmatch %}
# Initialize hook. # Initialize hook.
{%- if hook == Hook::None %} if [ "${__zoxide_hooked}" != '1' ]; then
{{ NOT_CONFIGURED }} __zoxide_hooked='1'
{%- if hook == Hook::None %}
{%- else %} {{ NOT_CONFIGURED }}
case "${PROMPT_COMMAND}" in {%- else %}
*__zoxide_hook*) ;; PROMPT_COMMAND="${PROMPT_COMMAND:+${PROMPT_COMMAND};}__zoxide_hook"
*) PROMPT_COMMAND="${PROMPT_COMMAND:+${PROMPT_COMMAND};}__zoxide_hook" ;; {%- endif %}
esac fi
{%- endif %}
{{ 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,18 +30,18 @@ end
# #
# Initialize hook to add new entries to the database. # Initialize hook to add new entries to the database.
{%- match hook %} if test "$__zoxide_hooked" != '1'
{%- when Hook::None %} set __zoxide_hooked '1'
function __zoxide_hook {%- match hook %}
{%- when Hook::None %}
{%- when Hook::Prompt %} function __zoxide_hook
function __zoxide_hook --on-event fish_prompt {%- when Hook::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 }}

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.
{%- match hook %} if [ "${__zoxide_hooked}" != '1' ]; then
{%- when Hook::None %} __zoxide_hooked='1'
{{ NOT_CONFIGURED }} {%- match hook %}
{%- when Hook::None %}
{%- when Hook::Prompt %} {{ NOT_CONFIGURED }}
case "${PS1}" in {%- when Hook::Prompt %}
*\$\(__zoxide_hook\)*) ;; PS1="${PS1}\$(__zoxide_hook)"
*) PS1="${PS1}\$(__zoxide_hook)" ;; {%- when Hook::Pwd %}
esac \printf "%s\n%s\n" \
"zoxide: PWD hooks are not supported on POSIX shells." \
{%- when Hook::Pwd %} " Use '--hook prompt' when initializing zoxide."
{{ NOT_CONFIGURED }} {%- endmatch %}
fi
{%- 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
@ -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,30 +28,30 @@ function __zoxide_hook {
} }
# Initialize hook. # Initialize hook.
{%- match hook %} if ($__zoxide_hooked -ne '1') {
{%- when Hook::None %} $__zoxide_hooked = '1'
{{ NOT_CONFIGURED }} {%- match hook %}
{%- when Hook::None %}
{%- when Hook::Prompt %} {{ NOT_CONFIGURED }}
$PreZoxidePrompt = $function:prompt {%- when Hook::Prompt %}
function prompt { $__zoxide_prompt_old = $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
# desired. # desired.

View File

@ -76,18 +76,18 @@ def __zoxide_errhandler(func):
# #
# Initialize hook to add new entries to the database. # Initialize hook to add new entries to the database.
{%- match hook %} if globals().get("__zoxide_hooked") is not True:
{%- when Hook::None %} globals()["__zoxide_hooked"] = True
{{ NOT_CONFIGURED }}
{%- when Hook::Prompt %} {% match hook -%}
@events.on_post_prompt # type: ignore # pylint:disable=undefined-variable {%- when Hook::None -%}
{{ NOT_CONFIGURED }}
{%- when Hook::Pwd %} {%- when Hook::Prompt -%}
@events.on_chdir # type: ignore # pylint:disable=undefined-variable @events.on_post_prompt # type: ignore # pylint:disable=undefined-variable
{%- when Hook::Pwd -%}
{%- endmatch %} @events.on_chdir # type: ignore # pylint:disable=undefined-variable
def __zoxide_hook(**_kwargs): {%- endmatch %}
def __zoxide_hook(**_kwargs):
"""Hook to add new entries to the database.""" """Hook to add new entries to the database."""
pwd = __zoxide_pwd() pwd = __zoxide_pwd()
subprocess.run(["zoxide", "add", pwd], check=False) subprocess.run(["zoxide", "add", pwd], check=False)

View File

@ -29,19 +29,17 @@ function __zoxide_hook() {
} }
# Initialize hook. # Initialize hook.
{%- match hook %} if [ "${__zoxide_hooked}" != '1' ]; then
{%- when Hook::None %} __zoxide_hooked='1'
{{ NOT_CONFIGURED }} {%- match hook %}
{%- when Hook::None %}
{%- when Hook::Prompt %} {{ NOT_CONFIGURED }}
[[ -n "${precmd_functions[(r)__zoxide_hook]}" ]] || { {%- when Hook::Prompt %}
precmd_functions+=(__zoxide_hook) precmd_functions+=(__zoxide_hook)
} {%- when Hook::Pwd %}
chpwd_functions=(${chpwd_functions[@]} "__zoxide_hook")
{%- when Hook::Pwd %} {%- endmatch %}
chpwd_functions=(${chpwd_functions[@]} "__zoxide_hook") fi
{%- 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
@ -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