123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- if [ "$__prompt_themed" = "true" ]; then
- return
- fi
- autoload -U colors && colors
- setopt promptsubst
- setopt promptpercent
- local time='%F{cyan}%B[%D{%H:%M}]%b%f'
- local user='%F{yellow}%B%n%b%f'
- local host='%F{blue}%B%M%b%f'
- local dir='%F{yellow}%~%f'
- local git='$(git_prompt_string)'
- local nix='$(nix_shell_prompt_string)'
- local k8s='$(k8s_prompt_string)'
- local right=''
- if [ -n "$IN_NIX_SHELL" ]; then
- right+="$nix"
- fi
- if [ -n "$IN_K8S" ]; then
- right+="$k8s"
- fi
- local error='%F{red}%B%?%b%f'
- PROMPT="$time $user@$host $dir $git%# "
- RPROMPT="%(?#$right#$error)"
- # Show Git branch/tag, or name-rev if on detached head
- parse_git_branch() {
- (git symbolic-ref -q HEAD || git rev-parse --short HEAD) 2> /dev/null
- }
- # Show different symbols as appropriate for various Git repository states
- parse_git_state() {
- # Compose this value via multiple conditional appends.
- local GIT_STATE=""
- local GIT_CHANGED="$(git diff --name-only --diff-filter=ACDMRT | wc -l | tr -d ' ')"
- local GIT_STAGED="$(git diff --staged --name-status --diff-filter=ACDMRT | wc -l | tr -d ' ')"
- local GIT_CONFLICTS="$(git diff --staged --name-status --diff-filter=U | wc -l | tr -d ' ')"
- local GIT_UNTRACKED="$(git ls-files --other --exclude-standard | sed q | wc -l | tr -d ' ')"
- if [ $GIT_STAGED -ne 0 ]; then
- GIT_STATE="$GIT_STATE%F{green}+$GIT_STAGED%f"
- fi
- if [ $GIT_CONFLICTS -ne 0 ]; then
- GIT_STATE="$GIT_STATE%F{red}x$GIT_CONFLICTS%f"
- fi
- if [ $GIT_CHANGED -ne 0 ]; then
- GIT_STATE="$GIT_STATE%F{red}*$GIT_CHANGED%f"
- fi
- if [ $GIT_UNTRACKED -ne 0 ]; then
- GIT_STATE="$GIT_STATE.."
- fi
- if [ $GIT_CHANGED -eq 0 -a $GIT_STAGED -eq 0 -a $GIT_CONFLICTS -eq 0 -a $GIT_UNTRACKED -eq 0 ]; then
- GIT_STATE="$GIT_STATE%B%F{green}ok%f%b"
- fi
- if [[ -n $GIT_STATE ]]; then
- echo $GIT_STATE
- fi
- }
- # If inside a Git repository, print its branch and state
- git_prompt_string() {
- local git_where="$(parse_git_branch)"
- if [ -n "$git_where" ]; then
- git_where="%B%F{magenta}${git_where#(refs/heads/|tags/)}%f%b"
- local NUM_AHEAD="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d ' ')"
- if [ "$NUM_AHEAD" -gt 0 ]; then
- git_where="$git_where^$NUM_AHEAD"
- fi
- local NUM_BEHIND="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d ' ')"
- if [ "$NUM_BEHIND" -gt 0 ]; then
- git_where=$git_where"v$NUM_BEHIND"
- fi
- echo "($git_where|$(parse_git_state))"
- fi
- }
- nix_shell_prompt_string() {
- if [ -n "$NIX_SHELL_NAME" ]; then
- echo "[%F{yellow}nix%f:%F{green}$NIX_SHELL_NAME%f]"
- else
- echo "[%F{yellow}nix%f]"
- fi
- }
- k8s_prompt_string() {
- local k8s_context=$(kubectl config current-context)
- local k8s_namespace=$(kubectl config get-contexts --no-headers $k8s_context | awk '{ print $5 }')
- echo "[%F{magenta}$k8s_context%f|%F{blue}$k8s_namespace%f]"
- }
|