From 40b2f97a40977f5637e0637cb97b7a706946fcab Mon Sep 17 00:00:00 2001 From: Chris Cowley Date: Tue, 23 Oct 2018 10:23:53 +0200 Subject: [PATCH 1/3] Added support for kubectl context --- bash-powerline.sh | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/bash-powerline.sh b/bash-powerline.sh index eb6eadc..d42644d 100644 --- a/bash-powerline.sh +++ b/bash-powerline.sh @@ -1,12 +1,14 @@ #!/usr/bin/env bash ## Uncomment to disable git info +#POWERLINE_KUBE=0 #POWERLINE_GIT=0 __powerline() { # Colorscheme readonly RESET='\[\033[m\]' readonly COLOR_CWD='\[\033[0;34m\]' # blue + readonly COLOR_KUBE='\[\033[0;35m\]' # light blue readonly COLOR_GIT='\[\033[0;36m\]' # cyan readonly COLOR_SUCCESS='\[\033[0;32m\]' # green readonly COLOR_FAILURE='\[\033[0;31m\]' # red @@ -16,6 +18,8 @@ __powerline() { readonly SYMBOL_GIT_PUSH='↑' readonly SYMBOL_GIT_PULL='↓' + readonly SYMBOL_KUBE='❆' + if [[ -z "$PS_SYMBOL" ]]; then case "$(uname)" in Darwin) PS_SYMBOL='';; @@ -24,6 +28,14 @@ __powerline() { esac fi + __kube_info() { + [[ $POWERLINE_KUBE = 0 ]] && return 0 + hash kubectl 2>/dev/null || return # kubectl not found + + # get current context + local ref=$(kubectl config current-context) + printf " $SYMBOL_KUBE $ref" + } __git_info() { [[ $POWERLINE_GIT = 0 ]] && return # disabled hash git 2>/dev/null || return # git not found @@ -75,14 +87,17 @@ __powerline() { # POC: https://github.com/njhartwell/pw3nage # Related fix in git-bash: https://github.com/git/git/blob/9d77b0405ce6b471cb5ce3a904368fc25e55643d/contrib/completion/git-prompt.sh#L324 if shopt -q promptvars; then + __powerline_kube_info="$(__kube_info)" __powerline_git_info="$(__git_info)" + local kube="$COLOR_KUBE\${__powerline_kube_info}$RESET" local git="$COLOR_GIT\${__powerline_git_info}$RESET" else # promptvars is disabled. Avoid creating unnecessary env var. + local kube="$COLOR_KUBE$(__kube_info)$RESET" local git="$COLOR_GIT$(__git_info)$RESET" fi - PS1="$cwd$git$symbol" + PS1="$cwd$kube$git\n$symbol" } PROMPT_COMMAND="ps1${PROMPT_COMMAND:+; $PROMPT_COMMAND}" From 8862320032628bcbe84b5c75ba72732cfd0584e7 Mon Sep 17 00:00:00 2001 From: Chris Cowley Date: Tue, 23 Oct 2018 10:25:10 +0200 Subject: [PATCH 2/3] Added kubectl note to README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f857b04..8a28f63 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Powerline for Bash in pure Bash script. * Git: show "*" symbol with uncommited modifications. * Git: show "↑" symbol and number of commits ahead of remote. * Git: show "↓" symbol and number of commits behind remote. +* Kubernetes: Show current-context * Platform-dependent prompt symbols. * Color-coded prompt symbol according to previous command execution status. * Use Bash builtin when possible to reduce delay. Delay sucks! From a103f58d399965c41ab0601e190180107d5d0740 Mon Sep 17 00:00:00 2001 From: Chris Cowley Date: Tue, 23 Oct 2018 10:32:52 +0200 Subject: [PATCH 3/3] Control kubectl support --- README.md | 6 ++++++ bash-powerline.sh | 7 +++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8a28f63..ca4dbee 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,12 @@ And source it in your `.bashrc` source ~/.bash-powerline.sh +To enable `kubectl` context support, add the `POWERLINE_KUBE` variable to your +profile before sourcing the script: + + POWERLINE_KUBE=1 + source ~/.bash-powerline.sh + For best result, use [Solarized colorscheme](https://github.com/altercation/solarized) for your terminal emulator. Or hack your own colorscheme by modifying the script. It's really diff --git a/bash-powerline.sh b/bash-powerline.sh index d42644d..2f12813 100644 --- a/bash-powerline.sh +++ b/bash-powerline.sh @@ -1,14 +1,17 @@ #!/usr/bin/env bash +if [ -z ${POWERLINE_KUBE+x} ] +then + POWERLINE_KUBE=0 +fi ## Uncomment to disable git info -#POWERLINE_KUBE=0 #POWERLINE_GIT=0 __powerline() { # Colorscheme readonly RESET='\[\033[m\]' readonly COLOR_CWD='\[\033[0;34m\]' # blue - readonly COLOR_KUBE='\[\033[0;35m\]' # light blue + readonly COLOR_KUBE='\[\033[0;35m\]' # violet readonly COLOR_GIT='\[\033[0;36m\]' # cyan readonly COLOR_SUCCESS='\[\033[0;32m\]' # green readonly COLOR_FAILURE='\[\033[0;31m\]' # red