Skip to content

Instantly share code, notes, and snippets.

@adamhepton
Last active September 4, 2015 10:29
Show Gist options
  • Save adamhepton/a686c5739d6432c38324 to your computer and use it in GitHub Desktop.
Save adamhepton/a686c5739d6432c38324 to your computer and use it in GitHub Desktop.
If you SSH between boxes regularly and want a little mental signpost, here's something for 256-colour terminals you might like that gives you colours to use in your prompt based on hostname.
# ... insert this somewhere in your .bash_profile or .bashrc
if [ -f ~/.bash_prompt ]; then
. ~/.bash_prompt
fi
# Host-specific calculation
LC_CTYPE=C
ASCII_TOTAL=0
for ((i=0; i<${#HOSTNAME}; i++)); do
VAL=$(printf '%d' "'${HOSTNAME:$i:1}")
ASCII_TOTAL=$(( ${ASCII_TOTAL}+${VAL} ))
done
ASCII_TOTAL=$(( ${ASCII_TOTAL} % 255 ))
if [ ${ASCII_TOTAL} = ${i} ]
then
ASCII_TOTAL=199
fi
COL_HOST_BG=$(tput setab ${ASCII_TOTAL})
COL_HOST_FG=$(tput setaf ${i})
# Colour commands
COL_BRIGHT=$(tput bold)
COL_REVERSE=$(tput smso)
COL_REVERSE_OFF=$(tput rmso)
COL_NORMAL=$(tput sgr0)
# Unicode Characters
CH_TRIANGLE_R=$'\xe2\x96\xba'
HOST='\[$COL_HOST_BG$COL_HOST_FG$COL_BRIGHT\]\h '
WORKING_DIR='\[$COL_NORMAL$COL_REVERSE$COL_HOST_BG$COL_HOST_FG\] \w '
END='\[$COL_REVERSE_OFF$COL_HOST_BG$COL_HOST_FG\] \[$CH_TRIANGLE_R\] \[$COL_NORMAL\] '
PS1=\[$HOST$WORKING_DIR$END\]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment