A collection of scripts and configurations for Bash (and others)
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

192 lines
4.9KB

  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. # $4 - Font style
  41. # $5 - Ignore section separator
  42. function section(){
  43. ignore=$5
  44. if [ "$ignore" != true ]; then
  45. ignore=false
  46. fi
  47. if [ "$last_section_bg" ]; then
  48. if [ "$ignore" = false ]; then
  49. section_separator ${2} ${3}
  50. fi
  51. fi
  52. last_section_bg=$3
  53. PS1+="$(_CC ${2} ${3} ${4})${1}"
  54. }
  55. # 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
  56. # $1 - Default text (foreground) color
  57. # $2 - Background color
  58. function section_git_status() {
  59. local default untracked stash clean ahead behind staged dirty diverged
  60. default=$(_CC $1 $2)
  61. untracked=$(_CC $GREEN $2)
  62. stash=$(_CC $GREEN $2)
  63. clean=$(_CC $GREEN $2)
  64. ahead=$(_CC $YELLOW $2)
  65. behind=$(_CC $YELLOW $2)
  66. staged=$(_CC $CYAN $2)
  67. dirty=$(_CC $RED $2)
  68. diverged=$(_CC $RED $2)
  69. branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
  70. if [[ -n "$branch" ]]; then
  71. if [ "$last_section_bg" ]; then
  72. section_separator $1 $2
  73. fi
  74. last_section_bg=$2
  75. PS1+="${default} ${symbols[git]} $branch "
  76. git_status=$(git status 2> /dev/null)
  77. # If nothing changes the color, we can spot unhandled cases.
  78. if [[ $git_status =~ 'Untracked files' ]]; then
  79. PS1+="${symbols[untracked]}"
  80. fi
  81. if git stash show &>/dev/null; then
  82. PS1+="${symbols[stash]}"
  83. fi
  84. if [[ $git_status =~ 'Your branch is ahead' ]]; then
  85. PS1+="${symbols[ahead]}"
  86. fi
  87. if [[ $git_status =~ 'Your branch is behind' ]]; then
  88. PS1+="${symbols[behind]}"
  89. fi
  90. if [[ $git_status =~ 'Changes to be committed' ]]; then
  91. PS1+="${symbols[tick]}"
  92. fi
  93. if [[ $git_status =~ 'Changed but not updated' ||
  94. $git_status =~ 'Changes not staged' ||
  95. $git_status =~ 'Unmerged paths' ]]; then
  96. PS1+="${symbols[cross]}"
  97. fi
  98. if [[ $git_status =~ 'Your branch'.+diverged ]]; then
  99. PS1+="${symbols[enter]}"
  100. fi
  101. PS1+=" "
  102. fi
  103. }
  104. # $1 - FG local
  105. # $2 - BG User
  106. # $3 - FG Remote
  107. # $4 - BG Root
  108. section_tty () {
  109. local TTY=`ps aux | grep $$ | grep bash | awk '{ print $7 }'`
  110. local SESS_SRC=`who | grep $TTY | awk '{ print $6 }'`
  111. local USR=0 # 0 = User | 1 = Root
  112. local FG=$1
  113. local BG=$2
  114. # Okay...Now who we be?
  115. if [ `/usr/bin/whoami` = "root" ] ; then
  116. USR=1
  117. BG=$4
  118. fi
  119. SSH_IP=`echo $SSH_CLIENT | awk '{ print $1 }'`
  120. if [ $SSH_IP ] ; then
  121. FG=$3
  122. else
  123. SSH_IP=`echo $SSH2_CLIENT | awk '{ print $1 }'`
  124. if [ $SSH_IP ]; then
  125. FG=$3
  126. fi
  127. fi
  128. if [ "$last_section_bg" ]; then
  129. section_separator $1 $2
  130. fi
  131. last_section_bg=$2
  132. PS1+="$(_CC $FG $BG) ${TTY} "
  133. }
  134. # NOTE: Removed this as it does not handle paths with spaces well.
  135. #short_PWD ()
  136. #{
  137. # local PRE= NAME="$1" LENGTH="$2";
  138. # [[ "$NAME" != "${NAME#$HOME/}" || -z "${NAME#$HOME}" ]] &&
  139. # PRE+='~' NAME="${NAME#$HOME}" LENGTH=$[LENGTH-1];
  140. # ((${#NAME}>$LENGTH)) && NAME="/...${NAME:$[${#NAME}-LENGTH+4]}";
  141. # echo "$PRE$NAME"
  142. #}
  143. set_prompt() {
  144. local lec="$?"
  145. if [ -z "$PROMPT_DIRTRIM" ]; then
  146. PROMPT_DIRTRIM=2
  147. fi
  148. local color=$GREEN
  149. local bg=$BG_LBLUE
  150. local smilie=" ${symbols[smilie]} "
  151. local DL="\033[1B"
  152. local UL2="\033[1A"
  153. if [ $COLOR_PROMPT = yes ]; then
  154. if [ $lec -ne 0 ]; then
  155. color=$RED
  156. smilie=" ${symbols[frownie]} "
  157. fi
  158. 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} "
  159. section "\n\033[1A$(printf "%*s" $(($(tput cols)-2)) "") \r" $BLUE $BG_GREY
  160. section $smilie $color $BG_BLACK 1 true
  161. section_tty $BLACK $BG_LBLUE $RED $BG_YELLOW
  162. section " \u@\h " $BLUE $BG_LCYAN
  163. section " \w " $LCYAN $BG_BLUE
  164. #section " $(short_PWD $PWD 20) " $LCYAN $BG_BLUE
  165. section_git_status $YELLOW $BG_LBLUE
  166. section "\n" $DEFAULT $BG_GREY
  167. section_separator
  168. PS1+="> \[\033[K\]"
  169. unset last_section_bg
  170. else
  171. PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w\$ "
  172. fi
  173. }
  174. PROMPT_COMMAND="set_prompt; $PROMPT_COMMAND"