123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- #!/bin/bash
-
- ######################################################
- ### things to do AFTER host/user-specific settings ###
- ######################################################
-
- #shellcheck disable=2120
- __bashum__lastrv() {
- local rvfile # path to return value cache file
- local lastrv # actual last return value
- local do_rm # true if we should remove the cache
- rvfile="$BASHUM__RV_TMP/$$.lastrv"
- lastrv="$(cat "$rvfile")"
- do_rm=false
- test "$1" == "-r" && do_rm=true
- if [ "0$lastrv" -gt 0 ];
- then
- echo "!$lastrv!"
- else
- echo ""
- fi
- $do_rm && rm -f "$rvfile"
- }
-
- __bashum__save_rv() {
- local rvfile="$BASHUM__RV_TMP/$$.lastrv"
- [ -w "${rvfile%/*}" ] && echo "$1" > "$rvfile"
- }
-
- __bashum__task_context() {
- $BASHUM_TASK_CTX || return 0
- local ctx # TaskWarrior context
- type -t task >/dev/null || return 1
- ctx=$(task _get rc.context)
- test -n "$ctx" || return
- echo -n "|$ctx"
- }
-
- #shellcheck disable=SC2154 disable=2016
- __bashum__mkps1() {
- # these functions must be already defined by ~/.bash/user/*.bashrc
- # and ~/.bash/host/*.bashrc
- test -n "$SSH_CONNECTION" \
- && echo -n "$(__bashum__mkps1user)" # username only when on ssh
- echo -n "$BASHUM_PS_COLOR_LWHITE@" # nice shiny at sign
- echo -n "$(__bashum__mkps1host)" # hostname colored per host
- echo -n "$BASHUM_PS_COLOR_LYELLOW" #\
- test -n "$BASHUM_CTXSYM" \
- && echo -n "[$BASHUM_CTXSYM]" # > bashum's context symbol
- echo -n '$(__bashum__task_context)' # > taskwarrior's context
- echo -n "$BASHUM_PS_COLOR_NORMAL" #/
- echo -n "$BASHUM_PS_COLOR_LWHITE:" # nice shiny colon
- echo -n "$BASHUM_PS_COLOR_BLUE\w$BASHUM_PS_COLOR_NORMAL" # current workdir
- echo -n "$BASHUM_PS_COLOR_GREEN" #\
- echo -n '$(__git_ps1 "(%s)")' # > git's PS1
- echo -n "$BASHUM_PS_COLOR_NORMAL" #/
- echo -n "$BASHUM_PS_COLOR_LRED" #\
- echo -n '$(__bashum__lastrv -r)' # > last exit status (nothing if zero)
- echo -n "$BASHUM_PS_COLOR_NORMAL" #/
- echo '$ ' # obligatory dollar
- }
-
- #shellcheck disable=SC2154
- __bashum__mkps2() {
- echo "$BASHUM_PS_COLOR_LWHITE·$BASHUM_PS_COLOR_BLUE>$BASHUM_PS_COLOR_YELLOW>$BASHUM_PS_COLOR_WHITE>$BASHUM_PS_COLOR_NORMAL "
- }
-
- # and use to assemble own PS1
- PS1=$(__bashum__mkps1)
- PS2=$(__bashum__mkps2)
-
-
- __bashum__set_title() {
- #
- # Show the currently running command in the terminal title:
- #
- # http://www.davidpashley.com/articles/xterm-titles-with-bash.html
- #
- case "$BASH_COMMAND" in
- *"\033]"0*)
- # The command is trying to set the title bar as well;
- # this is most likely the execution of $PROMPT_COMMAND.
- # In any case nested escapes confuse the terminal, so don't
- # output them.
- ;;
- "")
- echo -ne "\033]0;$(__bashum__mkicon) "
- echo -n "$(__bashum__lastrv)$(__bashum__mkhostid)$(__bashum__wdir normal)\$"
- echo -ne "\007"
- ;;
- *)
- echo -ne "\033]0;$(__bashum__mkicon) "
- echo -n "${BASH_COMMAND} ($(__bashum__mkhostid)$(__bashum__wdir normal))"
- echo -ne "\007"
- ;;
- esac
- }
-
- __bashum__wdir() {
- #
- # Create abbreviated form of workdir
- #
- local mode=${1:-normal}
- case $mode:$PWD in
- shrinky:*) __bashum__shrinkypath "$PWD" ;;
- normal:*) echo "${PWD/$HOME/\~}" ;;
- short:$HOME) echo "~" ;;
- short:$HOME/??????????) echo "${PWD/$HOME/\~}" ;;
- short:$HOME/*/*) echo "…${PWD##*/}" ;;
- short:$HOME/*) echo "${PWD/$HOME/\~}" ;;
- short:*) echo "$PWD" ;;
- esac
- }
-
- __bashum__shrinkypath() {
- #
- # Convert path $1 to small path
- #
- local path=$1
- case $path in
- $HOME) echo "~"; return ;;
- $HOME/*) echo -n "~"; __bashum__shrink_relpath "${path#$HOME/}" ;;
- *) echo "$path"; return ;;
- esac
- }
-
- __bashum__shrink_relpath() {
- #
- # Shrink relative path according to rules
- #
- # Rules are:
- #
- # * if path has 7 chars or less, it's left as is and printed;
- # * else if there is only one element, it's left as is, and path is printed'
- # * else if there are three or more elements, elements
- # between first and last are replaced by '...',
- # * if the first element has 3 chars or less, it's left as is
- # and path is printed,
- # * else, the first element is
- # * broken into words by period,
- # * and each word, if longer than 2 characters,
- # is shortened to its first character followed by '...'
- # * and the shortened elements are joined back by period,
- # * finally the path is printed (first element shortened. '...', and
- # last element intact)
- #
- local path=$1
- local elems
- local frst
- local last
- local elnum
- local elem
- test "${#path}" -le 7 && echo -n "$path" && return 0
- while IFS= read -r elem; do
- elems+=("$elem"); elnum=${#elems[@]}
- done <<<"$(sed 's|/|\n|' <<<"$path")"
- frst=${elems[0]}
- last=${elems[$((elnum-1))]}
- case $elnum in
- 0) true ;; # hardly possible (caught by -le 7 above)
- 1) echo -n "$path"; return ;;
- 2) __bashum__shrink_elem "$frst"; echo -n "/$last" ;;
- *) __bashum__shrink_elem "$frst"; echo -n "/…/$last" ;;
- esac
- }
-
- __bashum__shrink_elem() {
- #
- # Shrink element by words
- #
- local buff=$1
- local word
- local words
- local head=true
- words=(${buff//./ }); wnum=${#words[@]}
- test "$wnum" -eq 0 && echo -n "$buff" && return 0
- for word in "${words[@]}"; do
- $head || { echo -n .; head=false; }
- case $word in
- ?|??) echo -n "$word" ;;
- *) echo -n "${word:0:1}…" ;;
- esac
- done
- }
-
- #shellcheck disable=SC2016
- __bashum__mkpc() {
- #
- # Compose PROMPT_COMMAND body
- #
- echo -n '__bashum__save_rv $?;'
- case "$TERM" in
- xterm*|rxvt*|screen*)
- echo -n 'echo -ne "\033]0;'
- echo -n "$(__bashum__mkicon) "
- echo -n '$(__bashum__lastrv)'
- echo -n "$(__bashum__mkhostid)"
- echo -n '$(__bashum__wdir shrinky)'
- echo -n '\$'
- echo -n '\007"'
- ;;
- esac
- }
-
- __bashum__mkicon() {
- case "$SSH_CONNECTION" in
- "") echo -n ;;
- *) echo -n ;;
- esac
- test -n "$BASHUM_CTXSYM" \
- && echo " [$BASHUM_CTXSYM]"
- }
-
- __bashum__mkhostid() {
- test -n "$SSH_CONNECTION" || return
- echo "${HOSTNAME%%.*}:"
- }
-
- __bashum__setup_traps() {
- case "$TERM" in
- xterm*|rxvt*|screen*) trap __bashum__set_title DEBUG ;;
- esac
- }
-
- __bashum__setup_traps
-
- PROMPT_COMMAND=$(__bashum__mkpc)
-
- $BASHUM_BMO_NAG && bmo nag
|