Skip to content

Instantly share code, notes, and snippets.

@akrito
Created March 31, 2011 19:19
Show Gist options
  • Save akrito/897027 to your computer and use it in GitHub Desktop.
Save akrito/897027 to your computer and use it in GitHub Desktop.
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment