.bash_prompt (3219B)
1 # Emacs? -*- shell-script -*- please. 2 3 __show-hostname () 4 { 5 test "${SSH_CONNECTION}" 6 } 7 8 __set-title () 9 { 10 local title=${USER} 11 12 __show-hostname && title+=@${HOSTNAME} 13 14 # Empirically, no need to escape slashes in $HOME: Bash seems to 15 # parse ${VAR/#PAT/REP} by (1) splitting VAR, PAT & REP (2) *then* 16 # expanding PAT? 17 title+=:${PWD/#${HOME}/\~} 18 19 # Cf. console_codes(4): 20 # 21 # ESC ] 2 ; txt ST Set window title to txt. 22 # 23 # ST is a "string terminator", either "ESC \" or "BEL". 24 25 printf $'\E]2;%s\a' "${title}" 26 } 27 28 # Bash relies on \[ \] to distinguish non-printing characters in prompts. 29 __start-nonprinting () 30 { 31 test ${BUILDING_PS} && printf '\[' 32 } 33 34 __end-nonprinting () 35 { 36 test ${BUILDING_PS} && printf '\]' 37 } 38 39 __fontify () 40 { 41 local -r text=$1 42 shift 43 44 local -Ar codes=( 45 [bold]=1 46 [dim]=2 47 [reverse]=7 48 [red]=31 49 [green]=32 50 [blue]=34 51 ) 52 53 local -a params=() 54 local c 55 for c 56 do 57 params+=(${codes[${c}]}) 58 done 59 60 local params_seq 61 IFS=';' eval 'params_seq="${params[*]}"' 62 63 __start-nonprinting 64 printf $'\E[%sm' "${params_seq}" 65 __end-nonprinting 66 67 printf %s "${text}" 68 69 __start-nonprinting 70 printf $'\E[0m' 71 __end-nonprinting 72 } 73 74 __current-column () 75 { 76 # Cf. console_codes(4) ยง ECMA-48 Status Report Commands. 77 78 local position 79 read -sdR -p$'\E[6n' position 80 81 local -r pattern='\[[0-9]+;([0-9]+)' 82 83 [[ ${position} =~ ${pattern} ]] || { 84 echo 0 85 return 86 } 87 echo ${BASH_REMATCH[1]} 88 } 89 90 __signal-no-newline () 91 { 92 local -ir col=$(__current-column) 93 ((col > 1)) || return 94 95 __fontify โ bold red 96 echo 97 } 98 99 __signal-rc () 100 { 101 local -ir rc=$1 102 local -r rc_hex=$(printf %02x ${rc}) 103 104 if ((rc)) 105 then 106 __fontify ${rc_hex} bold red 107 else 108 __fontify ${rc_hex} dim 109 fi 110 } 111 112 __smart-term/set-ps1 () 113 { 114 local -ir rc=$1 115 116 PS1=$( 117 BUILDING_PS=t 118 __fontify '[ ' dim 119 120 __signal-rc ${rc} 121 printf ' ' 122 __fontify '\u' dim green 123 __show-hostname && { 124 __fontify @ dim 125 __fontify '\H' bold green 126 } 127 printf ' ' 128 __fontify '\w' bold blue 129 130 __fontify ' ]' dim 131 132 printf '\\n' 133 __fontify '\$' dim 134 printf ' ' 135 ) 136 } 137 138 __smart-term/refresh () 139 { 140 local -ir rc=$? 141 142 __set-title 143 __signal-no-newline 144 __smart-term/set-ps1 ${rc} 145 } 146 147 __smart-term/init () 148 { 149 # Prompts. 150 151 PROMPT_COMMAND=__smart-term/refresh 152 153 PS2=$( 154 BUILDING_PS=t 155 __fontify 'โฆ ' dim 156 ) 157 158 # Bindings. 159 160 bind -f ~/.bash_inputrc 161 162 # Unset the TTY's "stop" char, so that readline receives C-s. 163 tty -s && stty stop '' 164 } 165 166 __dumb-term/set-ps1 () 167 { 168 local -ir rc=$1 169 170 PS1=$( 171 BUILDING_PS=t 172 __fontify '[ ' dim 173 __signal-rc ${rc} 174 printf ' ' 175 __fontify '\W' bold blue 176 __fontify ' ]\$ ' dim 177 ) 178 } 179 180 __dumb-term/refresh () 181 { 182 local -ir rc=$? 183 __dumb-term/set-ps1 ${rc} 184 } 185 186 __dumb-term/init () 187 { 188 PROMPT_COMMAND=__dumb-term/refresh 189 } 190 191 192 case "${TERM}" 193 in 194 dumb*) __dumb-term/init ;; 195 *) __smart-term/init ;; 196 esac