强制"git status"在终端上输出颜色(在脚本内) [英] Force "git status" to output color on the terminal (inside a script)

查看:97
本文介绍了强制"git status"在终端上输出颜色(在脚本内)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建议对颜色进行解析是一个通常不正确的主意.

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!

原始问题如下.

我喜欢看颜色,因为我的脚本足够强大(到目前为止)可以处理颜色代码.看来我在这里背叛了话题,但老实说,我看不出必须解析脚本中的转义代码之类的东西有什么大不了的.如果颜色有助于交互式使用,为什么它们不能在脚本使用中帮助我聚集数据并处理比手工更多的数据?颜色会变得更重要吗?

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?

无论如何,我有一个简洁的小shell脚本,我写了一些git status输出,而我只是想使此脚本保持原样.设置了我的全局git配置,以便在git状态下以彩色显示更改和未跟踪的文件列表.不幸的是,与git diff不同,我无法找到为git status强制设置颜色的选项.

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.

要明确地说,这是问题所在:

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/"

不产生彩色的git status输出,您甚至可以在这里看到我正在将前导哈希字符显式转换为蓝色,因为它有助于突出显示脚本输出的不同区域.

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.

有人知道如何获得色彩吗?我是否可以使用一个可用作假终端" STDIN/STDOUT管道的标准程序?实际上,我也在开发pty伪终端工具,因此我可以为此目的当然使用它,但这是一个比较笨拙的解决方案(由于我尚未完成构建,因此尚未准备就绪).

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).

推荐答案

为避免更改git的配置,您可以通过使用-c传递配置变量来仅为当前命令启用颜色.

To avoid changing your git config, you can enable colour just for the current command by passing a config variable with -c.

对于 status 命令,变量为color.status:

    git -c color.status=always status | less -REX

对于 diff show log grep 命令,变量是color.ui:

    git -c color.ui=always diff | less -REX

请注意,-c必须在diff参数之前出现,而不是在之后.

Note that -c must come before the status or diff argument, and not after.

或者,对于 diff show log grep 命令,您可以在命令之后使用--color=always

Alternatively, for diff, show, log and grep commands, you can use --color=always after the command:

    git diff --color=always | less -REX


注意:正如史蒂文所说,如果您要提取有意义的数据,则可以使用--porcelain来获取更易于分析的输出,而不是解析颜色来提取含义.


Note: As Steven said, if you are trying to extract meaningful data, then instead of parsing colours to extract meaning, you can use --porcelain to get more parser-friendly output.

    git status --porcelain | awk ...

然后,如果需要,可以稍后重新引入颜色.

Then if you wanted, you could reintroduce colours later.

要获取用户配置的颜色,可以使用git config --get-colour:

To get the user's configured colours, you can use 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}"

更多示例此处.

这篇关于强制"git status"在终端上输出颜色(在脚本内)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