https://github.com/ku1ik/rainbow
Ruby gem for colorizing printed text on ANSI terminals
https://github.com/ku1ik/rainbow
Keywords
colorization gem ruby
Keywords from Contributors
rubygems activerecord static-code-analysis code-formatter rubocop crash-reporting activejob mvc rspec rack
Last synced: about 11 hours ago
JSON representation
Repository metadata
Ruby gem for colorizing printed text on ANSI terminals
- Host: GitHub
- URL: https://github.com/ku1ik/rainbow
- Owner: ku1ik
- License: mit
- Created: 2008-10-30T09:34:00.000Z (about 17 years ago)
- Default Branch: master
- Last Pushed: 2025-11-25T07:44:55.000Z (17 days ago)
- Last Synced: 2025-12-06T17:17:05.326Z (6 days ago)
- Topics: colorization, gem, ruby
- Language: Ruby
- Homepage:
- Size: 215 KB
- Stars: 825
- Watchers: 9
- Forks: 71
- Open Issues: 10
- Releases: 0
-
Metadata Files:
- Readme: README.markdown
- Changelog: Changelog.md
- License: LICENSE
README.markdown
Rainbow
Rainbow is a ruby gem for colorizing printed text on ANSI terminals.
It provides a string presenter object, which adds several methods to your
strings for wrapping them in ANSI escape
codes. These codes when printed
in a terminal change text attributes like text color, background color,
intensity etc.
Usage
To make your string colored wrap it with Rainbow() presenter and call
.color(<color name>) on it.
Example
require 'rainbow'
puts Rainbow("this is red").red + " and " + Rainbow("this on yellow bg").bg(:yellow) + " and " + Rainbow("even bright underlined!").underline.bright
# => "\e[31mthis is red\e[0m and \e[43mthis on yellow bg\e[0m and \e[4m\e[1meven bright underlined!\e[0m"

