romkatv / powerlevel10k

A Zsh theme
MIT License
46.78k stars 2.2k forks source link

When using `POWERLEVEL9K_KUBECONTEXT_SHORTEN`, you are unable to add namespace to the text #1850

Closed jwhitcraft closed 2 years ago

jwhitcraft commented 2 years ago

Since I'm using EKS for my Kubernetes clusters, I added POWERLEVEL9K_KUBECONTEXT_SHORTEN=(eks) to my .p10k.zsh file.

What what I lost was the ability to add the namespace if it's not default. I think the fix would be changed this bit of code:

https://github.com/romkatv/powerlevel10k/blob/master/internal/p10k.zsh#L4530-L4535

to:

      if [[ -z $text ]]; then
        text=$name
      fi
      if [[ $_POWERLEVEL9K_KUBECONTEXT_SHOW_DEFAULT_NAMESPACE == 1 || $namespace != (default|$name) ]]; then
        text+="/$namespace"
      fi
romkatv commented 2 years ago

What are you trying to achieve?

Have you read the comments in ~/.p10k.zsh that pertain to the configuration of kubecontext? You can find them by searching "kubecontext:".

Have you tried achieving what you want to achieve by tweaking the parameters documented in ~/.p10k.zsh?

jwhitcraft commented 2 years ago

Yes i have,I have POWERLEVEL9K_KUBECONTEXT_SHOW_ON_COMMAND commented out, so my k8s context is always shown, and since i'm using the the SHORTEN variable, it never adds the namespace to my command prompt to even when i'm not on the default namespace.

romkatv commented 2 years ago

Have you seen this block in ~/.p10k.zsh? https://github.com/romkatv/powerlevel10k/blob/0b026542699ca0f2de7c5354fe0ff1184e63a3f3/config/p10k-lean.zsh#L1202-L1246

Which value of POWERLEVEL9K_KUBECONTEXT_DEFAULT_CONTENT_EXPANSION gives you prompt that most closely matches what you want?

Please give an example of the actual and expected prompt where they aren't the same.

P.S.

You didn't answer all questions from my previous comments. That's OK. Please do answer all questions/requests from this comment. There are 3 of them.

jwhitcraft commented 2 years ago

Have you seen this block in ~/.p10k.zsh? Yes I have

