Skip to content

Instantly share code, notes, and snippets.

@bldewolf
Last active August 29, 2015 14:13
Show Gist options
  • Save bldewolf/df366e8347556252c886 to your computer and use it in GitHub Desktop.
Save bldewolf/df366e8347556252c886 to your computer and use it in GitHub Desktop.
256 color per-host foreground color picker
#!/bin/bash
# source me, I'm a shell script!
function _foreground_calc {
# reject these colors for being too dark
local reject=([0]=a [16]=a [17]=a [18]=a [19]=a [232]=a [233]=a [234]=a [235]=a [236]=a [237]=a [238]=a)
echo "$1" | shasum | while read -n2 num; do
local num=$((16#$num))
if [ -z "${reject[$num]}" ]; then
echo $num
return
fi
done
}
_path_calc_pwd=
_path_calc_color=
function _path_calc {
if [ "$_path_calc_pwd" != "$1" ]; then
_path_calc_color="$(_foreground_calc "$1")"
_path_calc_pwd="$1"
fi
}
PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND; }'_path_calc "$PWD"'
#export PS1='[\u@\[\e[38;5;'"$(_foreground_calc $(hostname))"'m\]\h\[\e[0m\] \[\e[38;5;${_path_calc_color}m\]\W\[\e[0m\]]\$ '
_calc_tmp="\[\e[38;5;$(_foreground_calc $(hostname))m\]\\h\[\e[0m\]"
PS1="${PS1/\\h/$_calc_tmp}"
_calc_tmp="\[\e[38;5;\${_path_calc_color}m\]\\W\[\e[0m\]"
PS1="${PS1/\\W/$_calc_tmp}"
unset _calc_tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment