ZSH中的管道正在从Git日志输出中剥离颜色

时间:2020-12-19 14:08:42

I'm trying to utilize GRB's git helpers (https://github.com/garybernhardt/dotfiles/blob/master/.githelpers) for pretty printing my git log.

我正在尝试使用GRB的githelper (https://github.com/garybernhardt/dotfiles/blob/master/.githelpers)来漂亮地打印我的git日志。

The git log --graph --pretty="tformat:${LOG_FORMAT}" command from line #62 works just fine; printing with colors in iTerm. But as soon as it's piped to pretty_git_format there are no colors displayed.

第62行中的git log—graph—pretty=“tformat:${LOG_FORMAT}”命令工作得很好;在迭代中使用颜色进行打印。但是一旦它被导入到pretty_git_format,就不会显示任何颜色。

This has been working for several years until just a few weeks ago. Was there a change in zsh that I now need to configure differently?

这种方法已经使用了几年,直到几周前。我现在需要用不同的方式配置zsh吗?

I'm using iTerm2 and zsh version zsh 5.4.2 (x86_64-apple-darwin16.7.0).

我使用的是迭代m2和zsh版本zsh 5.4.2 (x86_64-苹果-达尔文16.7.0)。

2 个解决方案

#1


2  

The default behavior for git is to produce color if the output is being printed to a terminal (directly or through a git-spawned pager). When your output goes somewhere else, like a pipe, git turns off colors.

git的默认行为是,如果输出被打印到终端(直接或通过git衍生的寻呼机),则生成颜色。当输出到其他地方(如管道)时,git关闭颜色。

You can set the color.ui option to always on the command line like this: git -c color.ui=always log --graph --pretty="tformat:${LOG_FORMAT}" (and yes, that's where the -c option goes). If you want to do this frequently, it can be done with a shell alias in .gitconfig.

你可以设置颜色。ui选项总是在命令行上,比如:git -c颜色。ui=always log -graph -pretty="tformat:${LOG_FORMAT}"(是的,这就是-c选项的位置)。如果您希望经常这样做,可以使用.gitconfig中的shell别名来完成。

While you can set this in your .gitconfig as well, you probably don't want to. Most external programs assume color is turned off, and you can break other parts of git, as well as tools like editor integrations, if you set this in .gitconfig.

虽然您也可以在.gitconfig中设置它,但是您可能不想这样做。大多数外部程序假定颜色被关闭,并且您可以破坏git的其他部分,以及编辑器集成之类的工具,如果您将其设置为.gitconfig。

#2


1  

This started happening to me today, after updating to OSX High Sierra and updating my homebrew packages.

在更新到OSX High Sierra和更新我的自制程序包之后,这一切在我今天开始发生。

I suspect it is something to do with the latest Git version (2.15.0), as those release notes mention changing how the git log colors work:

我怀疑这与最新的Git版本(2.15.0)有关,因为这些发布说明提到了更改Git日志颜色的工作方式:

Fixes since v2.14
-----------------

 * "%C(color name)" in the pretty print format always produced ANSI
   color escape codes, which was an early design mistake.  They now
   honor the configuration (e.g. "color.ui = never") and also tty-ness
   of the output medium.

So I think Brian's solution is probably the best one, but I do note that it does seem to affect the column formatting of pretty_git_format, which I mentioned in a comment above.

所以我认为Brian的解决方案可能是最好的,但是我注意到它似乎确实影响了pretty_git_format的列格式,我在上面的评论中提到过。

#1


2  

The default behavior for git is to produce color if the output is being printed to a terminal (directly or through a git-spawned pager). When your output goes somewhere else, like a pipe, git turns off colors.

git的默认行为是,如果输出被打印到终端(直接或通过git衍生的寻呼机),则生成颜色。当输出到其他地方(如管道)时,git关闭颜色。

You can set the color.ui option to always on the command line like this: git -c color.ui=always log --graph --pretty="tformat:${LOG_FORMAT}" (and yes, that's where the -c option goes). If you want to do this frequently, it can be done with a shell alias in .gitconfig.

你可以设置颜色。ui选项总是在命令行上,比如:git -c颜色。ui=always log -graph -pretty="tformat:${LOG_FORMAT}"(是的,这就是-c选项的位置)。如果您希望经常这样做,可以使用.gitconfig中的shell别名来完成。

While you can set this in your .gitconfig as well, you probably don't want to. Most external programs assume color is turned off, and you can break other parts of git, as well as tools like editor integrations, if you set this in .gitconfig.

虽然您也可以在.gitconfig中设置它,但是您可能不想这样做。大多数外部程序假定颜色被关闭,并且您可以破坏git的其他部分,以及编辑器集成之类的工具,如果您将其设置为.gitconfig。

#2


1  

This started happening to me today, after updating to OSX High Sierra and updating my homebrew packages.

在更新到OSX High Sierra和更新我的自制程序包之后,这一切在我今天开始发生。

I suspect it is something to do with the latest Git version (2.15.0), as those release notes mention changing how the git log colors work:

我怀疑这与最新的Git版本(2.15.0)有关,因为这些发布说明提到了更改Git日志颜色的工作方式:

Fixes since v2.14
-----------------

 * "%C(color name)" in the pretty print format always produced ANSI
   color escape codes, which was an early design mistake.  They now
   honor the configuration (e.g. "color.ui = never") and also tty-ness
   of the output medium.

So I think Brian's solution is probably the best one, but I do note that it does seem to affect the column formatting of pretty_git_format, which I mentioned in a comment above.

所以我认为Brian的解决方案可能是最好的,但是我注意到它似乎确实影响了pretty_git_format的列格式,我在上面的评论中提到过。