[ACCEPTED]-How can I change the text color in the windows command prompt-colors

Accepted answer
Score: 28

On windows, you can do it easily in three 6 ways:

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

Now you could extend String with a 5 small method called red

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

 puts "Hello, World!".red

Also you can extend 4 String like this to get more colors:

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

Or, if 3 you can install gems:

gem install term-ansicolor

And in your program:

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

Please 2 see the docs for term/ansicolor for more 1 information and possible usage.

Score: 7

You need to access the Win32 Console API. Unfortunately, I 6 don't know how you'd do that from Ruby. In 5 Perl, I'd use the Win32::Console module. The Windows 4 console does not respond to ANSI escape 3 codes.

According to the article on colorizing Ruby output that artur02 mentioned, you 2 need to install & load the win32console 1 gem.

Score: 3

You can read here a good and illustrated 4 article: http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/

I think setting console text color 3 is pretty language-specific. Here is an 2 example in C# from MSDN:

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 is the property 1 for setting text color.

Score: 2

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

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

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

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

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

Hope this 1 helps!

Score: 2

on ANSI escape codes:

32-bit character-mode 5 (subsystem:console) Windows applications 4 don't write ANSI escape sequences to the 3 console

They must interpret the escape code 2 actions and call the native Console API 1 instead

Thanks microsoft :-(

Score: 2

color [background][foreground]

Where colors are defined as follows:

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

For 2 example, to change the background to blue 1 and the foreground to gray, you would type:

color 18

Score: 2

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

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

To 5 use it, simply:

gem install color-console

ColorConsole provides methods 4 for outputting lines of text in different 3 colors, using the Console.write and Console.puts 2 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 1 https://github.com/agardiner/color-console for more details.

Score: 0

As far as I know it is not possible with 1 a command line, it is just one color...

Score: 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();
        }
    }
}

You can change the color using a simple 2 C# program, http://powerof2games.com/node/31 describes how you can wrap 1 console output to achieve the effect.

Score: 0

You want ANSI escape codes.

0

Score: 0

A lot of the old ANSI Color Codes work. The code for 8 a red foreground is something like Escape-[31m. Escape 7 is character 27, so that's "\033[31m" or 6 "\x1B[31m", depending on your 5 escaping scheme.

[39m is the code to return 4 to default color.

It's also possible to specify 3 multiple codes at once to set foreground 2 and background color simultaneously.

You 1 may have to load ANSI.sys, see this page.

Score: 0

The standard C/C++ specification for outputting 20 to the command line doesn't specify any 19 capabilities for changing the color of the 18 console window. That said, there are many 17 functions in Win32 for doing such a thing.

The 16 easiest way to change the color of the Win32 15 console is through the system command in 14 iostream.h. This function invokes a DOS 13 command. To change colors, we will use it 12 to invoke the color command. For example, system("Color F1"); will 11 make the console darkblue on white.

DOS Colors

The 10 colors available for use with the command 9 are the sixteen DOS colors each represented 8 with a hex digit. The first being the background 7 and the second being the 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

Just 6 this little touch of color makes console 5 programs more visually pleasing. However, the 4 Color command will change the color of the 3 entire console. To control individual cells, we 2 need to use functions from windows.h.

Do 1 do that you need to use the SetConsoleAttribute function

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

Score: 0

Ultimately you need to call SetConsoleTextAttribute. You can get 1 a console screen buffer handle from GetStdHandle.

Score: 0

I've been using a freeware windows tail 9 program called baretail (google it) for 8 ages that lets you do a windows-appified 7 version of unix tail command. It lets you 6 colorize lines dependent on whatever keywords 5 you define. What's nice about it as a solution 4 is its not tied to a specific language or 3 setup, etc, you just define your color scheme 2 and its on like donkey kong. In my personal 1 top 10 freeware helpers!

More Related questions