My dotfiles. Period.

post.bashrc 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "$lwhite@" # nice shiny at sign
  41. echo -n "$(__bashum__mkps1host)" # hostname colored per host
  42. echo -n "$yellow" #\
  43. echo -n '$(__bashum__task_context)' # > taskwarrior's context
  44. echo -n "$normal" #/
  45. echo -n "$lwhite:" # nice shiny colon
  46. echo -n "$lblue\w$normal" # current workdir
  47. echo -n "$green" #\
  48. echo -n '$(__git_ps1 "(%s)")' # > git's PS1
  49. echo -n "$normal" #/
  50. echo -n "$lred" #\
  51. echo -n '$(__bashum__lastrv -r)' # > last exit status (nothing if zero)
  52. echo -n "$normal" #/
  53. echo '$ ' # obligatory dollar
  54. }
  55. #shellcheck disable=SC2154
  56. __bashum__mkps2() {
  57. echo "$lwhite·$blue>$yellow>$white>$normal "
  58. }
  59. # and use to assemble own PS1
  60. PS1=$(__bashum__mkps1)
  61. PS2=$(__bashum__mkps2)
  62. __bashum__set_title() {
  63. #
  64. # Show the currently running command in the terminal title:
  65. #
  66. # http://www.davidpashley.com/articles/xterm-titles-with-bash.html
  67. #
  68. case "$BASH_COMMAND" in
  69. *"\033]"0*)
  70. # The command is trying to set the title bar as well;
  71. # this is most likely the execution of $PROMPT_COMMAND.
  72. # In any case nested escapes confuse the terminal, so don't
  73. # output them.
  74. ;;
  75. "")
  76. echo -ne "\033]0;$(__bashum__mkicon) "
  77. echo -n "$(__bashum__lastrv)$(__bashum__mkhostid)$(__bashum__wdir normal)\$"
  78. echo -ne "\007"
  79. ;;
  80. *)
  81. echo -ne "\033]0;$(__bashum__mkicon) "
  82. echo -n "${BASH_COMMAND} ($(__bashum__mkhostid)$(__bashum__wdir normal))"
  83. echo -ne "\007"
  84. ;;
  85. esac
  86. }
  87. __bashum__wdir() {
  88. #
  89. # Create abbreviated form of workdir
  90. #
  91. local mode=${1:-normal}
  92. case $mode:$PWD in
  93. shrinky:*) __bashum__shrinkypath "$PWD" ;;
  94. normal:*) echo "${PWD/$HOME/\~}" ;;
  95. short:$HOME) echo "~" ;;
  96. short:$HOME/??????????) echo "${PWD/$HOME/\~}" ;;
  97. short:$HOME/*/*) echo "…${PWD##*/}" ;;
  98. short:$HOME/*) echo "${PWD/$HOME/\~}" ;;
  99. short:*) echo "$PWD" ;;
  100. esac
  101. }
  102. __bashum__shrinkypath() {
  103. #
  104. # Convert path $1 to small path
  105. #
  106. local path=$1
  107. case $path in
  108. $HOME) echo "~"; return ;;
  109. $HOME/*) echo -n "~"; __bashum__shrink_relpath "${path#$HOME/}" ;;
  110. *) echo "$path"; return ;;
  111. esac
  112. }
  113. __bashum__shrink_relpath() {
  114. #
  115. # Shrink relative path according to rules
  116. #
  117. # Rules are:
  118. #
  119. # * if path has 7 chars or less, it's left as is and printed;
  120. # * else if there is only one element, it's left as is, and path is printed'
  121. # * else if there are three or more elements, elements
  122. # between first and last are replaced by '...',
  123. # * if the first element has 3 chars or less, it's left as is
  124. # and path is printed,
  125. # * else, the first element is
  126. # * broken into words by period,
  127. # * and each word, if longer than 2 characters,
  128. # is shortened to its first character followed by '...'
  129. # * and the shortened elements are joined back by period,
  130. # * finally the path is printed (first element shortened. '...', and
  131. # last element intact)
  132. #
  133. local path=$1
  134. local elems
  135. local frst
  136. local last
  137. local elnum
  138. local elem
  139. test "${#path}" -le 7 && echo -n "$path" && return 0
  140. while IFS= read -r elem; do
  141. elems+=("$elem"); elnum=${#elems[@]}
  142. done <<<"$(sed 's|/|\n|' <<<"$path")"
  143. frst=${elems[0]}
  144. last=${elems[$((elnum-1))]}
  145. case $elnum in
  146. 0) true ;; # hardly possible (caught by -le 7 above)
  147. 1) echo -n "$path"; return ;;
  148. 2) __bashum__shrink_elem "$frst"; echo -n "/$last" ;;
  149. *) __bashum__shrink_elem "$frst"; echo -n "/…/$last" ;;
  150. esac
  151. }
  152. __bashum__shrink_elem() {
  153. #
  154. # Shrink element by words
  155. #
  156. local buff=$1
  157. local word
  158. local words
  159. local head=true
  160. words=(${buff//./ }); wnum=${#words[@]}
  161. test "$wnum" -eq 0 && echo -n "$buff" && return 0
  162. for word in "${words[@]}"; do
  163. $head || { echo -n .; head=false; }
  164. case $word in
  165. ?|??) echo -n "$word" ;;
  166. *) echo -n "${word:0:1}…" ;;
  167. esac
  168. done
  169. }
  170. #shellcheck disable=SC2016
  171. __bashum__mkpc() {
  172. #
  173. # Compose PROMPT_COMMAND body
  174. #
  175. echo -n '__bashum__save_rv $?;'
  176. case "$TERM" in
  177. xterm*|rxvt*|screen*)
  178. echo -n 'echo -ne "\033]0;'
  179. echo -n "$(__bashum__mkicon) "
  180. echo -n '$(__bashum__lastrv)'
  181. echo -n "$(__bashum__mkhostid)"
  182. echo -n '$(__bashum__wdir shrinky)'
  183. echo -n '\$'
  184. echo -n '\007"'
  185. ;;
  186. esac
  187. }
  188. __bashum__mkicon() {
  189. case "$SSH_CONNECTION" in
  190. "") echo  ;;
  191. *) echo  ;;
  192. esac
  193. }
  194. __bashum__mkhostid() {
  195. test -n "$SSH_CONNECTION" || return
  196. echo "${HOSTNAME%%.*}:"
  197. }
  198. __bashum__setup_traps() {
  199. case "$TERM" in
  200. xterm*|rxvt*|screen*) trap __bashum__set_title DEBUG ;;
  201. esac
  202. }
  203. __bashum__setup_traps
  204. PROMPT_COMMAND=$(__bashum__mkpc)
  205. $BASHUM_BMO_NAG && bmo nag