Skip to content

Instantly share code, notes, and snippets.

@akrito
Created March 31, 2011 19:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akrito/897054 to your computer and use it in GitHub Desktop.
Save akrito/897054 to your computer and use it in GitHub Desktop.
All together
ps1_venv () {
if [ -n "$VIRTUAL_ENV" ]; then
printf ' (%s)' `basename $VIRTUAL_ENV`
fi
}
ps1_err () {
errcode=$?
local prompt=$1
printf "$(ansi_hash ${prompt})"
if [ "$errcode" != 0 ]; then
printf " \e[0;31m($errcode)"
fi
}
ansi_hash () {
# Colors a string based on a hash of its contents
# Usage: ansi_hash [-s style] [-o offset] text ..
style=0 # default is normal, 1 is bold, 4 is underline
offset=30 # default is normal, 90 is high intensity
OPTIND=0
while getopts s:o: a
do case "$a" in
s) style="$OPTARG";;
o) offset="$OPTARG";;
[?]) print >&2 "Usage: $0 [-s style] [-o offset] text ...";;
esac
done
shift `expr $OPTIND - 1`
hashme="$@"
hex=`echo "$hashme" | md5sum | cut -b 1-6`
decimal=$((0x$hex))
part=`expr $decimal % 8 + $offset`
printf '\e[%s;%sm%s\e[0m' $style $part $hashme
}
export PS1="\$(ps1_err \" \w\") \$(ansi_hash \$(__git_ps1))\$(ansi_hash \" \$(ps1_venv)\")\e[0m\n▷ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment