How to Output My Ruby Commandline Text in Different Colours

How to output my ruby commandline text in different colours

I found this article describing a very easy way to write coloured texts to the console. The article describes this little example which seems to do the trick (I took the liberty to improve it slightly):

def colorize(text, color_code)
"\e[#{color_code}m#{text}\e[0m"
end

def red(text); colorize(text, 31); end
def green(text); colorize(text, 32); end

# Actual example
puts 'Importing categories [ ' + green('DONE') + ' ]'
puts 'Importing tags [' + red('FAILED') + ']'

Best seems to define some of the colours. You can extent the example when you need also different background colours (see bottom of article).

When using Window XP, the author mentions the requirement of a gem called win32console.

How can I use Ruby to colorize the text output to a terminal?

Colorize is my favorite gem! :-)

Check it out:

https://github.com/fazibear/colorize

Installation:

gem install colorize

Usage:

require 'colorize'

puts "I am now red".red
puts "I am now blue".blue
puts "Testing".yellow

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

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

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

Ruby on Rails - How to print log messages in color

Basically what you want to do is embed ANSI escape sequences for color into your debug strings, just like you would in a regular Ruby program. There are several ways to go about this:

  1. Use the rainbow gem, which allows you to do this:

    require 'rainbow'
    Rails.logger.debug(Rainbow("This message is Green").green)

    or require the mixin to add methods directly to the string class:

    Updated March 2021: The Rainbow Gem API has changed. Use this now:

    require 'rainbow/refinement'
    using Rainbow
    Rails.logger.debug("This is Green - ".green + "This is Blue".blue)

    Previous Version (for posterity):

    require 'rainbow/ext/string'
    Rails.logger.debug("This is Green - ".green + "This is Blue".blue)

    End Update

    The Rainbow gem will automatically add the beginning and ending escape sequences to the string.

  2. Use the colorize gem, which does the same thing as the rainbow mixin to the String class:

    require 'colorize'
    Rails.logger.debug("This is Green - ".green + "This is Blue".blue)

  3. Put the escape sequences in yourself, using something like this:

    Rails.logger.debug("\033[32mThis message is Green\033[0m")

  4. Write your own extension to the String class. See this answer or this answer for an example.

For more ideas, see How can I use Ruby to colorize the text output to a terminal?

Simple clone of unix watch using Ruby (with coloured output)

You're correct that the commands you are running switch to a non-colour output when invoked from a script. They detect that their standard output is not a terminal and modify their output accordingly.

Fortunately, you should be able to trick the commands into thinking they are outputting to a terminal by using a pseudo-terminal. You can do this using the PTY module in Ruby.

I took the following from this answer and tested it on Ruby 1.9.3-p392.

require 'pty'

PTY.spawn('ls --color=auto') do |stdin, stdout, pid|
begin
stdin.each {|line| print line}
rescue Errno::EIO
# End of input
end
end

Text coloring when piping output from Cucumber

Run cucumber with the -c option. Otherwise cucumber determines that the terminal (the pipe) does not support coloring and will drop it if not explicitly forced on with -c.

https://github.com/cucumber/cucumber/blob/master/lib/cucumber/cli/options.rb#L211

...
opts.on("-c", "--[no-]color",
"Whether or not to use ANSI color in the output. Cucumber decides",
"based on your platform and the output destination if not specified.")
...

How can I preserve output color when executing a process in Ruby?

Kernel.exec() gets me the solution I want (colored rspec output), but it does so by replacing my ruby script process with the rspec process. That means I can't do anything with the output or run anything after the rspec call.

That's acceptable in my particular situation, but less than ideal as a general solution. So I'd like a better answer if available.

How do I get colour with Windows command prompt using RSpec in Ruby?

UPDATE:
Win32Console no longer works with rspec. ANSICON recommended.
https://github.com/rspec/rspec-rails/issues/487#issuecomment-3556806



Related Topics



Leave a reply



Submit