prompt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 [ ! -e "${HOME}/.no_git_prompt" ]; then
  98. if [[ $(command -v git) ]]; then
  99. ## $GSP: git status porcelain
  100. local GSP="$(git status --porcelain=2 --branch 2>/dev/null)"
  101. if [ -n "${GSP}" ]; then
  102. ### Fetch Time Check ### {{{
  103. local LAST=$(stat -c %Y $(git rev-parse --git-dir 2>/dev/null)/FETCH_HEAD 2>/dev/null)
  104. if [ -n "${LAST}" ]; then
  105. local TIME=$(echo $(($(date +"%s") - ${LAST})))
  106. ## Check if more than 60 minutes since last
  107. if [ "${TIME}" -gt "3600" ]; then
  108. local GF=1 ## Git Fetch True
  109. fi
  110. else
  111. local GF=1 ## Git Fetch True
  112. fi
  113. if [ -n "${GF}" ] && [ "${GF}" == "1" ]; then
  114. git fetch 2>/dev/null
  115. PS1+=' +'
  116. ## Refresh var
  117. local GSP="$(git status --porcelain=2 --branch 2>/dev/null)"
  118. fi
  119. ### End Fetch Check ### }}}
  120. ### Branch Indicator Color ### {{{
  121. ## GSP Change Color; Reuses GSP Modified code
  122. local GSPcc="$(grep -c "^[12] .M" <<< "${GSP}")"
  123. if [ "${GSPcc}" == "0" ]; then
  124. local GBC=$Gre ## Branch Color
  125. else
  126. local GBC=$Red ## Branch Color
  127. fi
  128. ### End Branch Indicator Color ### }}}
  129. ### Find Branch ### {{{
  130. ## GSP Current Branch; branch name in 3rd spot
  131. local GSPcb="$(awk '/branch.head/ {print $3}' <<< "${GSP}")"
  132. if [ -n "${GSPcb}" ]; then
  133. GSPcb="[${GSPcb}]" ## Add brackets for final output. Will now test against brackets as well.
  134. if [ "${GSPcb}" == "[master]" ]; then
  135. local GSPcb="[M]" ## Because why waste space
  136. fi
  137. ## Test if in detached head state, and set output to first 8char of hash
  138. if [ "${GSPcb}" == "[(detached)]" ]; then
  139. local GSPcb="$(awk '/branch.oid/ {print substr($3,0,8)}' <<< "${GSP}")"
  140. fi
  141. else
  142. ## Note: No braces applied to emphasis that there is an issue, and that you aren't in a branch named "ERROR".
  143. local GSPcb="ERROR" ## It could happen?
  144. fi
  145. ### End Branch ### }}}
  146. PS1+=" ${GBC}${GSPcb}${RCol}" ## Add result to prompt
  147. ### Find Commit Status ### {{{
  148. ## GSP Commit Ahead; 3rd spot; Knock off leading symbol; Check exist and gt 0
  149. local GSPca="$(awk '/branch.ab/ {print substr($3,2)}' <<< "${GSP}")"
  150. if [ -n "${GSPca}" ] && [ "${GSPca}" -gt 0 ]; then
  151. PS1+="${Gre}↑${RCol}${GSPca}" ## Ahead
  152. fi
  153. ## Needs a `git fetch` to be accurate
  154. ## GSP Commit Behind; 4rd spot; Knock off leading symbol; Check exist and gt 0
  155. local GSPcb="$(awk '/branch.ab/ {print substr($4,2)}' <<< "${GSP}")"
  156. if [ -n "${GSPcb}" ] && [ "${GSPcb}" -gt 0 ]; then
  157. PS1+="${Red}↓${RCol}${GSPcb}" ## Behind
  158. fi
  159. ## Read about "[ MARC]" from https://git-scm.com/docs/git-status
  160. ## GSP Modified
  161. local GSPm="$(grep -c "^[12] .M" <<< "${GSP}")"
  162. if [ "${GSPm}" -gt "0" ]; then
  163. PS1+="${Pur}≠${RCol}${GSPm}" ## Modified
  164. fi
  165. ## GSP Deleted
  166. local GSPd="$(grep -c "^[12] D" <<< "${GSP}")"
  167. if [ "${GSPd}" -gt "0" ]; then
  168. PS1+="${Red}✖${RCol}${GSPd}" ## Deleted
  169. fi
  170. ## GSP Added
  171. local GSPa="$(grep -c "^[12] A" <<< "${GSP}")"
  172. if [ "${GSPa}" -gt "0" ]; then
  173. PS1+="${Gre}✚${RCol}${GSPa}" ## Added
  174. fi
  175. ## GSP Renamed
  176. local GSPr="$(grep -c "^[12] R" <<< "${GSP}")"
  177. if [ "${GSPr}" -gt "0" ]; then
  178. PS1+="${Blu}☇${RCol}${GSPr}" ## Renamed
  179. fi
  180. ## GSP Untracked
  181. local GSPu="$(grep -c "^?" <<< "${GSP}")"
  182. if [ "${GSPu}" -gt "0" ]; then
  183. PS1+="${Yel}?${RCol}${GSPu}" ## Untracked
  184. fi
  185. ### End Commit Status ### }}}
  186. fi
  187. else
  188. MISSING_ITEMS+="git-prompt, "
  189. fi
  190. fi
  191. ### End Git Status ### }}}
  192. ### z.sh _z running ### {{{
  193. ## Add z.sh to prompt command, since I have a fancy one and I source exports too early.
  194. ## https://github.com/rupa/z
  195. ## Test if _z function exists
  196. if [ "$(type -t _z)" = "function" ]; then
  197. (_z --add "$(command pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null &)
  198. fi
  199. ### End z.sh _z running ### }}}
  200. PS1+=" ${PSCol}-> ${RCol}" ## End of PS1
  201. fi
  202. }