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

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

问题描述

我喜欢看颜色,因为我的脚本足够强壮(迄今为止)处理颜色代码。这看起来好像我在这里反对谷物,但我真的不知道要在脚本中解析诸如转义码之类的东西是什么大问题。如果颜色有助于交互式使用,为什么他们不能帮助脚本使用,我可能会聚集数据并处理比手动更多的数据?颜色不会变得更重要?



无论如何,我有一个整齐的小外壳脚本,我写道: git status 输出,我只是想让这个脚本保持颜色不变。我的全球git配置已设置,以便更改和未跟踪文件的列表在git状态下以彩色显示。不幸的是,与 git diff 不同,我无法为 git status 强制颜色。



清楚地说明,这是问题:

  $ git status 

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

  git status | seds / ^#/ \ x1b [34m#[0m /

code> git status 输出,你甚至可以在这里看到我明确地将前导散列字符转换为蓝色,因为它有助于突出显示脚本输出的不同区域。

有谁知道如何让它熄灭颜色?我可以使用的标准程序是否可以用作假终端STDIN / STDOUT管道?实际上,我也在研究一种伪终端工具,所以我可以肯定这是为了这个目的,但这是一个相当笨拙的解决方案(并没有准备好,因为我还没有完成它)。

解决方案

为了避免改变你的git配置,你可以通过使用<$ c
$ b

  git -c color.status = always status | code> -c 

less -REX

该变量用于 状态 命令。对于 差异 show log 变量是 color.ui

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

请注意 -c 必须> 状态 diff 参数变为,而不是之后。


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 do this just for the current command by passing the config variable with -c:

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

That variable is for the status command only. For diff, show and log the variable is color.ui:

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

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

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

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