Easily colorize terminal text by each column.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install colcolor
You want to colorize terminal outputs in which each column(separated by whitespaces) have different color.
It can be easily achieved with
String#colco method as follows:
require "colcolor" list = <<-EOS Charlie\t21\tprogrammer Bill\t43\tdoctor Liz\t18\tstudent EOS list.each_line do |line| puts line.colco(:green, :yellow, :blue) end
If you want to have background colors or some decorations onto them, chain the names with underscores:
list.each_line do |line| puts line.colco(:green, :red_yellow, :blue_underline) end
If you prefer ages(second column) without color, pass
nil for it:
list.each_line do |line| puts line.colco(:green, nil, :blue_underline) end
You have full names in the list and want to apply bgcolor for them,
regexp option might helps you.
list = <<-EOS Charlie Brown\t21\tprogrammer Bill Clinton\t43\tdoctor Liz Taylor\t18\tstudent EOS # less appropriate example: list.each_line do |line| puts line.colco(:bg_green, :bg_green, :yellow, :blue_underline) end puts # example with regexp option: re = /^.*?(?=\t)|\S+/ # match any characters before the first tab or non whitespaces list.each_line do |line| puts line.colco(:bg_green, :yellow, :blue_underline, regexp:re) end
If you want to apply colors periodically to a text, set
cyclic option to true.
"H E L L O W O R L D".colco(:red, :green, :yellow, cyclic:true)
- Fork it ( https://github.com/[my-github-username]/colcolor/fork )
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create a new Pull Request