【问题标题】:How can I change the text color in the windows command prompt如何更改 windows 命令提示符中的文本颜色
【发布时间】:2010-09-09 19:35:02
【问题描述】:

我有一个命令行程序,它将日志记录输出到屏幕。

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

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

类似:

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

print "#{red} ERROR: IT BROKE #{white}"
print "other stuff"

【问题讨论】:

    标签: windows ruby command-line colors


    【解决方案1】:

    在 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
    

    你也可以像这样扩展 String 以获得更多颜色:

    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 的文档。

    【讨论】:

    • 或者,您可以使用 gem 'colorize'。 注意 在 Windows 上使用时需要手动 require 'win32console,因为“着色”仅检查 if RUBY_PLATFORM =~ /win32/,在某些情况下 RUBY_PLATFORM 返回 i386-mingw32
    • 我认为您缺少end
    【解决方案2】:

    您需要访问Win32 Console API。不幸的是,我不知道你如何从 Ruby 中做到这一点。在 Perl 中,我会使用 Win32::Console 模块。 Windows 控制台不响应 ANSI 转义码。

    根据artur02提到的article on colorizing Ruby output,你需要安装&加载win32console gem。

    【讨论】:

      【解决方案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是设置文字颜色的属性。

      【讨论】:

        【解决方案4】:

        您可以使用 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 恢复默认颜色。

        希望这会有所帮助!

        【讨论】:

          【解决方案5】:

          关于 ANSI 转义码:

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

          他们必须解释转义码操作并改为调用本机控制台 API

          感谢微软 :-(

          【讨论】:

            【解决方案6】:

            color [background][foreground]

            其中颜色定义如下:

            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'
            【解决方案7】:

            我编写了一个小型跨平台 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了解更多详情。

            【讨论】:

              【解决方案8】:

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

              【讨论】:

                【解决方案9】:
                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 描述了如何包装控制台输出以实现效果。

                【讨论】:

                  【解决方案10】:

                  你想要ANSI escape codes

                  【讨论】:

                  • ANSI 转义码在 Windows 命令提示符中不可用,本机。
                  【解决方案11】:

                  用于输出到命令行的标准 C/C++ 规范没有指定任何更改控制台窗口颜色的功能。也就是说,Win32 中有很多函数可以做这样的事情。

                  更改 Win32 控制台颜色的最简单方法是通过 iostream.h 中的系统命令。此函数调用 DOS 命令。要更改颜色,我们将使用它来调用颜色命令。例如,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
                  

                  只是这一点点颜色使控制台程序在视觉上更令人愉悦。但是,颜色命令会改变整个控制台的颜色。要控制单个单元格,我们需要使用 windows.h 中的函数。

                  这样做你需要使用SetConsoleAttribute函数

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

                  【讨论】:

                    【解决方案12】:

                    很多旧的 ANSI Color Codes 工作。红色前景的代码类似于 Escape-[31m.转义是字符 27,因此是“\033[31m”或“\x1B[31m”,具体取决于您的转义方案。

                    [39m是返回默认颜色的代码。

                    也可以一次指定多个代码同时设置前景色和背景色。

                    您可能需要加载 ANSI.sys,请参阅 this page

                    【讨论】:

                      【解决方案13】:

                      最终您需要致电SetConsoleTextAttribute。您可以从GetStdHandle 获取控制台屏幕缓冲区句柄。

                      【讨论】:

                        【解决方案14】:

                        多年来,我一直在使用一个名为 baretail (google it) 的免费 Windows tail 程序,它可以让您执行 Windows 应用版本的 unix tail 命令。它使您可以根据定义的任何关键字对线条进行着色。作为一种解决方案,它的好处在于它不依赖于特定的语言或设置等,你只需定义你的配色方案,它就像 donkey kong 一样。在我个人排名前 10 的免费软件帮手中!

                        【讨论】:

                          猜你喜欢
                          • 2012-08-15
                          • 1970-01-01
                          • 2022-01-25
                          • 2015-09-15
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 2019-08-11
                          相关资源
                          最近更新 更多