X-Git-Url: http://git.iain.cx/?p=profile.git;a=blobdiff_plain;f=.profile.d%2Fps1.bashrc;h=991095bcca59aad63effe4eba9dcfef4b4e5c407;hp=528b49d67791a7b8aea6e4dc7bf75c0b21bd73ec;hb=2be76ed436fada8dffc368a0544575eb69098431;hpb=4f25d8785934b70ae74c04e300041da947b728b5 diff --git a/.profile.d/ps1.bashrc b/.profile.d/ps1.bashrc index 528b49d..991095b 100644 --- a/.profile.d/ps1.bashrc +++ b/.profile.d/ps1.bashrc @@ -22,7 +22,13 @@ # 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 sixth part of the prompt is the number of background jobs managed +# by the shell, in square brackets. If all background jobs are running, +# their number will be shown as [n]. If some are stopped, the number of +# running (r) and total (t) jobs will be shown as [r/t]. +# This part will be shown only if __ps1_bg is 1. By default it is 0. + # The final part of the prompt is the (full) working directory and $ string. # If the shell is running as root the # string's colour can be changed by # modifying $ROOT_OK_COLOUR and $ROOT_FAILED_COLOUR. @@ -47,6 +53,7 @@ # SVN: magenta. case $(tput colors) in 256) + PROMPT_BACKGROUND_COLOUR="0;48;5;26" PROMPT_OK_COLOUR="1;38;5;34" PROMPT_FAILED_COLOUR="1;38;5;160" ROOT_OK_COLOUR="0" @@ -57,6 +64,7 @@ case $(tput colors) in ;; 88) + PROMPT_BACKGROUND_COLOUR="0;48;5;18" PROMPT_OK_COLOUR="1;38;5;24" PROMPT_FAILED_COLOUR="1;38;5;48" ROOT_OK_COLOUR="0" @@ -67,6 +75,7 @@ case $(tput colors) in ;; *) + PROMPT_BACKGROUND_COLOUR="0;44" PROMPT_OK_COLOUR="1;32" PROMPT_FAILED_COLOUR="1;31" ROOT_OK_COLOUR="0" @@ -81,7 +90,7 @@ function __ps1() { # Default __ps1_user to 1. [ -z "$__ps1_user" ] && __ps1_user=1 - PS1='\[\033[0m\]$(__ps1_user $? \u@)\[\033[$(__ps1_col $? 2>/dev/null)m\]$(__ps1_user $? \h)\[\033[$(__ps1_colour_escape $? $GIT_COLOUR)m\]$(__ps1_git $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $P4_COLOUR)m\]$(__ps1_p4 $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $SVN_COLOUR)m\]$(__ps1_svn $? 2>/dev/null)\[\033[0m\]$(__ps1_ret $? 2>/dev/null)$(__ps1_colon $?)\w\[\033[$(__ps1_root $? 2>/dev/null)m\]\$\[\033[0m\] ' + PS1='\[\033[$(__ps1_background $?)m\]$(__ps1_user $? \u@)\[\033[$(__ps1_col $? 2>/dev/null)m\]$(__ps1_user $? \h)\[\033[$(__ps1_colour_escape $? $GIT_COLOUR $PROMPT_BACKGROUND_COLOUR)m\]$(__ps1_git $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $P4_COLOUR $PROMPT_BACKGROUND_COLOUR)m\]$(__ps1_p4 $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $SVN_COLOUR $PROMPT_BACKGROUND_COLOUR)m\]$(__ps1_svn $? 2>/dev/null)\[\033[$(__ps1_background $?)m\]$(__ps1_ret $? 2>/dev/null)$(__ps1_bg $?)$(__ps1_colon $?)$(__ps1_short $?)\[\033[$(__ps1_root $? 2>/dev/null)m\]\$\[\033[0m\] ' return 0 } @@ -90,17 +99,26 @@ function __ps1_colour_escape() { local ret=$1; shift local bold="${1%%;*}" local colour="${1#*;}" + local bgcolour="${2#*;}" echo -en "${bold}m\033[$colour" + [ "$__ps1_background" = 1 ] && echo -en "m\033[$bgcolour" + return $ret +} + +function __ps1_background() { + local ret=$1; shift + echo -n "0" + [ "$__ps1_background" = 1 ] && echo -en "m\033[${PROMPT_BACKGROUND_COLOUR}" return $ret } function __ps1_col() { local ret=$1; shift if [ $ret -gt 0 ]; then - __ps1_colour_escape $ret "$PROMPT_FAILED_COLOUR" + __ps1_colour_escape $ret "$PROMPT_FAILED_COLOUR" "$PROMPT_BACKGROUND_COLOUR" else - __ps1_colour_escape $ret "$PROMPT_OK_COLOUR" + __ps1_colour_escape $ret "$PROMPT_OK_COLOUR" "$PROMPT_BACKGROUND_COLOUR" fi return $ret } @@ -108,9 +126,9 @@ function __ps1_col() { function __ps1_root() { local ret=$1; shift if [ $ret -gt 0 ]; then - __ps1_colour_escape $ret "$ROOT_FAILED_COLOUR" + __ps1_colour_escape $ret "$ROOT_FAILED_COLOUR" "$PROMPT_BACKGROUND_COLOUR" else - __ps1_colour_escape $ret "$ROOT_OK_COLOUR" + __ps1_colour_escape $ret "$ROOT_OK_COLOUR" "$PROMPT_BACKGROUND_COLOUR" fi return $ret } @@ -158,12 +176,75 @@ function __ps1_svn() { return $1 } +function __ps1_bg() { + [ "$__ps1_bg" = "1" ] || return $1 + local job + local running=0; for job in $(builtin jobs -pr); do running=$((running+1)); done + local total=0; for job in $(builtin jobs -p); do total=$((total+1)); done + [ $total = 0 ] && return $1 + if [ -z "$2" ]; then + [ "$__ps1_user" = "1" ] && echo -n " " + echo -n "[" + [ $running = $total ] || echo -n "$running/" + echo -n "$total]" + else + echo $2 + fi + return $1 +} + function __ps1_colon() { - local all="$__ps1_user$__ps1_git$__ps1_p4$__ps1_svn" + local all="$__ps1_user$__ps1_git$__ps1_p4$__ps1_svn$(__ps1_bg $1 1)" [ "${all/1/}" = "$all" ] || echo -n ":" return $1 } +function __ps1_short() { + local home=${HOME%%/} + local pwd=${PWD/#$home/\~} + local dirtrim=${PROMPT_DIRTRIM//[^0-9]/} + + if [ "${dirtrim:0:1}" = "0" ]; then + echo "$pwd" + return $1 + fi + + dirtrim=${dirtrim##0} + if [ -z "$dirtrim" ]; then + local prompt="$USER$HOSTNAME$pwd" + local width=$(((COLUMNS*2)/3)) + if [ ${#prompt} -le ${width:-53} ]; then + echo "$pwd" + return $1 + else + dirtrim=1 + fi + fi + + local dirname=${pwd##*/} + local basename=${pwd%/$dirname} + local reversed= + local component + for component in ${basename//\// }; do + reversed="$component $reversed" + done + local n=1 + local short= + for component in $reversed; do + [ $n = 1 -a "$PWD" = "$pwd" ] || short="/$short" + if [ $n -ge $dirtrim ]; then + short="${component:0:1}$short" + else + short="$component$short" + fi + n=$((n+1)) + done + + [ "${short:0:1}" = "~" ] || short="/$short" + echo "$short/$dirname" + return $1 +} + function prompt() { local blurb="Usage: prompt hide|show "