Skip to content

Commit

Permalink
Simplify hook logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ajeetdsouza committed Sep 9, 2021
1 parent a33806f commit bc89589
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 118 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 24 additions & 44 deletions templates/bash.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,40 @@ function __zoxide_cd() {
# Hook configuration for zoxide.
#

{# Custom prompts often use "$?" to show the exit status of the previous
# command. Adding __zoxide_hook to the front of $PROMPT_COMMAND would change
# the exit status, so we must capture it and return it manually instead. -#}

# Hook to add new entries to the database.
{%- match hook %}
{%- when InitHook::None %}
{% if hook == InitHook::None -%}
{{ not_configured }}

{%- when InitHook::Prompt %}
{%- else -%}
# Hook to add new entries to the database.
{%- if hook == InitHook::Prompt %}
function __zoxide_hook() {
\builtin local -r retval="$?"
zoxide add -- "$(__zoxide_pwd)"
return "${retval}"
}

{%- when InitHook::Pwd %}
{%- else if hook == InitHook::Pwd %}
function __zoxide_hook() {
\builtin local -r retval="$?"
\builtin local -r pwd_tmp="$(__zoxide_pwd)"
if [ -z "${__zoxide_pwd_old}" ]; then
__zoxide_pwd_old="${pwd_tmp}"
elif [ "${__zoxide_pwd_old}" != "${pwd_tmp}" ]; then
__zoxide_pwd_old="${pwd_tmp}"
zoxide add -- "${__zoxide_pwd_old}"
if [ -z "${__zoxide_oldpwd}" ]; then
__zoxide_oldpwd="${pwd_tmp}"
elif [ "${__zoxide_oldpwd}" != "${pwd_tmp}" ]; then
__zoxide_oldpwd="${pwd_tmp}"
zoxide add -- "${__zoxide_oldpwd}"
fi
return "${retval}"
}

{%- endmatch %}

{# $PROMPT_COMMAND cannot contain two semicolons in sequence. It can end with a
# semicolon, but it cannot start with one. Therefore, always put the hook
# at the start of $PROMPT_COMMAND. -#}
{%- endif %}

# Initialize hook.
if [ "${__zoxide_hooked}" != '1' ]; then
__zoxide_hooked='1'
{%- if hook == InitHook::None %}
{{ not_configured }}
{%- else %}
if [ -z "${PROMPT_COMMAND}" ]; then
PROMPT_COMMAND='__zoxide_hook'
else
PROMPT_COMMAND="__zoxide_hook;${PROMPT_COMMAND#;}"
fi
{%- endif %}
if [ -z "${PROMPT_COMMAND}" ]; then
PROMPT_COMMAND='__zoxide_hook'
elif [[ ${PROMPT_COMMAND} != *'__zoxide_hook'* ]]; then
PROMPT_COMMAND="__zoxide_hook;${PROMPT_COMMAND#;}"
fi

{% endif -%}

{{ section }}
# When using zoxide with --no-aliases, alias these internal functions as
# desired.
Expand All @@ -83,13 +68,7 @@ function __zoxide_z() {
if [ "$#" -eq 0 ]; then
__zoxide_cd ~
elif [ "$#" -eq 1 ] && [ "$1" = '-' ]; then
if [ -n "${OLDPWD}" ]; then
__zoxide_cd "${OLDPWD}"
else
# shellcheck disable=SC2016
\builtin printf 'zoxide: $OLDPWD is not set\n'
return 1
fi
__zoxide_cd "${OLDPWD}"
elif [ "$#" -eq 1 ] && [ -d "$1" ]; then
__zoxide_cd "$1"
else
Expand Down Expand Up @@ -118,12 +97,12 @@ function __zoxide_unset() {
\builtin unalias "$@" &>/dev/null || \builtin :
}

__zoxide_unset '{{cmd}}'
__zoxide_unset {{cmd}}
function {{cmd}}() {
__zoxide_z "$@"
}

__zoxide_unset '{{cmd}}i'
__zoxide_unset {{cmd}}i
function {{cmd}}i() {
__zoxide_zi "$@"
}
Expand All @@ -135,8 +114,8 @@ if [[ :"${SHELLOPTS}": =~ :(vi|emacs): ]] && [ "${TERM}" != 'dumb' ]; then
{# Use `printf '\e[5n'` to redraw line after fzf closes. -#}
\builtin bind '"\e[0n": redraw-current-line' &>/dev/null

function __zoxide_z_complete() {
[ {{ "${#COMP_WORDS[@]}" }} -eq 2 ] || return
function _{{cmd}}() {
[[ {{ "${#COMP_WORDS[@]}" }} -eq 2 && ${COMP_POINT} -eq {{ "${#COMP_LINE}" }} ]] || return

\builtin local -r trigger='**'
\builtin local query="${COMP_WORDS[1]}"
Expand All @@ -152,13 +131,14 @@ if [[ :"${SHELLOPTS}": =~ :(vi|emacs): ]] && [ "${TERM}" != 'dumb' ]; then
--reverse \
--select-1 \
" zoxide query -i -- "${query}")")
[[ $? -eq 130 ]] && COMPREPLY=("${query}")
\builtin printf '\e[5n'
else
\builtin mapfile -t COMPREPLY < <(compgen -A directory -S / -- "${query}")
fi
}

\builtin complete -F __zoxide_z_complete -o nospace -- '{{cmd}}'
\builtin complete -F _{{cmd}} -o nospace -- {{cmd}}
fi

{%- when None %}
Expand Down
19 changes: 11 additions & 8 deletions templates/elvish.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ var oldpwd = $builtin:pwd
set builtin:before-chdir = [$@builtin:before-chdir [_]{ edit:add-var oldpwd $builtin:pwd }]

# Initialize hook to add directories to zoxide.
if (builtin:not (builtin:eq $E:__zoxide_hooked_elvish $E:SHLVL)) {
set E:__zoxide_hooked_elvish = $E:SHLVL
{%- match hook %}
{%- when InitHook::None %}
{{ not_configured }}
{%- when InitHook::Prompt %}
{%- if hook == InitHook::None %}
{{ not_configured }}

{%- else %}
if (builtin:not (builtin:eq $E:__zoxide_shlvl $E:SHLVL)) {
set E:__zoxide_shlvl = $E:SHLVL
{%- if hook == InitHook::Prompt %}
set edit:before-readline = [$@edit:before-readline []{ zoxide add -- $pwd }]
{%- when InitHook::Pwd %}
{%- else if hook == InitHook::Pwd %}
set builtin:after-chdir = [$@builtin:after-chdir [_]{ zoxide add -- $pwd }]
{%- endmatch %}
{%- endif %}
}

{%- endif %}

{{ section }}
# When using zoxide with --no-aliases, alias these internal functions as
# desired.
Expand Down
26 changes: 13 additions & 13 deletions templates/fish.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ end
# Hook configuration for zoxide.
#

{% if hook == InitHook::None -%}
{{ not_configured }}

{%- else -%}
# Initialize hook to add new entries to the database.
if test "$__zoxide_hooked" != 1
set __zoxide_hooked 1
{%- match hook %}
{%- when InitHook::None %}
function __zoxide_hook
{%- when InitHook::Prompt %}
function __zoxide_hook --on-event fish_prompt
{%- when InitHook::Pwd %}
function __zoxide_hook --on-variable PWD
{%- endmatch %}
test -z "$fish_private_mode"
and command zoxide add -- (__zoxide_pwd)
end
{%- if hook == InitHook::Prompt %}
function __zoxide_hook --on-event fish_prompt
{%- else if hook == InitHook::Pwd %}
function __zoxide_hook --on-variable PWD
{%- endif %}
test -z "$fish_private_mode"
and command zoxide add -- (__zoxide_pwd)
end

{%- endif %}

{{ section }}
# When using zoxide with --no-aliases, alias these internal functions as
# desired.
Expand Down
20 changes: 10 additions & 10 deletions templates/nushell.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ def __zoxide_prompt [] {

{%- when InitHook::Prompt %}
def __zoxide_hook [] {
shells | where active == $true && name == filesystem | get path | each { zoxide add -- $it }
shells | where active == $true && name == filesystem | get path | each {
zoxide add -- $it
}
}

# Initialize hook.
if ($nu.env | select PROMPT_STRING | empty?) {
if ($nu.config | select prompt | empty?) {
let-env PROMPT_STRING = '__zoxide_hook;__zoxide_prompt'
} {
let-env PROMPT_STRING = $'__zoxide_hook;($nu.config.prompt)'
}
} {
let-env PROMPT_STRING = $'__zoxide_hook;($nu.env.PROMPT_STRING)'
}
let-env PROMPT_STRING = (
let prompt = (if ($nu.env | select PROMPT_STRING | empty?) {
if ($nu.config | select prompt | empty?) { '__zoxide_prompt' } { $nu.config.prompt }
} { $nu.env.PROMPT_STRING });

if ($prompt | str contains '__zoxide_hook') { $prompt } { $'__zoxide_hook;($prompt)' }
)

{%- when InitHook::Pwd %}
$'zoxide: PWD hooks are not supported on Nushell.(char nl)Use (char sq)zoxide init nushell --hook prompt(char sq) instead.(char nl)'
Expand Down
32 changes: 12 additions & 20 deletions templates/posix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,28 @@ __zoxide_cd() {
# Hook configuration for zoxide.
#

# Hook to add new entries to the database.
{%- match hook %}
{%- when InitHook::None %}
{% match hook %}
{%- when InitHook::None -%}
{{ not_configured }}

{%- when InitHook::Prompt %}
{%- when InitHook::Prompt -%}
# Hook to add new entries to the database.
__zoxide_hook() {
zoxide add -- "$(__zoxide_pwd)"
}

{%- when InitHook::Pwd %}
{{ not_configured }}

{%- endmatch %}

# Initialize hook.
if [ "${__zoxide_hooked}" != '1' ]; then
__zoxide_hooked='1'
{%- match hook %}
{%- when InitHook::None %}
{{ not_configured }}
{%- when InitHook::Prompt %}
if [ "${PS1#*\$(__zoxide_hook)}" = "${PS1}" ]; then
PS1="${PS1}\$(__zoxide_hook)"
{%- when InitHook::Pwd %}
\printf "%s\n%s\n" \
"zoxide: PWD hooks are not supported on POSIX shells." \
" Use 'zoxide init posix --hook prompt' instead."
{%- endmatch %}
fi

{%- when InitHook::Pwd -%}
\printf "%s\n%s\n" \
"zoxide: PWD hooks are not supported on POSIX shells." \
" Use 'zoxide init posix --hook prompt' instead."

{%- endmatch %}

{{ section }}
# When using zoxide with --no-aliases, alias these internal functions as
# desired.
Expand Down
2 changes: 1 addition & 1 deletion templates/powershell.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function __zoxide_hook {
# Initialize hook.
{# Initialize $__zoxide_hooked if it does not exist. Removing this will cause
# an unset variable error in StrictMode. #}
$__zoxide_hooked = (Get-Variable __zoxide_hooked -ValueOnly -ErrorAction silentlycontinue)
$__zoxide_hooked = (Get-Variable __zoxide_hooked -ValueOnly -ErrorAction SilentlyContinue)
if ($__zoxide_hooked -ne 1) {
$__zoxide_hooked = 1
{%- match hook %}
Expand Down
18 changes: 10 additions & 8 deletions templates/xonsh.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,26 @@ def __zoxide_errhandler(func):
# Hook configuration for zoxide.
#

{% if hook == InitHook::None -%}
{{ not_configured }}

{%- else -%}
# Initialize hook to add new entries to the database.
if globals().get("__zoxide_hooked") is not True:
globals()["__zoxide_hooked"] = True
{% match hook -%}
{%- when InitHook::None %}
{{ not_configured }}
{%- when InitHook::Prompt %}
if "__zoxide_hook" not in globals():
{% if hook == InitHook::Prompt %}
@builtins.events.on_post_prompt # type: ignore # pylint:disable=no-member
{%- when InitHook::Pwd %}
{%- else if hook == InitHook::Pwd %}
@builtins.events.on_chdir # type: ignore # pylint:disable=no-member
{%- endmatch %}
{%- endif %}
def __zoxide_hook(**_kwargs):
"""Hook to add new entries to the database."""
pwd = __zoxide_pwd()
zoxide = __zoxide_bin()
subprocess.run([zoxide, "add", "--", pwd], check=False)


{% endif -%}

{{ section }}
# When using zoxide with --no-aliases, alias these internal functions as
# desired.
Expand Down
19 changes: 11 additions & 8 deletions templates/zsh.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,27 @@ function __zoxide_cd() {
# Hook configuration for zoxide.
#

{% if hook == InitHook::None -%}
{{ not_configured -}}

{% else -%}
# Hook to add new entries to the database.
function __zoxide_hook() {
zoxide add -- "$(__zoxide_pwd)"
}

# Initialize hook.
if [ "${__zoxide_hooked}" != '1' ]; then
__zoxide_hooked='1'
{%- match hook %}
{%- when InitHook::None %}
{{ not_configured }}
{%- when InitHook::Prompt %}
# shellcheck disable=SC2154
if [[ ${precmd_functions[(Ie)__zoxide_hook]} -eq 0 ]] && [[ ${chpwd_functions[(Ie)__zoxide_hook]} -eq 0 ]]; then
{%- if hook == InitHook::Prompt %}
precmd_functions+=(__zoxide_hook)
{%- when InitHook::Pwd %}
{%- else if hook == InitHook::Pwd %}
chpwd_functions=("${chpwd_functions[@]}" "__zoxide_hook")
{%- endmatch %}
{%- endif %}
fi

{%- endif %}

{{ section }}
# When using zoxide with --no-aliases, alias these internal functions as
# desired.
Expand Down

0 comments on commit bc89589

Please sign in to comment.