Question
I want git to automatically colorize the output when it is going to a device that can handle color and not colorize it when it cannot. How would one do this?
Background
I sometimes develop code for older machines using the machines themselves. Some of them can handle ANSI color and some of them cannot. On UNIX systems we used to have a database called TERMINFO which listed capabilities of each terminal. It was easy to tell if a terminal supported color by checking the colors capability. If it was -1, then a program should definitely not send ANSI color sequences.
$ tput colors
-1
Ideally, git would use TERMINFO to automatically detect if ANSI color sequences are appropriate. But it doesn't and checks only isatty(). I suspect it is not a high priority for the git developers to add TERMINFO support, so I'm looking for any workaround that will give the same functionality.
I already know how to disable git color using git config and that is not what I'm asking. I want it to only be disabled when I log in from a terminal that does not support ANSI colors, such as a Digital VT340.
I also have already seen the GIT_CONFIG_PARAMETERS="'color.ui=never'" environment variable, but according to @bk2204 and @torek, that variable is going to disappear soon.
Variant 1: a shell function in your
~/.bashrc:The disadvantage is it cannot be used in shell scripts. I.e. if you run a shell script
gitwill try to use colors anyway. So Variant 2: a shell script:Name the script
git, make it executable and put in a directory that precede/usr/binin$PATH. For example I havePATHthat starts with$HOME/bin:$HOME/.local/bin:/usr/local/bin:/usr/bin:…; I put personal scripts into$HOME/binand system-wide scripts into/usr/local/binTo make things simpler you could name the script something like
gitc, remove/usr/bin/and train your fingers to typegitcinstead ofgit. Then you can put the script anywhere.