git-prompt.plugin.zsh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. __GIT_PROMPT_DIR="${0:A:h}"
  2. ## Hook function definitions
  3. function chpwd_update_git_vars() {
  4. update_current_git_vars
  5. }
  6. function preexec_update_git_vars() {
  7. case "$2" in
  8. git*|hub*|gh*|stg*)
  9. __EXECUTED_GIT_COMMAND=1
  10. ;;
  11. esac
  12. }
  13. function precmd_update_git_vars() {
  14. if [ -n "$__EXECUTED_GIT_COMMAND" ] || [ ! -n "$ZSH_THEME_GIT_PROMPT_CACHE" ]; then
  15. update_current_git_vars
  16. unset __EXECUTED_GIT_COMMAND
  17. fi
  18. }
  19. autoload -U add-zsh-hook
  20. add-zsh-hook chpwd chpwd_update_git_vars
  21. add-zsh-hook precmd precmd_update_git_vars
  22. add-zsh-hook preexec preexec_update_git_vars
  23. ## Function definitions
  24. function update_current_git_vars() {
  25. unset __CURRENT_GIT_STATUS
  26. local gitstatus="$__GIT_PROMPT_DIR/gitstatus.py"
  27. _GIT_STATUS=$(python ${gitstatus} 2>/dev/null)
  28. __CURRENT_GIT_STATUS=("${(@s: :)_GIT_STATUS}")
  29. GIT_BRANCH=$__CURRENT_GIT_STATUS[1]
  30. GIT_AHEAD=$__CURRENT_GIT_STATUS[2]
  31. GIT_BEHIND=$__CURRENT_GIT_STATUS[3]
  32. GIT_STAGED=$__CURRENT_GIT_STATUS[4]
  33. GIT_CONFLICTS=$__CURRENT_GIT_STATUS[5]
  34. GIT_CHANGED=$__CURRENT_GIT_STATUS[6]
  35. GIT_UNTRACKED=$__CURRENT_GIT_STATUS[7]
  36. GIT_STASHED=$__CURRENT_GIT_STATUS[8]
  37. GIT_CLEAN=$__CURRENT_GIT_STATUS[9]
  38. }
  39. git_super_status() {
  40. precmd_update_git_vars
  41. if [ -n "$__CURRENT_GIT_STATUS" ]; then
  42. STATUS="$ZSH_THEME_GIT_PROMPT_PREFIX$ZSH_THEME_GIT_PROMPT_BRANCH$GIT_BRANCH%{${reset_color}%}"
  43. if [ "$GIT_BEHIND" -ne "0" ]; then
  44. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_BEHIND$GIT_BEHIND%{${reset_color}%}"
  45. fi
  46. if [ "$GIT_AHEAD" -ne "0" ]; then
  47. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_AHEAD$GIT_AHEAD%{${reset_color}%}"
  48. fi
  49. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_SEPARATOR"
  50. if [ "$GIT_STAGED" -ne "0" ]; then
  51. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_STAGED$GIT_STAGED%{${reset_color}%}"
  52. fi
  53. if [ "$GIT_CONFLICTS" -ne "0" ]; then
  54. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CONFLICTS$GIT_CONFLICTS%{${reset_color}%}"
  55. fi
  56. if [ "$GIT_CHANGED" -ne "0" ]; then
  57. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CHANGED$GIT_CHANGED%{${reset_color}%}"
  58. fi
  59. if [ "$GIT_UNTRACKED" -ne "0" ]; then
  60. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_UNTRACKED$GIT_UNTRACKED%{${reset_color}%}"
  61. fi
  62. if [ "$GIT_STASHED" -ne "0" ]; then
  63. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_STASHED$GIT_STASHED%{${reset_color}%}"
  64. fi
  65. if [ "$GIT_CLEAN" -eq "1" ]; then
  66. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CLEAN"
  67. fi
  68. STATUS="$STATUS%{${reset_color}%}$ZSH_THEME_GIT_PROMPT_SUFFIX"
  69. echo "$STATUS"
  70. fi
  71. }
  72. # Default values for the appearance of the prompt.
  73. ZSH_THEME_GIT_PROMPT_PREFIX="("
  74. ZSH_THEME_GIT_PROMPT_SUFFIX=")"
  75. ZSH_THEME_GIT_PROMPT_SEPARATOR="|"
  76. ZSH_THEME_GIT_PROMPT_BRANCH="%{$fg_bold[magenta]%}"
  77. ZSH_THEME_GIT_PROMPT_STAGED="%{$fg[red]%}%{●%G%}"
  78. ZSH_THEME_GIT_PROMPT_CONFLICTS="%{$fg[red]%}%{✖%G%}"
  79. ZSH_THEME_GIT_PROMPT_CHANGED="%{$fg[blue]%}%{✚%G%}"
  80. ZSH_THEME_GIT_PROMPT_BEHIND="%{↓%G%}"
  81. ZSH_THEME_GIT_PROMPT_AHEAD="%{↑%G%}"
  82. ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg[cyan]%}%{…%G%}"
  83. ZSH_THEME_GIT_PROMPT_STASHED="%{$fg_bold[blue]%}%{⚑%G%}"
  84. ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg_bold[green]%}%{✔%G%}"
  85. # Set the prompt.
  86. RPROMPT='$(git_super_status)'