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

后端 未结 14 2668
南旧
南旧 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:13

    You could use an ANSI escape sequence, but that won't do what you want under modern versions of Windows. Wikipedia has a very informative article:

    http://en.wikipedia.org/wiki/ANSI_escape_code

    So the answer to your original question is almost certainly "no." However, you can change the foreground color without writing an escape sequence, for example by invoking a Win32 API function. I don't know how to do this sort of thing in Ruby off the top of my head, but somebody else seems to have managed:

    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241925

    I imagine you'd want to use 4 for dark red or 12 for bright red, and 7 to restore the default color.

    Hope this helps!

提交回复
热议问题