Skip to content

Instantly share code, notes, and snippets.

@zanshin
Forked from piger/zsh_hostname_color.zsh
Created April 12, 2014 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zanshin/10518401 to your computer and use it in GitHub Desktop.
Save zanshin/10518401 to your computer and use it in GitHub Desktop.
# assign a prompt color by hashing the letters of the hostname
# idea copied from the irssi script 'nickcolor.pl'
# Daniel Kertesz <daniel@spatof.org>
autoload -U colors
colors
setopt prompt_subst
colnames=(
black
red
green
yellow
blue
magenta
cyan
white
default
)
# Create color variables for foreground and background colors
for color in $colnames; do
eval f$color='%{${fg[$color]}%}'
eval b$color='%{${bg[$color]}%}'
done
# Hash the hostname and return a fixed "random" color
function _hostname_color() {
local chash=0
foreach letter ( ${(ws::)HOST[(ws:.:)1]} )
(( chash += #letter ))
end
local crand=$(( $chash % $#colnames ))
local crandname=$colnames[$crand]
echo "%{${fg[$crandname]}%}"
}
hostname_color=$(_hostname_color)
PROMPT='${hostname_color}%n${fdefault}@${hostname_color}%m${fdefault} %# '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment