From c6a671912516eb2014a42c7665d0e3b0456f73f4 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Wed, 21 Oct 2015 13:34:06 +0100 Subject: [PATCH 1/1] Show number of background jobs in the prompt. If __ps1_bg is set to 1, show any background jobs in square brackets. The format is either [r/t] or [t] where r is the number of running jobs and t is the total number of running and stopped jobs. The shorter format is used when all background jobs are running. Nothing will be shown when no background jobs are being managed by the shell. --- .profile.d/ps1.bashrc | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/.profile.d/ps1.bashrc b/.profile.d/ps1.bashrc index 556a057..278f107 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. @@ -84,7 +90,7 @@ function __ps1() { # Default __ps1_user to 1. [ -z "$__ps1_user" ] && __ps1_user=1 - 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_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 $?)\w\[\033[$(__ps1_root $? 2>/dev/null)m\]\$\[\033[0m\] ' return 0 } @@ -170,8 +176,25 @@ 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 } -- 2.7.4