prompt 12 KB

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