From 5a45671be03119875f1c5b5f9afc7f35315dc4c6 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Fri, 20 Feb 2015 15:37:55 +0000 Subject: [PATCH] Enable a background highlight for the prompt. When __ps1_background is 1, set the background colour for the prompt according to the value of PROMPT_BACKGROUND_COLOUR. Use "prompt show background" or "prompt hide background" to control __ps1_background and hence the highlighting. --- .profile.d/ps1.bashrc | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/.profile.d/ps1.bashrc b/.profile.d/ps1.bashrc index 528b49d..556a057 100644 --- a/.profile.d/ps1.bashrc +++ b/.profile.d/ps1.bashrc @@ -47,6 +47,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 +58,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 +69,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 +84,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_colon $?)\w\[\033[$(__ps1_root $? 2>/dev/null)m\]\$\[\033[0m\] ' return 0 } @@ -90,17 +93,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 +120,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 } -- 2.20.1