Simplify hook logic

This commit is contained in:
Ajeet D'Souza 2021-09-07 02:41:17 +05:30
parent a33806feea
commit bc8958943b
9 changed files with 98 additions and 118 deletions

12
Cargo.lock generated
View File

@ -57,9 +57,9 @@ dependencies = [
[[package]] [[package]]
name = "assert_cmd" name = "assert_cmd"
version = "2.0.0" version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54f002ce7d0c5e809ebb02be78fd503aeed4a511fd0fcaff6e6914cbdabbfa33" checksum = "b800c4403e8105d959595e1f88119e78bc12bc874c4336973658b648a746ba93"
dependencies = [ dependencies = [
"bstr", "bstr",
"doc-comment", "doc-comment",
@ -336,9 +336,9 @@ dependencies = [
[[package]] [[package]]
name = "ordered-float" name = "ordered-float"
version = "2.7.0" version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "039f02eb0f69271f26abe3202189275d7aa2258b903cb0281b5de710a2570ff3" checksum = "97c9d06878b3a851e8026ef94bf7fef9ba93062cd412601da4d9cf369b1cc62d"
dependencies = [ dependencies = [
"num-traits", "num-traits",
] ]
@ -572,9 +572,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.75" version = "1.0.76"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7f58f7e8eaa0009c5fec437aabf511bd9933e4b2d7407bd05273c01a8906ea7" checksum = "c6f107db402c2c2055242dbf4d2af0e69197202e9faacbef9571bbe47f5a1b84"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@ -24,55 +24,40 @@ function __zoxide_cd() {
# Hook configuration for zoxide. # Hook configuration for zoxide.
# #
{# Custom prompts often use "$?" to show the exit status of the previous {% if hook == InitHook::None -%}
# 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 %}
{{ not_configured }} {{ not_configured }}
{%- when InitHook::Prompt %} {%- else -%}
# Hook to add new entries to the database.
{%- if hook == InitHook::Prompt %}
function __zoxide_hook() { function __zoxide_hook() {
\builtin local -r retval="$?" \builtin local -r retval="$?"
zoxide add -- "$(__zoxide_pwd)" zoxide add -- "$(__zoxide_pwd)"
return "${retval}" return "${retval}"
} }
{%- else if hook == InitHook::Pwd %}
{%- when InitHook::Pwd %}
function __zoxide_hook() { function __zoxide_hook() {
\builtin local -r retval="$?" \builtin local -r retval="$?"
\builtin local -r pwd_tmp="$(__zoxide_pwd)" \builtin local -r pwd_tmp="$(__zoxide_pwd)"
if [ -z "${__zoxide_pwd_old}" ]; then if [ -z "${__zoxide_oldpwd}" ]; then
__zoxide_pwd_old="${pwd_tmp}" __zoxide_oldpwd="${pwd_tmp}"
elif [ "${__zoxide_pwd_old}" != "${pwd_tmp}" ]; then elif [ "${__zoxide_oldpwd}" != "${pwd_tmp}" ]; then
__zoxide_pwd_old="${pwd_tmp}" __zoxide_oldpwd="${pwd_tmp}"
zoxide add -- "${__zoxide_pwd_old}" zoxide add -- "${__zoxide_oldpwd}"
fi fi
return "${retval}" return "${retval}"
} }
{%- endif %}
{%- 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. -#}
# Initialize hook. # Initialize hook.
if [ "${__zoxide_hooked}" != '1' ]; then if [ -z "${PROMPT_COMMAND}" ]; then
__zoxide_hooked='1' PROMPT_COMMAND='__zoxide_hook'
{%- if hook == InitHook::None %} elif [[ ${PROMPT_COMMAND} != *'__zoxide_hook'* ]]; then
{{ not_configured }} PROMPT_COMMAND="__zoxide_hook;${PROMPT_COMMAND#;}"
{%- else %}
if [ -z "${PROMPT_COMMAND}" ]; then
PROMPT_COMMAND='__zoxide_hook'
else
PROMPT_COMMAND="__zoxide_hook;${PROMPT_COMMAND#;}"
fi
{%- endif %}
fi 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
# desired. # desired.
@ -83,13 +68,7 @@ function __zoxide_z() {
if [ "$#" -eq 0 ]; then if [ "$#" -eq 0 ]; then
__zoxide_cd ~ __zoxide_cd ~
elif [ "$#" -eq 1 ] && [ "$1" = '-' ]; then elif [ "$#" -eq 1 ] && [ "$1" = '-' ]; then
if [ -n "${OLDPWD}" ]; then __zoxide_cd "${OLDPWD}"
__zoxide_cd "${OLDPWD}"
else
# shellcheck disable=SC2016
\builtin printf 'zoxide: $OLDPWD is not set\n'
return 1
fi
elif [ "$#" -eq 1 ] && [ -d "$1" ]; then elif [ "$#" -eq 1 ] && [ -d "$1" ]; then
__zoxide_cd "$1" __zoxide_cd "$1"
else else
@ -118,12 +97,12 @@ function __zoxide_unset() {
\builtin unalias "$@" &>/dev/null || \builtin : \builtin unalias "$@" &>/dev/null || \builtin :
} }
__zoxide_unset '{{cmd}}' __zoxide_unset {{cmd}}
function {{cmd}}() { function {{cmd}}() {
__zoxide_z "$@" __zoxide_z "$@"
} }
__zoxide_unset '{{cmd}}i' __zoxide_unset {{cmd}}i
function {{cmd}}i() { function {{cmd}}i() {
__zoxide_zi "$@" __zoxide_zi "$@"
} }
@ -135,8 +114,8 @@ if [[ :"${SHELLOPTS}": =~ :(vi|emacs): ]] && [ "${TERM}" != 'dumb' ]; then
{# Use `printf '\e[5n'` to redraw line after fzf closes. -#} {# Use `printf '\e[5n'` to redraw line after fzf closes. -#}
\builtin bind '"\e[0n": redraw-current-line' &>/dev/null \builtin bind '"\e[0n": redraw-current-line' &>/dev/null
function __zoxide_z_complete() { function _{{cmd}}() {
[ {{ "${#COMP_WORDS[@]}" }} -eq 2 ] || return [[ {{ "${#COMP_WORDS[@]}" }} -eq 2 && ${COMP_POINT} -eq {{ "${#COMP_LINE}" }} ]] || return
\builtin local -r trigger='**' \builtin local -r trigger='**'
\builtin local query="${COMP_WORDS[1]}" \builtin local query="${COMP_WORDS[1]}"
@ -152,13 +131,14 @@ if [[ :"${SHELLOPTS}": =~ :(vi|emacs): ]] && [ "${TERM}" != 'dumb' ]; then
--reverse \ --reverse \
--select-1 \ --select-1 \
" zoxide query -i -- "${query}")") " zoxide query -i -- "${query}")")
[[ $? -eq 130 ]] && COMPREPLY=("${query}")
\builtin printf '\e[5n' \builtin printf '\e[5n'
else else
\builtin mapfile -t COMPREPLY < <(compgen -A directory -S / -- "${query}") \builtin mapfile -t COMPREPLY < <(compgen -A directory -S / -- "${query}")
fi fi
} }
\builtin complete -F __zoxide_z_complete -o nospace -- '{{cmd}}' \builtin complete -F _{{cmd}} -o nospace -- {{cmd}}
fi fi
{%- when None %} {%- when None %}

View File

@ -25,18 +25,21 @@ var oldpwd = $builtin:pwd
set builtin:before-chdir = [$@builtin:before-chdir [_]{ edit:add-var oldpwd $builtin:pwd }] set builtin:before-chdir = [$@builtin:before-chdir [_]{ edit:add-var oldpwd $builtin:pwd }]
# Initialize hook to add directories to zoxide. # Initialize hook to add directories to zoxide.
if (builtin:not (builtin:eq $E:__zoxide_hooked_elvish $E:SHLVL)) { {%- if hook == InitHook::None %}
set E:__zoxide_hooked_elvish = $E:SHLVL {{ not_configured }}
{%- match hook %}
{%- when InitHook::None %} {%- else %}
{{ not_configured }} if (builtin:not (builtin:eq $E:__zoxide_shlvl $E:SHLVL)) {
{%- when InitHook::Prompt %} set E:__zoxide_shlvl = $E:SHLVL
{%- if hook == InitHook::Prompt %}
set edit:before-readline = [$@edit:before-readline []{ zoxide add -- $pwd }] 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 }] set builtin:after-chdir = [$@builtin:after-chdir [_]{ zoxide add -- $pwd }]
{%- endmatch %} {%- endif %}
} }
{%- 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
# desired. # desired.

View File

@ -37,22 +37,22 @@ end
# Hook configuration for zoxide. # Hook configuration for zoxide.
# #
{% if hook == InitHook::None -%}
{{ not_configured }}
{%- else -%}
# Initialize hook to add new entries to the database. # Initialize hook to add new entries to the database.
if test "$__zoxide_hooked" != 1 {%- if hook == InitHook::Prompt %}
set __zoxide_hooked 1 function __zoxide_hook --on-event fish_prompt
{%- match hook %} {%- else if hook == InitHook::Pwd %}
{%- when InitHook::None %} function __zoxide_hook --on-variable PWD
function __zoxide_hook {%- endif %}
{%- when InitHook::Prompt %} test -z "$fish_private_mode"
function __zoxide_hook --on-event fish_prompt and command zoxide add -- (__zoxide_pwd)
{%- when InitHook::Pwd %}
function __zoxide_hook --on-variable PWD
{%- endmatch %}
test -z "$fish_private_mode"
and command zoxide add -- (__zoxide_pwd)
end
end end
{%- 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
# desired. # desired.

View File

@ -29,19 +29,19 @@ def __zoxide_prompt [] {
{%- when InitHook::Prompt %} {%- when InitHook::Prompt %}
def __zoxide_hook [] { 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. # Initialize hook.
if ($nu.env | select PROMPT_STRING | empty?) { let-env PROMPT_STRING = (
if ($nu.config | select prompt | empty?) { let prompt = (if ($nu.env | select PROMPT_STRING | empty?) {
let-env PROMPT_STRING = '__zoxide_hook;__zoxide_prompt' if ($nu.config | select prompt | empty?) { '__zoxide_prompt' } { $nu.config.prompt }
} { } { $nu.env.PROMPT_STRING });
let-env PROMPT_STRING = $'__zoxide_hook;($nu.config.prompt)'
} if ($prompt | str contains '__zoxide_hook') { $prompt } { $'__zoxide_hook;($prompt)' }
} { )
let-env PROMPT_STRING = $'__zoxide_hook;($nu.env.PROMPT_STRING)'
}
{%- when InitHook::Pwd %} {%- 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)' $'zoxide: PWD hooks are not supported on Nushell.(char nl)Use (char sq)zoxide init nushell --hook prompt(char sq) instead.(char nl)'

View File

@ -24,36 +24,28 @@ __zoxide_cd() {
# Hook configuration for zoxide. # Hook configuration for zoxide.
# #
# Hook to add new entries to the database. {% match hook %}
{%- match hook %} {%- when InitHook::None -%}
{%- when InitHook::None %}
{{ not_configured }} {{ not_configured }}
{%- when InitHook::Prompt %} {%- when InitHook::Prompt -%}
# Hook to add new entries to the database.
__zoxide_hook() { __zoxide_hook() {
zoxide add -- "$(__zoxide_pwd)" zoxide add -- "$(__zoxide_pwd)"
} }
{%- when InitHook::Pwd %}
{{ not_configured }}
{%- endmatch %}
# Initialize hook. # Initialize hook.
if [ "${__zoxide_hooked}" != '1' ]; then if [ "${PS1#*\$(__zoxide_hook)}" = "${PS1}" ]; then
__zoxide_hooked='1'
{%- match hook %}
{%- when InitHook::None %}
{{ not_configured }}
{%- when InitHook::Prompt %}
PS1="${PS1}\$(__zoxide_hook)" 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 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 }} {{ 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

@ -36,7 +36,7 @@ function __zoxide_hook {
# Initialize hook. # Initialize hook.
{# Initialize $__zoxide_hooked if it does not exist. Removing this will cause {# Initialize $__zoxide_hooked if it does not exist. Removing this will cause
# an unset variable error in StrictMode. #} # 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) { if ($__zoxide_hooked -ne 1) {
$__zoxide_hooked = 1 $__zoxide_hooked = 1
{%- match hook %} {%- match hook %}

View File

@ -78,17 +78,17 @@ def __zoxide_errhandler(func):
# Hook configuration for zoxide. # Hook configuration for zoxide.
# #
{% if hook == InitHook::None -%}
{{ not_configured }}
{%- else -%}
# Initialize hook to add new entries to the database. # Initialize hook to add new entries to the database.
if globals().get("__zoxide_hooked") is not True: if "__zoxide_hook" not in globals():
globals()["__zoxide_hooked"] = True {% if hook == InitHook::Prompt %}
{% match hook -%}
{%- when InitHook::None %}
{{ not_configured }}
{%- when InitHook::Prompt %}
@builtins.events.on_post_prompt # type: ignore # pylint:disable=no-member @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 @builtins.events.on_chdir # type: ignore # pylint:disable=no-member
{%- endmatch %} {%- endif %}
def __zoxide_hook(**_kwargs): 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()
@ -96,6 +96,8 @@ if globals().get("__zoxide_hooked") is not True:
subprocess.run([zoxide, "add", "--", pwd], check=False) subprocess.run([zoxide, "add", "--", pwd], check=False)
{% 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
# desired. # desired.

View File

@ -24,24 +24,27 @@ function __zoxide_cd() {
# Hook configuration for zoxide. # Hook configuration for zoxide.
# #
{% if hook == InitHook::None -%}
{{ not_configured -}}
{% else -%}
# Hook to add new entries to the database. # Hook to add new entries to the database.
function __zoxide_hook() { function __zoxide_hook() {
zoxide add -- "$(__zoxide_pwd)" zoxide add -- "$(__zoxide_pwd)"
} }
# Initialize hook. # Initialize hook.
if [ "${__zoxide_hooked}" != '1' ]; then # shellcheck disable=SC2154
__zoxide_hooked='1' if [[ ${precmd_functions[(Ie)__zoxide_hook]} -eq 0 ]] && [[ ${chpwd_functions[(Ie)__zoxide_hook]} -eq 0 ]]; then
{%- match hook %} {%- if hook == InitHook::Prompt %}
{%- when InitHook::None %}
{{ not_configured }}
{%- when InitHook::Prompt %}
precmd_functions+=(__zoxide_hook) precmd_functions+=(__zoxide_hook)
{%- when InitHook::Pwd %} {%- else if hook == InitHook::Pwd %}
chpwd_functions=("${chpwd_functions[@]}" "__zoxide_hook") chpwd_functions=("${chpwd_functions[@]}" "__zoxide_hook")
{%- endmatch %} {%- endif %}
fi 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
# desired. # desired.