Use variable to prevent hook redefinition #1

This commit is contained in:
Ajeet D'Souza 2021-03-06 02:39:25 +05:30
parent 22d19a74b2
commit 1789a70b55
3 changed files with 22 additions and 22 deletions

View File

@ -51,10 +51,10 @@ function __zoxide_hook() {
{{ NOT_CONFIGURED }}
{%- else %}
case "${PROMPT_COMMAND}" in
*__zoxide_hook*) ;;
*) PROMPT_COMMAND="${PROMPT_COMMAND:+${PROMPT_COMMAND};}__zoxide_hook" ;;
esac
if [ "${__zoxide_hooked}" != '1' ]; then
PROMPT_COMMAND="${PROMPT_COMMAND:+${PROMPT_COMMAND};}__zoxide_hook"
__zoxide_hooked='1'
fi
{%- endif %}

View File

@ -52,10 +52,10 @@ __zoxide_hook() {
{{ NOT_CONFIGURED }}
{%- when Hook::Prompt %}
case "${PS1}" in
*\$\(__zoxide_hook\)*) ;;
*) PS1="${PS1}\$(__zoxide_hook)" ;;
esac
if [ "${__zoxide_hooked}" != '1' ]; then
PS1="${PS1}\$(__zoxide_hook)"
__zoxide_hooked='1'
fi
{%- when Hook::Pwd %}
{{ NOT_CONFIGURED }}

View File

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