Here is my block from my ~/.p10k.zsh file

  #############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]#############
  # Show kubecontext only when the command you are typing invokes one of these tools.
  # Tip: Remove the next line to always show kubecontext.
  #typeset -g POWERLEVEL9K_KUBECONTEXT_SHOW_ON_COMMAND='kubectl|helm|kubens|kubectx|oc|istioctl|kogito|k9s|helmfile|flux|fluxctl|stern|kubeseal|skaffold'

  # Kubernetes context classes for the purpose of using different colors, icons and expansions with
  # different contexts.
  #
  # POWERLEVEL9K_KUBECONTEXT_CLASSES is an array with even number of elements. The first element
  # in each pair defines a pattern against which the current kubernetes context gets matched.
  # More specifically, it's P9K_CONTENT prior to the application of context expansion (see below)
  # that gets matched. If you unset all POWERLEVEL9K_KUBECONTEXT_*CONTENT_EXPANSION parameters,
  # you'll see this value in your prompt. The second element of each pair in
  # POWERLEVEL9K_KUBECONTEXT_CLASSES defines the context class. Patterns are tried in order. The
  # first match wins.
  #
  # For example, given these settings:
  #
  #   typeset -g POWERLEVEL9K_KUBECONTEXT_CLASSES=(
  #     '*prod*'  PROD
  #     '*test*'  TEST
  #     '*'       DEFAULT)
  #
  # If your current kubernetes context is "deathray-testing/default", its class is TEST
  # because "deathray-testing/default" doesn't match the pattern '*prod*' but does match '*test*'.
  #
  # You can define different colors, icons and content expansions for different classes:
  #
  #   typeset -g POWERLEVEL9K_KUBECONTEXT_TEST_FOREGROUND=28
  #   typeset -g POWERLEVEL9K_KUBECONTEXT_TEST_VISUAL_IDENTIFIER_EXPANSION='⭐'
  #   typeset -g POWERLEVEL9K_KUBECONTEXT_TEST_CONTENT_EXPANSION='> ${P9K_CONTENT} <'
  typeset -g POWERLEVEL9K_KUBECONTEXT_CLASSES=(
      '*prod*'  PROD    # These values are examples that are unlikely
      # '*test*'  TEST    # to match your needs. Customize them as needed.
      '*'       DEFAULT)
  typeset -g POWERLEVEL9K_KUBECONTEXT_DEFAULT_FOREGROUND=252
  typeset -g POWERLEVEL9K_KUBECONTEXT_PROD_FOREGROUND=195
  typeset -g POWERLEVEL9K_KUBECONTEXT_DEFAULT_BACKGROUND=24
  typeset -g POWERLEVEL9K_KUBECONTEXT_PROD_BACKGROUND=196
  typeset -g POWERLEVEL9K_KUBECONTEXT_SHOW_DEFAULT_NAMESPACE=0
  typeset -g POWERLEVEL9K_KUBECONTEXT_SHORTEN=(eks)
  # typeset -g POWERLEVEL9K_KUBECONTEXT_DEFAULT_VISUAL_IDENTIFIER_EXPANSION='⭐'

  # Use POWERLEVEL9K_KUBECONTEXT_CONTENT_EXPANSION to specify the content displayed by kubecontext
  # segment. Parameter expansions are very flexible and fast, too. See reference:
  # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion.
  #
  # Within the expansion the following parameters are always available:
  #
  # - P9K_CONTENT                The content that would've been displayed if there was no content
  #                              expansion defined.
  # - P9K_KUBECONTEXT_NAME       The current context's name. Corresponds to column NAME in the
  #                              output of `kubectl config get-contexts`.
  # - P9K_KUBECONTEXT_CLUSTER    The current context's cluster. Corresponds to column CLUSTER in the
  #                              output of `kubectl config get-contexts`.
  # - P9K_KUBECONTEXT_NAMESPACE  The current context's namespace. Corresponds to column NAMESPACE
  #                              in the output of `kubectl config get-contexts`. If there is no
  #                              namespace, the parameter is set to "default".
  #
  # If the context points to Google Kubernetes Engine (GKE) or Elastic Kubernetes Service (EKS),
  # the following extra parameters are available:
  #
  # - P9K_KUBECONTEXT_CLOUD_NAME     Either "gke" or "eks".
  # - P9K_KUBECONTEXT_CLOUD_ACCOUNT  Account/project ID.
  # - P9K_KUBECONTEXT_CLOUD_ZONE     Availability zone.
  # - P9K_KUBECONTEXT_CLOUD_CLUSTER  Cluster.
  #
  # P9K_KUBECONTEXT_CLOUD_* parameters are derived from P9K_KUBECONTEXT_CLUSTER. For example,
  # if P9K_KUBECONTEXT_CLUSTER is "gke_my-account_us-east1-a_my-cluster-01":
  #
  #   - P9K_KUBECONTEXT_CLOUD_NAME=gke
  #   - P9K_KUBECONTEXT_CLOUD_ACCOUNT=my-account
  #   - P9K_KUBECONTEXT_CLOUD_ZONE=us-east1-a
  #   - P9K_KUBECONTEXT_CLOUD_CLUSTER=my-cluster-01
  #
  # If P9K_KUBECONTEXT_CLUSTER is "arn:aws:eks:us-east-1:123456789012:cluster/my-cluster-01":
  #
  #   - P9K_KUBECONTEXT_CLOUD_NAME=eks
  #   - P9K_KUBECONTEXT_CLOUD_ACCOUNT=123456789012
  #   - P9K_KUBECONTEXT_CLOUD_ZONE=us-east-1
  #   - P9K_KUBECONTEXT_CLOUD_CLUSTER=my-cluster-01
  typeset -g POWERLEVEL9K_KUBECONTEXT_DEFAULT_CONTENT_EXPANSION=
  # Show P9K_KUBECONTEXT_CLOUD_CLUSTER if it's not empty and fall back to P9K_KUBECONTEXT_NAME.
  POWERLEVEL9K_KUBECONTEXT_DEFAULT_CONTENT_EXPANSION+='${P9K_KUBECONTEXT_CLOUD_CLUSTER:-${P9K_KUBECONTEXT_NAME}}'
  # Append the current context's namespace if it's not "default".
  POWERLEVEL9K_KUBECONTEXT_DEFAULT_CONTENT_EXPANSION+='${${:-/$P9K_KUBECONTEXT_NAMESPACE}:#/default}'

  # Custom prefix.
  #typeset -g POWERLEVEL9K_KUBECONTEXT_PREFIX='\u2388 '

I am trying to achieve this with EXPANSION disabled and SHORTEN enabled:

'$P9K_KUBECONTEXT_CLOUD_CLUSTER${${:-/$P9K_KUBECONTEXT_NAMESPACE}:#/default}'
image

But I end up with is this:

image
'$P9K_KUBECONTEXT_CLOUD_CLUSTER'

So far the only way I've be able to do it, is to make the change I suggested above or I enable EXPANSION, which i explicitly have turned off.

romkatv commented 2 years ago

I am trying to achieve this with EXPANSION disabled

Why?

jwhitcraft commented 2 years ago

Because I alway like to have my context shown, it's how I've have my prompt for years even before switching it p10k a year ago.

romkatv commented 2 years ago

Defining POWERLEVEL9K_KUBECONTEXT_DEFAULT_CONTENT_EXPANSION doesn't hide context. It allows you to specify the format in which context is displayed. If you don't define POWERLEVEL9K_KUBECONTEXT_DEFAULT_CONTENT_EXPANSION, it's the same as setting it to '$P9K_CONTENT'.

romkatv commented 2 years ago

Please give an example of the actual and expected prompt where they aren't the same.

Please ☝️

jwhitcraft commented 2 years ago

So I just spent a while testing various settings of these, and nothing i put in the POWERLEVEL9K_KUBECONTEXT_DEFAULT_CONTENT_EXPANSION setting works or changes the output, besides what i'm seeing.

I how ever did try another theme and it's working correctly. Is there a setting that I may be missing from an older p10k.zsh file?

Thanks,

jwhitcraft commented 2 years ago

OK, so now i feel stupid, I didn't realize that I had to set a unique variable since i'm using POWERLEVEL9K_KUBECONTEXT_CLASSES

Once i added POWERLEVEL9K_KUBECONTEXT_PROD_CONTENT_EXPANSION it started working just fine. 🤦🏻

romkatv commented 2 years ago

OK, so now i feel stupid, I didn't realize that I had to set a unique variable since i'm using POWERLEVEL9K_KUBECONTEXT_CLASSES

I should've noticed that you'd uncommented that line. I'm glad that you figured out what was going on.