My dotfiles. Period.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. test "${#path}" -le 7 && echo -n "$path" && return 0
  139. elems=(${path//// }); elnum=${#elems[@]}
  140. frst=${elems[0]}
  141. last=${elems[$((elnum-1))]}
  142. case $elnum in
  143. 0) true ;; # hardly possible (caught by -le 7 above)
  144. 1) echo -n "$path"; return ;;
  145. 2) __bashum__shrink_elem "$frst"; echo -n "/$last" ;;
  146. *) __bashum__shrink_elem "$frst"; echo -n "/…/$last" ;;
  147. esac
  148. }
  149. __bashum__shrink_elem() {
  150. #
  151. # Shrink element by words
  152. #
  153. local buff=$1
  154. local word
  155. local words
  156. local head=true
  157. words=(${buff//./ }); wnum=${#words[@]}
  158. test "$wnum" -eq 0 && echo -n "$buff" && return 0
  159. for word in "${words[@]}"; do
  160. $head || { echo -n .; head=false; }
  161. case $word in
  162. ?|??) echo -n "$word" ;;
  163. *) echo -n "${word:0:1}…" ;;
  164. esac
  165. done
  166. }
  167. #shellcheck disable=SC2016
  168. __bashum__mkpc() {
  169. #
  170. # Compose PROMPT_COMMAND body
  171. #
  172. echo -n '__bashum__save_rv $?;'
  173. case "$TERM" in
  174. xterm*|rxvt*|screen*)
  175. echo -n 'echo -ne "\033]0;'
  176. echo -n "$(__bashum__mkicon) "
  177. echo -n '$(__bashum__lastrv)'
  178. echo -n "$(__bashum__mkhostid)"
  179. echo -n '$(__bashum__wdir shrinky)'
  180. echo -n '\$'
  181. echo -n '\007"'
  182. ;;
  183. esac
  184. }
  185. __bashum__mkicon() {
  186. case "$SSH_CONNECTION" in
  187. "") echo  ;;
  188. *) echo  ;;
  189. esac
  190. }
  191. __bashum__mkhostid() {
  192. test -n "$SSH_CONNECTION" || return
  193. echo "${HOSTNAME%%.*}:"
  194. }
  195. __bashum__setup_traps() {
  196. case "$TERM" in
  197. xterm*|rxvt*|screen*) trap __bashum__set_title DEBUG ;;
  198. esac
  199. }
  200. __bashum__setup_traps
  201. PROMPT_COMMAND=$(__bashum__mkpc)
  202. $BASHUM_BMO_NAG && bmo nag