Created
December 3, 2013 15:30
-
-
Save jamesoff/7771165 to your computer and use it in GitHub Desktop.
zsh function to hash a string Use it for hostname colouring in your prompt!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function string_hash() { | |
HASHSTR=$1 | |
HASHSIZE=$2 | |
HASHVAL=0 | |
for i in {1..${#HASHSTR}}; do; | |
THISCHAR=$HASHSTR[$i] | |
HASHVAL=$(( $HASHVAL + $((#THISCHAR)) )) | |
done | |
# Avoid 0 as that's black | |
HASHSIZE=$(( $HASHSIZE - 1 )) | |
HASHVAL=$(( $HASHVAL % $HASHSIZE )) | |
HASHVAL=$(( $HASHVAL + 1 )) | |
echo $HASHVAL | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment