Created
September 26, 2018 03:52
-
-
Save silenceshell/34dc4edbada81a8fcabf52fa67db5a92 to your computer and use it in GitHub Desktop.
fish prompt(ps1) with kubernetes server ip and namespace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fish_prompt --description 'Write out the prompt' | |
set laststatus $status | |
function _git_branch_name | |
echo (git symbolic-ref HEAD ^/dev/null | sed -e 's|^refs/heads/||') | |
end | |
function _is_git_dirty | |
echo (git status -s --ignore-submodules=dirty ^/dev/null) | |
end | |
function _k8s_cluster | |
echo (kubectl config view --minify --output 'jsonpath={..server}' 2>/dev/null | cut -d':' -f2 |cut -d'.' -f4) | |
end | |
if [ (_git_branch_name) ] | |
set -l git_branch (set_color -o blue)(_git_branch_name) | |
if [ (_is_git_dirty) ] | |
for i in (git branch -qv --no-color | string match -r '\*' | cut -d' ' -f4- | cut -d] -f1 | tr , \n)\ | |
(git status --porcelain | cut -c 1-2 | uniq) | |
switch $i | |
case "*[ahead *" | |
set git_status "$git_status"(set_color red)⬆ | |
case "*behind *" | |
set git_status "$git_status"(set_color red)⬇ | |
case "." | |
set git_status "$git_status"(set_color green)✚ | |
case " D" | |
set git_status "$git_status"(set_color red)✖ | |
case "*M*" | |
set git_status "$git_status"(set_color green)✱ | |
case "*R*" | |
set git_status "$git_status"(set_color purple)➜ | |
case "*U*" | |
set git_status "$git_status"(set_color brown)═ | |
case "??" | |
set git_status "$git_status"(set_color red)≠ | |
end | |
end | |
else | |
set git_status (set_color green): | |
end | |
set git_info "(git$git_status$git_branch"(set_color white)")" | |
end | |
set_color -b black | |
printf "%s%s-%s" (set_color -o blue) (_k8s_cluster) (kubectl config view --minify --output 'jsonpath={..namespace}' 2>/dev/null) | |
printf '%s%s%s%s%s%s%s%s%s%s%s' (set_color -o white) '' (set_color white) ' ' (set_color yellow) (echo $PWD | sed -e "s|^$HOME|~|") (set_color white) $git_info (set_color white) ' ' (set_color white) | |
if test $laststatus -eq 0 | |
printf "%s✔%s%s " (set_color -o green) (set_color white) (set_color normal) | |
else | |
printf "%s✘%s%s " (set_color -o red) (set_color white) (set_color normal) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment