Guest User

Pedro's bashrc

a guest
Dec 21st, 2016
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. #!/bin/bash
  2. ##########################################################
  3. #Please edit "User Configuration" section before using #
  4. ##########################################################
  5.  
  6. #=========================================================
  7. #Terminal Color Codes
  8. #=========================================================
  9. WHITE='\[\033[1;37m\]'
  10. LIGHTGRAY='\[\033[0;37m\]'
  11. GRAY='\[\033[1;30m\]'
  12. BLACK='\[\033[0;30m\]'
  13. RED='\[\033[0;31m\]'
  14. LIGHTRED='\[\033[1;31m\]'
  15. GREEN='\[\033[0;32m\]'
  16. LIGHTGREEN='\[\033[1;32m\]'
  17. BROWN='\[\033[0;33m\]' #Orange
  18. YELLOW='\[\033[1;33m\]'
  19. BLUE='\[\033[0;34m\]'
  20. LIGHTBLUE='\[\033[1;34m\]'
  21. PURPLE='\[\033[0;35m\]'
  22. PINK='\[\033[1;35m\]' #Light Purple
  23. CYAN='\[\033[0;36m\]'
  24. LIGHTCYAN='\[\033[1;36m\]'
  25. DEFAULT='\[\033[0m\]'
  26.  
  27. #=========================================================
  28. # User Configuration
  29. #=========================================================
  30. # Colors
  31. cLINES=$GRAY #Lines and Arrow
  32. cBRACKETS=$GRAY # Brackets around each data item
  33. cERROR=$LIGHTRED # Error block when previous command did not return 0
  34. cTIME=$LIGHTGRAY # The current time
  35. cMPX1=$YELLOW # Color for terminal multiplexer threshold 1
  36. cMPX2=$RED # Color for terminal multiplexer threshold 2
  37. cBGJ1=$YELLOW # Color for background job threshold 1
  38. cBGJ2=$RED # Color for background job threshold 2
  39. cSTJ1=$YELLOW # Color for background job threshold 1
  40. cSTJ2=$RED # Color for background job threshold 2
  41. cSSH=$PINK # Color for brackets if session is an SSH session
  42. cUSR=$LIGHTBLUE # Color of user
  43. cUHS=$GRAY # Color of the user and hostname separator, probably '@'
  44. cHST=$LIGHTGREEN # Color of hostname
  45. cRWN=$RED # Color of root warning
  46. cPWD=$BLUE # Color of current directory
  47. cCMD=$DEFAULT # Color of the command you type
  48.  
  49. # Enable block
  50. eNL=1 # Have a newline between previous command output and new prompt
  51. eERR=1 # Previous command return status tracker
  52. eMPX=1 # Terminal multiplexer tracker enabled
  53. eSSH=1 # Track if session is SSH
  54. eBGJ=1 # Track background jobs
  55. eSTJ=1 # Track stopped jobs
  56. eUSH=1 # Show user and host
  57. ePWD=1 # Show current directory
  58.  
  59. # Block settins
  60. MPXT1="0" # Terminal multiplexer threshold 1 value
  61. MPXT2="2" # Terminal multiplexer threshold 2 value
  62. BGJT1="0" # Background job threshold 1 value
  63. BGJT2="2" # Background job threshold 2 value
  64. STJT1="0" # Stopped job threshold 1 value
  65. STJT2="2" # Stopped job threshold 2 value
  66. UHS="@"
  67.  
  68. function promptcmd()
  69. {
  70. PREVRET=$?
  71. #=========================================================
  72. #check if user is in ssh session
  73. #=========================================================
  74. if [[ $SSH_CLIENT ]] || [[ $SSH2_CLIENT ]]; then
  75. lSSH_FLAG=1
  76. else
  77. lSSH_FLAG=0
  78. fi
  79.  
  80. #=========================================================
  81. # Insert a new line to clear space from previous command
  82. #=========================================================
  83. PS1="\n"
  84.  
  85. #=========================================================
  86. # Beginning of first line (arrow wrap around and color setup)
  87. #=========================================================
  88. PS1="${PS1}${cLINES}\342\224\214\342\224\200"
  89.  
  90. #=========================================================
  91. # First Dynamic Block - Previous Command Error
  92. #=========================================================
  93. if [ $PREVRET -ne 0 ] ; then
  94. PS1="${PS1}${cBRACKETS}[${cERROR}:(${cBRACKETS}]${cLINES}\342\224\200"
  95. fi
  96.  
  97. #=========================================================
  98. # First static block - Current time
  99. #=========================================================
  100. PS1="${PS1}${cBRACKETS}[${cTIME}\t${cBRACKETS}]${cLINES}\342\224\200"
  101.  
  102. #=========================================================
  103. # Detached Screen Sessions
  104. #=========================================================
  105. hTMUX=0
  106. hSCREEN=0
  107. MPXC=0
  108. hash tmux --help 2>/dev/null || hTMUX=1
  109. hash screen --version 2>/dev/null || hSCREEN=1
  110. if [ $hTMUX -eq 0 ] && [ $hSCREEN -eq 0 ] ; then
  111. MPXC=$(echo "$(screen -ls | grep -c -i detach) + $(tmux ls 2>/dev/null | grep -c -i -v attach)" | bc)
  112. elif [ $hTMUX -eq 0 ] && [ $hSCREEN -eq 1 ] ; then
  113. MPXC=$(tmux ls 2>/dev/null | grep -c -i -v attach)
  114. elif [ $hTMUX -eq 1 ] && [ $hSCREEN -eq 0 ] ; then
  115. MPXC=$(screen -ls | grep -c -i detach)
  116. fi
  117. if [[ $MPXC -gt $MPXT2 ]] ; then
  118. PS1="${PS1}${cBRACKETS}[${cMPX2}\342\230\220:${MPXC}${cBRACKETS}]${cLINES}\342\224\200"
  119. elif [[ $MPXC -gt $MPXT1 ]] ; then
  120. PS1="${PS1}${cBRACKETS}[${cMPX1}\342\230\220:${MPXC}${cBRACKETS}]${cLINES}\342\224\200"
  121. fi
  122.  
  123. #=========================================================
  124. # Backgrounded running jobs
  125. #=========================================================
  126. BGJC=$(jobs -r | wc -l )
  127. if [ $BGJC -gt $BGJT2 ] ; then
  128. PS1="${PS1}${cBRACKETS}[${cBGJ2}&:${BGJC}${cBRACKETS}]${cLINES}\342\224\200"
  129. elif [ $BGJC -gt $BGJT1 ] ; then
  130. PS1="${PS1}${cBRACKETS}[${cBGJ1}&:${BGJC}${cBRACKETS}]${cLINES}\342\224\200"
  131. fi
  132.  
  133. #=========================================================
  134. # Stopped Jobs
  135. #=========================================================
  136. STJC=$(jobs -s | wc -l )
  137. if [ $STJC -gt $STJT2 ] ; then
  138. PS1="${PS1}${cBRACKETS}[${cSTJ2}\342\234\227:${STJC}${cBRACKETS}]${cLINES}\342\224\200"
  139. elif [ $STJC -gt $STJT1 ] ; then
  140. PS1="${PS1}${cBRACKETS}[${cSTJ1}\342\234\227:${STJC}${cBRACKETS}]${cLINES}\342\224\200"
  141. fi
  142.  
  143. #=========================================================
  144. # Second Static block - User@host
  145. #=========================================================
  146. # set color for brackets if user is in ssh session
  147. if [ $lSSH_FLAG -eq 1 ] ; then
  148. sesClr="$cSSH"
  149. else
  150. sesClr="$cBRACKETS"
  151. fi
  152. # don't display user if root
  153. if [ $EUID -eq 0 ] ; then
  154. PS1="${PS1}${sesClr}[${cRWN}!"
  155. else
  156. PS1="${PS1}${sesClr}[${cUSR}\u${cUHS}${UHS}"
  157. fi
  158. PS1="${PS1}${cHST}\h${sesClr}]${cLINES}\342\224\200"
  159.  
  160. #=========================================================
  161. # Third Static Block - Current Directory
  162. #=========================================================
  163. PS1="${PS1}[${cPWD}\w${cBRACKETS}]"
  164.  
  165. #=========================================================
  166. # Second Line
  167. #=========================================================
  168. PS1="${PS1}\n${cLINES}\342\224\224\342\224\200\342\224\200> ${cCMD}"
  169. }
  170.  
  171. function load_prompt () {
  172. # Get PIDs
  173. local parent_process=$(cat /proc/$PPID/cmdline | cut -d \. -f 1)
  174. local my_process=$(cat /proc/$$/cmdline | cut -d \. -f 1)
  175.  
  176. if [[ $parent_process == script* ]]; then
  177. PROMPT_COMMAND=""
  178. PS1="\t - \# - \u@\H { \w }\$ "
  179. elif [[ $parent_process == emacs* || $parent_process == xemacs* ]]; then
  180. PROMPT_COMMAND=""
  181. PS1="\u@\h { \w }\$ "
  182. else
  183. export DAY=$(date +%A)
  184. PROMPT_COMMAND=promptcmd
  185. fi
  186. export PS1 PROMPT_COMMAND
  187. }
  188.  
  189. load_prompt
Add Comment
Please, Sign In to add comment