X-Git-Url: http://git.iain.cx/?p=profile.git;a=blobdiff_plain;f=.profile.d%2Fps1.bashrc;h=852c73652c99a42feca2102e2279fff362e22b15;hp=6f81794a47fe98f4ab90c4be63f4f8a1a6265056;hb=4987e750cc82a9be75f2cc304782abdafa474707;hpb=8c75606a52da5a4df6018134e0806e7041ca69e1 diff --git a/.profile.d/ps1.bashrc b/.profile.d/ps1.bashrc index 6f81794..852c736 100644 --- a/.profile.d/ps1.bashrc +++ b/.profile.d/ps1.bashrc @@ -1,18 +1,79 @@ -# $Id$ -# VERSION 1.2 (2001-10-31) +#!bash Coloured prompts. +# To use, add a call to __ps1 in your .bash_profile file. # -# coloured prompts for bash +# The prompt comprises multiple parts, some of which may be hidden by unsetting +# shell variables or using the ``prompt'' function. # -# prompt is user@host:/dir$ where host is highlighted in green if the last -# command exited 0 or in red (followed by the error code) otherwise +# The first part of the prompt is user@host where host is highlighted in +# green if the last command exited 0 or in red otherwise. +# This part will be shown only if __ps1_user is 1. By default it is 1. +# The success and failure colours can be changed by modifying +# $PROMPT_OK_COLOUR and $PROMPT_FAILURE_COLOUR respectively. # -# to use, add a call to __ps1 in your .bash_profile +# The second part of the prompt is taken from git-completion.bashrc. +# It is shown only if __ps1_git is 1. By default it is 0. +# +# The third part of the prompt is taken from p4-completion.bashrc. +# It is shown only if __ps1_p4 is 1. By default it is 0. +# +# The fourth part of the prompt is taken from svn-completion.bashrc. +# It is shown only if __ps1_svn is 1. By default it is 0. +# +# The fifth part of the prompt is the exit status of the last command. +# This part will be shown only if __ps1_user is set and the exit status is +# non-zero. +# +# The final part of the prompt is the (full) working directory and $ string. +# +# Colouring is performed by the __ps1_col() and __ps1_ret() functions. +# We redirect stderr to /dev/null when calling these functions to prevent +# bash complaining about not knowing them when you su to another user, +# retaining PS1 but not the function definitions. +# +# Note that $? is passed as an argument to - and is returned from - all +# functions. As $? is set following any shell activity it is only guaranteed +# to represent the return code of the last command at the beginning of __ps1(). +# By passing between subsequent functions we ensure that it is available for +# __ps1_ret(). +# + +# Pick a colour based on the terminal capabilities. +# OK: dark green. +# Failed: dark red. +# Git: royal blue. +# P4: yellow. +# SVN: magenta. +case $(tput colors) in + 256) + export PROMPT_OK_COLOUR="1;38;5;34" + export PROMPT_FAILED_COLOUR="1;38;5;160" + export GIT_COLOUR="0;38;5;33" + export SVN_COLOUR="0;38;5;127" + export P4_COLOUR="0;38;5;142" + ;; -export PROMPT_OK_COLOUR=32 -export PROMPT_FAILED_COLOUR=31 + 88) + export PROMPT_OK_COLOUR="1;38;5;24" + export PROMPT_FAILED_COLOUR="1;38;5;48" + export GIT_COLOUR="0;38;5;23" + export SVN_COLOUR="0;38;5;49" + export P4_COLOUR="0;38;5;56" + ;; + + *) + export PROMPT_OK_COLOUR="1;32" + export PROMPT_FAILED_COLOUR="1;31" + export GIT_COLOUR="0;36" + export SVN_COLOUR="0;35" + export P4_COLOUR="0;33" + ;; +esac function __ps1() { - export PS1='\u@\[\033[1;$(__ps1_col $?)m\]\h\[\033[0m\]$(__ps1_ret $?):\w\$ ' + # Default __ps1_user to 1. + [ -z "$__ps1_user" ] && __ps1_user=1 + + export PS1='$(__ps1_user $? \u@)\[\033[$(__ps1_col $? 2>/dev/null)m\]$(__ps1_user $? \h)\[\033[${GIT_COLOUR}m\]$(__ps1_git $? 2>/dev/null)\[\033[0m\]\[\033[${P4_COLOUR}m\]$(__ps1_p4 $? 2>/dev/null)\[\033[0m\]\[\033[${SVN_COLOUR}m\]$(__ps1_svn $? 2>/dev/null)\[\033[0m\]$(__ps1_ret $? 2>/dev/null):\w\$ ' return 0 } @@ -22,6 +83,67 @@ function __ps1_col() { } function __ps1_ret() { + [ "$__ps1_user" = "1" ] || return $1 [ $1 -gt 0 ] && echo -n " ($1)" - return 0 + return $1 +} + +function __ps1_user() { + local ret=$1; shift + [ "$__ps1_user" = "1" ] || return $ret + echo -n ${1+"$@"} + return $ret +} + +function __ps1_git() { + [ "$__ps1_git" = "1" ] || return $1 + if [ "$__ps1_user" = "1" ]; then + __git_ps1 ' %s' + else + __git_ps1 '%s' + fi + return $1 +} + +function __ps1_p4() { + [ "$__ps1_p4" = "1" ] || return $1 + if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" ]; then + __p4_ps1 ' %s' + else + __p4_ps1 '%s' + fi + return $1 +} + +function __ps1_svn() { + [ "$__ps1_svn" = "1" ] || return $1 + if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" -o "$__ps1_p4" = "1" ]; then + __svn_ps1 ' %s' + else + __svn_ps1 '%s' + fi + return $1 +} + +function prompt() { + local blurb="Usage: prompt hide|show " + + if [ $# -lt 2 ]; then + echo >&2 "$blurb" + return 1 + fi + + action="$1" + if [ ! "$action" = "hide" -a ! "$action" = "show" ]; then + echo >&2 "$blurb" + return 1 + fi + if [ "$action" = "hide" ]; then + action=0 + else + action=1 + fi + + what="$(echo $2 | env LANG= LC_ALL= LC_CTYPE= tr '[:upper:]' '[:lower:]')" + eval __ps1_$what=$action }