简体   繁体   English

仅当我从某些终端登录时禁用 git 颜色

[英]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.我希望git在输出到可以处理颜色的设备时自动对输出进行着色,而在不能处理时不对其进行着色。 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.其中一些可以处理 ANSI 颜色,而另一些则不能。 On UNIX systems we used to have a database called TERMINFO which listed capabilities of each terminal.在 UNIX 系统上,我们曾经有一个名为TERMINFO的数据库,它列出了每个终端的功能。 It was easy to tell if a terminal supported color by checking the colors capability.通过检查colors功能很容易判断终端是否支持颜色。 If it was -1, then a program should definitely not send ANSI color sequences.如果它是 -1,那么程序绝对不应该发送 ANSI 颜色序列。

$ tput colors
-1

Ideally, git would use TERMINFO to automatically detect if ANSI color sequences are appropriate.理想情况下,git 将使用TERMINFO自动检测 ANSI 颜色序列是否合适。 But it doesn't and checks only isatty() .但它不会并且只检查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.我怀疑 git 开发人员添加TERMINFO支持并不是一个高优先级,所以我正在寻找任何能够提供相同功能的解决方法。

I already know how to disable git color using git config and that is not what I'm asking.我已经知道如何使用git config禁用 git color ,这不是我要问的。 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.我希望它仅在我从不支持 ANSI 颜色的终端(例如 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.我也已经看到了GIT_CONFIG_PARAMETERS="'color.ui=never'"环境变量,但是根据@bk2204 和@torek,该变量很快就会消失。

Variant 1: a shell function in your ~/.bashrc :变体 1: ~/.bashrc的 shell 函数:

if ! tput colors >/dev/null 2>&1; then
    git() {command git -c color.ui=never "$@"; }
fi

The disadvantage is it cannot be used in shell scripts.缺点是不能在 shell 脚本中使用。 Ie if you run a shell script git will try to use colors anyway.也就是说,如果你运行一个 shell 脚本, git无论如何都会尝试使用颜色。 So Variant 2: a shell script:所以变体2:一个shell脚本:

#! /bin/sh
if tput colors >/dev/null 2>&1; 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 .将脚本命名为git ,使其可执行并放入$PATH /usr/bin之前的目录中。 For example I have PATH that starts with $HOME/bin:$HOME/.local/bin:/usr/local/bin:/usr/bin:… ;例如,我的PATH$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我将个人脚本放入$HOME/bin并将系统范围的脚本放入/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 .为了让事情更简单,您可以将脚本命名为gitc类的名称,删除/usr/bin/并训练您的手指输入gitc而不是git Then you can put the script anywhere.然后你可以把脚本放在任何地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM