# Environment variables: # # PS1_SHOWHOSTNAME # 'auto' (default): Only show hostname over SSH # 'yes': Always show hostname # anything else: Never show hostname __show-hostname () { case ${PS1_SHOWHOSTNAME} in (''|auto) [ "${SSH_CONNECTION}" ];; (yes) true;; (*) false;; esac } __have-gitprompt () { [ -f /usr/lib/git-core/git-sh-prompt ] } __set-title () { local title=${USER} if __show-hostname then title+="@${HOSTNAME}" fi title+=: local path=${PWD/~/\~} local git_root=$(git rev-parse --show-toplevel 2> /dev/null) if [ -z "${git_root}" -o "${git_root}" = ~ ] then title+=${path} else local project=$(basename "${git_root}") path=$(realpath --relative-to "${git_root}" "${PWD}") if [ "${path}" = . ] then title+=${project} else title+=${project}/${path} fi fi # Cf. console_codes(4): # # ESC ] 2 ; txt ST Set window title to txt. # # ST is a "string terminator", either "ESC \" or "BEL". echo -ne "\E]2;${title}\a" } # In order to know how much space PS1 takes, Bash needs us to delimit # non-printing characters with \[ and \]. __start-nonprinting () { if [ ${BUILDING_PS1} ] then echo -en '\[' fi } __end-nonprinting () { if [ ${BUILDING_PS1} ] then echo -en '\]' fi } __fontify () { local -A codes=( [bold]=1 [dim]=2 [red]=31 [green]=32 [blue]=34 ) local text=$1 shift __start-nonprinting echo -en '\E[' local i for ((i=1; i<=$#; i++)) do echo -en ${codes[${!i}]} if ((i<$#)) then echo -en ';' else echo -en 'm' fi done __end-nonprinting echo -en "${text}" __start-nonprinting echo -en '\E[0m' __end-nonprinting } __set-prompt () { BUILDING_PS1=t PS1="$(__write-context)\n$(__fontify '\$' dim) " unset BUILDING_PS1 } __current-column () { # Cf. console_codes(4) § ECMA-48 Status Report Commands local position read -sdR -p$'\E[6n' position local pattern='\[[0-9]+;([0-9]+)' if [[ ${position} =~ ${pattern} ]] then echo ${BASH_REMATCH[1]} else echo 0 fi } __signal-no-newline () { echo $(__fontify ∅ bold red) } __draw-rule () { local i line for ((i=$(__current-column); i