Guest User

Untitled

a guest
Jul 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #!/bin/bash
  2. # Git status in git projects....
  3. # in your .bashrc file:
  4. # if [ -f ~/.prompt ]; then
  5. # . ~/.prompt
  6. # fi
  7. RED="\[\033[0;31m\]"
  8. YELLOW="\[\033[0;33m\]"
  9. GREEN="\[\033[0;32m\]"
  10. BLUE="\[\033[0;34m\]"
  11. LIGHT_RED="\[\033[1;31m\]"
  12. LIGHT_GREEN="\[\033[1;32m\]"
  13. WHITE="\[\033[1;37m\]"
  14. LIGHT_GRAY="\[\033[0;37m\]"
  15. COLOR_NONE="\[\e[0m\]"
  16.  
  17. DIRTY_BRANCH="⚡"
  18. RETURN="➔"
  19. AHEAD_ORIGIN="↑"
  20. BEHIND_ORIGIN="↓"
  21. DIVERGE_ORIGIN="↕"
  22.  
  23. function parse_git_branch () {
  24. git rev-parse --git-dir &> /dev/null
  25. git_status="$(git status 2> /dev/null)"
  26. branch_pattern="^# On branch ([^${IFS}]*)"
  27. remote_pattern="# Your branch is (.*) of"
  28. diverge_pattern="# Your branch and (.*) have diverged"
  29. if [[ ! ${git_status}} =~ "working directory clean" ]]; then
  30. state="${RED}${DIRTY_BRANCH}"
  31. fi
  32. # add an else if or two here if you want to get more specific
  33. if [[ ${git_status} =~ ${remote_pattern} ]]; then
  34. if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
  35. remote="${YELLOW}${AHEAD_ORIGIN}"
  36. else
  37. remote="${YELLOW}${BEHIND_ORIGIN}"
  38. fi
  39. fi
  40. if [[ ${git_status} =~ ${diverge_pattern} ]]; then
  41. remote="${YELLOW}${DIVERGE_ORIGIN}"
  42. fi
  43. if [[ ${git_status} =~ ${branch_pattern} ]]; then
  44. branch=${BASH_REMATCH[1]}
  45. echo " (${branch})${remote}${state}"
  46. fi
  47. }
  48. function prompt_func () {
  49. case $TERM in
  50. xterm*|rxvt*)
  51. local TITLEBAR='\[\033]0;\u@\h: \w\007\]'
  52. ;;
  53. *)
  54. local TITLEBAR=""
  55. ;;
  56. esac
  57. previous_return_value=$?;
  58. prompt="${LIGHT_GRAY}\u@\h: [${BLUE}\W ${GREEN}$(parse_git_branch)${LIGHT_GRAY}]${COLOR_NONE} "
  59. if test $previous_return_value -eq 0
  60. then
  61. PS1="$TITLEBAR${prompt}${RETURN} "
  62. else
  63. PS1="$TITLEBAR${prompt}${RED}${RETURN}${COLOR_NONE} "
  64. fi
  65. }
  66. PROMPT_COMMAND=prompt_func
Add Comment
Please, Sign In to add comment