diff --git a/templates/bash.txt b/templates/bash.txt index 1bf1cf0..c3b33d1 100644 --- a/templates/bash.txt +++ b/templates/bash.txt @@ -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 %} diff --git a/templates/posix.txt b/templates/posix.txt index 41bdd26..e516037 100644 --- a/templates/posix.txt +++ b/templates/posix.txt @@ -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 }} diff --git a/templates/xonsh.txt b/templates/xonsh.txt index 1e59cf4..ab74f96 100644 --- a/templates/xonsh.txt +++ b/templates/xonsh.txt @@ -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 }}