A collection of scripts and configurations for Bash (and others)
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

126 lines
3.5KB

  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]="☹"
  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="${PS1}$(_CC ${1} ${2})${symbols[soft_separator]}"
  28. else
  29. if [ "$2" ]; then
  30. PS1="${PS1}$(_CC ${last_section_bg} ${2})${symbols[hard_separator]}"
  31. else
  32. PS1="${PS1}${NORMAL}$(_CFG ${last_section_bg})${symbols[hard_separator]}${NORMAL}"
  33. fi
  34. fi
  35. }
  36. # $1 - Content
  37. # $2 - Text (FG) color
  38. # $3 - BG Color
  39. function section(){
  40. if [ $last_section_bg ]; then
  41. $(section_separator $2 $3)
  42. fi
  43. last_section_bg=$3
  44. PS1="${PS1}$(_CC ${2} ${3})${1}"
  45. echo "$(_CC ${2} ${3})${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. $(section_separator $1 $2)
  64. last_section_bg=$2
  65. PS1="${PS1}${default}${symbols[git]} $branch "
  66. git_status=$(git status 2> /dev/null)
  67. # If nothing changes the color, we can spot unhandled cases.
  68. if [[ $git_status =~ 'Untracked files' ]]; then
  69. PS1="${PS1}${untracked}${symbols[untracked]}"
  70. fi
  71. if git stash show &>/dev/null; then
  72. PS1="${PS1}${stash}${symbols[stash]}"
  73. fi
  74. if [[ $git_status =~ 'Your branch is ahead' ]]; then
  75. PS1="${PS1}${ahead}${symbols[ahead]}"
  76. fi
  77. if [[ $git_status =~ 'Your branch is behind' ]]; then
  78. PS1="${PS1}${behind}${symbols[behind]}"
  79. fi
  80. if [[ $git_status =~ 'Changes to be committed' ]]; then
  81. PS1="${PS1}${staged}${symbols[tick]}"
  82. fi
  83. if [[ $git_status =~ 'Changed but not updated' ||
  84. $git_status =~ 'Changes not staged' ||
  85. $git_status =~ 'Unmerged paths' ]]; then
  86. PS1="${PS1}${dirty}${symbols[cross]}"
  87. fi
  88. if [[ $git_status =~ 'Your branch'.+diverged ]]; then
  89. PS1="${PS1}${diverged}${symbols[enter]}"
  90. fi
  91. fi
  92. }
  93. set_prompt() {
  94. local lec="$?"
  95. local color=$GREEN
  96. local bg=$BG_LBLUE
  97. local smilie=${symbols[smilie]}
  98. if [ $COLOR_PROMPT = yes ]; then
  99. if [ $lec -ne 0 ]; then
  100. color=$RED
  101. smilie=${symbols[frownie]}
  102. fi
  103. PS1="${debian_chroot:+($debian_chroot)}Hi" #$(_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} "
  104. #$(section $smilie $color $bg)
  105. $(section "\\\\u" $GREEN $BG_GREY)
  106. #$(section "\\\\w" $BLUE $BG_GREY)
  107. #$(section_git_status $YELLOW $BG_LBLUE)
  108. #$(section_separator)
  109. else
  110. PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w\$ "
  111. fi
  112. }
  113. PROMPT_COMMAND="set_prompt; $PROMPT_COMMAND"