prompt.zsh 2.7 KB

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