git-prompt.plugin.zsh 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. chpwd_functions+=(chpwd_update_git_vars)
  20. precmd_functions+=(precmd_update_git_vars)
  21. preexec_functions+=(preexec_update_git_vars)
  22. ## Function definitions
  23. function update_current_git_vars() {
  24. unset __CURRENT_GIT_STATUS
  25. local gitstatus="$__GIT_PROMPT_DIR/gitstatus.py"
  26. _GIT_STATUS=$(python ${gitstatus} 2>/dev/null)
  27. __CURRENT_GIT_STATUS=("${(@s: :)_GIT_STATUS}")
  28. GIT_BRANCH=$__CURRENT_GIT_STATUS[1]
  29. GIT_AHEAD=$__CURRENT_GIT_STATUS[2]
  30. GIT_BEHIND=$__CURRENT_GIT_STATUS[3]
  31. GIT_STAGED=$__CURRENT_GIT_STATUS[4]
  32. GIT_CONFLICTS=$__CURRENT_GIT_STATUS[5]
  33. GIT_CHANGED=$__CURRENT_GIT_STATUS[6]
  34. GIT_UNTRACKED=$__CURRENT_GIT_STATUS[7]
  35. }
  36. git_super_status() {
  37. precmd_update_git_vars
  38. if [ -n "$__CURRENT_GIT_STATUS" ]; then
  39. STATUS="$ZSH_THEME_GIT_PROMPT_PREFIX$ZSH_THEME_GIT_PROMPT_BRANCH$GIT_BRANCH%{${reset_color}%}"
  40. if [ "$GIT_BEHIND" -ne "0" ]; then
  41. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_BEHIND$GIT_BEHIND%{${reset_color}%}"
  42. fi
  43. if [ "$GIT_AHEAD" -ne "0" ]; then
  44. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_AHEAD$GIT_AHEAD%{${reset_color}%}"
  45. fi
  46. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_SEPARATOR"
  47. if [ "$GIT_STAGED" -ne "0" ]; then
  48. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_STAGED$GIT_STAGED%{${reset_color}%}"
  49. fi
  50. if [ "$GIT_CONFLICTS" -ne "0" ]; then
  51. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CONFLICTS$GIT_CONFLICTS%{${reset_color}%}"
  52. fi
  53. if [ "$GIT_CHANGED" -ne "0" ]; then
  54. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CHANGED$GIT_CHANGED%{${reset_color}%}"
  55. fi
  56. if [ "$GIT_UNTRACKED" -ne "0" ]; then
  57. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_UNTRACKED%{${reset_color}%}"
  58. fi
  59. if [ "$GIT_CHANGED" -eq "0" ] && [ "$GIT_CONFLICTS" -eq "0" ] && [ "$GIT_STAGED" -eq "0" ] && [ "$GIT_UNTRACKED" -eq "0" ]; then
  60. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CLEAN"
  61. fi
  62. STATUS="$STATUS%{${reset_color}%}$ZSH_THEME_GIT_PROMPT_SUFFIX"
  63. echo "$STATUS"
  64. fi
  65. }
  66. # Default values for the appearance of the prompt.
  67. ZSH_THEME_GIT_PROMPT_PREFIX="("
  68. ZSH_THEME_GIT_PROMPT_SUFFIX=")"
  69. ZSH_THEME_GIT_PROMPT_SEPARATOR="|"
  70. ZSH_THEME_GIT_PROMPT_BRANCH="%{$fg_bold[magenta]%}"
  71. ZSH_THEME_GIT_PROMPT_STAGED="%{$fg[red]%}%{●%G%}"
  72. ZSH_THEME_GIT_PROMPT_CONFLICTS="%{$fg[red]%}%{✖%G%}"
  73. ZSH_THEME_GIT_PROMPT_CHANGED="%{$fg[blue]%}%{✚%G%}"
  74. ZSH_THEME_GIT_PROMPT_BEHIND="%{↓%G%}"
  75. ZSH_THEME_GIT_PROMPT_AHEAD="%{↑%G%}"
  76. ZSH_THEME_GIT_PROMPT_UNTRACKED="%{…%G%}"
  77. ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg_bold[green]%}%{✔%G%}"
  78. # Set the prompt.
  79. RPROMPT='$(git_super_status)'