prompt.zsh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. if [ "$__prompt_themed" = "true" ]; then
  2. return
  3. fi
  4. autoload -U colors && colors
  5. setopt promptsubst
  6. setopt promptpercent
  7. local time='%F{cyan}%B[%D{%H:%M}]%b%f'
  8. local user='%F{yellow}%B%n%b%f'
  9. local host='%F{blue}%B%M%b%f'
  10. local dir='%F{yellow}%~%f'
  11. local git='$(git_prompt_string)'
  12. local nix='$(nix_shell_prompt_string)'
  13. local k8s='$(k8s_prompt_string)'
  14. local right=''
  15. if [ -n "$IN_NIX_SHELL" ]; then
  16. right+="$nix"
  17. fi
  18. if [ -n "$IN_K8S" ]; then
  19. right+="$k8s"
  20. fi
  21. local error='%F{red}%B%?%b%f'
  22. PROMPT="$time $user@$host $dir $git%# "
  23. RPROMPT="%(?#$right#$error)"
  24. # Show Git branch/tag, or name-rev if on detached head
  25. parse_git_branch() {
  26. (git symbolic-ref -q HEAD || git rev-parse --short HEAD) 2> /dev/null
  27. }
  28. # Show different symbols as appropriate for various Git repository states
  29. parse_git_state() {
  30. # Compose this value via multiple conditional appends.
  31. local GIT_STATE=""
  32. local GIT_CHANGED="$(git diff --name-only --diff-filter=ACDMRT | wc -l | tr -d ' ')"
  33. local GIT_STAGED="$(git diff --staged --name-status --diff-filter=ACDMRT | wc -l | tr -d ' ')"
  34. local GIT_CONFLICTS="$(git diff --staged --name-status --diff-filter=U | wc -l | tr -d ' ')"
  35. local GIT_UNTRACKED="$(git ls-files --other --exclude-standard | sed q | wc -l | tr -d ' ')"
  36. if [ $GIT_STAGED -ne 0 ]; then
  37. GIT_STATE="$GIT_STATE%F{green}+$GIT_STAGED%f"
  38. fi
  39. if [ $GIT_CONFLICTS -ne 0 ]; then
  40. GIT_STATE="$GIT_STATE%F{red}x$GIT_CONFLICTS%f"
  41. fi
  42. if [ $GIT_CHANGED -ne 0 ]; then
  43. GIT_STATE="$GIT_STATE%F{red}*$GIT_CHANGED%f"
  44. fi
  45. if [ $GIT_UNTRACKED -ne 0 ]; then
  46. GIT_STATE="$GIT_STATE.."
  47. fi
  48. if [ $GIT_CHANGED -eq 0 -a $GIT_STAGED -eq 0 -a $GIT_CONFLICTS -eq 0 -a $GIT_UNTRACKED -eq 0 ]; then
  49. GIT_STATE="$GIT_STATE%B%F{green}ok%f%b"
  50. fi
  51. if [[ -n $GIT_STATE ]]; then
  52. echo $GIT_STATE
  53. fi
  54. }
  55. # If inside a Git repository, print its branch and state
  56. git_prompt_string() {
  57. local git_where="$(parse_git_branch)"
  58. if [ -n "$git_where" ]; then
  59. git_where="%B%F{magenta}${git_where#(refs/heads/|tags/)}%f%b"
  60. local NUM_AHEAD="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d ' ')"
  61. if [ "$NUM_AHEAD" -gt 0 ]; then
  62. git_where="$git_where^$NUM_AHEAD"
  63. fi
  64. local NUM_BEHIND="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d ' ')"
  65. if [ "$NUM_BEHIND" -gt 0 ]; then
  66. git_where=$git_where"v$NUM_BEHIND"
  67. fi
  68. echo "($git_where|$(parse_git_state))"
  69. fi
  70. }
  71. nix_shell_prompt_string() {
  72. if [ -n "$NIX_SHELL_NAME" ]; then
  73. echo "[%F{yellow}nix%f:%F{green}$NIX_SHELL_NAME%f]"
  74. else
  75. echo "[%F{yellow}nix%f]"
  76. fi
  77. }
  78. k8s_prompt_string() {
  79. local k8s_context=$(kubectl config current-context)
  80. local k8s_namespace=$(kubectl config get-contexts --no-headers $k8s_context | awk '{ print $5 }')
  81. echo "[%F{magenta}$k8s_context%f|%F{blue}$k8s_namespace%f]"
  82. }