My dotfiles. Period.

post.bashrc 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. local ctx # TaskWarrior context
  28. type -t task >/dev/null || return 1
  29. ctx=$(task _get rc.context)
  30. test -n "$ctx" || return
  31. echo -n "|$ctx"
  32. }
  33. #shellcheck disable=SC2154 disable=2016
  34. __bashum__mkps1() {
  35. # these functions must be already defined by ~/.bash/user/*.bashrc
  36. # and ~/.bash/host/*.bashrc
  37. test -n "$SSH_CONNECTION" \
  38. && echo -n "$(__bashum__mkps1user)" # username only when on ssh
  39. echo -n "$lwhite@" # nice shiny at sign
  40. echo -n "$(__bashum__mkps1host)" # hostname colored per host
  41. echo -n "$yellow" #\
  42. echo -n '$(__bashum__task_context)' # > taskwarrior's context
  43. echo -n "$normal" #/
  44. echo -n "$lwhite:" # nice shiny colon
  45. echo -n "$lblue\w$normal" # current workdir
  46. echo -n "$green" #\
  47. echo -n '$(__git_ps1 "(%s)")' # > git's PS1
  48. echo -n "$normal" #/
  49. echo -n "$lred" #\
  50. echo -n '$(__bashum__lastrv -r)' # > last exit status (nothing if zero)
  51. echo -n "$normal" #/
  52. echo '$ ' # obligatory dollar
  53. }
  54. #shellcheck disable=SC2154
  55. __bashum__mkps2() {
  56. echo "$white>$yellow>$lyellow>$normal "
  57. }
  58. # and use to assemble own PS1
  59. PS1=$(__bashum__mkps1)
  60. PS2=$(__bashum__mkps2)
  61. __bashum__set_title() {
  62. #
  63. # Show the currently running command in the terminal title:
  64. #
  65. # http://www.davidpashley.com/articles/xterm-titles-with-bash.html
  66. #
  67. case "$BASH_COMMAND" in
  68. *\033]0*)
  69. # The command is trying to set the title bar as well;
  70. # this is most likely the execution of $PROMPT_COMMAND.
  71. # In any case nested escapes confuse the terminal, so don't
  72. # output them.
  73. ;;
  74. "")
  75. echo -ne "\033]0;"
  76. echo -n "$(__bashum__lastrv)$(__bashum__mkhostid)${PWD/$HOME/\~}\$"
  77. echo -ne "\007"
  78. ;;
  79. *)
  80. echo -ne "\033]0;"
  81. echo -n "${BASH_COMMAND} ($(__bashum__mkhostid)${PWD/$HOME/\~})"
  82. echo -ne "\007"
  83. ;;
  84. esac
  85. }
  86. #shellcheck disable=SC2016
  87. __bashum__mkpc() {
  88. #
  89. # Compose PROMPT_COMMAND body
  90. #
  91. echo -n '__bashum__save_rv $?;'
  92. case "$TERM" in
  93. xterm*|rxvt*|screen*)
  94. echo -n 'echo -ne "\033]0;'
  95. echo -n '$(__bashum__lastrv)'
  96. echo -n "$(__bashum__mkhostid)"
  97. echo -n '${PWD/$HOME/\~}'
  98. echo -n '\$'
  99. echo -n '\007"'
  100. ;;
  101. esac
  102. }
  103. __bashum__mkhostid() {
  104. test -n "$SSH_CONNECTION" || return
  105. echo "${HOSTNAME%%.*}:"
  106. }
  107. __bashum__setup_traps() {
  108. case "$TERM" in
  109. xterm*|rxvt*|screen*) trap __bashum__set_title DEBUG ;;
  110. esac
  111. }
  112. __bashum__setup_traps
  113. PROMPT_COMMAND=$(__bashum__mkpc)
  114. tasknag