如何在Windows命令提示符中更改文本颜色

13

我有一个命令行程序,它会在屏幕上输出日志信息。

我希望错误信息以红色显示。是否有一些特殊的字符代码可以输出来将文本颜色切换为红色,然后再切换回白色?

我正在使用Ruby,但我想这在任何其他语言中都是一样的。

类似于:

red = "\0123" # character code
white = "\0223"

print "#{red} ERROR: IT BROKE #{white}"
print "other stuff"
14个回答

28

在Windows上,你有三种简单的方法可以做到:

require 'win32console'
puts "\e[31mHello, World!\e[0m"

现在你可以使用一个叫做red的小方法扩展String

 require 'win32console'
 class String
   def red
     "\e[31m#{self}\e[0m"
   end
 end

 puts "Hello, World!".red

你也可以像这样扩展字符串,以获得更多的颜色:

require 'win32console'

class String
  { :reset          =>  0,
    :bold           =>  1,
    :dark           =>  2,
    :underline      =>  4,
    :blink          =>  5,
    :negative       =>  7,
    :black          => 30,
    :red            => 31,
    :green          => 32,
    :yellow         => 33,
    :blue           => 34,
    :magenta        => 35,
    :cyan           => 36,
    :white          => 37,
  }.each do |key, value|
    define_method key do
      "\e[#{value}m" + self + "\e[0m"
    end
  end
end

puts "Hello, World!".red

或者,如果您可以安装gems:

gem install term-ansicolor

在你的程序中:

require 'win32console'
require 'term/ansicolor'

class String
  include Term::ANSIColor
end

puts "Hello, World!".red
puts "Hello, World!".blue
puts "Annoy me!".blink.yellow.bold
请参考 term/ansicolor 文档以获取更多信息和可能的用法。

1
或者,您可以使用gem“colorize”。注意在Windows上使用它时,您需要手动require 'win32console',因为“colorize”仅检查if RUBY_PLATFORM =~ /win32/,而在某些情况下,RUBY_PLATFORM返回i386-mingw32 - AZ.
我认为你缺少了一个 end - anon

7

3
你可以在这里阅读一篇好的插图文章:http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/ 我认为设置控制台文本颜色非常与语言相关。下面是MSDN上C#的示例:
for (int x = 0; x < colorNames.Length; x++)
{
  Console.Write("{0,2}: ", x);
  Console.BackgroundColor = ConsoleColor.Black;
  Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);
  Console.Write("This is foreground color {0}.", colorNames[x]);
  Console.ResetColor();
  Console.WriteLine();
}

Console.ForegroundColor是用于设置文本颜色的属性。


3

颜色 [背景][前景]

其中颜色定义如下:

0 = Black    8 = Gray
1 = Blue     9 = Light Blue
2 = Green    A = Light Green
3 = Aqua     B = Light Aqua
4 = Red      C = Light Red
5 = Purple   D = Light Purple
6 = Yellow   E = Light Yellow
7 = White    F = Bright White

例如,要将背景改为蓝色,前景改为灰色,您需要输入:color 18

用于Cygwin:alias color ='cmd / c color' - zzapper

2
您可以使用 ANSI 转义序列,但在现代版本的 Windows 下,这并不能实现您想要的效果。维基百科有一篇非常详细的文章:http://en.wikipedia.org/wiki/ANSI_escape_code。所以,对于您最初的问题,答案几乎肯定是“不行”。但是,您可以更改前景色而无需编写转义序列,例如通过调用 Win32 API 函数。我不知道如何在 Ruby 中做到这种事情,但其他人似乎已经成功了:http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241925。我想您可能想使用 4 来表示深红色或者 12 来表示亮红色,使用 7 来恢复默认颜色。希望这能帮到您!

2

关于 ANSI 转义码:

32 位字符模式(子系统:控制台)的 Windows 应用程序不会向控制台写入 ANSI 转义序列

它们必须解释转义码操作并调用本地的控制台 API

感谢微软 :-(


2
我编写了一个小型跨平台宝石(gem),可以在Windows或POSIX系统上无缝处理此问题,同时支持MRI和JRuby。
它没有任何依赖,对于POSIX系统使用ANSI代码,对于Windows系统使用FFI (JRuby) 或 Fiddler (MRI).
要使用它,只需按照以下方式操作:
gem install color-console

ColorConsole提供了使用Console.write和Console.puts函数以不同颜色输出文本行的方法。

require 'color-console'

Console.puts "Some text"                    # Outputs text using the current  console colours
Console.puts "Some other text", :red        # Outputs red text with the current background
Console.puts "Yet more text", nil, :blue    # Outputs text using the current foreground and a blue background

# The following lines output BlueRedGreen on a single line, each word in the appropriate color
Console.write "Blue ", :blue
Console.write "Red ", :red
Console.write "Green", :green

访问项目主页https://github.com/agardiner/color-console以获取更多详细信息。


0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Console_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.WriteLine("Hello World");
            Console.ReadKey();
        }
    }
}

您可以使用简单的C#程序更改颜色,http://powerof2games.com/node/31描述了如何包装控制台输出以实现效果。


0

标准的C/C++命令行输出规范并没有指定更改控制台窗口颜色的任何功能。不过,在Win32中有许多函数可以实现这样的操作。

改变Win32控制台颜色最简单的方法是通过iostream.h中的system命令。该函数会调用DOS命令。为了改变颜色,我们将使用它来调用color命令。例如,system("Color F1"); 将使控制台呈现深蓝底白字。

DOS颜色

可用于该命令的颜色是十六种DOS颜色,每种颜色都用一个十六进制数字表示。第一个数字代表背景色,第二个数字代表前景色。

0 = Black    8 = Gray
1 = Blue     9 = Light Blue
2 = Green    A = Light Green
3 = Aqua     B = Light Aqua
4 = Red      C = Light Red
5 = Purple   D = Light Purple
6 = Yellow   E = Light Yellow
7 = White    F = Bright White

仅仅加上一点颜色就可以让控制台程序更具视觉吸引力。然而,Color命令会改变整个控制台的颜色。要控制单个单元格,我们需要使用windows.h中的函数。

为了做到这一点,您需要使用SetConsoleAttribute函数。

http://msdn.microsoft.com/en-us/library/ms686047.aspx


0
据我所知,用命令行是不可能的,它只有一种颜色...

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