prompt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 [ -e "${HOME}/.config/dotconf/prompt_date" ]; then
  28. PS1+="\D{%m%d.%H%M} "
  29. fi
  30. if [ ${UID} -eq "0" ];then
  31. PS1+="${Red}\h \W ->${RCol} " ## Set prompt for root
  32. else
  33. local PSCol="" ## Declare so null var fine
  34. local PSOpt="" ## Above, and fixes repeat issue
  35. if [ ${EXIT} != 0 ]; then
  36. PS1+="${Red}${EXIT}${RCol}" ## Add exit code, if non 0
  37. fi
  38. ### Machine Test ### {{{
  39. local SHost="$(echo $HOSTNAME | awk 'match($0,/^[a-zA-Z0-9]+/) {print substr($0,RSTART,RLENGTH)}')"
  40. if [ ${HOSTNAME} == 'moving-computer-of-doom' ]; then
  41. local PSCol="$Cya" ## For Main Computer
  42. elif [ ${HOSTNAME} == 'vps-of-doom' ]; then
  43. local PSCol="$Blu" ## For VPS
  44. local PSOpt="${SHost} "
  45. elif [ ${HOSTNAME} == 'tablet-of-doom' ]; then
  46. local PSCol="$Gre" ## For tablet
  47. local PSOpt="${SHost} "
  48. elif [ ${HOSTNAME} == 'ma.sdf.org' ]; then
  49. local PSCol="${Yel}" #@ For MetaArray
  50. local PSOpt="\h "
  51. elif [ ${HOSTNAME} == 'fencepost.gnu.org' ]; then
  52. local PSCol="${On_Pur}" ## For Gnu
  53. local PSOpt="\h "
  54. elif [ ${HOSTTYPE} == 'arm' ]; then
  55. local PSCol="$Gre" #@ For pi
  56. local PSOpt="${SHost} "
  57. elif [ ${OSTYPE} == 'linux-android' ]; then
  58. local PSCol="$Gre" #@ For Android Termux`
  59. elif [[ ${MACHTYPE} =~ arm-apple-darwin ]]; then
  60. local PSCol="$Gre" ## For iOS
  61. elif [ ${MACHTYPE} == 'i486-pc-linux-gnu' ]; then
  62. local PSCol="$BBla" ## For Netbook
  63. local PSOpt="${SHost} "
  64. elif [[ "${MACHTYPE}" == "x86_64--netbsd" && "${OSTYPE}" == "netbsd" ]]; then
  65. local PSCol="${Yel}" ## For Main Cluster
  66. local PSOpt="\h "
  67. elif [ "${MACHTYPE}" == "mips-openwrt-linux-gnu" ]; then
  68. local PSCol="${On_Pur}" ## For OpenWrt
  69. local PSOpt="\h "
  70. else
  71. local PSCol="${Pur}" ## Un-designated catch-all
  72. if [ ! ${HOSTNAME} == 'localhost' ]; then
  73. local PSOpt="\h "
  74. else
  75. PSOS="$(awk -F '=' '/^ID=/ {print $2}' /etc/*release)"
  76. local PSOpt="[${PSOS}] "
  77. fi
  78. MISSING_ITEMS+="machine-prompt, "
  79. fi
  80. ### End Machine Test ### }}}
  81. PS1+="${PSCol}${PSOpt}\W${RCol}" ## Current working dir
  82. ### Check Jobs ### {{{
  83. type jobs &>/dev/null
  84. if [ ${PIPESTATUS[-1]} == 0 ]; then
  85. ## Backgrounded running jobs
  86. local BKGJBS=$(jobs -r | wc -l | tr -d ' ')
  87. if [ ${BKGJBS} -gt 2 ]; then
  88. PS1+=" ${Red}[bg:${BKGJBS}]${RCol}"
  89. elif [ ${BKGJBS} -gt 0 ]; then
  90. PS1+=" ${Yel}[bg:${BKGJBS}]${RCol}"
  91. fi
  92. ## Stopped Jobs
  93. local STPJBS=$(jobs -s | wc -l | tr -d ' ')
  94. if [ ${STPJBS} -gt 2 ]; then
  95. PS1+=" ${Red}[stp:${STPJBS}]${RCol}"
  96. elif [ ${STPJBS} -gt 0 ]; then
  97. PS1+=" ${Yel}[stp:${STPJBS}]${RCol}"
  98. fi
  99. fi
  100. ### End Jobs ### }}}
  101. ### Add Git Status ### {{{
  102. ## Inspired by http://www.terminally-incoherent.com/blog/2013/01/14/whats-in-your-bash-prompt/
  103. if [ ! -e "${HOME}/.config/dotconf/no_git_prompt" ]; then
  104. if [[ $(command -v git) ]]; then
  105. ## $GSP: git status porcelain
  106. local GSP="$(git status --porcelain=2 --branch 2>/dev/null)"
  107. if [ -n "${GSP}" ]; then
  108. ### Fetch Time Check ### {{{
  109. local LAST=$(stat -c %Y $(git rev-parse --git-dir 2>/dev/null)/FETCH_HEAD 2>/dev/null)
  110. if [ -n "${LAST}" ]; then
  111. local TIME=$(echo $(($(date +"%s") - ${LAST})))
  112. ## Check if more than 60 minutes since last
  113. if [ "${TIME}" -gt "3600" ]; then
  114. local GF=1 ## Git Fetch True
  115. fi
  116. else
  117. local GF=1 ## Git Fetch True
  118. fi
  119. if [ -n "${GF}" ] && [ "${GF}" == "1" ]; then
  120. git fetch 2>/dev/null
  121. PS1+=' +'
  122. ## Refresh var
  123. local GSP="$(git status --porcelain=2 --branch 2>/dev/null)"
  124. fi
  125. ### End Fetch Check ### }}}
  126. ### Branch Indicator Color ### {{{
  127. ## GSP Change Color; Reuses GSP Modified code
  128. local GSPcc="$(grep -c "^[12] .M" <<< "${GSP}")"
  129. if [ "${GSPcc}" == "0" ]; then
  130. local GBC=$Gre ## Branch Color
  131. else
  132. local GBC=$Red ## Branch Color
  133. fi
  134. ### End Branch Indicator Color ### }}}
  135. ### Find Branch ### {{{
  136. ## GSP Current Branch; branch name in 3rd spot
  137. local GSPcb="$(awk '/branch.head/ {print $3}' <<< "${GSP}")"
  138. if [ -n "${GSPcb}" ]; then
  139. GSPcb="[${GSPcb}]" ## Add brackets for final output. Will now test against brackets as well.
  140. if [ "${GSPcb}" == "[master]" ]; then
  141. local GSPcb="[M]" ## Because why waste space
  142. fi
  143. ## Test if in detached head state, and set output to first 8char of hash
  144. if [ "${GSPcb}" == "[(detached)]" ]; then
  145. local GSPcb="$(awk '/branch.oid/ {print substr($3,0,8)}' <<< "${GSP}")"
  146. fi
  147. else
  148. ## Note: No braces applied to emphasis that there is an issue, and that you aren't in a branch named "ERROR".
  149. local GSPcb="ERROR" ## It could happen?
  150. fi
  151. ### End Branch ### }}}
  152. PS1+=" ${GBC}${GSPcb}${RCol}" ## Add result to prompt
  153. ### Find Commit Status ### {{{
  154. ## GSP Commit Ahead; 3rd spot; Knock off leading symbol; Check exist and gt 0
  155. local GSPca="$(awk '/branch.ab/ {print substr($3,2)}' <<< "${GSP}")"
  156. if [ -n "${GSPca}" ] && [ "${GSPca}" -gt 0 ]; then
  157. PS1+="${Gre}↑${RCol}${GSPca}" ## Ahead
  158. fi
  159. ## Needs a `git fetch` to be accurate
  160. ## GSP Commit Behind; 4rd spot; Knock off leading symbol; Check exist and gt 0
  161. local GSPcb="$(awk '/branch.ab/ {print substr($4,2)}' <<< "${GSP}")"
  162. if [ -n "${GSPcb}" ] && [ "${GSPcb}" -gt 0 ]; then
  163. PS1+="${Red}↓${RCol}${GSPcb}" ## Behind
  164. fi
  165. ## Read about "[ MARC]" from https://git-scm.com/docs/git-status
  166. ## GSP Modified
  167. local GSPm="$(grep -c "^[12] .M" <<< "${GSP}")"
  168. if [ "${GSPm}" -gt "0" ]; then
  169. PS1+="${Pur}≠${RCol}${GSPm}" ## Modified
  170. fi
  171. ## GSP Deleted
  172. local GSPd="$(grep -c "^[12] D" <<< "${GSP}")"
  173. if [ "${GSPd}" -gt "0" ]; then
  174. PS1+="${Red}✖${RCol}${GSPd}" ## Deleted
  175. fi
  176. ## GSP Added
  177. local GSPa="$(grep -c "^[12] A" <<< "${GSP}")"
  178. if [ "${GSPa}" -gt "0" ]; then
  179. PS1+="${Gre}✚${RCol}${GSPa}" ## Added
  180. fi
  181. ## GSP Renamed
  182. local GSPr="$(grep -c "^[12] R" <<< "${GSP}")"
  183. if [ "${GSPr}" -gt "0" ]; then
  184. PS1+="${Blu}☇${RCol}${GSPr}" ## Renamed
  185. fi
  186. ## GSP Untracked
  187. local GSPu="$(grep -c "^?" <<< "${GSP}")"
  188. if [ "${GSPu}" -gt "0" ]; then
  189. PS1+="${Yel}?${RCol}${GSPu}" ## Untracked
  190. fi
  191. ### End Commit Status ### }}}
  192. fi
  193. else
  194. MISSING_ITEMS+="git-prompt, "
  195. fi
  196. fi
  197. ### End Git Status ### }}}
  198. ### z.sh _z running ### {{{
  199. ## Add z.sh to prompt command, since I have a fancy one and I source exports too early.
  200. ## https://github.com/rupa/z
  201. ## Test if _z function exists
  202. if [ "$(type -t _z)" = "function" ]; then
  203. (_z --add "$(command pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null &)
  204. fi
  205. ### End z.sh _z running ### }}}
  206. PS1+=" ${PSCol}-> ${RCol}" ## End of PS1
  207. fi
  208. }