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.

246 lines
7.9 KiB

8 months ago
8 months ago
6 months ago
8 months ago
6 months ago
8 months ago
8 months ago
8 months ago
6 months ago
8 months ago
8 months ago
6 months ago
8 months ago
6 months ago
8 months ago
6 months ago
8 months ago
6 months ago
8 months ago
6 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 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. # set xterm title
  15. echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"
  16. PS1=''
  17. if [[ -n ${container_hostname:-} ]]; then
  18. PS1+="${Gra}[LXC:${R}"
  19. local host
  20. host=$(builtin echo $container_hostname | tr ' ' ':')
  21. case ${host:0:4} in
  22. 'beta')
  23. PS1+="${Yel}"
  24. ;;
  25. 'prod')
  26. PS1+="${Red}"
  27. ;;
  28. *) ;;
  29. esac
  30. PS1+="${host}${Gra}] "
  31. fi
  32. if [[ "$(id -u)" -eq 0 ]]; then
  33. PS1+="$Yel\u$R"
  34. PROMPT='$'
  35. else
  36. PS1+="$Gre\u$R"
  37. PROMPT='#'
  38. fi
  39. PS1+="${Gra}@"
  40. case ${HOSTNAME:0:4} in
  41. 'beta')
  42. PS1+="${Yel}"
  43. ;;
  44. 'prod')
  45. PS1+="${Red}"
  46. ;;
  47. *)
  48. PS1+="${R}"
  49. ;;
  50. esac
  51. PS1+="\h"
  52. local pwd='~'
  53. [ "$PWD" != "$HOME" ] && pwd=${PWD/#$HOME\//\~\/}
  54. PS1+=" ${Cya}${pwd}$(__vte_osc7)${R}"
  55. if hash git 2>&-; then
  56. # git command exists
  57. if git rev-parse --git-dir >/dev/null 2>&1; then
  58. # current dir is version controlled
  59. local branch tag_release dirty
  60. branch=$(git branch 2>/dev/null | grep -e ^* | cut -d ' ' -f2)
  61. PS1+=" ${R}[${Mag}${branch}${R}|"
  62. tag_release=$(git describe --tags 2>/dev/null | cut -d'-' -f1)
  63. dirty=$(git status -s | wc -l)
  64. if [[ $dirty -ne 0 ]]; then
  65. PS1+="${Yel}${DIVERGENT_GIT_SYMBOL}${R}|${tag_release}${Yel}…$dirty"
  66. else
  67. local ahead
  68. ahead=$(git rev-list --branches --not --remotes | wc -l)
  69. if [[ $ahead -ne 0 ]]; then
  70. PS1+="${DIVERGENT_GIT_SYMBOL}|${tag_release}${Cya}↶${ahead}"
  71. else
  72. if [[ -z $tag_release ]]; then
  73. PS1+="${SYNCHRONIZED_GIT_SYMBOL}"
  74. else
  75. local commit_ahead
  76. commit_ahead=$(git log "$tag_release"..HEAD --oneline | wc -l)
  77. if [[ $commit_ahead -gt 0 ]]; then
  78. PS1+="${DIVERGENT_GIT_SYMBOL}|${tag_release}${Yel}↟${commit_ahead}"
  79. else
  80. PS1+="${SYNCHRONIZED_GIT_SYMBOL}|${Cya}${tag_release}"
  81. fi
  82. fi
  83. fi
  84. fi
  85. PS1+="${R}]"
  86. fi
  87. fi
  88. if [ $EXIT != 0 ]; then
  89. PS1+="$Mag" # Add red if exit code non 0
  90. else
  91. PS1+="$Gra"
  92. fi
  93. PS1+=" $PROMPT ${R}"
  94. }
  95. function __vte_osc7 {
  96. # HINT: special character vte to get TILIX show proper hostname and pwd
  97. # Use \[...\] around the parts of PS1 that have length 0.
  98. # https://unix.stackexchange.com/questions/28827/why-is-my-bash-prompt-getting-bugged-when-i-browse-the-history#28828
  99. VTE_INFO="${HOSTNAME:-}"
  100. if [[ -n ${container_hostname:-} ]]; then
  101. local host
  102. host=$(builtin echo $container_hostname | tr ' ' ':')
  103. VTE_INFO="$host:$HOSTNAME"
  104. fi
  105. printf "\[\033]7;file://%s%s\a\]" "${VTE_INFO}" "$(__vte_urlencode "${PWD}")"
  106. }
  107. function __vte_urlencode {
  108. # This is important to make sure string manipulation is handled
  109. # byte-by-byte.
  110. LC_ALL=C
  111. str="$1"
  112. while [ -n "$str" ]; do
  113. safe="${str%%[!a-zA-Z0-9/:_\.\-\!\'\(\)~]*}"
  114. printf "%s" "$safe"
  115. str="${str#"$safe"}"
  116. if [ -n "$str" ]; then
  117. printf "%%%02X" "'$str"
  118. str="${str#?}"
  119. fi
  120. done
  121. }
  122. ###------
  123. ### MAIN
  124. ###------
  125. # If not running interactively, don't do anything
  126. case $- in
  127. *i*) ;;
  128. *) return ;;
  129. esac
  130. # don't put duplicate lines or lines starting with space in the history.
  131. HISTCONTROL=ignoreboth
  132. # append to the history file, don't overwrite it
  133. shopt -s histappend
  134. # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
  135. HISTSIZE=10000
  136. HISTFILESIZE=20000
  137. # check the window size after each command and, if necessary,
  138. # update the values of LINES and COLUMNS.
  139. shopt -s checkwinsize
  140. # make less more friendly for non-text input files, see lesspipe(1)
  141. [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
  142. # set a fancy prompt (non-color, unless we know we "want" color)
  143. case "$TERM" in
  144. xterm-color | *-256color)
  145. color_prompt=yes
  146. export COLOR_OPTIONS=' --color=auto'
  147. ;;
  148. esac
  149. 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:"
  150. export LESS="r"
  151. # enable programmable completion features (you don't need to enable
  152. # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
  153. # sources /etc/bash.bashrc).
  154. if ! shopt -oq posix; then
  155. if [ -f /usr/share/bash-completion/bash_completion ]; then
  156. . /usr/share/bash-completion/bash_completion
  157. elif [ -f /etc/bash_completion ]; then
  158. . /etc/bash_completion
  159. fi
  160. fi
  161. # prevent CTRL+S to freeze in vim, CTRL+Q to unfreeze!
  162. stty -ixon
  163. PROMPT_COMMAND='__prompt_command' # Func to gen PS1 after CMDs
  164. MIAOU_BASH_DIR=/opt/miaou-bash
  165. # ALIASES
  166. alias sudo='sudo ' # smart sudo alias trick!
  167. alias ls='ls $COLOR_OPTIONS -h'
  168. alias ll='ls $COLOR_OPTIONS -lh'
  169. alias la='ls $COLOR_OPTIONS -lah'
  170. alias ltr='ls $COLOR_OPTIONS -ltrh'
  171. alias l=ls
  172. alias cd..='cd ..'
  173. alias ..=cd..
  174. alias grep='grep $COLOR_OPTIONS'
  175. alias fgrep='fgrep $COLOR_OPTIONS'
  176. alias egrep='egrep $COLOR_OPTIONS'
  177. alias transfer_cp='rsync -a --info=progress2 --no-inc-recursive'
  178. # shellcheck disable=SC2142
  179. 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 }'"
  180. # shellcheck disable=SC2142
  181. 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 }'"
  182. # Alias definitions.
  183. # You may want to put all your additions into a separate file like
  184. # ~/.bash_aliases, instead of adding them here directly.
  185. # See /usr/share/doc/bash-doc/examples in the bash-doc package.
  186. if [ -f "$HOME/.bash_aliases" ]; then
  187. . "$HOME/.bash_aliases"
  188. fi
  189. # Add path to any tools folder in /opt/miaou-*
  190. for i in {/opt,$HOME}/miaou-*/tools; do
  191. if [ -d "$i" ]; then
  192. PATH=$PATH:$i
  193. fi
  194. done
  195. ## Add path for TOOLBOX if any
  196. if [ -d "/TOOLBOX" ]; then
  197. PATH=$PATH:/TOOLBOX
  198. fi