【zsh】Powerlevel10kでKubernetes用の設定をする

スポンサーリンク

はじめに

zshのテーマを変更してくれるプラグインのPowerlevel10kでKubernetes向けの設定をしていきたいと思います。

Powerlevel10kの導入については下記を参照してください。

zinit+iTerm2+powerlevel10kでzshのプロンプトをいい感じにカスタマイズする
はじめにzinit+iTerm2環境でのPowerlevel10kを導入する手順について解説していきます。zinitのざっくりな使い方については下記を参照してください。Powerlevel10kとはPowerlevel10kとは、zshのテ...

Powerlevel10kの設定

Powerlevel10kの設定は~/.p10k.zshに保存されています。

今回は、Powerlevel10kを導入した時の初回の設定ではできなかったことを、設定ファイル(~/.p10k.zsh)を変更することで設定していきます。

コンテキストを常に表示する

まずは、kubectlなどのコマンドを入力した時にしか表示されないコンテキストを、常に表示されるようにします。

下記のようにPOWERLEVEL9K_KUBECONTEXT_SHOW_ON_COMMANDをコメントアウトすることで常に表示されるようになります。

...

  #############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]#############
  # Show kubecontext only when the 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'

...

コンテキストを左側に表示する

次に、コンテキストがプロンプトの右側に表示されているのを、左側に表示されるようにします。

下記のようにPOWERLEVEL9K_LEFT_PROMPT_ELEMENTSに要素を入れると左側に表示され、POWERLEVEL9K_RIGHT_PROMPT_ELEMENTSに入れると右側に表示されるようになっています。

...

  # The list of segments shown on the left. Fill it with the most important segments.
  typeset -g POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(
    # =========================[ Line #1 ]=========================
    os_icon                 # os identifier
    dir                     # current directory
    vcs                     # git status
    # ----- git statusの横にコンテキストが表示されるようにする -----
    kubecontext             # current kubernetes context (https://kubernetes.io/)
    # =========================[ Line #2 ]=========================
    newline                 # \n
    prompt_char             # prompt symbol
  )

  # The list of segments shown on the right. Fill it with less important segments.
  # Right prompt on the last prompt line (where you are typing your commands) gets
  # automatically hidden when the input line reaches it. Right prompt above the
  # last prompt line gets hidden if it would overlap with left prompt.
  typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(
    # =========================[ Line #1 ]=========================
    status                  # exit code of the last command
    command_execution_time  # duration of the last command
    background_jobs         # presence of background jobs
    direnv                  # direnv status (https://direnv.net/)
    asdf                    # asdf version manager (https://github.com/asdf-vm/asdf)
    virtualenv              # python virtual environment (https://docs.python.org/3/library/venv.html)
    anaconda                # conda environment (https://conda.io/)
    pyenv                   # python environment (https://github.com/pyenv/pyenv)
    goenv                   # go environment (https://github.com/syndbg/goenv)
    nodenv                  # node.js version from nodenv (https://github.com/nodenv/nodenv)
    nvm                     # node.js version from nvm (https://github.com/nvm-sh/nvm)
    nodeenv                 # node.js environment (https://github.com/ekalinin/nodeenv)
    # node_version          # node.js version
    # go_version            # go version (https://golang.org)
    # rust_version          # rustc version (https://www.rust-lang.org)
    # dotnet_version        # .NET version (https://dotnet.microsoft.com)
    # php_version           # php version (https://www.php.net/)
    # laravel_version       # laravel php framework version (https://laravel.com/)
    # java_version          # java version (https://www.java.com/)
    # package               # [email protected] from package.json (https://docs.npmjs.com/files/package.json)
    rbenv                   # ruby version from rbenv (https://github.com/rbenv/rbenv)
    rvm                     # ruby version from rvm (https://rvm.io)
    fvm                     # flutter version management (https://github.com/leoafarias/fvm)
    luaenv                  # lua version from luaenv (https://github.com/cehoffman/luaenv)
    jenv                    # java version from jenv (https://github.com/jenv/jenv)
    plenv                   # perl version from plenv (https://github.com/tokuhirom/plenv)
    phpenv                  # php version from phpenv (https://github.com/phpenv/phpenv)
    scalaenv                # scala version from scalaenv (https://github.com/scalaenv/scalaenv)
    haskell_stack           # haskell version from stack (https://haskellstack.org/)
    # ----- 右側には表示されないように削除する -----
    # kubecontext             # current kubernetes context (https://kubernetes.io/)
    terraform               # terraform workspace (https://www.terraform.io)
    # terraform_version     # terraform version (https://www.terraform.io)
    aws                     # aws profile (https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html)
    aws_eb_env              # aws elastic beanstalk environment (https://aws.amazon.com/elasticbeanstalk/)
    azure                   # azure account name (https://docs.microsoft.com/en-us/cli/azure)
    gcloud                  # google cloud cli account and project (https://cloud.google.com/)
    google_app_cred         # google application credentials (https://cloud.google.com/docs/authentication/production)
    toolbox                 # toolbox name (https://github.com/containers/toolbox)
    context                 # [email protected]
    nordvpn                 # nordvpn connection status, linux only (https://nordvpn.com/)
    ranger                  # ranger shell (https://github.com/ranger/ranger)
    nnn                     # nnn shell (https://github.com/jarun/nnn)
    xplr                    # xplr shell (https://github.com/sayanarijit/xplr)
    vim_shell               # vim shell indicator (:sh)
    midnight_commander      # midnight commander shell (https://midnight-commander.org/)
    nix_shell               # nix shell (https://nixos.org/nixos/nix-pills/developing-with-nix-shell.html)
    # vi_mode               # vi mode (you don't need this if you've enabled prompt_char)
    # vpn_ip                # virtual private network indicator
    # load                  # CPU load
    # disk_usage            # disk usage
    # ram                   # free RAM
    # swap                  # used swap
    todo                    # todo items (https://github.com/todotxt/todo.txt-cli)
    timewarrior             # timewarrior tracking status (https://timewarrior.net/)
    taskwarrior             # taskwarrior task count (https://taskwarrior.org/)
    time                    # current time
    # =========================[ Line #2 ]=========================
    newline                 # \n
    # ip                    # ip address and bandwidth usage for a specified network interface
    # public_ip             # public IP address
    # proxy                 # system-wide http/https/ftp proxy
    # battery               # internal battery
    # wifi                  # wifi speed
    # example               # example user-defined segment (see prompt_example function below)
  )

