git-prompt.plugin.zsh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Handle $0 according to the standard:
  2. # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
  3. 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
  4. 0="${${(M)0:#/*}:-$PWD/$0}"
  5. __GIT_PROMPT_DIR="${0:A:h}"
  6. ## Hook function definitions
  7. function chpwd_update_git_vars() {
  8. update_current_git_vars
  9. }
  10. function preexec_update_git_vars() {
  11. case "$2" in
  12. git*|hub*|gh*|stg*)
  13. __EXECUTED_GIT_COMMAND=1
  14. ;;
  15. esac
  16. }
  17. function precmd_update_git_vars() {
  18. if [ -n "$__EXECUTED_GIT_COMMAND" ] || [ ! -n "$ZSH_THEME_GIT_PROMPT_CACHE" ]; then
  19. update_current_git_vars
  20. unset __EXECUTED_GIT_COMMAND
  21. fi
  22. }
  23. autoload -U add-zsh-hook
  24. add-zsh-hook chpwd chpwd_update_git_vars
  25. add-zsh-hook precmd precmd_update_git_vars
  26. add-zsh-hook preexec preexec_update_git_vars
  27. ## Function definitions
  28. function update_current_git_vars() {
  29. unset __CURRENT_GIT_STATUS
  30. local gitstatus="$__GIT_PROMPT_DIR/gitstatus.py"
  31. _GIT_STATUS=$(python3 ${gitstatus} 2>/dev/null)
  32. __CURRENT_GIT_STATUS=("${(@s: :)_GIT_STATUS}")
  33. GIT_BRANCH=$__CURRENT_GIT_STATUS[1]
  34. GIT_AHEAD=$__CURRENT_GIT_STATUS[2]
  35. GIT_BEHIND=$__CURRENT_GIT_STATUS[3]
  36. GIT_STAGED=$__CURRENT_GIT_STATUS[4]
  37. GIT_CONFLICTS=$__CURRENT_GIT_STATUS[5]
  38. GIT_CHANGED=$__CURRENT_GIT_STATUS[6]
  39. GIT_UNTRACKED=$__CURRENT_GIT_STATUS[7]
  40. GIT_STASHED=$__CURRENT_GIT_STATUS[8]
  41. GIT_CLEAN=$__CURRENT_GIT_STATUS[9]
  42. GIT_DELETED=$__CURRENT_GIT_STATUS[10]
  43. if [ -z ${ZSH_THEME_GIT_SHOW_UPSTREAM+x} ]; then
  44. GIT_UPSTREAM=
  45. else
  46. GIT_UPSTREAM=$(git rev-parse --abbrev-ref --symbolic-full-name "@{upstream}" 2>/dev/null) && GIT_UPSTREAM="${ZSH_THEME_GIT_PROMPT_UPSTREAM_SEPARATOR}${GIT_UPSTREAM}"
  47. fi
  48. }
  49. git_super_status() {
  50. precmd_update_git_vars
  51. if [ -n "$__CURRENT_GIT_STATUS" ]; then
  52. STATUS="$ZSH_THEME_GIT_PROMPT_PREFIX$ZSH_THEME_GIT_PROMPT_BRANCH$GIT_BRANCH$GIT_UPSTREAM%{${reset_color}%}"
  53. if [ "$GIT_BEHIND" -ne "0" ]; then
  54. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_BEHIND$GIT_BEHIND%{${reset_color}%}"
  55. fi
  56. if [ "$GIT_AHEAD" -ne "0" ]; then
  57. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_AHEAD$GIT_AHEAD%{${reset_color}%}"
  58. fi
  59. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_SEPARATOR"
  60. if [ "$GIT_STAGED" -ne "0" ]; then
  61. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_STAGED$GIT_STAGED%{${reset_color}%}"
  62. fi
  63. if [ "$GIT_CONFLICTS" -ne "0" ]; then
  64. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CONFLICTS$GIT_CONFLICTS%{${reset_color}%}"
  65. fi
  66. if [ "$GIT_CHANGED" -ne "0" ]; then
  67. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CHANGED$GIT_CHANGED%{${reset_color}%}"
  68. fi
  69. if [ "$GIT_DELETED" -ne "0" ]; then
  70. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_DELETED$GIT_DELETED%{${reset_color}%}"
  71. fi
  72. if [ "$GIT_UNTRACKED" -ne "0" ]; then
  73. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_UNTRACKED$GIT_UNTRACKED%{${reset_color}%}"
  74. fi
  75. if [ "$GIT_STASHED" -ne "0" ]; then
  76. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_STASHED$GIT_STASHED%{${reset_color}%}"
  77. fi
  78. if [ "$GIT_CLEAN" -eq "1" ]; then
  79. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CLEAN"
  80. fi
  81. STATUS="$STATUS%{${reset_color}%}$ZSH_THEME_GIT_PROMPT_SUFFIX"
  82. echo "$STATUS"
  83. fi
  84. }
  85. # Default values for the appearance of the prompt.
  86. ZSH_THEME_GIT_PROMPT_PREFIX="("
  87. ZSH_THEME_GIT_PROMPT_SUFFIX=")"
  88. ZSH_THEME_GIT_PROMPT_SEPARATOR="|"
  89. ZSH_THEME_GIT_PROMPT_BRANCH="%{$fg_bold[magenta]%}"
  90. ZSH_THEME_GIT_PROMPT_STAGED="%{$fg[red]%}%{●%G%}"
  91. ZSH_THEME_GIT_PROMPT_CONFLICTS="%{$fg[red]%}%{✖%G%}"
  92. ZSH_THEME_GIT_PROMPT_CHANGED="%{$fg[blue]%}%{✚%G%}"
  93. ZSH_THEME_GIT_PROMPT_DELETED="%{$fg[blue]%}%{-%G%}"
  94. ZSH_THEME_GIT_PROMPT_BEHIND="%{↓%G%}"
  95. ZSH_THEME_GIT_PROMPT_AHEAD="%{↑%G%}"
  96. ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg[cyan]%}%{…%G%}"
  97. ZSH_THEME_GIT_PROMPT_STASHED="%{$fg_bold[blue]%}%{⚑%G%}"
  98. ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg_bold[green]%}%{✔%G%}"
  99. ZSH_THEME_GIT_PROMPT_UPSTREAM_SEPARATOR="->"
  100. # Set the prompt.
  101. RPROMPT='$(git_super_status)'