Rainbow presenter API
Rainbow presenter adds the following methods to presented string:
color(c)(withforeground, andfgaliases)background(c)(withbgalias)brightunderlineblinkinversehidefaint(not well supported by terminal emulators)italic(not well supported by terminal emulators)cross_out,strike
Text color can also be changed by calling a method named by a color:
blackredgreenyellowbluemagentacyanwhiteaquasilveraliceblueindianred
All of the methods return self (the presenter object) so you can chain method
calls:
Rainbow("hola!").blue.bright.underline
The Rainbow module also provides an uncolor method for stripping away any
existing color or formatting. This may be handy for determining the visual
length of strings.
text = Rainbow("this is green!").green
# => "\e[32mthis is green!\e[0m"
Rainbow.uncolor(text)
# => "this is green!"
Refinement
If you want to use the Refinements version, you can:
require 'rainbow/refinement'
using Rainbow
puts "Hi!".green
Here's an IRB session example:
>> 'Hello, World!'.blue.bright.underline
NoMethodError: undefined method `blue' for "Hello, World!":String
(ripl):1:in `<main>'
>> using Rainbow
=> main
>> 'Hello, World!'.blue.bright.underline
=> "\e[34m\e[1m\e[4mHello, World!\e[0m"
Color specification
Both color and background accept color specified in any
of the following ways:
-
ANSI color number (where 0 is black, 1 is red, 2 is green and so on):
Rainbow("hello").color(1) -
ANSI color name or X11 color name as a symbol:
Rainbow("hello").color(:yellow).
This can be simplified toRainbow("hello").yellowSee Color list for all available color names.
Note that ANSI colors can be changed in accordance with terminal setting.
But X11 color is just a syntax sugar for RGB triplet. So you always see what you specified. -
RGB triplet as separate values in the range 0-255:
Rainbow("hello").color(115, 23, 98) -
RGB triplet as a hex string:
Rainbow("hello").color("FFC482")orRainbow("hello").color("#FFC482")
When you specify a color with a RGB triplet rainbow finds the nearest match
from 256 colors palette. Note that it requires a 256-colors capable terminal to
display correctly.
Example: Choose a random color
You can pick a random color with Rainbow, it's a one-liner:
colors = Range.new(0,7).to_a
"whoop dee doop".chars.map { |char| Rainbow(char).color(colors.sample) }.join
# => "\e[36mw\e[0m\e[37mh\e[0m\e[34mo\e[0m\e[34mo\e[0m\e[37mp\e[0m\e[34m \e[0m\e[36md\e[0m\e[33me\e[0m\e[34me\e[0m\e[37m \e[0m\e[32md\e[0m\e[35mo\e[0m\e[33mo\e[0m\e[36mp\e[0m"
colors = [:aliceblue, :antiquewhite, :aqua, :aquamarine, :azure, :beige, :bisque, :blanchedalmond, :blueviolet]
"whoop dee doop".chars.map { |char| Rainbow(char).color(colors.sample) }.join
# => "\e[38;5;135mw\e[0m\e[38;5;230mh\e[0m\e[38;5;231mo\e[0m\e[38;5;135mo\e[0m\e[38;5;231mp\e[0m\e[38;5;231m \e[0m\e[38;5;122md\e[0m\e[38;5;231me\e[0m\e[38;5;231me\e[0m\e[38;5;230m \e[0m\e[38;5;122md\e[0m\e[38;5;51mo\e[0m\e[38;5;51mo\e[0m\e[38;5;51mp\e[0m"
Configuration
Rainbow can be enabled/disabled globally by setting:
Rainbow.enabled = true/false
When disabled all the methods return an unmodified string
(Rainbow("hello").red == "hello").
It's enabled by default, unless STDOUT/STDERR is not a TTY or a terminal is
dumb.
Advanced usage
Rainbow() and Rainbow.enabled operate on the global Rainbow wrapper
instance. If you would like to selectively enable/disable coloring in separate
parts of your application you can get a new Rainbow wrapper instance for each
of them and control the state of coloring during the runtime.
rainbow_one = Rainbow.new
rainbow_two = Rainbow.new
rainbow_one.enabled = false
Rainbow("hello").red # => "\e[31mhello\e[0m" ("hello" if not on TTY)
rainbow_one.wrap("hello").red # => "hello"
rainbow_two.wrap("hello").red # => "\e[31mhello\e[0m" ("hello" if not on TTY)
By default each new instance inherits enabled/disabled state from the global
Rainbow.enabled.
This feature comes handy for example when you have multiple output formatters
in your application and some of them print to a terminal but others write to a
file. Normally rainbow would detect that STDIN/STDERR is a TTY and would
colorize all the strings, even the ones that go through file writing
formatters. You can easily solve that by disabling coloring for the Rainbow
instances that are used by formatters with file output.
Installation
Add it to your Gemfile:
gem 'rainbow'
Or just install it via rubygems:
gem install rainbow
Color list
ANSI colors
black, red, green, yellow, blue, magenta, cyan, white
X11 colors
aliceblue, antiquewhite, aqua, aquamarine, azure, beige, bisque,
blanchedalmond, blueviolet, brown, burlywood, cadetblue, chartreuse,
chocolate, coral, cornflower, cornsilk, crimson, darkblue,
darkcyan, darkgoldenrod, darkgray, darkgreen, darkkhaki,
darkmagenta, darkolivegreen, darkorange, darkorchid, darkred,
darksalmon, darkseagreen, darkslateblue, darkslategray, darkturquoise,
darkviolet, deeppink, deepskyblue, dimgray, dodgerblue, firebrick,
floralwhite, forestgreen, fuchsia, gainsboro, ghostwhite, gold,
goldenrod, gray, greenyellow, honeydew, hotpink, indianred,
indigo, ivory, khaki, lavender, lavenderblush, lawngreen,
lemonchiffon, lightblue, lightcoral, lightcyan, lightgoldenrod,
lightgray, lightgreen, lightpink, lightsalmon, lightseagreen,
lightskyblue, lightslategray, lightsteelblue, lightyellow, lime,
limegreen, linen, maroon, mediumaquamarine, mediumblue,
mediumorchid, mediumpurple, mediumseagreen, mediumslateblue,
mediumspringgreen, mediumturquoise, mediumvioletred, midnightblue,
mintcream, mistyrose, moccasin, navajowhite, navyblue, oldlace,
olive, olivedrab, orange, orangered, orchid, palegoldenrod,
palegreen, paleturquoise, palevioletred, papayawhip, peachpuff,
peru, pink, plum, powderblue, purple, rebeccapurple, rosybrown,
royalblue, saddlebrown, salmon, sandybrown, seagreen, seashell,
sienna, silver, skyblue, slateblue, slategray, snow, springgreen,
steelblue, tan, teal, thistle, tomato, turquoise, violet,
webgray, webgreen, webmaroon, webpurple, wheat, whitesmoke,
yellowgreen
Authors
Owner metadata
- Name: Marcin Kulik
- Login: ku1ik
- Email:
- Kind: user
- Description: Software engineer, consultant, specializing in Elixir and Rust. Creator of @asciinema.
- Website: https://ku1ik.com
- Location: Poland
- Twitter:
- Company: @asciinema
- Icon url: https://avatars.githubusercontent.com/u/17589?u=1f51a13592061f7f7e3819fd20f88f31eaa7e053&v=4
- Repositories: 117
- Last ynced at: 2025-11-29T23:09:41.727Z
- Profile URL: https://github.com/ku1ik
GitHub Events
Total
- Issues event: 1
- Watch event: 17
- Delete event: 3
- Issue comment event: 3
- Push event: 6
- Pull request review event: 3
- Pull request review comment event: 2
- Pull request event: 13
- Fork event: 5
- Create event: 2
Last Year
- Watch event: 11
- Delete event: 3
- Issue comment event: 3
- Push event: 6
- Pull request review event: 3
- Pull request review comment event: 2
- Pull request event: 11
- Fork event: 3
- Create event: 2
Committers metadata
Last synced: 10 days ago
Total Commits: 261
Total Committers: 48
Avg Commits per committer: 5.438
Development Distribution Score (DDS): 0.674
Commits in past year: 7
Committers in past year: 4
Avg Commits per committer in past year: 1.75
Development Distribution Score (DDS) in past year: 0.571
| Name | Commits | |
|---|---|---|
| Marcin Kulik | m@k****m | 85 |
| Olle Jonsson | o****n@g****m | 58 |
| Marcin Kulik | m****k@g****m | 27 |
| lkdjiin | x****c@g****m | 15 |
| Masafumi Koba | 4****s | 6 |
| Marcin Kulik | m****k@l****l | 4 |
| sato-s | s****u@g****m | 3 |
| dependabot[bot] | 4****] | 3 |
| Yuya Tanaka | y****o@g****m | 3 |
| Felipe Coury | f****y@g****m | 3 |
| Erik Michaels-Ober | s****k@g****m | 3 |
| David Rodríguez | d****z@r****t | 3 |
| Chi Leung | c****g@g****m | 3 |
| Bryan Johnson | b****j@p****m | 3 |
| David Rodríguez | d****z@g****m | 3 |
| Logan Koester | l****r@a****m | 2 |
| Christian Paling | c****g@g****m | 2 |
| Claudio Bley | c****y@g****m | 2 |
| Igor Victor | g****a@y****u | 2 |
| mishina | t****8@g****m | 2 |
| Eli Clemente Gordillo Foster | e****y@g****m | 2 |
| tobyh | t****h | 1 |
| sato | s****o@e****m | 1 |
| Mark Smith | m****h@o****m | 1 |
| tbpgr | t****r@t****p | 1 |
| fatkodima | f****3@g****m | 1 |
| Sunny Ripert | s****y@s****g | 1 |
| Stephan Nordnes Eriksen | s****r@g****m | 1 |
| Stefan Wrobel | s****l | 1 |
| Sebastian Schuberth | s****h@g****m | 1 |
| and 18 more... | ||
Committer domains:
- raxys.net: 1
- spicycode.com: 1
- garytou.com: 1
- goltsov.info: 1
- oobik.com: 1
- logankoester.com: 1
- pocke.me: 1
- filespanker.com: 1
- sunfox.org: 1
- tbpgr.jp: 1
- objectbynature.com: 1
- yandex.ru: 1
- agoragames.com: 1
- pcsoln.com: 1
- riseup.net: 1
- llp.pl: 1
- ku1ik.com: 1
Issue and Pull Request metadata
Last synced: 16 days ago
Total issues: 2
Total pull requests: 13
Average time to close issues: 7 days
Average time to close pull requests: 1 day
Total issue authors: 2
Total pull request authors: 6
Average comments per issue: 0.5
Average comments per pull request: 0.38
Merged pull request: 8
Bot issues: 0
Bot pull requests: 3
Past year issues: 0
Past year pull requests: 6
Past year average time to close issues: N/A
Past year average time to close pull requests: about 7 hours
Past year issue authors: 0
Past year pull request authors: 4
Past year average comments per issue: 0
Past year average comments per pull request: 0.83
Past year merged pull request: 5
Past year bot issues: 0
Past year bot pull requests: 2
Top Issue Authors
- TylerRick (1)
- tobyh (1)
Top Pull Request Authors
- TylerRick (4)
- olleolleolle (3)
- dependabot[bot] (3)
- splattael (1)
- tobyh (1)
- garyhtou (1)
Top Issue Labels
Top Pull Request Labels
- dependencies (3)
- github_actions (2)
Package metadata
- Total packages: 1
- Total downloads: unknown
- Total dependent packages: 0
- Total dependent repositories: 0
- Total versions: 20
proxy.golang.org: github.com/ku1ik/rainbow
- Homepage:
- Documentation: https://pkg.go.dev/github.com/ku1ik/rainbow#section-documentation
- Licenses: mit
- Latest release: v3.1.1+incompatible (published almost 4 years ago)
- Last Synced: 2025-12-04T16:21:40.971Z (8 days ago)
- Versions: 20
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent packages count: 5.442%
- Average: 5.624%
- Dependent repos count: 5.807%
Dependencies
- actions/checkout v2 composite
- ruby/setup-ruby v1 composite
- coveralls >= 0 development
- guard >= 0 development
- guard-rspec >= 0 development
- mutant-rspec >= 0 development
- rspec >= 0 development
- rubocop = 1.7.0 development
- rake >= 0
- bundler >= 1.3, < 3 development
Score: -Infinity