Parent directory

kubectx.plugin.zsh

1581 bytes
 1typeset -g -A kubectx_mapping
 2
 3function _omz_kubectx_prompt_info() {
 4  (( $+commands[kubectl] )) || return
 5
 6  local current_ctx=$(kubectl config current-context 2> /dev/null)
 7
 8  [[ -n "$current_ctx" ]] || return
 9
10  # Use value in associative array if it exists, otherwise fall back to the context name
11  #
12  # Note: we need to escape the % character in the prompt string when coming directly from
13  # the context name, as it could contain a % character.
14  echo "${kubectx_mapping[$current_ctx]:-${current_ctx:gs/%/%%}}"
15}
16
17function kubectx_prompt_info() {
18  if (( ${+_OMZ_ASYNC_OUTPUT} )) \
19    && [[ -n "${_OMZ_ASYNC_OUTPUT[_omz_kubectx_prompt_info]-}" ]]; then
20    echo -n "${_OMZ_ASYNC_OUTPUT[_omz_kubectx_prompt_info]}"
21  fi
22}
23
24local _style
25if zstyle -t ':omz:alpha:plugins:kubectx' async-prompt \
26  || { is-at-least 5.0.6 && zstyle -T ':omz:alpha:plugins:kubectx' async-prompt }; then
27  function _defer_async_kubectx_register() {
28    case "${PS1}:${PS2}:${PS3}:${PS4}:${RPROMPT-}:${RPS1-}:${RPS2-}:${RPS3-}:${RPS4-}" in
29    *(\$\(kubectx_prompt_info\)|\`kubectx_prompt_info\`)*)
30      _omz_register_handler _omz_kubectx_prompt_info
31      ;;
32    esac
33
34    add-zsh-hook -d precmd _defer_async_kubectx_register
35    unset -f _defer_async_kubectx_register
36  }
37
38  autoload -Uz add-zsh-hook
39  precmd_functions=(_defer_async_kubectx_register $precmd_functions)
40elif zstyle -s ':omz:alpha:plugins:kubectx' async-prompt _style && [[ $_style == "force" ]]; then
41  _omz_register_handler _omz_kubectx_prompt_info
42else
43  function kubectx_prompt_info() {
44    _omz_kubectx_prompt_info
45  }
46fi