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 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 do I print / puts with color?
Do you mean in the context of irb? If so, I'd suggest Wirble to colourize your output.
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.
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:
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.
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)Put the escape sequences in yourself, using something like this:
Rails.logger.debug("\033[32mThis message is Green\033[0m")
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?
Related Topics
Restarting a Loop from the Top
How to Check If a Value Exists in an Array in Ruby
How to Match the Last Occurrence of a Pattern
How to Install a Gem or Update Rubygems If It Fails With a Permissions Error
Error: While Executing Gem ... (Gem::Filepermissionerror)
Rails Activerecord Perform Group, Sum and Count in One Query
How to Calculate Number of Chars Common to Two Strings
Why Is It Bad Style to 'Rescue Exception =≫ E' in Ruby
How to Call Shell Commands from Ruby
Why Is "Slurping" a File Not a Good Practice
How to Remove Rvm (Ruby Version Manager) from My System
How to Use the Conditional Operator (? :) in Ruby
Ssl Error When Installing Rubygems, Unable to Pull Data from 'Https://Rubygems.Org/
Installed Ruby 1.9.3 With Rvm But Command Line Doesn't Show Ruby -V