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

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

问题描述

我想提出一个建议,即解析颜色通常是一个构思不周的想法.

我想要它的部分原因是这样我既可以解析它,又可以在我自己的脚本输出中传递它.这是......好吧,但使用瓷器或类似的东西并自己重新构建彩色部件可能会更明智!

原始问题如下.

<小时>

我喜欢看到颜色,因为我的脚本足够强大(到目前为止)来处理颜色代码.似乎我在这里违背了原则,但老实说,我不明白必须解析脚本中的转义码之类的东西有什么大不了的.如果颜色有助于交互式使用,为什么它们在脚本使用中没有帮助,我可能会聚合数据并处理比手动处理更多的数据?颜色会不会更重要?

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

非常清楚,这就是问题所在:

$ git status

产生完美的输出,但是(以下是我脚本的摘录)

git status |sed "s/^#/x1b[34m#[0m/"

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

有谁知道如何让它熄灭颜色?是否有我可以使用的标准程序可以用作假终端"STDIN/STDOUT 管道?事实上,我也在开发一个 pty 伪终端工具,所以我当然可以为此目的使用它,但它是一个相当笨拙的解决方案(并且还没有准备好使用,因为我还没有完成它的构建).

解决方案

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

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

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

对于diffshowlog>和grep命令,变量为color.ui:

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

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

或者,对于diffshowloggrep 命令,你可以使用 --color=always after 命令:

 git diff --color=always |少 -REX


注意:正如 Steven 所说,如果您想提取有意义的数据,那么您可以使用 --porcelain 来获得对解析器更友好的输出,而不是解析颜色来提取含义.>

 git status --porcelain |哇...

然后,如果您愿意,可以稍后重新引入颜色.

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

 reset_color=$(tput sgr0)";remote_branch_color=$(git config --get-color color.branch.remote white)";echo "推送到 ${remote_branch_color}${branch_name}${reset_color}";

更多示例 here一>.

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.

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.

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

解决方案

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

For the status command, the variable is color.status:

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

For diff, show, log and grep commands, the variable is color.ui:

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

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

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

    git diff --color=always | less -REX


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.

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

Some more examples here.

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

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