A collection of scripts and configurations for Bash (and others)
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

174 linhas
4.4KB

  1. # define symbols
  2. declare -A symbols=(
  3. [hard_separator]=""
  4. [soft_separator]="|"
  5. [git]=""
  6. [lock]=""
  7. [flag]="⚑"
  8. [plus]="✚"
  9. [tick]="✔"
  10. [cross]="✘"
  11. [enter]="⏎"
  12. [python]="λ"
  13. [battery_charging]="⚡"
  14. [battery_discharging]="▮"
  15. [untracked]="U"
  16. [stash]="🐿"
  17. [ahead]="+"
  18. [behind]="-"
  19. [smilie]="^_^"
  20. [frownie]="O_o"
  21. )
  22. last_section_bg=
  23. # $1 - Text (FG) color of new section
  24. # $2 - BG of new section
  25. function section_separator() {
  26. if [ "$2" = "$last_section_bg" ]; then
  27. PS1+="$(_CC ${BLACK} ${2})${symbols[soft_separator]}"
  28. else
  29. if [ "$2" ]; then
  30. PS1+="$(_CC $(_CBG2FG ${last_section_bg}) ${2})${symbols[hard_separator]}"
  31. else
  32. #echo "${last_section_bg} GAH!"
  33. PS1+="${NORMAL}$(_CFG $(_CBG2FG ${last_section_bg}) )${symbols[hard_separator]}${NORMAL}"
  34. fi
  35. fi
  36. }
  37. # $1 - Content
  38. # $2 - Text (FG) color
  39. # $3 - BG Color
  40. function section(){
  41. if [ "$last_section_bg" ]; then
  42. section_separator ${2} ${3}
  43. fi
  44. last_section_bg=$3
  45. PS1+="$(_CC ${2} ${3} ${4})${1}"
  46. }
  47. # This function was modified from one found @ https://www.reddit.com/r/linux/comments/2uf5uu/this_is_my_bash_prompt_which_is_your_favorite/?st=jikgswh4&sh=371ba8bf
  48. # $1 - Default text (foreground) color
  49. # $2 - Background color
  50. function section_git_status() {
  51. local default untracked stash clean ahead behind staged dirty diverged
  52. default=$(_CC $1 $2)
  53. untracked=$(_CC $GREEN $2)
  54. stash=$(_CC $GREEN $2)
  55. clean=$(_CC $GREEN $2)
  56. ahead=$(_CC $YELLOW $2)
  57. behind=$(_CC $YELLOW $2)
  58. staged=$(_CC $CYAN $2)
  59. dirty=$(_CC $RED $2)
  60. diverged=$(_CC $RED $2)
  61. branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
  62. if [[ -n "$branch" ]]; then
  63. if [ "$last_section_bg" ]; then
  64. section_separator $1 $2
  65. fi
  66. last_section_bg=$2
  67. PS1+="${default} ${symbols[git]} $branch "
  68. git_status=$(git status 2> /dev/null)
  69. # If nothing changes the color, we can spot unhandled cases.
  70. if [[ $git_status =~ 'Untracked files' ]]; then
  71. PS1+="${symbols[untracked]}"
  72. fi
  73. if git stash show &>/dev/null; then
  74. PS1+="${symbols[stash]}"
  75. fi
  76. if [[ $git_status =~ 'Your branch is ahead' ]]; then
  77. PS1+="${symbols[ahead]}"
  78. fi
  79. if [[ $git_status =~ 'Your branch is behind' ]]; then
  80. PS1+="${symbols[behind]}"
  81. fi
  82. if [[ $git_status =~ 'Changes to be committed' ]]; then
  83. PS1+="${symbols[tick]}"
  84. fi
  85. if [[ $git_status =~ 'Changed but not updated' ||
  86. $git_status =~ 'Changes not staged' ||
  87. $git_status =~ 'Unmerged paths' ]]; then
  88. PS1+="${symbols[cross]}"
  89. fi
  90. if [[ $git_status =~ 'Your branch'.+diverged ]]; then
  91. PS1+="${symbols[enter]}"
  92. fi
  93. PS1+=" "
  94. fi
  95. }
  96. # $1 - FG local
  97. # $2 - BG User
  98. # $3 - FG Remote
  99. # $4 - BG Root
  100. section_tty () {
  101. local TTY=`ps aux | grep $$ | grep bash | awk '{ print $7 }'`
  102. local SESS_SRC=`who | grep $TTY | awk '{ print $6 }'`
  103. local USR=0 # 0 = User | 1 = Root
  104. local FG=$1
  105. local BG=$2
  106. # Okay...Now who we be?
  107. if [ `/usr/bin/whoami` = "root" ] ; then
  108. USR=1
  109. BG=$4
  110. fi
  111. SSH_IP=`echo $SSH_CLIENT | awk '{ print $1 }'`
  112. if [ $SSH_IP ] ; then
  113. FG=$3
  114. else
  115. SSH_IP=`echo $SSH2_CLIENT | awk '{ print $1 }'`
  116. if [ $SSH_IP ]; then
  117. FG=$3
  118. fi
  119. fi
  120. if [ "$last_section_bg" ]; then
  121. section_separator $1 $2
  122. fi
  123. last_section_bg=$2
  124. PS1+="$(_CC $FG $BG) ${TTY} "
  125. }
  126. short_PWD ()
  127. {
  128. local PRE= NAME="$1" LENGTH="$2";
  129. [[ "$NAME" != "${NAME#$HOME/}" || -z "${NAME#$HOME}" ]] &&
  130. PRE+='~' NAME="${NAME#$HOME}" LENGTH=$[LENGTH-1];
  131. ((${#NAME}>$LENGTH)) && NAME="/...${NAME:$[${#NAME}-LENGTH+4]}";
  132. echo "$PRE$NAME"
  133. }
  134. set_prompt() {
  135. local lec="$?"
  136. local color=$GREEN
  137. local bg=$BG_LBLUE
  138. local smilie=" ${symbols[smilie]} "
  139. if [ $COLOR_PROMPT = yes ]; then
  140. if [ $lec -ne 0 ]; then
  141. color=$RED
  142. smilie=" ${symbols[frownie]} "
  143. fi
  144. PS1="${debian_chroot:+($debian_chroot)}" #$(_CC $GREEN $BG_DGREY)\u $(_CC $LBLUE $BG_DGREY)\w$(_CC $DEFAULT $BG_DGREY)|${smilie}$(_CC $DEFAULT $BG_DGREY)|$(_git_status)${NORMAL}$(_CFG $DGREY)${symbols[hard_separator]}${NORMAL} "
  145. section $smilie $color $BG_BLACK 1
  146. section_tty $BLACK $BG_LBLUE $RED $BG_YELLOW
  147. section " \u@\h " $BLUE $BG_LCYAN
  148. section " $(short_PWD $PWD 20) " $LCYAN $BG_BLUE
  149. section_git_status $YELLOW $BG_LBLUE
  150. section_separator
  151. unset last_section_bg
  152. else
  153. PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w\$ "
  154. fi
  155. }
  156. PROMPT_COMMAND="set_prompt; $PROMPT_COMMAND"