git-prompt.plugin.zsh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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=$(python ${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. }
  43. git_super_status() {
  44. precmd_update_git_vars
  45. if [ -n "$__CURRENT_GIT_STATUS" ]; then
  46. STATUS="$ZSH_THEME_GIT_PROMPT_PREFIX$ZSH_THEME_GIT_PROMPT_BRANCH$GIT_BRANCH%{${reset_color}%}"
  47. if [ "$GIT_BEHIND" -ne "0" ]; then
  48. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_BEHIND$GIT_BEHIND%{${reset_color}%}"
  49. fi
  50. if [ "$GIT_AHEAD" -ne "0" ]; then
  51. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_AHEAD$GIT_AHEAD%{${reset_color}%}"
  52. fi
  53. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_SEPARATOR"
  54. if [ "$GIT_STAGED" -ne "0" ]; then
  55. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_STAGED$GIT_STAGED%{${reset_color}%}"
  56. fi
  57. if [ "$GIT_CONFLICTS" -ne "0" ]; then
  58. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CONFLICTS$GIT_CONFLICTS%{${reset_color}%}"
  59. fi
  60. if [ "$GIT_CHANGED" -ne "0" ]; then
  61. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CHANGED$GIT_CHANGED%{${reset_color}%}"
  62. fi
  63. if [ "$GIT_UNTRACKED" -ne "0" ]; then
  64. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_UNTRACKED$GIT_UNTRACKED%{${reset_color}%}"
  65. fi
  66. if [ "$GIT_STASHED" -ne "0" ]; then
  67. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_STASHED$GIT_STASHED%{${reset_color}%}"
  68. fi
  69. if [ "$GIT_CLEAN" -eq "1" ]; then
  70. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CLEAN"
  71. fi
  72. STATUS="$STATUS%{${reset_color}%}$ZSH_THEME_GIT_PROMPT_SUFFIX"
  73. echo "$STATUS"
  74. fi
  75. }
  76. # Default values for the appearance of the prompt.
  77. ZSH_THEME_GIT_PROMPT_PREFIX="("
  78. ZSH_THEME_GIT_PROMPT_SUFFIX=")"
  79. ZSH_THEME_GIT_PROMPT_SEPARATOR="|"
  80. ZSH_THEME_GIT_PROMPT_BRANCH="%{$fg_bold[magenta]%}"
  81. ZSH_THEME_GIT_PROMPT_STAGED="%{$fg[red]%}%{●%G%}"
  82. ZSH_THEME_GIT_PROMPT_CONFLICTS="%{$fg[red]%}%{✖%G%}"
  83. ZSH_THEME_GIT_PROMPT_CHANGED="%{$fg[blue]%}%{✚%G%}"
  84. ZSH_THEME_GIT_PROMPT_BEHIND="%{↓%G%}"
  85. ZSH_THEME_GIT_PROMPT_AHEAD="%{↑%G%}"
  86. ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg[cyan]%}%{…%G%}"
  87. ZSH_THEME_GIT_PROMPT_STASHED="%{$fg_bold[blue]%}%{⚑%G%}"
  88. ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg_bold[green]%}%{✔%G%}"
  89. # Set the prompt.
  90. RPROMPT='$(git_super_status)'