在Windows 7上的Git Bash中,当运行Cucumber或rspec时,颜色显示为代码。

11
在Windows 7上的Git Bash中,当运行cucumber场景或rspec规格时,有时会出现颜色编码失败的情况。偶尔会随机修复(其中随机==我不知道我做了什么来修复它)。因此,当我运行以下命令时:
$ bundle exec cucumber features

或者

$ bundle exec rspec spec

不要看到这种颜色:

 ......

 3 scenarios (3 passed)
 6 steps (6 passed)

我看到了这样的内容:

我看到了这样的内容:

 [32m.[0m[32m.[0m[32m.[0m[32m.[0m[32m.[0m[32m.[0m

 3 scenarios ([32m3 passed[0m)
 6 steps ([32m6 passed[0m)

我知道这些是颜色的代码表示,但我不知道为什么它停止显示颜色,也不知道如何修复它。我错过了什么吗?
从git config --list命令的输出:
core.symlinks=false
core.autocrlf=true
color.diff=auto
pack.packsizelimit=2g
help.format=html
http.sslcainfo=/bin/curl-ca-bundle.crt 
sendemail.smtpserver=/bin/msmtp.exe 
user.name=John Uhri 
user.email= ***** 
color.branch=auto 
color.diff=auto 
color.interactive=auto 
color.status=auto 
core.repositoryformatversion=0 
core.filemode=false 
core.bare=false 
core.logallrefupdates=true
core.symlinks=false 
core.ignorecase=true 
core.hidedotfiles=dotGitOnly 
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/* 
branch.master.remote=origin 
branch.master.merge=refs/heads/master

1
“git config --list” 会输出什么内容? - ralphtheninja
7个回答

12
在Windows上,git Bash使用内置终端,该终端包含在cmd提示符中。如果安装cygwin,您可以使用mintty终端仿真器(在开始菜单上安装为“Cygwin Terminal”)。
为什么这很重要?因为Windows cmd提示符终端不解释ANSI转义序列。它使用了M $颜色控制方案。如果您使用的程序在Windows上没有切换到此方案,也没有通过过滤器,则会看到原始的转义字符。 Cygwin的mintty控制台完全支持这些代码。
如果通常颜色正常工作,则这是由于从移植中的cucumber/rspec中存在错误。当打印颜色或其他内容时,某人忽略了在Windows上进行检查。在此得到修复之前,以下Python脚本是一个解决方法:
#!/usr/bin/env python
# Filter ANSI escapes from stdin to stdout
from __future__ import print_function
from colorama import init
import sys
init()

for line in sys.stdin.readlines():
    print(line)

您需要安装colorama库才能实现此操作。然后只需将输出通过脚本进行管道传输:

$ bundle exec rspec spec | colorFilter.py

有没有办法让默认安装的msysgit(Git Bash)中的shell显示颜色? - Travis Northcutt
@tnorthcutt 问题不在于shell,而在于term。除非你运行cygwin打包的x-windows模拟器和其中一个终端模拟器,否则你将被困在Windows终端模拟器中。这是与cmd shell一起打包的。由于你无法更改模拟器,而这就是问题所在,你必须按照注释将ANSI转义序列转换为Windows命令。 - Spencer Rathbun

9

我该如何让它始终工作?现在每次运行git bash之前都必须这样做:( - naneri

3
我也遇到了同样的问题,似乎如果取消设置TERM环境变量,则可以重新正常工作。

2

我在Git For Windows (V2.5.3)中使用PowerShell(在Window的普通控制台中)解决了这个问题,方法是设置三个环境变量:

  1. TERM=msys
  2. PAGER='"C:\Program Files\Git\usr\bin\less.exe"' (注意双引号需要作为值的一部分)。
  3. LESS的值中包括--RAW-CONTROL-CHARS(所以我的其他首选项为:--LONG-PROMPT --no-init --quit-if-one-screen --RAW-CONTROL-CHARS)。

就 Git Bash(版本 2.19.windows.1)在 mintty 中运行而言,我只需要执行第三步。TERM 仍然是 xterm,PAGER 未设置。 - Jabberwock

2
我使用 @aslakhellesoy 的 "wac" 项目。这有点烦人,因为你必须记得将命令通过它进行处理。但这是我见过唯一可行的方法。 https://github.com/aslakhellesoy/wac

1
有趣的是,WAC去掉了颜色代码,但不显示颜色。 - y0mbo

1
尝试安装win32console gem。
gem install win32console

1
我之前已经做过了。没有用。我尝试卸载并重新安装宝石,但无济于事。 - y0mbo
你解决过这个问题吗?你使用的是64位机器吗?我在Windows基本cmd中遇到了同样的问题。真是让人恼火。 - Richard Fortune
@RichardFortune - 这个 gem 无法解决这个问题。你需要 (a) 安装 ANSICON(请参考 @corg 的答案),或者 (2) 使用除 cmd 之外的其他 shell:Console2、bash 和 ConEmu 是一些可以考虑的选项。我使用 ConEmu 并喜欢它。其他人喜欢 Console 2。 - aenw

0

Richard的建议,即使在cmd.exe(而不是PowerShell)中,对我也起作用。

我正在运行:Windows 7(64位)和Git-Bash:GNU bash,版本3.1.23(6)-release(i686-pc-msys)

我必须更改PAGER位置以匹配我的系统。

export PAGER='"C:\Program Files (x86)\Git\bin\less.exe"'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接