...

改行(newline)を入れれば2行にして表示したりすることもできます。

カレントディレクトリを短縮して表示する

コンテキストを左側に表示したことでプロンプトが長くなってきたので、カレントディレクトリまでのパスを短縮して表示されるようにします。

下記のようにPOWERLEVEL9K_DIR_MAX_LENGTHを0にすることで、常にパスを短縮して表示してくれます。

...

  # Shorten directory if it's longer than this even if there is space for it. The value can
  # be either absolute (e.g., '80') or a percentage of terminal width (e.g, '50%'). If empty,
  # directory will be shortened only when prompt doesn't fit or when other parameters demand it
  # (see POWERLEVEL9K_DIR_MIN_COMMAND_COLUMNS and POWERLEVEL9K_DIR_MIN_COMMAND_COLUMNS_PCT below).
  # If set to `0`, directory will always be shortened to its minimum length.
  # ----- 0にします -----
  typeset -g POWERLEVEL9K_DIR_MAX_LENGTH=0

...

本番環境とそれ以外で色を変える

本番環境のコンテキストであるときにすぐにわかるように、コンテキストが表示される色を変えたいと思います。

下記のようにPOWERLEVEL9K_KUBECONTEXT_CLASSESにコンテキストの種類を定義して、それぞれで表示される内容を設定することができます。

デフォルトではコンテキストとNamespaceの色が同じになっていたので、別々の色になるように無理やり色を変えています。

...

  typeset -g POWERLEVEL9K_KUBECONTEXT_CLASSES=(
      # ----- prodという名前が含まれているコンテキストへの設定 -----
      '*prod*'  PROD
      # ----- それ以外への設定 -----
      '*'       DEFAULT)
  typeset -g POWERLEVEL9K_KUBECONTEXT_DEFAULT_FOREGROUND=134
  # typeset -g POWERLEVEL9K_KUBECONTEXT_DEFAULT_VISUAL_IDENTIFIER_EXPANSION='⭐'

...

  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+='%45F${P9K_KUBECONTEXT_CLOUD_CLUSTER:-${P9K_KUBECONTEXT_NAME}}'
  # Append the current context's namespace if it's not "default".
  POWERLEVEL9K_KUBECONTEXT_DEFAULT_CONTENT_EXPANSION+='%248F|%46F${${:-$P9K_KUBECONTEXT_NAMESPACE}:-default}'

  # ----- PRODへの設定 -----
  typeset -g POWERLEVEL9K_KUBECONTEXT_PROD_FOREGROUND=134
  typeset -g POWERLEVEL9K_KUBECONTEXT_PROD_CONTENT_EXPANSION=
  POWERLEVEL9K_KUBECONTEXT_PROD_CONTENT_EXPANSION+='%196F${P9K_KUBECONTEXT_CLOUD_CLUSTER:-${P9K_KUBECONTEXT_NAME}}'
  POWERLEVEL9K_KUBECONTEXT_PROD_CONTENT_EXPANSION+='%248F|%207F${${:-$P9K_KUBECONTEXT_NAMESPACE}:-default}'

...

色については下記のコマンドを実行して、自分好みの色を選択してください。

for i in {0..255}; do print -Pn "%K{$i}  %k%F{$i}${(l:3::0:)i}%f " ${${(M)$((i%6)):#3}:+$'\n'}; done

完成系

設定した内容で下記のようになりました。

dotfiles

dotfilesもあるので、参考にしてください。

GitHub - monda00/dotfiles: my dotfiles
my dotfiles. Contribute to monda00/dotfiles development by creating an account on GitHub.

参考

タイトルとURLをコピーしました