rvm-prompt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env bash
  2. add()
  3. {
  4. token=${1:-""}
  5. eval "${token}_flag=1"
  6. shift
  7. if
  8. [[ -n "$format" ]]
  9. then
  10. [[ ${previous_is_format_var:-0} == 1 ]] && eval "${token}_prefix_flag=1"
  11. format="${format}\$${token}"
  12. else
  13. format="\$${token}"
  14. fi
  15. previous_is_format_var=1
  16. }
  17. add_raw_token()
  18. {
  19. previous_is_format_var=0
  20. token=${1:-""}
  21. format="${format:-""}${token}"
  22. }
  23. rvm_gemset_separator="${rvm_gemset_separator:-"@"}"
  24. if
  25. [[ "${GEM_HOME:-}" == *rvm* && "$ruby_string" != "$rvm_path/bin/ruby" ]]
  26. then
  27. unset format
  28. while
  29. (( $# ))
  30. do
  31. case "$1" in
  32. i|interpreter) add "interpreter" ;;
  33. v|version) add "version" ;;
  34. p|patchlevel) add "patchlevel" ;;
  35. r|revision) add "revision" ;;
  36. a|architecture) add "architecture" ;;
  37. g|gemset) add "gemset" ;;
  38. u|unicode) add "unicode" ;;
  39. s|system) ;; #skip when in ruby
  40. -d|--no-default) no_default=1 ;;
  41. *) add_raw_token "$1" ;;
  42. esac
  43. shift
  44. done
  45. if
  46. [[ -z "${format:-""}" && -z "$no_default" ]]
  47. then
  48. for default in interpreter version patchlevel gemset
  49. do
  50. add "$default"
  51. done
  52. fi
  53. gemset=""
  54. ruby_string="${GEM_HOME:-""}"
  55. ruby_string="${ruby_string##*/}"
  56. case "${GEM_HOME:-""}" in
  57. (*${rvm_gemset_separator:-"@"}*)
  58. if (( ${gemset_flag:-0} ))
  59. then gemset="${rvm_gemset_separator:-"@"}${ruby_string##*${rvm_gemset_separator:-"@"}}"
  60. fi
  61. ruby_string="${ruby_string%%${rvm_gemset_separator:-"@"}*}"
  62. ;;
  63. esac
  64. if
  65. [[ -n "$no_default" && -n "${format:-""}" ]]
  66. then
  67. # Do not display anything if no default flag was provided
  68. # and we are using the default ruby
  69. # Only people who explicitly ask for this will have the
  70. # slight performance penalty associated.
  71. [[ "$(rvm tools identifier)" != "$(rvm alias show default)" ]] || exit 0
  72. fi
  73. strings=(${ruby_string//-/ })
  74. if
  75. (( ${interpreter_flag:-0} )) || [[ -n "$unicode_flag" ]]
  76. then
  77. interpreter=${strings[0]}
  78. (( ${interpreter_prefix_flag:-0} )) && interpreter="-${interpreter}"
  79. fi
  80. if
  81. (( ${version_flag:-0} )) || [[ -n "$unicode_flag" ]]
  82. then
  83. version=${strings[1]}
  84. (( ${version_prefix_flag:-0} )) && version="-${version}"
  85. fi
  86. if
  87. (( ${#strings[@]} > 2 ))
  88. then
  89. if
  90. (( ${patchlevel_flag:-0} ))
  91. then
  92. patchlevel=${strings[2]}
  93. (( ${patchlevel_prefix_flag:-0} )) && patchlevel="-${patchlevel}"
  94. fi
  95. fi
  96. if
  97. (( ${architecture_flag:-0} ))
  98. then
  99. architecture="$(echo "$(ruby -v)" | sed 's/^.*\[//' | sed 's/\].*$//')"
  100. (( ${architecture_prefix_flag:-0} )) && architecture="-${architecture}"
  101. fi
  102. if
  103. (( ${unicode_flag:-0} ))
  104. then
  105. case "$interpreter" in
  106. (jruby) unicode="☯" ;;
  107. (rbx) unicode="❖" ;;
  108. (ree) unicode="✈" ;;
  109. (macruby) unicode="⌘" ;;
  110. (maglev) unicode="㎖" ;;
  111. (ironruby) unicode="♭" ;;
  112. (system) unicode="★" ;;
  113. (topaz) unicode="🐍" ;; # TODO: what is this? can we use something more fonts support?
  114. (truffleruby) unicode="🚀" ;;
  115. (opal) unicode="⬢" ;;
  116. (ruby)
  117. case ${version:-""} in
  118. (1.8.6) unicode="➇❻" ;;
  119. (1.8.7) unicode="➇❼" ;;
  120. (1.8*) unicode="➇" ;;
  121. (1.9.1) unicode="➈❶" ;;
  122. (1.9.2) unicode="➈❷" ;;
  123. (1.9.3) unicode="➈❸" ;;
  124. (2.0.0) unicode="➋" ;;
  125. (2.1.*) unicode="➋➀" ;;
  126. (2.2.*) unicode="➋➁" ;;
  127. (2.3.*) unicode="➋➂" ;;
  128. (2.4.*) unicode="➋➃" ;;
  129. (*) unicode="⦿" ;;
  130. esac ;;
  131. (*) unicode="⦿" ;;
  132. esac
  133. if
  134. echo "$ruby_string" | GREP_OPTIONS="" \grep '-head' >/dev/null 2>&1
  135. then
  136. unicode="${unicode}⚡"
  137. fi
  138. (( ${unicode_prefix_flag:-0} )) && unicode="-${unicode}"
  139. fi
  140. eval "echo \"$format\""
  141. else
  142. while
  143. (( $# ))
  144. do
  145. case "$1" in
  146. s|system) echo "system" ;;
  147. esac
  148. shift
  149. done
  150. fi