hackerb9
hackerb9

Reputation: 1912

Disabling git colors only when I log in from certain terminals

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.

Upvotes: 0

Views: 218

Answers (1)

phd
phd

Reputation: 94407

Variant 1: a shell function in your ~/.bashrc:

if [ `tput colors` -lt 2 ]; then
    git() {command git -c color.ui=never "$@"; }
fi

The disadvantage is it cannot be used in shell scripts. I.e. if you run a shell script git will try to use colors anyway. So Variant 2: a shell script:

#! /bin/sh
if [ `tput colors` -ge 2 ]; then
    exec /usr/bin/git "$@"
else
    exec /usr/bin/git -c color.ui=never "$@"
fi

Name the script git, make it executable and put in a directory that precede /usr/bin in $PATH. For example I have PATH that starts with $HOME/bin:$HOME/.local/bin:/usr/local/bin:/usr/bin:…; I put personal scripts into $HOME/bin and system-wide scripts into /usr/local/bin

To make things simpler you could name the script something like gitc, remove /usr/bin/ and train your fingers to type gitc instead of git. Then you can put the script anywhere.

Upvotes: 1

Related Questions