强制“git status”在终端上输出颜色(在脚本内)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16073708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Force "git status" to output color on the terminal (inside a script)
提问by Steven Lu
EDIT:
编辑:
I would like to make a recommendation that parsing colors is a generally ill-conceived idea.
我想提出一个建议,即解析颜色通常是一个构思不周的想法。
Part of why i wanted it was so I can both parse it and pass it along in my own script output. This is... okay, but it would probably be saner to use porcelain or some such and re-build the colored parts myself!
我想要它的部分原因是这样我既可以解析它,又可以在我自己的脚本输出中传递它。这是......好吧,但使用瓷器或类似的东西并自己重新构建彩色部件可能会更明智!
Original question follows.
原始问题如下。
I like to see color because my scripting is robust enough (so far) to handle the color codes. It does seem like I'm going against the grain here, but I honestly don't see what the big deal is about having to parse stuff like escape codes in scripts. If colors help for interactive use, why wouldn't they help in script use where I might be aggregating data and crunching even more data than I would manually? Wouldn't colors be even more important?
我喜欢看到颜色,因为我的脚本足够强大(到目前为止)来处理颜色代码。似乎我在这里违背了原则,但老实说,我不明白必须解析脚本中的转义码之类的东西有什么大不了的。如果颜色有助于交互式使用,为什么它们在脚本使用中没有帮助,我可能会聚合数据并处理比手动处理更多的数据?颜色不是更重要吗?
Anyway, I have a neat little shell script I wrote that munges git status
output, and i'm just looking to make this script keep the colors intact. My global git config is set so that the lists of changed and untracked files show up in color in the git status. Unfortunately unlike git diff
there is no option for forcing color for git status
that I can find.
无论如何,我有一个整洁的小 shell 脚本,我编写了 mungesgit status
输出,我只是想让这个脚本保持颜色完整。我设置了全局 git 配置,以便更改和未跟踪文件的列表在 git 状态中以颜色显示。不幸的是,与我可以找到的git diff
没有强制颜色的选项不同git status
。
To be abundantly clear, this is the issue:
非常清楚,这是问题:
$ git status
produces perfect output, but (excerpt from my script follows)
产生完美的输出,但是(以下是我脚本的摘录)
git status | sed "s/^#/\x1b[34m#[0m/"
produces no colored git status
output, and you can even see here that I'm explicitly converting the leading hash-characters to blue because it helps highlight the different regions of output from my script.
不产生彩色git status
输出,您甚至可以在这里看到我将前导哈希字符显式转换为蓝色,因为它有助于突出显示脚本中输出的不同区域。
Does anyone know how to get it to put out the colors? Is there maybe a standard program I can use that can be used as a "fake terminal" STDIN/STDOUT pipe? I am in fact also working on a pty pseudoterminal tool so I could certainly make use of that for this purpose, but it's a rather heavy-handed solution (and not ready for use yet as I haven't finished building it).
有谁知道如何让它熄灭颜色?是否有我可以使用的标准程序可以用作“假终端”STDIN/STDOUT 管道?事实上,我也在开发一个 pty 伪终端工具,所以我当然可以为此目的使用它,但它是一个相当笨拙的解决方案(并且还没有准备好使用,因为我还没有完成它的构建)。
回答by joeytwiddle
To avoid changing your git config, you can enable colour just for the current command by passing a config variable with -c
.
为了避免更改您的 git 配置,您可以通过将配置变量传递给当前命令来为当前命令启用颜色-c
。
For the status
command, the variable is color.status
:
对于status
命令,变量是color.status
:
git -c color.status=always status | less -REX
For diff
, show
, log
and grep
commands, the variable is color.ui
:
为diff
,show
,log
和grep
命令中,变量是color.ui
:
git -c color.ui=always diff | less -REX
Note that -c
must come beforethe status
or diff
argument, and not after.
需要注意的是-c
一定要来之前的status
或diff
论点,而不是之后。
Alternatively, for diff
, show
, log
and grep
commands, you can use --color=always
afterthe command:
或者,diff
,show
,log
和grep
命令,你可以使用--color=always
之后的命令:
git diff --color=always | less -REX
Note: If you are trying to extract meaningful data, instead of parsing colours to extract meaning, you can use --porcelain
to get more parser-friendly output.
注意:如果您试图提取有意义的数据,而不是通过解析颜色来提取含义,您可以使用--porcelain
来获得对解析器更友好的输出。
git status --porcelain | awk ...
Then if you wanted, you could reintroduce colours later.
然后,如果您愿意,您可以稍后重新引入颜色。
To get the user's configured colours, you can use git config --get-colour
:
要获取用户配置的颜色,您可以使用git config --get-colour
:
reset_color="$(tput sgr0)"
remote_branch_color="$(git config --get-color color.branch.remote white)"
echo "Pushing to ${remote_branch_color}${branch_name}${reset_color}"
Some more examples here.
这里还有一些例子。
回答by Steven Lu
EDIT:
编辑:
I would like to make a strong recommendation that parsing colors is a generally ill-conceived idea.
我想强烈建议解析颜色通常是一个构思不周的想法。
Part of why i wanted it was so I can both parse it and pass it along in my own script output. This is... okay, but it would probably be saner to use porcelain or some such and re-build the colored parts myself!
我想要它的部分原因是这样我既可以解析它,又可以在我自己的脚本输出中传递它。这是......好吧,但使用瓷器或类似的东西并自己重新构建彩色部件可能会更明智!
Original answer follows.
原答案如下。
I keep finding answers really quickly after asking questions. Something to do with thinking about a problem long enough to write it out that you formulate better approaches for solving it. Anyway, the solution to this is just
在提出问题后,我一直很快就能找到答案。与思考一个问题的时间足够长以将其写出来有关,以便您制定更好的方法来解决它。无论如何,解决这个问题的方法只是
git config color.status always
I imagine that a general purpose solution involves expect
or something pty
related to force any programs that require it into thinking they are on a terminal.
我想通用解决方案涉及expect
或pty
与强制任何需要它的程序认为它们在终端上有关。
回答by dragon788
I had this same issue when using a git alias that executes a shell command. Apparently the git shell doesn't inherit from the current environment, so it knows nothing about my coloring settings.
使用执行 shell 命令的 git 别名时,我遇到了同样的问题。显然 git shell 没有从当前环境继承,所以它对我的着色设置一无所知。
In addition to adding the global git color ui setting, I fixed this by making my alias look like below, its the secondary command that requires being told to use colors, as git will by default as of whatever 1.8.x version people have mentioned.
除了添加全局 git color ui 设置之外,我还通过使别名看起来像下面这样来解决这个问题,它是需要被告知使用颜色的辅助命令,因为从人们提到的任何 1.8.x 版本开始,git 默认情况下都会这样做。
[alias]
ignored = !git ls-files -v|grep --color '^h'
This produces equivalent colorized output now when run as the alias the same as if I just ran the command.
现在当作为别名运行时,这会产生等效的彩色输出,就像我刚刚运行命令一样。
For sed, this other answer appears to work more reliably, use tput. https://unix.stackexchange.com/a/45954
对于 sed,这个其他答案似乎更可靠,使用 tput。 https://unix.stackexchange.com/a/45954