prompt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. ##### My (demuredemeanor) bashrc sub source prompt script
  2. # Uses tabstop=4; shiftwidth=4 tabs; foldmarker={{{,}}};
  3. # https://notabug.org/demure/dotfiles/
  4. # legacy repo http://github.com/demure/dotfiles
  5. # vim:set syntax=bash:
  6. ## This Changes The PS1
  7. export PROMPT_COMMAND=__prompt_command # Func to gen PS1 after CMDs
  8. function __prompt_command() {
  9. local EXIT=${PIPESTATUS[-1]} # This needs to be first
  10. PS1=""
  11. ### Colors to Vars ### {{{
  12. ## Inspired by http://wiki.archlinux.org/index.php/Color_Bash_Prompt#List_of_colors_for_prompt_and_Bash
  13. ## Terminal Control Escape Sequences: http://www.termsys.demon.co.uk/vtansi.htm
  14. ## Consider using some of: https://gist.github.com/bcap/5682077#file-terminal-control-sh
  15. ## Can unset with `unset -v {,B,U,I,BI,On_,On_I}{Bla,Red,Gre,Yel,Blu,Pur,Cya,Whi} RCol`
  16. local RCol='\[\e[0m\]' # Text Reset
  17. # Regular Bold Underline High Intensity BoldHigh Intensity Background High Intensity Backgrounds
  18. local Bla='\[\e[0;30m\]'; local BBla='\[\e[1;30m\]'; local UBla='\[\e[4;30m\]'; local IBla='\[\e[0;90m\]'; local BIBla='\[\e[1;90m\]'; local On_Bla='\e[40m'; local On_IBla='\[\e[0;100m\]';
  19. local Red='\[\e[0;31m\]'; local BRed='\[\e[1;31m\]'; local URed='\[\e[4;31m\]'; local IRed='\[\e[0;91m\]'; local BIRed='\[\e[1;91m\]'; local On_Red='\e[41m'; local On_IRed='\[\e[0;101m\]';
  20. local Gre='\[\e[0;32m\]'; local BGre='\[\e[1;32m\]'; local UGre='\[\e[4;32m\]'; local IGre='\[\e[0;92m\]'; local BIGre='\[\e[1;92m\]'; local On_Gre='\e[42m'; local On_IGre='\[\e[0;102m\]';
  21. local Yel='\[\e[0;33m\]'; local BYel='\[\e[1;33m\]'; local UYel='\[\e[4;33m\]'; local IYel='\[\e[0;93m\]'; local BIYel='\[\e[1;93m\]'; local On_Yel='\e[43m'; local On_IYel='\[\e[0;103m\]';
  22. local Blu='\[\e[0;34m\]'; local BBlu='\[\e[1;34m\]'; local UBlu='\[\e[4;34m\]'; local IBlu='\[\e[0;94m\]'; local BIBlu='\[\e[1;94m\]'; local On_Blu='\e[44m'; local On_IBlu='\[\e[0;104m\]';
  23. local Pur='\[\e[0;35m\]'; local BPur='\[\e[1;35m\]'; local UPur='\[\e[4;35m\]'; local IPur='\[\e[0;95m\]'; local BIPur='\[\e[1;95m\]'; local On_Pur='\e[45m'; local On_IPur='\[\e[0;105m\]';
  24. local Cya='\[\e[0;36m\]'; local BCya='\[\e[1;36m\]'; local UCya='\[\e[4;36m\]'; local ICya='\[\e[0;96m\]'; local BICya='\[\e[1;96m\]'; local On_Cya='\e[46m'; local On_ICya='\[\e[0;106m\]';
  25. local Whi='\[\e[0;37m\]'; local BWhi='\[\e[1;37m\]'; local UWhi='\[\e[4;37m\]'; local IWhi='\[\e[0;97m\]'; local BIWhi='\[\e[1;97m\]'; local On_Whi='\e[47m'; local On_IWhi='\[\e[0;107m\]';
  26. ### End Color Vars ### }}}
  27. if [ ${UID} -eq "0" ];then
  28. PS1+="${Red}\h \W ->${RCol} " ## Set prompt for root
  29. else
  30. local PSCol="" ## Declare so null var fine
  31. local PSOpt="" ## Above, and fixes repeat issue
  32. if [ ${EXIT} != 0 ]; then
  33. PS1+="${Red}${EXIT}${RCol}" ## Add exit code, if non 0
  34. fi
  35. ### Machine Test ### {{{
  36. local SHost="$(echo $HOSTNAME | awk 'match($0,/^[a-zA-Z0-9]+/) {print substr($0,RSTART,RLENGTH)}')"
  37. if [ ${HOSTNAME} == 'moving-computer-of-doom' ]; then
  38. local PSCol="$Cya" ## For Main Computer
  39. elif [ ${HOSTNAME} == 'vps-of-doom' ]; then
  40. local PSCol="$Blu" ## For VPS
  41. local PSOpt="${SHost} "
  42. elif [ ${HOSTNAME} == 'ma.sdf.org' ]; then
  43. local PSCol="${Yel}" #@ For MetaArray
  44. local PSOpt="\h "
  45. elif [ ${HOSTNAME} == 'fencepost.gnu.org' ]; then
  46. local PSCol="${On_Pur}" ## For Gnu
  47. local PSOpt="\h "
  48. elif [ ${HOSTTYPE} == 'arm' ]; then
  49. local PSCol="$Gre" #@ For pi
  50. local PSOpt="${SHost} "
  51. elif [ ${OSTYPE} == 'linux-android' ]; then
  52. local PSCol="$Gre" #@ For Android Termux`
  53. elif [[ ${MACHTYPE} =~ arm-apple-darwin ]]; then
  54. local PSCol="$Gre" ## For iOS
  55. elif [ ${MACHTYPE} == 'i486-pc-linux-gnu' ]; then
  56. local PSCol="$BBla" ## For Netbook
  57. local PSOpt="${SHost} "
  58. elif [[ "${MACHTYPE}" == "x86_64--netbsd" && "${OSTYPE}" == "netbsd" ]]; then
  59. local PSCol="${Yel}" ## For Main Cluster
  60. local PSOpt="\h "
  61. elif [ "${MACHTYPE}" == "mips-openwrt-linux-gnu" ]; then
  62. local PSCol="${On_Pur}" ## For OpenWrt
  63. local PSOpt="\h "
  64. else
  65. local PSCol="${Pur}" ## Un-designated catch-all
  66. if [ ! ${HOSTNAME} == 'localhost' ]; then
  67. local PSOpt="\h "
  68. else
  69. PSOS="$(awk -F '=' '/^ID=/ {print $2}' /etc/*release)"
  70. local PSOpt="[${PSOS}] "
  71. fi
  72. MISSING_ITEMS+="machine-prompt, "
  73. fi
  74. ### End Machine Test ### }}}
  75. PS1+="${PSCol}${PSOpt}\W${RCol}" ## Current working dir
  76. ### Check Jobs ### {{{
  77. type jobs &>/dev/null
  78. if [ ${PIPESTATUS[-1]} == 0 ]; then
  79. ## Backgrounded running jobs
  80. local BKGJBS=$(jobs -r | wc -l | tr -d ' ')
  81. if [ ${BKGJBS} -gt 2 ]; then
  82. PS1+=" ${Red}[bg:${BKGJBS}]${RCol}"
  83. elif [ ${BKGJBS} -gt 0 ]; then
  84. PS1+=" ${Yel}[bg:${BKGJBS}]${RCol}"
  85. fi
  86. ## Stopped Jobs
  87. local STPJBS=$(jobs -s | wc -l | tr -d ' ')
  88. if [ ${STPJBS} -gt 2 ]; then
  89. PS1+=" ${Red}[stp:${STPJBS}]${RCol}"
  90. elif [ ${STPJBS} -gt 0 ]; then
  91. PS1+=" ${Yel}[stp:${STPJBS}]${RCol}"
  92. fi
  93. fi
  94. ### End Jobs ### }}}
  95. ### Add Git Status ### {{{
  96. ## Inspired by http://www.terminally-incoherent.com/blog/2013/01/14/whats-in-your-bash-prompt/
  97. if [[ $(command -v git) ]]; then
  98. ## $GSP: git status porcelain
  99. local GSP="$(git status --porcelain=2 --branch 2>/dev/null)"
  100. if [ -n "${GSP}" ]; then
  101. ### Fetch Time Check ### {{{
  102. local LAST=$(stat -c %Y $(git rev-parse --git-dir 2>/dev/null)/FETCH_HEAD 2>/dev/null)
  103. if [ -n "${LAST}" ]; then
  104. local TIME=$(echo $(($(date +"%s") - ${LAST})))
  105. ## Check if more than 60 minutes since last
  106. if [ "${TIME}" -gt "3600" ]; then
  107. local GF=1 ## Git Fetch True
  108. fi
  109. else
  110. local GF=1 ## Git Fetch True
  111. fi
  112. if [ -n "${GF}" ] && [ "${GF}" == "1" ]; then
  113. git fetch 2>/dev/null
  114. PS1+=' +'
  115. ## Refresh var
  116. local GSP="$(git status --porcelain=2 --branch 2>/dev/null)"
  117. fi
  118. ### End Fetch Check ### }}}
  119. ### Branch Indicator Color ### {{{
  120. ## GSP Change Color; Reuses GSP Modified code
  121. local GSPcc="$(grep -c "^[12] .M" <<< "${GSP}")"
  122. if [ "${GSPcc}" == "0" ]; then
  123. local GBC=$Gre ## Branch Color
  124. else
  125. local GBC=$Red ## Branch Color
  126. fi
  127. ### End Branch Indicator Color ### }}}
  128. ### Find Branch ### {{{
  129. ## GSP Current Branch; branch name in 3rd spot
  130. local GSPcb="$(awk '/branch.head/ {print $3}' <<< "${GSP}")"
  131. if [ -n "${GSPcb}" ]; then
  132. GSPcb="[${GSPcb}]" ## Add brackets for final output. Will now test against brackets as well.
  133. if [ "${GSPcb}" == "[master]" ]; then
  134. local GSPcb="[M]" ## Because why waste space
  135. fi
  136. ## Test if in detached head state, and set output to first 8char of hash
  137. if [ "${GSPcb}" == "[(detached)]" ]; then
  138. local GSPcb="$(awk '/branch.oid/ {print substr($3,0,8)}' <<< "${GSP}")"
  139. fi
  140. else
  141. ## Note: No braces applied to emphasis that there is an issue, and that you aren't in a branch named "ERROR".
  142. local GSPcb="ERROR" ## It could happen?
  143. fi
  144. ### End Branch ### }}}
  145. PS1+=" ${GBC}${GSPcb}${RCol}" ## Add result to prompt
  146. ### Find Commit Status ### {{{
  147. ## GSP Commit Ahead; 3rd spot; Knock off leading symbol; Check exist and gt 0
  148. local GSPca="$(awk '/branch.ab/ {print substr($3,2)}' <<< "${GSP}")"
  149. if [ -n "${GSPca}" ] && [ "${GSPca}" -gt 0 ]; then
  150. PS1+="${Gre}↑${RCol}${GSPca}" ## Ahead
  151. fi
  152. ## Needs a `git fetch` to be accurate
  153. ## GSP Commit Behind; 4rd spot; Knock off leading symbol; Check exist and gt 0
  154. local GSPcb="$(awk '/branch.ab/ {print substr($4,2)}' <<< "${GSP}")"
  155. if [ -n "${GSPcb}" ] && [ "${GSPcb}" -gt 0 ]; then
  156. PS1+="${Red}↓${RCol}${GSPcb}" ## Behind
  157. fi
  158. ## Read about "[ MARC]" from https://git-scm.com/docs/git-status
  159. ## GSP Modified
  160. local GSPm="$(grep -c "^[12] .M" <<< "${GSP}")"
  161. if [ "${GSPm}" -gt "0" ]; then
  162. PS1+="${Pur}≠${RCol}${GSPm}" ## Modified
  163. fi
  164. ## GSP Deleted
  165. local GSPd="$(grep -c "^[12] D" <<< "${GSP}")"
  166. if [ "${GSPd}" -gt "0" ]; then
  167. PS1+="${Red}✖${RCol}${GSPd}" ## Deleted
  168. fi
  169. ## GSP Added
  170. local GSPa="$(grep -c "^[12] A" <<< "${GSP}")"
  171. if [ "${GSPa}" -gt "0" ]; then
  172. PS1+="${Gre}✚${RCol}${GSPa}" ## Added
  173. fi
  174. ## GSP Renamed
  175. local GSPr="$(grep -c "^[12] R" <<< "${GSP}")"
  176. if [ "${GSPr}" -gt "0" ]; then
  177. PS1+="${Blu}☇${RCol}${GSPr}" ## Renamed
  178. fi
  179. ## GSP Untracked
  180. local GSPu="$(grep -c "^?" <<< "${GSP}")"
  181. if [ "${GSPu}" -gt "0" ]; then
  182. PS1+="${Yel}?${RCol}${GSPu}" ## Untracked
  183. fi
  184. ### End Commit Status ### }}}
  185. fi
  186. else
  187. MISSING_ITEMS+="git-prompt, "
  188. fi
  189. ### End Git Status ### }}}
  190. ### z.sh _z running ### {{{
  191. ## Add z.sh to prompt command, since I have a fancy one and I source exports too early.
  192. ## https://github.com/rupa/z
  193. ## Test if _z function exists
  194. if [ "$(type -t _z)" = "function" ]; then
  195. (_z --add "$(command pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null &)
  196. fi
  197. ### End z.sh _z running ### }}}
  198. PS1+=" ${PSCol}-> ${RCol}" ## End of PS1
  199. fi
  200. }