Search code examples
rubycolorsconsole-application

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


Using Ruby, how can I perform background and foreground text colorization for output in the terminal?

I remember, when programming Pascal we all used to write our own textcolor(…) procedures to make our small educational programs look more pretty and presentational.

How would I go about coding an equivalent of that in Ruby? Is there any built-in support in the core library that lends itself to this? If not, what would be an idiomatic way to add it?


Solution

  • UPDATE: Colorize is copyleft.

    So i would try the rainbow gem:

    https://github.com/ku1ik/rainbow

    Installation:

    gem install rainbow
    

    Usage:

    require 'rainbow/refinement'
    using Rainbow
    
    puts "I am now red".red
    puts "I am now blue".blue
    puts "Testing".yellow