How can I change the text color in the windows command prompt

后端 未结 14 2483
南旧
南旧 2020-12-14 22:32

I have a command line program, which outputs logging to the screen.

I want error lines to show up in red. Is there some special character codes I can output to switc

14条回答
  •  醉梦人生
    2020-12-14 23:21

    I've authored a small cross-platform gem that handles this seamlessly running on Windows or POSIX-systems, under both MRI and JRuby.

    It has no dependencies, and uses ANSI codes on POSIX systems, and either FFI (JRuby) or Fiddler (MRI) for Windows.

    To use it, simply:

    gem install color-console
    

    ColorConsole provides methods for outputting lines of text in different colors, using the Console.write and Console.puts functions.

    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
    

    Visit the project home page at https://github.com/agardiner/color-console for more details.

提交回复
热议问题