如何使用 bundle exec rake test 在 Windows gitbash 中获取颜色

How to get color in Windows gitbash using bundle exec rake test

我正在关注 Rails 教程。我在第 3.7.1 节,我试图在 运行 bundle exec rake test 时在我的 gitbash 命令提示符中获取颜色。我已经尝试安装 ansi、win32console gem 和名为 turn 的 gem,但似乎没有任何效果。

当运行 bundle exec rake test时是否有可能得到红色和绿色?

您绝对可以显示彩色输出。但不能使用标准 Windows cmd.exe 或 powershell。宝石帮不上忙。您需要为您的系统添加 正确解释 ANSI 转义码 的能力——这是 cmd 和 powershell 无法做到的。 (ANSI.sys 是在 DOS 下处理 ANSI 转义码的方式。cmd 和 powershell 不要使用它。)

您需要 (1) 安装 ANSICON and/or (2) 安装并使用不同的 console/console 模拟器或 shell 来处理颜色(ANSI 转义码)。

(1) ANSICON 是 Windows 的一个程序,它解释 ANSI 转义码以便显示颜色。它运行 'on top of' cmd 或 powershell。您可以对其进行设置,以便在您使用 cmd 或 powershell 时始终使用它,或者仅在特定时间使用它。大多数人都安装了它并且非常满意。 Jason Karns wrote a blog post 关于安装和使用 ANSICON for git bash(在 Windows 上)人们发现有帮助。

(2) 以下是替代 cmd/powershell 的流行选项。这些是控制台模拟器和工具以及 shell 替代品:

  • ConEmu - ("ConEmu-Maximus5") 这是控制台模拟器,而不是 shell。您可以将它与任何 shell(cmd、powershell、cygwin、bash 等)一起使用。但是在日常使用中,一旦你设置好了,你就可以像使用cmd.exe或powershell等一样使用它。(我用这个所以知道关于它比其他选择更多。我有配置让它根据需要使用不同的 git/ruby version/rails 版本配置启动。我不使用 ANSICON 因为我在需要打开一个时使用 ConEmu控制台。)
  • Console 2 - Like ConEmu, this is a console emulator that adds functionality above and beyond cmd.exe (including displaying ANSI colors). You can use it with existing shells (e.g. powershell, etc.) Here's a SO answer that talks about using Console 2.
  • Console Z - a fork of Console 2 that is current and actively maintained. I haven't ever used it, but here is some info on how you'd configure Console Z to work with git bash.

  • 您也可以(或代替)使用类 Unix shell(或套件):

    • Cygwin 是在 Windows 中运行的最著名的类 Unix 命令套件。附带 shells(例如 MinTTY),可以使用并将与 ANSI 代码一起使用
      • Babun 位于 cygwin 安装之上的 windows shell。可配置;您可以创建配置(例如 git bash 或 rvm 或其他)并将它们作为插件共享。 (使用 mitty 控制台。)
    • MinGW - 一个 "Minimalist GNU for Windows" 具有用于 windows 的 GNU 工具集,专注于开发所需的那些工具。 (IOW,它比 Cygwin 提供的集合更小且略有不同。)DevKit,用于将 ruby gems 编译为本机 windows 代码,使用 MinGW。
    • MinSYS - “"Minimal SYStem" 的[A] 缩写,是 Bourne Shell 命令行解释器系统。作为 Microsoft cmd.exe 的替代品提供,它提供通用命令行环境,特别适合与 MinGW 一起使用,用于将许多开源应用程序移植到 MS-Windows 平台;Cygwin-1.3 的轻量级分支,它包括一小部分选择 Unix 工具是为了方便 objective。” (来自 MinGW 网站:)

某些搜索可能会出现其他选项。这些是最常讨论和使用的选项(基于与其他人讨论 IRL、Whosebug,以及现在和以前进行的搜索。)

安装并设置这些选项之一(不会花很长时间),然后您就可以看到 RSpec 或 git 或其他任何使用的有用颜色。