From bc8958943b7d1db3e1d6d2d50272bc7dd738e95b Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Tue, 7 Sep 2021 02:41:17 +0530 Subject: [PATCH] Simplify hook logic --- Cargo.lock | 12 +++---- templates/bash.txt | 68 ++++++++++++++-------------------------- templates/elvish.txt | 19 ++++++----- templates/fish.txt | 26 +++++++-------- templates/nushell.txt | 20 ++++++------ templates/posix.txt | 32 +++++++------------ templates/powershell.txt | 2 +- templates/xonsh.txt | 18 ++++++----- templates/zsh.txt | 19 ++++++----- 9 files changed, 98 insertions(+), 118 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9c4319b..350a895 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -57,9 +57,9 @@ dependencies = [ [[package]] name = "assert_cmd" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54f002ce7d0c5e809ebb02be78fd503aeed4a511fd0fcaff6e6914cbdabbfa33" +checksum = "b800c4403e8105d959595e1f88119e78bc12bc874c4336973658b648a746ba93" dependencies = [ "bstr", "doc-comment", @@ -336,9 +336,9 @@ dependencies = [ [[package]] name = "ordered-float" -version = "2.7.0" +version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "039f02eb0f69271f26abe3202189275d7aa2258b903cb0281b5de710a2570ff3" +checksum = "97c9d06878b3a851e8026ef94bf7fef9ba93062cd412601da4d9cf369b1cc62d" dependencies = [ "num-traits", ] @@ -572,9 +572,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.75" +version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7f58f7e8eaa0009c5fec437aabf511bd9933e4b2d7407bd05273c01a8906ea7" +checksum = "c6f107db402c2c2055242dbf4d2af0e69197202e9faacbef9571bbe47f5a1b84" dependencies = [ "proc-macro2", "quote", diff --git a/templates/bash.txt b/templates/bash.txt index 409cd53..5ac0c73 100644 --- a/templates/bash.txt +++ b/templates/bash.txt @@ -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. @@ -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 @@ -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 "$@" } @@ -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]}" @@ -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 %} diff --git a/templates/elvish.txt b/templates/elvish.txt index 4e399b4..dc88fca 100644 --- a/templates/elvish.txt +++ b/templates/elvish.txt @@ -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. diff --git a/templates/fish.txt b/templates/fish.txt index 7f33c3d..7af58b8 100644 --- a/templates/fish.txt +++ b/templates/fish.txt @@ -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. diff --git a/templates/nushell.txt b/templates/nushell.txt index 23642c2..9fea900 100644 --- a/templates/nushell.txt +++ b/templates/nushell.txt @@ -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)' diff --git a/templates/posix.txt b/templates/posix.txt index add0121..8598a5d 100644 --- a/templates/posix.txt +++ b/templates/posix.txt @@ -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. diff --git a/templates/powershell.txt b/templates/powershell.txt index 31a462f..a953eb2 100644 --- a/templates/powershell.txt +++ b/templates/powershell.txt @@ -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 %} diff --git a/templates/xonsh.txt b/templates/xonsh.txt index bb795cf..85292b7 100644 --- a/templates/xonsh.txt +++ b/templates/xonsh.txt @@ -78,17 +78,17 @@ 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() @@ -96,6 +96,8 @@ if globals().get("__zoxide_hooked") is not True: subprocess.run([zoxide, "add", "--", pwd], check=False) +{% endif -%} + {{ section }} # When using zoxide with --no-aliases, alias these internal functions as # desired. diff --git a/templates/zsh.txt b/templates/zsh.txt index 85da631..1815945 100644 --- a/templates/zsh.txt +++ b/templates/zsh.txt @@ -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.