A collection of scripts and configurations for Bash (and others)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.0KB

  1. # This function was modified from one found @ https://www.reddit.com/r/linux/comments/2uf5uu/this_is_my_bash_prompt_which_is_your_favorite/?st=jikgswh4&sh=371ba8bf
  2. function _git_status() {
  3. local unknown untracked stash clean ahead behind staged dirty diverged
  4. unknown=$BLUE
  5. untracked=$GREEN
  6. stash=$GREEN
  7. clean=$GREEN
  8. ahead=$YELLOW
  9. behind=$YELLOW
  10. staged=$CYAN
  11. dirty=$RED
  12. diverged=$RED
  13. branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
  14. if [[ -n "$branch" ]]; then
  15. git_status=$(git status 2> /dev/null)
  16. # If nothing changes the color, we can spot unhandled cases.
  17. color=$unknown
  18. if [[ $git_status =~ 'Untracked files' ]]; then
  19. color=$untracked
  20. branch="${branch}?"
  21. fi
  22. if git stash show &>/dev/null; then
  23. color=$stash
  24. branch="${branch}+"
  25. fi
  26. if [[ $git_status =~ 'working directory clean' ]]; then
  27. color=$clean
  28. fi
  29. if [[ $git_status =~ 'Your branch is ahead' ]]; then
  30. color=$ahead
  31. branch="${branch}>"
  32. fi
  33. if [[ $git_status =~ 'Your branch is behind' ]]; then
  34. color=$behind
  35. branch="${branch}<"
  36. fi
  37. if [[ $git_status =~ 'Changes to be committed' ]]; then
  38. color=$staged
  39. fi
  40. if [[ $git_status =~ 'Changed but not updated' ||
  41. $git_status =~ 'Changes not staged' ||
  42. $git_status =~ 'Unmerged paths' ]]; then
  43. color=$dirty
  44. fi
  45. if [[ $git_status =~ 'Your branch'.+diverged ]]; then
  46. color=$diverged
  47. branch="${branch}!"
  48. fi
  49. echo -n "${color}${branch}${NORMAL}|"
  50. fi
  51. }
  52. set_prompt() {
  53. local lec="$?"
  54. if [ $COLOR_PROMPT = yes ]; then
  55. if [ $lec -ne 0 ]; then
  56. smilie="${RED}:(${NORMAL}"
  57. else
  58. smilie="${GREEN}:)${NORMAL}"
  59. fi
  60. PS1="${debian_chroot:+($debian_chroot)}${BG_PURPLE}${GREEN}\u${NORMAL}${BG_PURPLE}|${BLUE}\w${NORMAL}${BG_PURPLE}|${smilie}${BG_PURPLE}|$(_git_status)${PURPLE}► "
  61. else
  62. PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w\$ "
  63. fi
  64. }
  65. PROMPT_COMMAND="set_prompt"