Colorful is a gem that provides color and effect support to native Ruby Strings when printed in an ANSI color supporting terminal.
gem install colorful or add
gem 'colorful' to your
Gemfile if you're using bundler.
The following code will print "Test" in red using the terminal's color scheme:
Background colors are also supported:
You can combine the two:
Add some effects:
And then remove some:
The supported terminal colors are:
The supported effects are:
Note that not all of these formats may not be supported in all environments. Using an unsupported effect will not cause any display issues, other than the style not being applied.
Additionally, Colorful supports full rgb256 or HTML color values
To use rgb256 true red as the foreground:
puts "Test".color(255, 0, 0)
For the HTML version:
These methods handle standard or shortened HTML codes, case insensitive, with or without # or _
Note that these methods require xterm 256 color support, and colors will be translated to the nearest possible valid color.
Lastly, some methods have been added that allow for cursor movement. The supported movement operations include:
To return to the beginning of the line:
10.times do |n| print n.to_s.and_go_to 0 end
10.times do |n| puts n.to_s.and_go_up 1 end
Note that some methods work better with puts, and others with print. Puts will implicitly add a new line to the end of the string it is printing, moving the cursor.
This is a work in progress but is stable. Let me know if you would like a feature added to the project.