My dotfiles. Period.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. normal:*) echo "${PWD/$HOME/\~}" ;;
  94. short:$HOME) echo "~" ;;
  95. short:$HOME/??????????) echo "${PWD/$HOME/\~}" ;;
  96. short:$HOME/*/*) echo "…${PWD##*/}" ;;
  97. short:$HOME/*) echo "${PWD/$HOME/\~}" ;;
  98. short:*) echo "$PWD" ;;
  99. esac
  100. }
  101. #shellcheck disable=SC2016
  102. __bashum__mkpc() {
  103. #
  104. # Compose PROMPT_COMMAND body
  105. #
  106. echo -n '__bashum__save_rv $?;'
  107. case "$TERM" in
  108. xterm*|rxvt*|screen*)
  109. echo -n 'echo -ne "\033]0;'
  110. echo -n "$(__bashum__mkicon) "
  111. echo -n '$(__bashum__lastrv)'
  112. echo -n "$(__bashum__mkhostid)"
  113. echo -n '$(__bashum__wdir short)'
  114. echo -n '\$'
  115. echo -n '\007"'
  116. ;;
  117. esac
  118. }
  119. __bashum__mkicon() {
  120. case "$SSH_CONNECTION" in
  121. "") echo  ;;
  122. *) echo  ;;
  123. esac
  124. }
  125. __bashum__mkhostid() {
  126. test -n "$SSH_CONNECTION" || return
  127. echo "${HOSTNAME%%.*}:"
  128. }
  129. __bashum__setup_traps() {
  130. case "$TERM" in
  131. xterm*|rxvt*|screen*) trap __bashum__set_title DEBUG ;;
  132. esac
  133. }
  134. __bashum__setup_traps
  135. PROMPT_COMMAND=$(__bashum__mkpc)
  136. $BASHUM_BMO_NAG && bmo nag