« Bash : Prompt avec statut SVN » : différence entre les versions

De Adadov.net wiki
Ligne 66 : Ligne 66 :
PROMPT_COMMAND=prompt
PROMPT_COMMAND=prompt
fi
fi
if [ $EUID -ne 0 ]; then
if [ $EUID -ne 0 ]; then
PS1=$UserPrompt" "
PS1=$UserPrompt" "

Version du 21 février 2012 à 16:46

Présentation

C'est suite à la lecture de cet article que j'ai décidé moi aussi de rafraîchir mon prompt.

Donc j'ai décidé de faire une petite variante, pour ma part je n'utilise pas GIT mais SVN.

J'ai donc repris la base qui était si bien expliquée et je l'ai un peu modifiée pour trois raisons.

  1. J'utilise Fedora et je ne connais pas Ubuntu mais je suppose qu'il existe de menues différences
  2. J'utilise SVN et non GIT. (Il serait tout à fait imaginable de faire un prompt qui gère les deux d'ailleurs ...)
  3. Je voulais conserver le comportement initial de $PROMPT_COMMAND qui, sous Fedora, adapte le titre de mon terminal (plus facile pour le repérer parmi les 50 ouverts)

J'en ai en plus profité pour sortir ce morceau de code du fichier .bashrc et le placer dans un endroit où il est valable pour tout le système. Il est donc situé dans /etc/profile.d/custom.sh.

custom.sh

# Colors
NoColor="\e[0;0m"
Cyan="\e[0;36m"
Green="\e[0;32m"
Red="\e[0;31m"
Yellow="\e[0;33m"

# Prompts chars
UserPrompt="$"
RootPrompt="#"

# Contextual prompt
prompt() {
	USERNAME=`whoami`
	HOSTNAME=`hostname`
	CURRENTPATH=`pwd | sed "s|$HOME|~|g"`
	
	LEFTPROMPT=$Cyan$USERNAME@$HOSTNAME":"$Yellow$CURRENTPATH
	let LEFTSIZE=$(echo -n "$USERNAME@$HOSTNAME:$CURRENTPATH" | wc -c)

	RIGHTPROMPT=""
	let RIGHTSIZE=0
	SVNSTATUS=$(svn status 2>&1)
	echo $SVNSTATUS | grep 'not a working copy' 2>&1 >/dev/null
	if [ $? -eq 1 ]; then
		echo $SVNSTATUS | grep -P '^\s*[AMDCR]' 2>&1 >/dev/null
		if [ $? -eq 0 ]; then
			RIGHTPROMPT=$Red
		else
			RIGHTPROMPT=$Green
		fi
		BRANCH=$(svn info | grep 'Repository Root' | sed -r 's|^.*/([a-z]+)$|\1|')
		RIGHTPROMPT=$RIGHTPROMPT"[SVN Repository $BRANCH]"
		let RIGHTSIZE=$(echo -n "[SVN Repository $BRANCH]" | wc -c)
	fi

	let BLANCKSIZE=${COLUMNS}-${LEFTSIZE}-${RIGHTSIZE}
	RIGHTPROMPT=$RIGHTPROMPT$NoColor

	echo -e -n "\n"$LEFTPROMPT
	printf "%$(($BLANCKSIZE))s"
	echo -e $RIGHTPROMPT
}

# Main prompt
if [ ! -z $PROMPT_COMMAND ]; then
	PROMPT_COMMAND="prompt;$PROMPT_COMMAND"
else
	PROMPT_COMMAND=prompt
fi

if [ $EUID -ne 0 ]; then
	PS1=$UserPrompt" "
else
	PS1=$RootPrompt" "
fi