refactor(zsh): using add-zsh-hook instead of add hook manually (#3537)

[1]: https://github.com/starship/starship/pull/3480#discussion_r790280291
[2]: https://zsh.sourceforge.io/Doc/Release/User-Contributions.html#Writing-Themes

Signed-off-by: Coelacanthus <coelacanthus@outlook.com>
This commit is contained in:
Coelacanthus 2022-02-06 14:37:34 +08:00 committed by GitHub
parent bbdb584f45
commit 589576d3eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 15 deletions

View File

@ -52,21 +52,10 @@ prompt_starship_preexec() {
__starship_get_time && STARSHIP_START_TIME=$STARSHIP_CAPTURED_TIME
}
# If precmd/preexec arrays are not already set, set them. If we don't do this,
# the code to detect whether prompt_starship_precmd is already in precmd_functions will
# fail because the array doesn't exist (and same for prompt_starship_preexec)
(( ! ${+precmd_functions} )) && precmd_functions=()
(( ! ${+preexec_functions} )) && preexec_functions=()
# If starship precmd/preexec functions are already hooked, don't double-hook them
# to avoid unnecessary performance degradation in nested shells
if [[ -z ${precmd_functions[(re)prompt_starship_precmd]} ]]; then
precmd_functions+=(prompt_starship_precmd)
fi
if [[ -z ${preexec_functions[(re)prompt_starship_preexec]} ]]; then
preexec_functions+=(prompt_starship_preexec)
fi
# Add hook functions
autoload -Uz add-zsh-hook
add-zsh-hook precmd prompt_starship_precmd
add-zsh-hook preexec prompt_starship_preexec
# Set up a function to redraw the prompt if the user switches vi modes
starship_zle-keymap-select() {