At my work, I use macOS with iTerm2 as a terminal. And iTerm2 has fancy escape codes for changing tab and window titles colors:
\033]6;1;bg;red;brightness;255\a
\033]6;1;bg;green;brightness;255\a
\033]6;1;bg;blue;brightness;255\a
So I thought that it will be nice to distinguish different ssh hosts by color. I found on Stack Overflow how to generate color from a string and wrote a python script that extracts host from command line arguments and prints fancy sequences:
#!/usr/bin/python
import sys
def get_host():
for arg in sys.argv[1:]:
if not arg.startswith('-'):
return arg
def str_to_color(s):
hash = 0
for c in s:
hash = ord(c) + ((hash << 5) - hash)
for i in range(3):
yield (hash >> (i * 8)) & 0xff
def generate_seqs(color):
seq = '\033]6;1;bg;{};brightness;{}\a'
names = ['red', 'green', 'blue']
for name, v in zip(names, color):
yield seq.format(name, v)
if __name__ == '__main__':
host = get_host()
if host:
color = str_to_color(host)
for seq in generate_seqs(color):
sys.stdout.write(seq)
In action:
➜ ./ssh_color.py mrw.wtf
]6;1;bg;red;brightness;173]6;1;bg;green;brightness;84]6;1;bg;blue;brightness;51
Now we need to create a bash/zsh function that will call our script,
run ssh
and reset color on exit:
ssh_color () {
ssh_color.py $* # I put script in /usr/local/bin/
trap 'echo -e "\033]6;1;bg;*;default\a"' INT EXIT
ssh $*
}
alias ssh=ssh_color
And it just works: