git-prompt.plugin.zsh 1.4 KB

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