Better Bash Prompts

After reading Steve Losh’s article on fancy, mercurial empowered Zsh prompts, the idea has been floating around in my head. After checking out an example for Bash on Codeography, it was time. Here’s what I ended up with:

And here’s the source for this:

C() {

	local color=

	if [ -z $1 ]; then

		color="0"

	else

		if [ -z $2 ]; then

			color="$1"

		else

			if [ -z $3 ]; then

				color="$1;$2"

			else

				if [ -z $4 ]; then

					color="$1;$2;$3"

					echo -e "\033[$1;$2;$3m"

				fi

			fi

		fi

	fi

	echo -e "\\[\033[${color}m\\]"

}



sc_rev() {

	local rev=

	if [[ -d ".svn" ]]; then

		rev=$(svn info | sed -n -e '/^Revision: \([0-9]*\).*$/s//\1/p')

	else

		rev=$(hg log -l 1 2>/dev/null | sed -n -e '/^changeset: *[^0-9]*\([0-9]*\).*/s//\1/p')

	fi

	if [ -n "$rev" ]; then

		echo " r$rev"

	fi

}

scsw() {

	local sym=

	if [[ -d ".svn" ]]; then

		sym="$2"

	else

		if hg summary 2>/dev/null 1>/dev/null; then

			sym="$3"

		else

			sym="$1"

		fi

	fi

	echo $sym

}



PS1="$(C 1)\u$(C 2)@$(C 1)\h$(C 2):\w$(C 1)\$(sc_rev)$(C)\n$(C 2)\T$(C) \$(scsw \$ '$(C 1 33)☇$(C)' '$(C 1 34)☿$(C)') "