ps1_functions 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env bash
  2. #
  3. # Opt-in for custom prompt through by setting:
  4. # rvm_ps1=1
  5. # in either /etc/rvmrc or $HOME/.rvmrc
  6. #
  7. # Then in order to set your prompt you simply do the following for example
  8. #
  9. # Examples:
  10. #
  11. # ps1_set --prompt ∫
  12. #
  13. # or
  14. #
  15. # ps1_set --prompt ∴
  16. #
  17. # This will yield a prompt like the following, for example,
  18. #
  19. # 00:00:50 wayneeseguin@GeniusAir:~/projects/db0/rvm/rvm (git:master:156d0b4) ruby-1.8.7-p334@rvm
  20. # ∴
  21. #
  22. ps1_titlebar()
  23. {
  24. case $TERM in
  25. (xterm*|rxvt*)
  26. printf "%s" "\033]0;\\u@\\h: \W\\007"
  27. ;;
  28. esac
  29. }
  30. ps1_identity()
  31. {
  32. if (( $UID == 0 )) ; then
  33. printf "%s" "\[\033[31m\]\\u\[\033[0m\]@\[\033[36m\]\\h\[\033[35m\]:\w\[\033[0m\] "
  34. else
  35. printf "%s" "\[\033[32m\]\\u\[\033[0m\]@\[\033[36m\]\\h\[\033[35m\]:\w\[\033[0m\] "
  36. fi
  37. }
  38. ps1_git()
  39. {
  40. local branch="" sha1="" line="" attr="" color=0
  41. shopt -s extglob # Important, for our nice matchers :)
  42. command -v git >/dev/null 2>&1 || {
  43. printf " \033[1;37m\033[41m[git not found]\033[m "
  44. return 0
  45. }
  46. branch=$(git symbolic-ref -q HEAD 2>/dev/null) || return 0 # Not in git repo.
  47. branch=${branch##refs/heads/}
  48. # Now we display the branch.
  49. sha1=$(git rev-parse --short --quiet HEAD)
  50. case "${branch:-"(no branch)"}" in
  51. production|prod) attr="1;37m\033[" ; color=41 ;; # red
  52. master|deploy) color=31 ;; # red
  53. stage|staging) color=33 ;; # yellow
  54. dev|develop|development) color=34 ;; # blue
  55. next) color=36 ;; # gray
  56. *)
  57. if [[ -n "${branch}" ]] ; then # Feature Branch :)
  58. color=32 # green
  59. else
  60. color=0 # reset
  61. fi
  62. ;;
  63. esac
  64. [[ $color -gt 0 ]] &&
  65. printf "\[\033[${attr}${color}m\](git:${branch}$(ps1_git_status):$sha1)\[\033[0m\] "
  66. }
  67. ps1_git_status()
  68. {
  69. local git_status="$(git status 2>/dev/null)"
  70. [[ "${git_status}" = *deleted* ]] && printf "%s" "-"
  71. [[ "${git_status}" = *Untracked[[:space:]]files:* ]] && printf "%s" "+"
  72. [[ "${git_status}" = *modified:* ]] && printf "%s" "*"
  73. }
  74. ps1_rvm()
  75. {
  76. command -v rvm-prompt >/dev/null 2>&1 && printf "%s" " $(rvm-prompt) "
  77. }
  78. ps1_update()
  79. {
  80. local prompt_char='$' separator="\n" notime=0
  81. (( $UID == 0 )) && prompt_char='#'
  82. while [[ $# -gt 0 ]] ; do
  83. local token="$1" ; shift
  84. case "$token" in
  85. --trace)
  86. export PS4="+ \${BASH_SOURCE##\${rvm_path:-}} : \${FUNCNAME[0]:+\${FUNCNAME[0]}()} \${LINENO} > "
  87. set -o xtrace
  88. ;;
  89. --prompt)
  90. prompt_char="$1"
  91. shift
  92. ;;
  93. --noseparator)
  94. separator=""
  95. ;;
  96. --separator)
  97. separator="$1"
  98. shift
  99. ;;
  100. --notime)
  101. notime=1
  102. ;;
  103. *)
  104. true # Ignore everything else.
  105. ;;
  106. esac
  107. done
  108. if (( notime > 0 )) ; then
  109. PS1="$(ps1_titlebar)$(ps1_identity)$(ps1_git)$(ps1_rvm)${separator}${prompt_char} "
  110. else
  111. PS1="$(ps1_titlebar)\D{%H:%M:%S} $(ps1_identity)$(ps1_git)$(ps1_rvm)${separator}${prompt_char} "
  112. fi
  113. }
  114. ps2_set()
  115. {
  116. PS2=" \[\033[0;40m\]\[\033[0;33m\]> \[\033[1;37m\]\[\033[1m\]"
  117. }
  118. ps4_set()
  119. {
  120. export PS4="+ \${BASH_SOURCE##\${rvm_path:-}} : \${FUNCNAME[0]:+\${FUNCNAME[0]}()} \${LINENO} > "
  121. }
  122. # WARNING: This clobbers your PROMPT_COMMAND so if you need to write your own, call
  123. # ps1_update within your PROMPT_COMMAND with the same arguments you pass
  124. # to ps1_set
  125. #
  126. # The PROMPT_COMMAND is used to help the prompt work if the separator is not a new line.
  127. # In the event that the separator is not a new line, the prompt line may become distorted if
  128. # you add or delete a certian number of characters, making the string wider than the
  129. # $COLUMNS + len(your_input_line).
  130. #
  131. # This orginally was done with callbacks within the PS1 to add in things like the git
  132. # commit, but this results in the PS1 being of an unknown width which results in the prompt
  133. # being distorted if you add or remove a certain number of characters. To work around this
  134. # it now uses the PROMPT_COMMAND callback to re-set the PS1 with a known width of chracters
  135. # each time a new command is entered. See PROMPT_COMMAND for more details.
  136. #
  137. ps1_set()
  138. {
  139. PROMPT_COMMAND="ps1_update $@"
  140. }