My dotfiles. Period.

post.bashrc 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/bin/bash
  2. ######################################################
  3. ### things to do AFTER host/user-specific settings ###
  4. ######################################################
  5. #shellcheck disable=2120
  6. __bashum__lastrv() {
  7. local rvfile # path to return value cache file
  8. local lastrv # actual last return value
  9. local do_rm # true if we should remove the cache
  10. rvfile="$RV_TMP/$$.lastrv"
  11. lastrv="$(cat "$rvfile")"
  12. do_rm=false
  13. test "$1" == "-r" && do_rm=true
  14. if [ "0$lastrv" -gt 0 ];
  15. then
  16. echo "!$lastrv!"
  17. else
  18. echo ""
  19. fi
  20. $do_rm && rm -f "$rvfile"
  21. }
  22. __bashum__save_rv() {
  23. local rvfile="$RV_TMP/$$.lastrv"
  24. [ -w "${rvfile%/*}" ] && echo "$1" > "$rvfile"
  25. }
  26. __bashum__task_context() {
  27. $BASHUM_TASK_CTX || return 0
  28. local ctx # TaskWarrior context
  29. type -t task >/dev/null || return 1
  30. ctx=$(task _get rc.context)
  31. test -n "$ctx" || return
  32. echo -n "|$ctx"
  33. }
  34. #shellcheck disable=SC2154 disable=2016
  35. __bashum__mkps1() {
  36. # these functions must be already defined by ~/.bash/user/*.bashrc
  37. # and ~/.bash/host/*.bashrc
  38. test -n "$SSH_CONNECTION" \
  39. && echo -n "$(__bashum__mkps1user)" # username only when on ssh
  40. echo -n "$BASHUM_PS_COLOR_LWHITE@" # nice shiny at sign
  41. echo -n "$(__bashum__mkps1host)" # hostname colored per host
  42. echo -n "$BASHUM_PS_COLOR_LYELLOW" #\
  43. test -n "$BASHUM_CTXSYM" \
  44. && echo -n "[$BASHUM_CTXSYM]" # > bashum's context symbol
  45. echo -n '$(__bashum__task_context)' # > taskwarrior's context
  46. echo -n "$BASHUM_PS_COLOR_NORMAL" #/
  47. echo -n "$BASHUM_PS_COLOR_LWHITE:" # nice shiny colon
  48. echo -n "$BASHUM_PS_COLOR_BLUE\w$BASHUM_PS_COLOR_NORMAL" # current workdir
  49. echo -n "$BASHUM_PS_COLOR_GREEN" #\
  50. echo -n '$(__git_ps1 "(%s)")' # > git's PS1
  51. echo -n "$BASHUM_PS_COLOR_NORMAL" #/
  52. echo -n "$BASHUM_PS_COLOR_LRED" #\
  53. echo -n '$(__bashum__lastrv -r)' # > last exit status (nothing if zero)
  54. echo -n "$BASHUM_PS_COLOR_NORMAL" #/
  55. echo '$ ' # obligatory dollar
  56. }
  57. #shellcheck disable=SC2154
  58. __bashum__mkps2() {
  59. echo "$BASHUM_PS_COLOR_LWHITE·$BASHUM_PS_COLOR_BLUE>$BASHUM_PS_COLOR_YELLOW>$BASHUM_PS_COLOR_WHITE>$BASHUM_PS_COLOR_NORMAL "
  60. }
  61. # and use to assemble own PS1
  62. PS1=$(__bashum__mkps1)
  63. PS2=$(__bashum__mkps2)
  64. __bashum__set_title() {
  65. #
  66. # Show the currently running command in the terminal title:
  67. #
  68. # http://www.davidpashley.com/articles/xterm-titles-with-bash.html
  69. #
  70. case "$BASH_COMMAND" in
  71. *"\033]"0*)
  72. # The command is trying to set the title bar as well;
  73. # this is most likely the execution of $PROMPT_COMMAND.
  74. # In any case nested escapes confuse the terminal, so don't
  75. # output them.
  76. ;;
  77. "")
  78. echo -ne "\033]0;$(__bashum__mkicon) "
  79. echo -n "$(__bashum__lastrv)$(__bashum__mkhostid)$(__bashum__wdir normal)\$"
  80. echo -ne "\007"
  81. ;;
  82. *)
  83. echo -ne "\033]0;$(__bashum__mkicon) "
  84. echo -n "${BASH_COMMAND} ($(__bashum__mkhostid)$(__bashum__wdir normal))"
  85. echo -ne "\007"
  86. ;;
  87. esac
  88. }
  89. __bashum__wdir() {
  90. #
  91. # Create abbreviated form of workdir
  92. #
  93. local mode=${1:-normal}
  94. case $mode:$PWD in
  95. shrinky:*) __bashum__shrinkypath "$PWD" ;;
  96. normal:*) echo "${PWD/$HOME/\~}" ;;
  97. short:$HOME) echo "~" ;;
  98. short:$HOME/??????????) echo "${PWD/$HOME/\~}" ;;
  99. short:$HOME/*/*) echo "…${PWD##*/}" ;;
  100. short:$HOME/*) echo "${PWD/$HOME/\~}" ;;
  101. short:*) echo "$PWD" ;;
  102. esac
  103. }
  104. __bashum__shrinkypath() {
  105. #
  106. # Convert path $1 to small path
  107. #
  108. local path=$1
  109. case $path in
  110. $HOME) echo "~"; return ;;
  111. $HOME/*) echo -n "~"; __bashum__shrink_relpath "${path#$HOME/}" ;;
  112. *) echo "$path"; return ;;
  113. esac
  114. }
  115. __bashum__shrink_relpath() {
  116. #
  117. # Shrink relative path according to rules
  118. #
  119. # Rules are:
  120. #
  121. # * if path has 7 chars or less, it's left as is and printed;
  122. # * else if there is only one element, it's left as is, and path is printed'
  123. # * else if there are three or more elements, elements
  124. # between first and last are replaced by '...',
  125. # * if the first element has 3 chars or less, it's left as is
  126. # and path is printed,
  127. # * else, the first element is
  128. # * broken into words by period,
  129. # * and each word, if longer than 2 characters,
  130. # is shortened to its first character followed by '...'
  131. # * and the shortened elements are joined back by period,
  132. # * finally the path is printed (first element shortened. '...', and
  133. # last element intact)
  134. #
  135. local path=$1
  136. local elems
  137. local frst
  138. local last
  139. local elnum
  140. local elem
  141. test "${#path}" -le 7 && echo -n "$path" && return 0
  142. while IFS= read -r elem; do
  143. elems+=("$elem"); elnum=${#elems[@]}
  144. done <<<"$(sed 's|/|\n|' <<<"$path")"
  145. frst=${elems[0]}
  146. last=${elems[$((elnum-1))]}
  147. case $elnum in
  148. 0) true ;; # hardly possible (caught by -le 7 above)
  149. 1) echo -n "$path"; return ;;
  150. 2) __bashum__shrink_elem "$frst"; echo -n "/$last" ;;
  151. *) __bashum__shrink_elem "$frst"; echo -n "/…/$last" ;;
  152. esac
  153. }
  154. __bashum__shrink_elem() {
  155. #
  156. # Shrink element by words
  157. #
  158. local buff=$1
  159. local word
  160. local words
  161. local head=true
  162. words=(${buff//./ }); wnum=${#words[@]}
  163. test "$wnum" -eq 0 && echo -n "$buff" && return 0
  164. for word in "${words[@]}"; do
  165. $head || { echo -n .; head=false; }
  166. case $word in
  167. ?|??) echo -n "$word" ;;
  168. *) echo -n "${word:0:1}…" ;;
  169. esac
  170. done
  171. }
  172. #shellcheck disable=SC2016
  173. __bashum__mkpc() {
  174. #
  175. # Compose PROMPT_COMMAND body
  176. #
  177. echo -n '__bashum__save_rv $?;'
  178. case "$TERM" in
  179. xterm*|rxvt*|screen*)
  180. echo -n 'echo -ne "\033]0;'
  181. echo -n "$(__bashum__mkicon) "
  182. echo -n '$(__bashum__lastrv)'
  183. echo -n "$(__bashum__mkhostid)"
  184. echo -n '$(__bashum__wdir shrinky)'
  185. echo -n '\$'
  186. echo -n '\007"'
  187. ;;
  188. esac
  189. }
  190. __bashum__mkicon() {
  191. case "$SSH_CONNECTION" in
  192. "") echo -n  ;;
  193. *) echo -n  ;;
  194. esac
  195. test -n "$BASHUM_CTXSYM" \
  196. && echo " [$BASHUM_CTXSYM]"
  197. }
  198. __bashum__mkhostid() {
  199. test -n "$SSH_CONNECTION" || return
  200. echo "${HOSTNAME%%.*}:"
  201. }
  202. __bashum__setup_traps() {
  203. case "$TERM" in
  204. xterm*|rxvt*|screen*) trap __bashum__set_title DEBUG ;;
  205. esac
  206. }
  207. __bashum__setup_traps
  208. PROMPT_COMMAND=$(__bashum__mkpc)
  209. $BASHUM_BMO_NAG && bmo nag