git-prompt.plugin.zsh 3.0 KB

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