perf(kubectx)!: load context asynchronously (#13825)
BREAKING CHANGE: `kubectx_prompt_info` uses the async prompt API by default on supported zsh versions. Set async-prompt to no for synchronous behavior, or force it when calling it through a wrapper function. See the plugin README for details.
Diff
1diff --git a/plugins/kubectx/README.md b/plugins/kubectx/README.md
2index e3987b42f4737bcc229c2bdfcc97ea427cfb9f21..f9474ca8aa2fa69cdf56bee44939a94ecd4d0193 100644
3--- a/plugins/kubectx/README.md
4+++ b/plugins/kubectx/README.md
5@@ -22,6 +22,21 @@ RPS1='$(kubectx_prompt_info)'
6 PROMPT="$PROMPT"'$(kubectx_prompt_info)'
7 ```
8 9+The context is loaded asynchronously on supported versions of zsh so that
10+`kubectl` does not block the prompt. To restore synchronous behavior, add this
11+before Oh My Zsh is sourced:
12+
13+```zsh
14+zstyle ':omz:alpha:plugins:kubectx' async-prompt no
15+```
16+
17+If your theme calls `kubectx_prompt_info` indirectly through another function,
18+force registration of the async handler instead:
19+
20+```zsh
21+zstyle ':omz:alpha:plugins:kubectx' async-prompt force
22+```
23+
24 ### Custom context names
2526 You can rename the default context name for better readability or additional formatting.
27diff --git a/plugins/kubectx/kubectx.plugin.zsh b/plugins/kubectx/kubectx.plugin.zsh
28index f1ca990ad923851576ca1a9a63d96260af8e62b1..a5d5ce8b847bf0d215787ce843b63b0859d0e901 100644
29--- a/plugins/kubectx/kubectx.plugin.zsh
30+++ b/plugins/kubectx/kubectx.plugin.zsh
31@@ -1,6 +1,6 @@
32 typeset -g -A kubectx_mapping
3334-function kubectx_prompt_info() {
35+function _omz_kubectx_prompt_info() {
36 (( $+commands[kubectl] )) || return
3738 local current_ctx=$(kubectl config current-context 2> /dev/null)
39@@ -13,3 +13,34 @@ function kubectx_prompt_info() {
40 # the context name, as it could contain a % character.
41 echo "${kubectx_mapping[$current_ctx]:-${current_ctx:gs/%/%%}}"
42 }
43+
44+function kubectx_prompt_info() {
45+ if (( ${+_OMZ_ASYNC_OUTPUT} )) \
46+ && [[ -n "${_OMZ_ASYNC_OUTPUT[_omz_kubectx_prompt_info]-}" ]]; then
47+ echo -n "${_OMZ_ASYNC_OUTPUT[_omz_kubectx_prompt_info]}"
48+ fi
49+}
50+
51+local _style
52+if zstyle -t ':omz:alpha:plugins:kubectx' async-prompt \
53+ || { is-at-least 5.0.6 && zstyle -T ':omz:alpha:plugins:kubectx' async-prompt }; then
54+ function _defer_async_kubectx_register() {
55+ case "${PS1}:${PS2}:${PS3}:${PS4}:${RPROMPT-}:${RPS1-}:${RPS2-}:${RPS3-}:${RPS4-}" in
56+ *(\$\(kubectx_prompt_info\)|\`kubectx_prompt_info\`)*)
57+ _omz_register_handler _omz_kubectx_prompt_info
58+ ;;
59+ esac
60+
61+ add-zsh-hook -d precmd _defer_async_kubectx_register
62+ unset -f _defer_async_kubectx_register
63+ }
64+
65+ autoload -Uz add-zsh-hook
66+ precmd_functions=(_defer_async_kubectx_register $precmd_functions)
67+elif zstyle -s ':omz:alpha:plugins:kubectx' async-prompt _style && [[ $_style == "force" ]]; then
68+ _omz_register_handler _omz_kubectx_prompt_info
69+else
70+ function kubectx_prompt_info() {
71+ _omz_kubectx_prompt_info
72+ }
73+fi