Skip to content

Instantly share code, notes, and snippets.

@piger
Last active December 30, 2015 17:09
Show Gist options
  • Save piger/7858892 to your computer and use it in GitHub Desktop.
Save piger/7858892 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 <[email protected]>
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