MIAOU-BASH is a collection of settings and helpers for leveraging BASH. Developer-friendly, it may be used as solo package with or without the miaou project.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
8.0 KiB

9 months ago
9 months ago
7 months ago
9 months ago
7 months ago
7 months ago
7 months ago
9 months ago
9 months ago
9 months ago
7 months ago
9 months ago
9 months ago
7 months ago
9 months ago
7 months ago
9 months ago
7 months ago
9 months ago
7 months ago
9 months ago
7 months ago
7 months ago
7 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
  1. #!/bin/bash
  2. function __prompt_command {
  3. local EXIT="$?" # This needs to be first
  4. local R='\[\e[0m\]'
  5. local Red='\[\e[31m\]'
  6. local Gre='\[\e[32m\]'
  7. local Yel='\[\e[93m\]'
  8. local Blu='\[\e[34m\]'
  9. local Cya='\[\e[36m\]'
  10. local Mag='\[\e[95m\]'
  11. local Gra='\[\e[90m\]'
  12. local DIVERGENT_GIT_SYMBOL="${Yel}➾${R}"
  13. local SYNCHRONIZED_GIT_SYMBOL="${Gre}✔${R}"
  14. local PUSHED_NEEDED_SYMBOL='⥱'
  15. local TAG_NEEDED_SYMBOL='⤽'
  16. # set xterm title
  17. echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"
  18. PS1=''
  19. if [[ -n ${container_hostname:-} ]]; then
  20. PS1+="${Gra}[LXC:${R}"
  21. local host
  22. host=$(builtin echo $container_hostname | tr ' ' ':')
  23. case ${host:0:4} in
  24. 'beta')
  25. PS1+="${Yel}"
  26. ;;
  27. 'prod')
  28. PS1+="${Red}"
  29. ;;
  30. *) ;;
  31. esac
  32. PS1+="${host}${Gra}] "
  33. fi
  34. if [[ "$(id -u)" -eq 0 ]]; then
  35. PS1+="$Yel\u$R"
  36. PROMPT='$'
  37. else
  38. PS1+="$Gre\u$R"
  39. PROMPT='#'
  40. fi
  41. PS1+="${Gra}@"
  42. case ${HOSTNAME:0:4} in
  43. 'beta')
  44. PS1+="${Yel}"
  45. ;;
  46. 'prod')
  47. PS1+="${Red}"
  48. ;;
  49. *)
  50. PS1+="${R}"
  51. ;;
  52. esac
  53. PS1+="\h"
  54. local pwd='~'
  55. [ "$PWD" != "$HOME" ] && pwd=${PWD/#$HOME\//\~\/}
  56. PS1+=" ${Cya}${pwd}$(__vte_osc7)${R}"
  57. if hash git 2>&-; then
  58. # git command exists
  59. if git rev-parse --git-dir >/dev/null 2>&1; then
  60. # current dir is version controlled
  61. local branch tag_release dirty
  62. branch=$(git branch 2>/dev/null | grep -e ^* | cut -d ' ' -f2)
  63. PS1+=" ${R}[${Mag}${branch}${R}|"
  64. tag_release=$(git describe --tags 2>/dev/null | cut -d'-' -f1)
  65. dirty=$(git status -s | wc -l)
  66. if [[ $dirty -ne 0 ]]; then
  67. PS1+="${Yel}${DIVERGENT_GIT_SYMBOL}${R}|${tag_release}${Yel}…$dirty"
  68. else
  69. local ahead
  70. ahead=$(git rev-list --branches --not --remotes | wc -l)
  71. if [[ $ahead -ne 0 ]]; then
  72. PS1+="${DIVERGENT_GIT_SYMBOL}|${tag_release}${Yel}${PUSHED_NEEDED_SYMBOL}${ahead}"
  73. else
  74. if [[ -z $tag_release ]]; then
  75. PS1+="${SYNCHRONIZED_GIT_SYMBOL}"
  76. else
  77. local commit_ahead
  78. commit_ahead=$(git log "$tag_release"..HEAD --oneline | wc -l)
  79. if [[ $commit_ahead -gt 0 ]]; then
  80. PS1+="${DIVERGENT_GIT_SYMBOL}|${tag_release}${Cya}${TAG_NEEDED_SYMBOL}${commit_ahead}"
  81. else
  82. PS1+="${SYNCHRONIZED_GIT_SYMBOL}|${Cya}${tag_release}"
  83. fi
  84. fi
  85. fi
  86. fi
  87. PS1+="${R}]"
  88. fi
  89. fi
  90. if [ $EXIT != 0 ]; then
  91. PS1+="$Mag" # Add red if exit code non 0
  92. else
  93. PS1+="$Gra"
  94. fi
  95. PS1+=" $PROMPT ${R}"
  96. }
  97. function __vte_osc7 {
  98. # HINT: special character vte to get TILIX show proper hostname and pwd
  99. # Use \[...\] around the parts of PS1 that have length 0.
  100. # https://unix.stackexchange.com/questions/28827/why-is-my-bash-prompt-getting-bugged-when-i-browse-the-history#28828
  101. VTE_INFO="${HOSTNAME:-}"
  102. if [[ -n ${container_hostname:-} ]]; then
  103. local host
  104. host=$(builtin echo $container_hostname | tr ' ' ':')
  105. VTE_INFO="$host:$HOSTNAME"
  106. fi
  107. printf "\[\033]7;file://%s%s\a\]" "${VTE_INFO}" "$(__vte_urlencode "${PWD}")"
  108. }
  109. function __vte_urlencode {
  110. # This is important to make sure string manipulation is handled
  111. # byte-by-byte.
  112. LC_ALL=C
  113. str="$1"
  114. while [ -n "$str" ]; do
  115. safe="${str%%[!a-zA-Z0-9/:_\.\-\!\'\(\)~]*}"
  116. printf "%s" "$safe"
  117. str="${str#"$safe"}"
  118. if [ -n "$str" ]; then
  119. printf "%%%02X" "'$str"
  120. str="${str#?}"
  121. fi
  122. done
  123. }
  124. ###------
  125. ### MAIN
  126. ###------
  127. # If not running interactively, don't do anything
  128. case $- in
  129. *i*) ;;
  130. *) return ;;
  131. esac
  132. # don't put duplicate lines or lines starting with space in the history.
  133. HISTCONTROL=ignoreboth
  134. # append to the history file, don't overwrite it
  135. shopt -s histappend
  136. # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
  137. HISTSIZE=10000
  138. HISTFILESIZE=20000
  139. # check the window size after each command and, if necessary,
  140. # update the values of LINES and COLUMNS.
  141. shopt -s checkwinsize
  142. # make less more friendly for non-text input files, see lesspipe(1)
  143. [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
  144. # set a fancy prompt (non-color, unless we know we "want" color)
  145. case "$TERM" in
  146. xterm-color | *-256color)
  147. color_prompt=yes
  148. export COLOR_OPTIONS=' --color=auto'
  149. ;;
  150. esac
  151. export LS_COLORS="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.sql.gz=01;35:*.mariadb.gz=01;35:*.postgres.gz=01;35:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:"
  152. export LESS="r"
  153. # enable programmable completion features (you don't need to enable
  154. # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
  155. # sources /etc/bash.bashrc).
  156. if ! shopt -oq posix; then
  157. if [ -f /usr/share/bash-completion/bash_completion ]; then
  158. . /usr/share/bash-completion/bash_completion
  159. elif [ -f /etc/bash_completion ]; then
  160. . /etc/bash_completion
  161. fi
  162. fi
  163. # prevent CTRL+S to freeze in vim, CTRL+Q to unfreeze!
  164. stty -ixon
  165. PROMPT_COMMAND='__prompt_command' # Func to gen PS1 after CMDs
  166. MIAOU_BASH_DIR=/opt/miaou-bash
  167. # ALIASES
  168. alias sudo='sudo ' # smart sudo alias trick!
  169. alias ls='ls $COLOR_OPTIONS -h'
  170. alias ll='ls $COLOR_OPTIONS -lh'
  171. alias la='ls $COLOR_OPTIONS -lah'
  172. alias ltr='ls $COLOR_OPTIONS -ltrh'
  173. alias l=ls
  174. alias cd..='cd ..'
  175. alias ..=cd..
  176. alias grep='grep $COLOR_OPTIONS'
  177. alias fgrep='fgrep $COLOR_OPTIONS'
  178. alias egrep='egrep $COLOR_OPTIONS'
  179. alias transfer_cp='rsync -a --info=progress2 --no-inc-recursive'
  180. # shellcheck disable=SC2142
  181. alias ssu="ss -ntelp | perl -pne 'if(/uid:(\\d+)/){@a=getpwuid(\$1);s/uid:(\\d+)/user:\$a[0]/}' | awk '{ print \$4 \"\\t\" \$7 \"\\t\" \$6 }'"
  182. # shellcheck disable=SC2142
  183. alias ssu4="ss -ntel4p | perl -pne 'if(/uid:(\\d+)/){@a=getpwuid(\$1);s/uid:(\\d+)/user:\$a[0]/}' | awk '{ print \$4 \"\\t\\t\" \$7 \"\\t\\t\" \$6 }'"
  184. # Alias definitions.
  185. # You may want to put all your additions into a separate file like
  186. # ~/.bash_aliases, instead of adding them here directly.
  187. # See /usr/share/doc/bash-doc/examples in the bash-doc package.
  188. if [ -f "$HOME/.bash_aliases" ]; then
  189. . "$HOME/.bash_aliases"
  190. fi
  191. # Add path to any tools folder in /opt/miaou-*
  192. for i in {/opt,$HOME}/miaou-*/tools; do
  193. if [ -d "$i" ]; then
  194. PATH=$PATH:$i
  195. fi
  196. done
  197. ## Add path for TOOLBOX if any
  198. if [ -d "/TOOLBOX" ]; then
  199. PATH=$PATH:/TOOLBOX
  200. fi