prompt.zsh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. autoload -U colors && colors
  2. setopt promptsubst
  3. setopt promptpercent
  4. local time='%F{cyan}%B[%D{%H:%M}]%b%f'
  5. local user='%F{yellow}%B%n%b%f'
  6. local host='%F{blue}%B%M%b%f'
  7. local dir='%F{yellow}%~%f'
  8. local git='$(git_prompt_string)'
  9. local error='%F{red}%B%(?..%?)%b%f'
  10. PROMPT="$time $user@$host $dir $git%# "
  11. RPROMPT="$error"
  12. # Show Git branch/tag, or name-rev if on detached head
  13. parse_git_branch() {
  14. (git symbolic-ref -q HEAD || git rev-parse --short HEAD) 2> /dev/null
  15. }
  16. # Show different symbols as appropriate for various Git repository states
  17. parse_git_state() {
  18. # Compose this value via multiple conditional appends.
  19. local GIT_STATE=""
  20. local GIT_CHANGED="$(git diff --name-only --diff-filter=ACDMRT | wc -l | tr -d ' ')"
  21. local GIT_STAGED="$(git diff --staged --name-status --diff-filter=ACDMRT | wc -l | tr -d ' ')"
  22. local GIT_CONFLICTS="$(git diff --staged --name-status --diff-filter=U | wc -l | tr -d ' ')"
  23. local GIT_UNTRACKED="$(git ls-files --other --exclude-standard | wc -l | tr -d ' ')"
  24. if [ $GIT_STAGED -ne 0 ]; then
  25. GIT_STATE="$GIT_STATE%F{green}+$GIT_STAGED%f"
  26. fi
  27. if [ $GIT_CONFLICTS -ne 0 ]; then
  28. GIT_STATE="$GIT_STATE%F{red}x$GIT_CONFLICTS%f"
  29. fi
  30. if [ $GIT_CHANGED -ne 0 ]; then
  31. GIT_STATE="$GIT_STATE%F{red}*$GIT_CHANGED%f"
  32. fi
  33. if [ $GIT_UNTRACKED -ne 0 ]; then
  34. GIT_STATE="$GIT_STATE.."
  35. fi
  36. if [ $GIT_CHANGED -eq 0 -a $GIT_STAGED -eq 0 -a $GIT_CONFLICTS -eq 0 -a $GIT_UNTRACKED -eq 0 ]; then
  37. GIT_STATE="$GIT_STATE%B%F{green}ok%f%b"
  38. fi
  39. if [[ -n $GIT_STATE ]]; then
  40. echo $GIT_STATE
  41. fi
  42. }
  43. # If inside a Git repository, print its branch and state
  44. git_prompt_string() {
  45. local git_where="$(parse_git_branch)"
  46. if [ -n "$git_where" ]; then
  47. git_where="%B%F{magenta}${git_where#(refs/heads/|tags/)}%f%b"
  48. local NUM_AHEAD="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d ' ')"
  49. if [ "$NUM_AHEAD" -gt 0 ]; then
  50. git_where="$git_where^$NUM_AHEAD"
  51. fi
  52. local NUM_BEHIND="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d ' ')"
  53. if [ "$NUM_BEHIND" -gt 0 ]; then
  54. git_where=$git_where"v$NUM_BEHIND"
  55. fi
  56. echo "($git_where|$(parse_git_state))"
  57. fi
  58. }