diff options
| author | Kévin Le Gouguec <kevin.legouguec@gmail.com> | 2016-11-23 07:14:21 +0100 |
|---|---|---|
| committer | Kévin Le Gouguec <kevin.legouguec@gmail.com> | 2017-01-16 07:21:45 +0100 |
| commit | d7279500270a28ad8588cc3ee17c1d18af0c73b5 (patch) | |
| tree | 0f369a7d5c1e19de7d56f93afe77a20ed6ba5828 /.bash_prompt | |
| parent | 992ba4284e114606215f597a62f0129076459d5d (diff) | |
| download | dotfiles-d7279500270a28ad8588cc3ee17c1d18af0c73b5.tar.xz | |
Rename script to fit existing convention
bash(1) already mentions _profile, _login, _logout and _history.
.bashrc adds .bash_aliases (although .bashrc itself is an exception).
Diffstat (limited to '.bash_prompt')
| -rw-r--r-- | .bash_prompt | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/.bash_prompt b/.bash_prompt new file mode 100644 index 0000000..a892c73 --- /dev/null +++ b/.bash_prompt @@ -0,0 +1,189 @@ +# 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 + local git_root=$(git root 2> /dev/null) + + if [ ${git_root} ] + then + title=$(basename ${git_root}) + else + local path=${PWD/~/\~} + + if __show-hostname + then + title="${USER}@${HOSTNAME}:${path}" + else + title="${USER}:${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=( + [red]=31 + [green]=32 + [blue]=34 + [bold]=1 + [dim]=2 + ) + + local text=$1 + shift + + __start-nonprinting + echo -en '\E[' + + 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 () +{ + local last_status=$1 + BUILDING_PS1=t + PS1='' + + if [ ${last_status} -ne 0 ] + then + PS1+=$(__fontify "${last_status} " bold red) + fi + + PS1+=$(__fontify '\u' green) + + if __show-hostname + then + PS1+=$(__fontify '@' dim) + PS1+=$(__fontify '\H' bold green) + fi + + PS1+=$(__fontify : dim) + PS1+=$(__fontify '\w' bold blue) + + if [ ${PS1_SHOWGITSTATUS} ] + then + PS1+=$(__fontify "$(__git_ps1 '(%s)')" red) + fi + + PS1+=$(__fontify '\$' dim) + PS1+=' ' + + 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 +} + +__draw-rule () +{ + local rule='' + local column=$(__current-column) + + for ((i=column; i<=COLUMNS; i++)) + do + rule+=— + done + __fontify ${rule} dim +} + +__refresh-terminal () +{ + local last_status=$? + + __set-title + __set-prompt ${last_status} + __draw-rule +} + + +if __have-gitprompt +then + . /usr/lib/git-core/git-sh-prompt + GIT_PS1_SHOWDIRTYSTATE=t + GIT_PS1_SHOWUPSTREAM=auto + + PS1_SHOWGITSTATUS=t +fi + +export PROMPT_COMMAND=__refresh-terminal |
