git-prompt.plugin.zsh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # ZSH Git Prompt Plugin from:
  2. # http://github.com/olivierverdier/zsh-git-prompt
  3. #
  4. export __GIT_PROMPT_DIR=$ZSH/plugins/git-prompt
  5. # Initialize colors.
  6. autoload -U colors
  7. colors
  8. # Allow for functions in the prompt.
  9. setopt PROMPT_SUBST
  10. ## Enable auto-execution of functions.
  11. typeset -ga preexec_functions
  12. typeset -ga precmd_functions
  13. typeset -ga chpwd_functions
  14. # Append git functions needed for prompt.
  15. preexec_functions+='preexec_update_git_vars'
  16. precmd_functions+='precmd_update_git_vars'
  17. chpwd_functions+='chpwd_update_git_vars'
  18. ## Function definitions
  19. function preexec_update_git_vars() {
  20. case "$2" in
  21. git*)
  22. __EXECUTED_GIT_COMMAND=1
  23. ;;
  24. esac
  25. }
  26. function precmd_update_git_vars() {
  27. if [ -n "$__EXECUTED_GIT_COMMAND" ]; then
  28. update_current_git_vars
  29. unset __EXECUTED_GIT_COMMAND
  30. fi
  31. }
  32. function chpwd_update_git_vars() {
  33. update_current_git_vars
  34. }
  35. function update_current_git_vars() {
  36. unset __CURRENT_GIT_STATUS
  37. local gitstatus="$__GIT_PROMPT_DIR/gitstatus.py"
  38. _GIT_STATUS=`python ${gitstatus}`
  39. __CURRENT_GIT_STATUS=("${(f)_GIT_STATUS}")
  40. }
  41. function prompt_git_info() {
  42. if [ -n "$__CURRENT_GIT_STATUS" ]; then
  43. echo "(%{${fg[red]}%}$__CURRENT_GIT_STATUS[1]%{${fg[default]}%}$__CURRENT_GIT_STATUS[2]%{${fg[magenta]}%}$__CURRENT_GIT_STATUS[3]%{${fg[default]}%})"
  44. fi
  45. }
  46. # Set the prompt.
  47. #PROMPT='%B%m%~%b$(prompt_git_info) %# '
  48. # for a right prompt:
  49. #RPROMPT='%b$(prompt_git_info)'
  50. RPROMPT='$(prompt_git_info)'