prompt.sh (2463B)
1 # Dynamic low-resource prompt for bash. 2 3 # define $_tty iff we know we're not on local desktop 4 unset _tty 5 _tty=$SSH_CONNECTION$SUDO_USER$DOAS_USER 6 [ "$XDG_SESSION_TYPE" = "tty" ] && _tty="yes" 7 8 # We use defined/undef logic on array indices to detect non-zero values inside 9 # the prompt, which is faster than shelling out 10 _noop[0]='array accesses turn 0/nonzero into defined/undef!' 11 12 # Detect colour support 13 case "$TERM" in 14 *[is]tty*|foot*|*color*) color_prompt=yes;; 15 *) [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null && color_prompt=yes;; 16 esac 17 18 # Show all of the characters for local git checkout state 19 GIT_PS1_SHOWDIRTYSTATE='y' 20 GIT_PS1_SHOWSTASHSTATE='y' 21 GIT_PS1_SHOWUNTRACKEDFILES='y' 22 GIT_PS1_DESCRIBE_STYLE='contains' 23 GIT_PS1_SHOWUPSTREAM='auto' 24 25 if [ "$color_prompt" = yes ]; then 26 GIT_PS1_SHOWCOLORHINTS='y' 27 else 28 # simpler, uncoloured version 29 unset GIT_PS1_SHOWCOLORHINTS 30 PS1='${_noop[$(($?==0))]:+($?)}${_noop[$((\j==0))]:+[\j]}' 31 PS1+='${debian_chroot:+($debian_chroot)}' 32 PS1+='${_tty:+\u${DOAS_USER:+<$DOAS_USER>}${SUDO_USER:+<$SUDO_USER>}@}' 33 PS1+='${SSH_CONNECTION:+\h:}\w' 34 type -t __git_ps1 > /dev/null && PS1+='$(__git_ps1 "{%s}")' 35 PS1+='\$ ' 36 return 37 fi 38 39 ################################# 40 # The full-colour prompt itself # 41 ################################# 42 43 # If the last exit code ($?) was non-zero, 44 # show it in red parentheses 45 PS1='${_noop[$(($?==0))]:+\[\e[1;91m\]($?)}' 46 # Likewise, show non-zero background/suspend count 47 # in yellow square brackets. 48 PS1+='${_noop[$((\j==0))]:+\[\e[1;33m\][\j]}' 49 # If the debian_chroot var is defined, show it in cyan. 50 PS1+='${debian_chroot:+\[\e[1;36m\]($debian_chroot)}' 51 # If this is our local machine and we didn't sudo, we should know 52 # our username already! Otherwise if it's "tty", show it in purple. 53 # If we did sudo/doas, show the username we came from in red. 54 PS1+='${_tty:+\[\e[1;35m\]\u\[\e[1;91m\]' 55 PS1+='${SUDO_USER:+<$SUDO_USER>}${DOAS_USER:+<$DOAS_USER>}' 56 PS1+='\[\e[1;33m\]@}' 57 # If we ssh'd here, show the hostname in green 58 PS1+='${SSH_CONNECTION:+\[\e[1;32m\]\h\[\e[1;33m\]:}' 59 # The only straightforward part: current directory in blue! 60 PS1+='\[\e[1;34m\]\w' 61 # If possible, show git branch info between gold curly braces 62 if type -t __git_ps1 > /dev/null; then 63 PS1+='$(__git_ps1 "\[\e[1;33m\]{%s\[\e[1;33m\]}")' 64 fi 65 # Finally, the classic $ or # that is either bold default, 66 # or blinking red if $? is nonzero 67 PS1+='\[\e[$(($?==0?0:91));$(($?==0?1:5))m\]\$\[\e[0m\] ' 68 69