Skip to content

Instantly share code, notes, and snippets.

@ldotlopez
Last active August 30, 2022 16:20
Show Gist options
  • Save ldotlopez/443e5421cbfa45a8a9f3359d7504d835 to your computer and use it in GitHub Desktop.
Save ldotlopez/443e5421cbfa45a8a9f3359d7504d835 to your computer and use it in GitHub Desktop.
function color_get_ps1 {
local hostname="${1:-$(hostname)}"
hostname="${hostname/.*/}"
if [[ "$(uname -s)" == "Darwin" ]]
then
hashfunc=$(type -p md5)
else
hashfunc=$(type -p md5sum)
fi
local hash=$("$hashfunc" <<< "$hostname")
hash=${hash/ */}
if [[ "$TERM" == *256* ]]; then
_color_hash_to_rgb "$hash"
else
_color_hash_to_15bit "$hash"
fi
}
function _color_hash_to_15bit {
local hash="$1"
local bright=${hash:0:12}
local color=${hash:20:12}
printf '\033[%d;%dm' \
$[$(printf '%d' "0x${bright}")%3] \
$[$[$(printf '%d' "0x${color}")%5]+32]
}
function _color_hash_to_rgb {
local hash="$1"
local R=${hash:0:8}
local G=${hash:12:8}
local B=${hash:24:8}
printf '\033[38;2;%d;%d;%dm' \
$[$(printf '%d' "0x${R}")%256] \
$[$(printf '%d' "0x${G}")%256] \
$[$(printf '%d' "0x${B}")%256]
}
color_code="$(color_get_ps1 "$(hostname -s)")"
PS1='${debian_chroot:+($debian_chroot)}\['"${color_code}"'\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
export PS1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment