<p>I have a command line program, which outputs logging to the screen.</p>

<p>I want error lines to show up in red. Is there some special character codes I can output to switch the text color to red, then switch it back to white?</p>

<p>I'm using ruby but I imagine this would be the same in any other language.</p>

<p>Something like:</p>

<pre><code>red = "\0123" # character code
white = "\0223"

print "#{red} ERROR: IT BROKE #{white}"
print "other stuff"
</code></pre>

[{'answer_id': 77795, 'author': 'Mitchel Sellers', 'author_id': 13279, 'author_profile': 'https://Stackoverflow.com/users/13279', 'pm_score': 0, 'selected': False, 'text': '<p>As far as I know it is not possible with a command line, it is just one color...</p>\n'}, {'answer_id': 77803, 'author': 'betelgeuce', 'author_id': 366182, 'author_profile': 'https://Stackoverflow.com/users/366182', 'pm_score': 0, 'selected': False, 'text': '<pre><code>using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\n\nnamespace Console_Test\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            Console.ForegroundColor = ConsoleColor.DarkRed;\n            Console.WriteLine("Hello World");\n            Console.ReadKey();\n        }\n    }\n}\n</code></pre>\n\n<p>You can change the color using a simple C# program, <a href="http://powerof2games.com/node/31" rel="nofollow noreferrer">http://powerof2games.com/node/31</a> describes how you can wrap console output to achieve the effect.</p>\n'}, {'answer_id': 77805, 'author': 'Adam Rosenfield', 'author_id': 9530, 'author_profile': 'https://Stackoverflow.com/users/9530', 'pm_score': 0, 'selected': False, 'text': '<p>You want <a href="http://en.wikipedia.org/wiki/ANSI_escape_code" rel="nofollow noreferrer">ANSI escape codes</a>.</p>\n'}, {'answer_id': 77831, 'author': 'davenpcj', 'author_id': 4777, 'author_profile': 'https://Stackoverflow.com/users/4777', 'pm_score': 0, 'selected': False, 'text': '<p>A lot of the old ANSI <a href="http://pueblo.sourceforge.net/doc/manual/ansi_color_codes.html" rel="nofollow noreferrer">Color Codes</a> work. The code for a red foreground is something like Escape-[31m. Escape is character 27, so that\'s "\\033[31m" or "\\x1B[31m", depending on your escaping scheme.</p>\n\n<p>[39m is the code to return to default color.</p>\n\n<p>It\'s also possible to specify multiple codes at once to set foreground and background color simultaneously.</p>\n\n<p>You may have to load ANSI.sys, see <a href="http://academic.evergreen.edu/projects/biophysics/technotes/program/ansi_esc.htm#notes" rel="nofollow noreferrer">this page</a>.</p>\n'}, {'answer_id': 77861, 'author': 'artur02', 'author_id': 13937, 'author_profile': 'https://Stackoverflow.com/users/13937', 'pm_score': 2, 'selected': False, 'text': '<p>You can read here a good and illustrated article:\n<a href="http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/" rel="nofollow noreferrer">http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/</a></p>\n\n<p>I think setting console text color is pretty language-specific. Here is an example in C# from MSDN:</p>\n\n<pre><code>for (int x = 0; x &lt; colorNames.Length; x++)\n{\n  Console.Write("{0,2}: ", x);\n  Console.BackgroundColor = ConsoleColor.Black;\n  Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);\n  Console.Write("This is foreground color {0}.", colorNames[x]);\n  Console.ResetColor();\n  Console.WriteLine();\n}\n</code></pre>\n\n<p><strong>Console.ForegroundColor</strong> is the property for setting text color.</p>\n'}, {'answer_id': 77867, 'author': 'Orion Adrian', 'author_id': 7756, 'author_profile': 'https://Stackoverflow.com/users/7756', 'pm_score': 0, 'selected': False, 'text': '<p>The standard C/C++ specification for outputting to the command line doesn\'t specify any capabilities for changing the color of the console window. That said, there are many functions in Win32 for doing such a thing.</p>\n<p>The easiest way to change the color of the Win32 console is through the system command in iostream.h. This function invokes a DOS command. To change colors, we will use it to invoke the color command. For example, <code>system(&quot;Color F1&quot;);</code> will make the console darkblue on white.</p>\n<p>DOS Colors</p>\n<p>The colors available for use with the command are the sixteen DOS colors each represented with a hex digit. The first being the background and the second being the foreground.</p>\n<pre><code>0 = Black    8 = Gray\n1 = Blue     9 = Light Blue\n2 = Green    A = Light Green\n3 = Aqua     B = Light Aqua\n4 = Red      C = Light Red\n5 = Purple   D = Light Purple\n6 = Yellow   E = Light Yellow\n7 = White    F = Bright White\n</code></pre>\n<p>Just this little touch of color makes console programs more visually pleasing. However, the Color command will change the color of the entire console. To control individual cells, we need to use functions from windows.h.</p>\n<p>Do do that you need to use the <code>SetConsoleAttribute</code> function</p>\n<p><a href="https://learn.microsoft.com/en-us/windows/console/setconsoletextattribute" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms686047.aspx</a></p>\n'}, {'answer_id': 77884, 'author': 'cjm', 'author_id': 8355, 'author_profile': 'https://Stackoverflow.com/users/8355', 'pm_score': 4, 'selected': True, 'text': '<p>You need to access the <a href="https://learn.microsoft.com/en-us/windows/console/console-functions" rel="nofollow noreferrer">Win32 Console API</a>.  Unfortunately, I don\'t know how you\'d do that from Ruby.  In Perl, I\'d use the <a href="http://search.cpan.org/perldoc?Win32::Console" rel="nofollow noreferrer">Win32::Console</a> module.  The Windows console does not respond to ANSI escape codes.</p>\n<p>According to the <a href="http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/" rel="nofollow noreferrer">article on colorizing Ruby output</a> that artur02 mentioned, you need to install &amp; load the win32console gem.</p>\n'}, {'answer_id': 77904, 'author': 'Mike Dimmick', 'author_id': 6970, 'author_profile': 'https://Stackoverflow.com/users/6970', 'pm_score': 0, 'selected': False, 'text': '<p>Ultimately you need to call <a href="http://msdn.microsoft.com/en-us/library/ms686047.aspx" rel="nofollow noreferrer">SetConsoleTextAttribute</a>. You can get a console screen buffer handle from <a href="http://msdn.microsoft.com/en-us/library/ms683231(VS.85).aspx" rel="nofollow noreferrer">GetStdHandle</a>.</p>\n'}, {'answer_id': 77940, 'author': 'zaphod', 'author_id': 13871, 'author_profile': 'https://Stackoverflow.com/users/13871', 'pm_score': 2, 'selected': False, 'text': '<p>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:</p>\n\n<p><a href="http://en.wikipedia.org/wiki/ANSI_escape_code" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/ANSI_escape_code</a></p>\n\n<p>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:</p>\n\n<p><a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241925" rel="nofollow noreferrer">http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241925</a></p>\n\n<p>I imagine you\'d want to use 4 for dark red or 12 for bright red, and 7 to restore the default color.</p>\n\n<p>Hope this helps!</p>\n'}, {'answer_id': 77981, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 2, 'selected': False, 'text': "<p>on ANSI escape codes:</p>\n<blockquote>\n<p>32-bit character-mode (subsystem:console) Windows applications don't write ANSI escape sequences to the console</p>\n<p>They must interpret the escape code actions and call the native Console API instead</p>\n</blockquote>\n<p>Thanks microsoft :-(</p>\n"}, {'answer_id': 78002, 'author': 'Michael', 'author_id': 13379, 'author_profile': 'https://Stackoverflow.com/users/13379', 'pm_score': 2, 'selected': False, 'text': '<p><code>color [background][foreground]</code></p>\n\n<p>Where colors are defined as follows: </p>\n\n<pre><code>0 = Black    8 = Gray\n1 = Blue     9 = Light Blue\n2 = Green    A = Light Green\n3 = Aqua     B = Light Aqua\n4 = Red      C = Light Red\n5 = Purple   D = Light Purple\n6 = Yellow   E = Light Yellow\n7 = White    F = Bright White\n</code></pre>\n\n<p>For example, to change the background to blue and the foreground to gray, you would type:</p>\n\n<p><code>color 18</code></p>\n'}, {'answer_id': 78622, 'author': 'Mike Duncan', 'author_id': 14483, 'author_profile': 'https://Stackoverflow.com/users/14483', 'pm_score': 0, 'selected': False, 'text': "<p>I've been using a freeware windows tail program called baretail (google it) for ages that lets you do a windows-appified version of unix tail command.  It lets you colorize lines dependent on whatever keywords you define.  What's nice about it as a solution is its not tied to a specific language or setup, etc, you just define your color scheme and its on like donkey kong.  In my personal top 10 freeware helpers!</p>\n"}, {'answer_id': 78741, 'author': 'manveru', 'author_id': 8367, 'author_profile': 'https://Stackoverflow.com/users/8367', 'pm_score': 5, 'selected': False, 'text': '<p>On windows, you can do it easily in three ways:</p>\n\n<pre><code>require \'win32console\'\nputs "\\e[31mHello, World!\\e[0m"\n</code></pre>\n\n<p>Now you could extend String with a small method called <code>red</code></p>\n\n<pre><code> require \'win32console\'\n class String\n   def red\n     "\\e[31m#{self}\\e[0m"\n   end\n end\n\n puts "Hello, World!".red\n</code></pre>\n\n<p>Also you can extend String like this to get more colors:</p>\n\n<pre><code>require \'win32console\'\n\nclass String\n  { :reset          =&gt;  0,\n    :bold           =&gt;  1,\n    :dark           =&gt;  2,\n    :underline      =&gt;  4,\n    :blink          =&gt;  5,\n    :negative       =&gt;  7,\n    :black          =&gt; 30,\n    :red            =&gt; 31,\n    :green          =&gt; 32,\n    :yellow         =&gt; 33,\n    :blue           =&gt; 34,\n    :magenta        =&gt; 35,\n    :cyan           =&gt; 36,\n    :white          =&gt; 37,\n  }.each do |key, value|\n    define_method key do\n      "\\e[#{value}m" + self + "\\e[0m"\n    end\n  end\nend\n\nputs "Hello, World!".red\n</code></pre>\n\n<p>Or, if you can install gems:</p>\n\n<pre><code>gem install term-ansicolor\n</code></pre>\n\n<p>And in your program:</p>\n\n<pre><code>require \'win32console\'\nrequire \'term/ansicolor\'\n\nclass String\n  include Term::ANSIColor\nend\n\nputs "Hello, World!".red\nputs "Hello, World!".blue\nputs "Annoy me!".blink.yellow.bold\n</code></pre>\n\n<p>Please see the docs for term/ansicolor for more information and possible usage.</p>\n'}, {'answer_id': 31743032, 'author': 'Adam', 'author_id': 3254245, 'author_profile': 'https://Stackoverflow.com/users/3254245', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve authored a small cross-platform gem that handles this seamlessly running on Windows or POSIX-systems, under both MRI and JRuby.</p>\n\n<p>It has no dependencies, and uses ANSI codes on POSIX systems, and either FFI (JRuby) or Fiddler (MRI) for Windows.</p>\n\n<p>To use it, simply:</p>\n\n<pre><code>gem install color-console\n</code></pre>\n\n<p>ColorConsole provides methods for outputting lines of text in different colors, using the Console.write and Console.puts functions.</p>\n\n<pre><code>require \'color-console\'\n\nConsole.puts "Some text"                    # Outputs text using the current  console colours\nConsole.puts "Some other text", :red        # Outputs red text with the current background\nConsole.puts "Yet more text", nil, :blue    # Outputs text using the current foreground and a blue background\n\n# The following lines output BlueRedGreen on a single line, each word in the appropriate color\nConsole.write "Blue ", :blue\nConsole.write "Red ", :red\nConsole.write "Green", :green\n</code></pre>\n\n<p>Visit the project home page at <a href="https://github.com/agardiner/color-console" rel="nofollow">https://github.com/agardiner/color-console</a> for more details.</p>\n'}]
['https://Stackoverflow.com/questions/77744', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/234/']