My dotfiles. Period.

post.bashrc 3.7KB

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