Skip to content

Instantly share code, notes, and snippets.

@jonspeicher
Created January 24, 2010 22:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonspeicher/285486 to your computer and use it in GitHub Desktop.
Save jonspeicher/285486 to your computer and use it in GitHub Desktop.
Add colored status to your bash prompt while in a Mercurial repo.
# Define a few colors for later use. The escaped brackets tell bash that they
# are non-printable and keep word-wrapping sane.
TXT_RED='\['`tput setaf 1`'\]'
TXT_GREEN='\['`tput setaf 2`'\]'
TXT_RESET='\['`tput sgr0`'\]'
# Build a prompt decorator if we're in a hg repo. The branch name is included
# in green if the branch is default, red otherwise. A symbol appears if the
# working directory is not clean. This uses Steve Losh's excellent hg-prompt
# to get all the state with one Python init: http://bitbucket.org/sjl/hg-prompt
function parse_hg_branch {
hg_prompt_decorator=""
hg_info=`hg prompt "{root};{branch};{status}" 2> /dev/null`
pattern="^(.*);(.*);(.*)$"
if [[ ${hg_info} =~ ${pattern} ]]; then
root="${BASH_REMATCH[1]}"
branch="${BASH_REMATCH[2]}"
status="${BASH_REMATCH[3]}"
color=${TXT_RED}
if [[ ${branch} == "default" ]]; then
color=${TXT_GREEN}
fi
hg_prompt_decorator="[${color}${branch}${TXT_RESET}${status}] "
fi
}
# Set up a function that is run every time the shell prompt is generated.
function set_shell_prompt {
parse_hg_branch
PS1="\h:\W "${hg_prompt_decorator}"\$ "
}
export PROMPT_COMMAND=set_shell_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment