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.

228 lines
7.4 KiB

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