Created by: iKevinY
I noticed that the hostname colours only support 6 different colours, even on terminals where many more colours are available. I used the following command to check all of the available colours on my xterm-256color
terminal with two different colour schemes (the default OS X Terminal theme with a white background and a darker one that I use on a regular basis):
for i in {1..255}; do tput setaf $i; echo -n "$i "; done
On the white background, there is an issue with colours 226–231 being extremely difficult (or impossible) to read. Therefore, I opted to use tput colors
to detect the number of colours that are available, and taking 75% of those colours. This has a couple benefits:
- The previous behaviour of the hostname colour algorithm chose a random colour in the interval [1, 6]. This implementation behaves exactly the same when the output of
tput colors
is 8. - 75% of 256 is 192, and choosing a colour in the interval [1, 192] avoids the use of colours 226–231 on
xterm-256color
terminals.
However, I'm not too certain about how well this solution will work on other systems, since I've only tested it on my OS X machine. Is it reasonable to assume that tput colors
will be available everywhere? If not, I presume it would be a pretty trivial patch (just falling back to % 6
if tput colors
is unavailable).