This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
# Rainbow changelog
|
||||
|
||||
## 3.1.0 (2020-08-26)
|
||||
|
||||
- added `cross_out` aka `strike`
|
||||
- hexadecimal color names supported better, see #83
|
||||
- gemspec: list files using a Ruby expression, avoiding git
|
||||
|
||||
## 3.0.0 (2017-11-29)
|
||||
|
||||
* added String refinement
|
||||
* added new `Rainbow.uncolor` method
|
||||
* dropped MRI 1.9.3 compatibility
|
||||
* dropped MRI 2.0 compatibility
|
||||
* removed Rake dependency
|
||||
|
||||
## 2.2.2 (2017-04-21)
|
||||
|
||||
* added explicit rake dependency to fix installation issue
|
||||
|
||||
## 2.2.1 (2016-12-28)
|
||||
|
||||
* fixed gem installation (2.2.0 was a broken release)
|
||||
|
||||
## 2.2.0 (2016-12-27)
|
||||
|
||||
* improved Windows support
|
||||
* added Ruby 2.4 support
|
||||
* added `bold` alias method for `bright`
|
||||
|
||||
## 2.1.0 (2016-01-24)
|
||||
|
||||
* added X11 color support
|
||||
* fixed `require` issue when rainbow is used as a dependency in another gem
|
||||
* improved Windows support
|
||||
|
||||
## 2.0.0 (2014-01-24)
|
||||
|
||||
* disable string mixin by default
|
||||
|
||||
## 1.99.2 (2014-01-24)
|
||||
|
||||
* bring back ruby 1.8 support
|
||||
|
||||
## 1.99.1 (2013-12-28)
|
||||
|
||||
* drop support for ruby 1.8
|
||||
* `require "rainbow/string"` -> `require "rainbow/ext/string"`
|
||||
* custom rainbow wrapper instances (with separate enabled/disabled state)
|
||||
* shortcut methods for changing text color (`Rainbow("foo").red`)
|
||||
|
||||
## 1.99.0 (2013-12-26)
|
||||
|
||||
* preparation for dropping String monkey patching
|
||||
* `require "rainbow/string"` if you want to use monkey patched String
|
||||
* introduction of Rainbow() wrapper
|
||||
* support for MRI 1.8.7, 1.9.2, 1.9.3, 2.0 and 2.1, JRuby and Rubinius
|
||||
* deprecation of Sickill::Rainbow namespace (use Rainbow.enabled = true instead)
|
||||
|
||||
## 1.1.4 (2012-4-28)
|
||||
|
||||
* option for forcing coloring even when STDOUT is not a TTY (CLICOLOR_FORCE env var)
|
||||
* fix for frozen strings
|
||||
|
||||
## 1.1.3 (2011-12-6)
|
||||
|
||||
* improved compatibility with MRI 1.8.7
|
||||
* fix for regression with regards to original string mutation
|
||||
|
||||
## 1.1.2 (2011-11-13)
|
||||
|
||||
* improved compatibility with MRI 1.9.3
|
||||
|
||||
## 1.1.1 (2011-2-7)
|
||||
|
||||
* improved Windows support
|
||||
|
||||
## 1.1 (2010-6-7)
|
||||
|
||||
* option for enabling/disabling of escape code wrapping
|
||||
* auto-disabling when STDOUT is not a TTY
|
||||
|
||||
## 1.0.4 (2009-11-27)
|
||||
|
||||
* support for 256 colors
|
||||
|
||||
## 1.0.3 (2009-7-26)
|
||||
|
||||
* rainbow methods don't mutate the original string object anymore
|
||||
|
||||
## 1.0.2 (2009-5-15)
|
||||
|
||||
* improved support for ruby 1.8.6 and 1.9.1
|
||||
|
||||
## 1.0.1 (2009-3-19)
|
||||
|
||||
* Windows support
|
||||
|
||||
## 1.0.0 (2008-7-21)
|
||||
|
||||
* initial version
|
||||
@@ -0,0 +1,20 @@
|
||||
Copyright (c) Marcin Kulik
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
@@ -0,0 +1,227 @@
|
||||
# Rainbow
|
||||
|
||||
[](https://rubygems.org/gems/rainbow)
|
||||
[](https://travis-ci.org/sickill/rainbow)
|
||||
[](https://ci.appveyor.com/project/sickill/rainbow)
|
||||
[](https://codeclimate.com/github/sickill/rainbow)
|
||||
[](https://coveralls.io/r/sickill/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](http://en.wikipedia.org/wiki/ANSI_escape_code). 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
|
||||
|
||||
```ruby
|
||||
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"
|
||||
```
|
||||
|
||||

|
||||
|
||||
Or, [watch this video example](https://asciinema.org/a/J928KpHoUQ0sl54ulOSOLE71E?rows=20&speed=2.5)
|
||||
|
||||
### Rainbow presenter API
|
||||
|
||||
Rainbow presenter adds the following methods to presented string:
|
||||
|
||||
* `color(c)` (with `foreground`, and `fg` aliases)
|
||||
* `background(c)` (with `bg` alias)
|
||||
* `bright`
|
||||
* `underline`
|
||||
* `blink`
|
||||
* `inverse`
|
||||
* `hide`
|
||||
* `faint` (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:
|
||||
|
||||
* `black`
|
||||
* `red`
|
||||
* `green`
|
||||
* `yellow`
|
||||
* `blue`
|
||||
* `magenta`
|
||||
* `cyan`
|
||||
* `white`
|
||||
* `aqua`
|
||||
* `silver`
|
||||
* `aliceblue`
|
||||
* `indianred`
|
||||
|
||||
All of the methods return `self` (the presenter object) so you can chain method
|
||||
calls:
|
||||
|
||||
```ruby
|
||||
Rainbow("hola!").blue.bright.underline
|
||||
```
|
||||
|
||||
### Refinement
|
||||
|
||||
If you want to use the Refinements version, you can:
|
||||
|
||||
```ruby
|
||||
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](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors) name or [X11 color](https://en.wikipedia.org/wiki/X11_color_names) name as a symbol:
|
||||
`Rainbow("hello").color(:yellow)`.
|
||||
This can be simplified to `Rainbow("hello").yellow`
|
||||
|
||||
See [Color list](#user-content-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")` or `Rainbow("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:
|
||||
|
||||
```ruby
|
||||
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:
|
||||
|
||||
```ruby
|
||||
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.
|
||||
|
||||
```ruby
|
||||
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:
|
||||
|
||||
```ruby
|
||||
gem 'rainbow'
|
||||
```
|
||||
|
||||
Or just install it via rubygems:
|
||||
|
||||
```ruby
|
||||
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
|
||||
|
||||
[Marcin Kulik](http://ku1ik.com/) and [great open-source contributors](https://github.com/sickill/rainbow/graphs/contributors).
|
||||
@@ -0,0 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'rainbow/global'
|
||||
|
||||
module Rainbow
|
||||
def self.new
|
||||
Wrapper.new(global.enabled)
|
||||
end
|
||||
|
||||
self.enabled = false unless STDOUT.tty? && STDERR.tty?
|
||||
self.enabled = false if ENV['TERM'] == 'dumb'
|
||||
self.enabled = true if ENV['CLICOLOR_FORCE'] == '1'
|
||||
end
|
||||
@@ -0,0 +1,150 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Rainbow
|
||||
class Color
|
||||
attr_reader :ground
|
||||
|
||||
def self.build(ground, values)
|
||||
unless [1, 3].include?(values.size)
|
||||
raise ArgumentError,
|
||||
"Wrong number of arguments for color definition, should be 1 or 3"
|
||||
end
|
||||
|
||||
color = values.size == 1 ? values.first : values
|
||||
|
||||
case color
|
||||
# NOTE: Properly handle versions before/after Ruby 2.4.0.
|
||||
# Ruby 2.4+ unifies Fixnum & Bignum into Integer.
|
||||
# However previous versions would still use Fixnum.
|
||||
# To avoid missing `Fixnum` input, call `0.class` which would
|
||||
# return either `Integer` or `Fixnum`.
|
||||
when 0.class
|
||||
Indexed.new(ground, color)
|
||||
when Symbol
|
||||
if Named.color_names.include?(color)
|
||||
Named.new(ground, color)
|
||||
elsif X11Named.color_names.include?(color)
|
||||
X11Named.new(ground, color)
|
||||
else
|
||||
raise ArgumentError,
|
||||
"Unknown color name, valid names: " +
|
||||
(Named.color_names + X11Named.color_names).join(', ')
|
||||
end
|
||||
when Array
|
||||
RGB.new(ground, *color)
|
||||
when String
|
||||
RGB.new(ground, *parse_hex_color(color))
|
||||
end
|
||||
end
|
||||
|
||||
def self.parse_hex_color(hex)
|
||||
unless hex =~ /^#?[a-f0-9]{6}/i
|
||||
raise ArgumentError,
|
||||
"Invalid hexadecimal RGB triplet. Valid format: /^#?[a-f0-9]{6}/i"
|
||||
end
|
||||
|
||||
hex = hex.sub(/^#/, '')
|
||||
r = hex[0..1].to_i(16)
|
||||
g = hex[2..3].to_i(16)
|
||||
b = hex[4..5].to_i(16)
|
||||
|
||||
[r, g, b]
|
||||
end
|
||||
|
||||
class Indexed < Color
|
||||
attr_reader :num
|
||||
|
||||
def initialize(ground, num)
|
||||
@ground = ground
|
||||
@num = num
|
||||
end
|
||||
|
||||
def codes
|
||||
code = num + (ground == :foreground ? 30 : 40)
|
||||
|
||||
[code]
|
||||
end
|
||||
end
|
||||
|
||||
class Named < Indexed
|
||||
NAMES = {
|
||||
black: 0,
|
||||
red: 1,
|
||||
green: 2,
|
||||
yellow: 3,
|
||||
blue: 4,
|
||||
magenta: 5,
|
||||
cyan: 6,
|
||||
white: 7,
|
||||
default: 9
|
||||
}.freeze
|
||||
|
||||
def self.color_names
|
||||
NAMES.keys
|
||||
end
|
||||
|
||||
def self.valid_names
|
||||
color_names.join(', ')
|
||||
end
|
||||
|
||||
def initialize(ground, name)
|
||||
unless Named.color_names.include?(name)
|
||||
raise ArgumentError,
|
||||
"Unknown color name, valid names: #{self.class.valid_names}"
|
||||
end
|
||||
|
||||
super(ground, NAMES[name])
|
||||
end
|
||||
end
|
||||
|
||||
class RGB < Indexed
|
||||
attr_reader :r, :g, :b
|
||||
|
||||
def self.to_ansi_domain(value)
|
||||
(6 * (value / 256.0)).to_i
|
||||
end
|
||||
|
||||
def initialize(ground, *values)
|
||||
if values.min.negative? || values.max > 255
|
||||
raise ArgumentError, "RGB value outside 0-255 range"
|
||||
end
|
||||
|
||||
super(ground, 8)
|
||||
@r, @g, @b = values
|
||||
end
|
||||
|
||||
def codes
|
||||
super + [5, code_from_rgb]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def code_from_rgb
|
||||
16 + self.class.to_ansi_domain(r) * 36 +
|
||||
self.class.to_ansi_domain(g) * 6 +
|
||||
self.class.to_ansi_domain(b)
|
||||
end
|
||||
end
|
||||
|
||||
class X11Named < RGB
|
||||
include X11ColorNames
|
||||
|
||||
def self.color_names
|
||||
NAMES.keys
|
||||
end
|
||||
|
||||
def self.valid_names
|
||||
color_names.join(', ')
|
||||
end
|
||||
|
||||
def initialize(ground, name)
|
||||
unless X11Named.color_names.include?(name)
|
||||
raise ArgumentError,
|
||||
"Unknown color name, valid names: #{self.class.valid_names}"
|
||||
end
|
||||
|
||||
super(ground, *NAMES[name])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,64 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rainbow'
|
||||
|
||||
module Rainbow
|
||||
module Ext
|
||||
module String
|
||||
module InstanceMethods
|
||||
def foreground(*color)
|
||||
Rainbow(self).foreground(*color)
|
||||
end
|
||||
|
||||
alias color foreground
|
||||
alias colour foreground
|
||||
|
||||
def background(*color)
|
||||
Rainbow(self).background(*color)
|
||||
end
|
||||
|
||||
def reset
|
||||
Rainbow(self).reset
|
||||
end
|
||||
|
||||
def bright
|
||||
Rainbow(self).bright
|
||||
end
|
||||
|
||||
def faint
|
||||
Rainbow(self).faint
|
||||
end
|
||||
|
||||
def italic
|
||||
Rainbow(self).italic
|
||||
end
|
||||
|
||||
def underline
|
||||
Rainbow(self).underline
|
||||
end
|
||||
|
||||
def blink
|
||||
Rainbow(self).blink
|
||||
end
|
||||
|
||||
def inverse
|
||||
Rainbow(self).inverse
|
||||
end
|
||||
|
||||
def hide
|
||||
Rainbow(self).hide
|
||||
end
|
||||
|
||||
def cross_out
|
||||
Rainbow(self).cross_out
|
||||
end
|
||||
|
||||
alias strike cross_out
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class String
|
||||
include Rainbow::Ext::String::InstanceMethods
|
||||
end
|
||||
@@ -0,0 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'wrapper'
|
||||
|
||||
module Rainbow
|
||||
def self.global
|
||||
@global ||= Wrapper.new
|
||||
end
|
||||
|
||||
def self.enabled
|
||||
global.enabled
|
||||
end
|
||||
|
||||
def self.enabled=(value)
|
||||
global.enabled = value
|
||||
end
|
||||
|
||||
def self.uncolor(string)
|
||||
StringUtils.uncolor(string)
|
||||
end
|
||||
end
|
||||
|
||||
def Rainbow(string)
|
||||
Rainbow.global.wrap(string.to_s)
|
||||
end
|
||||
@@ -0,0 +1,100 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Rainbow
|
||||
class NullPresenter < ::String
|
||||
def color(*_values)
|
||||
self
|
||||
end
|
||||
|
||||
def background(*_values)
|
||||
self
|
||||
end
|
||||
|
||||
def reset
|
||||
self
|
||||
end
|
||||
|
||||
def bright
|
||||
self
|
||||
end
|
||||
|
||||
def faint
|
||||
self
|
||||
end
|
||||
|
||||
def italic
|
||||
self
|
||||
end
|
||||
|
||||
def underline
|
||||
self
|
||||
end
|
||||
|
||||
def blink
|
||||
self
|
||||
end
|
||||
|
||||
def inverse
|
||||
self
|
||||
end
|
||||
|
||||
def hide
|
||||
self
|
||||
end
|
||||
|
||||
def cross_out
|
||||
self
|
||||
end
|
||||
|
||||
def black
|
||||
self
|
||||
end
|
||||
|
||||
def red
|
||||
self
|
||||
end
|
||||
|
||||
def green
|
||||
self
|
||||
end
|
||||
|
||||
def yellow
|
||||
self
|
||||
end
|
||||
|
||||
def blue
|
||||
self
|
||||
end
|
||||
|
||||
def magenta
|
||||
self
|
||||
end
|
||||
|
||||
def cyan
|
||||
self
|
||||
end
|
||||
|
||||
def white
|
||||
self
|
||||
end
|
||||
|
||||
def method_missing(method_name, *args)
|
||||
if Color::X11Named.color_names.include?(method_name) && args.empty?
|
||||
self
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def respond_to_missing?(method_name, *args)
|
||||
Color::X11Named.color_names.include?(method_name) && args.empty? || super
|
||||
end
|
||||
|
||||
alias foreground color
|
||||
alias fg color
|
||||
alias bg background
|
||||
alias bold bright
|
||||
alias dark faint
|
||||
alias strike cross_out
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,144 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'string_utils'
|
||||
require_relative 'x11_color_names'
|
||||
require_relative 'color'
|
||||
|
||||
module Rainbow
|
||||
class Presenter < ::String
|
||||
TERM_EFFECTS = {
|
||||
reset: 0,
|
||||
bright: 1,
|
||||
faint: 2,
|
||||
italic: 3,
|
||||
underline: 4,
|
||||
blink: 5,
|
||||
inverse: 7,
|
||||
hide: 8,
|
||||
cross_out: 9
|
||||
}.freeze
|
||||
|
||||
# Sets color of this text.
|
||||
def color(*values)
|
||||
wrap_with_sgr(Color.build(:foreground, values).codes)
|
||||
end
|
||||
|
||||
alias foreground color
|
||||
alias fg color
|
||||
|
||||
# Sets background color of this text.
|
||||
def background(*values)
|
||||
wrap_with_sgr(Color.build(:background, values).codes)
|
||||
end
|
||||
|
||||
alias bg background
|
||||
|
||||
# Resets terminal to default colors/backgrounds.
|
||||
#
|
||||
# It shouldn't be needed to use this method because all methods
|
||||
# append terminal reset code to end of string.
|
||||
def reset
|
||||
wrap_with_sgr(TERM_EFFECTS[:reset])
|
||||
end
|
||||
|
||||
# Turns on bright/bold for this text.
|
||||
def bright
|
||||
wrap_with_sgr(TERM_EFFECTS[:bright])
|
||||
end
|
||||
|
||||
alias bold bright
|
||||
|
||||
# Turns on faint/dark for this text (not well supported by terminal
|
||||
# emulators).
|
||||
def faint
|
||||
wrap_with_sgr(TERM_EFFECTS[:faint])
|
||||
end
|
||||
|
||||
alias dark faint
|
||||
|
||||
# Turns on italic style for this text (not well supported by terminal
|
||||
# emulators).
|
||||
def italic
|
||||
wrap_with_sgr(TERM_EFFECTS[:italic])
|
||||
end
|
||||
|
||||
# Turns on underline decoration for this text.
|
||||
def underline
|
||||
wrap_with_sgr(TERM_EFFECTS[:underline])
|
||||
end
|
||||
|
||||
# Turns on blinking attribute for this text (not well supported by terminal
|
||||
# emulators).
|
||||
def blink
|
||||
wrap_with_sgr(TERM_EFFECTS[:blink])
|
||||
end
|
||||
|
||||
# Inverses current foreground/background colors.
|
||||
def inverse
|
||||
wrap_with_sgr(TERM_EFFECTS[:inverse])
|
||||
end
|
||||
|
||||
# Hides this text (set its color to the same as background).
|
||||
def hide
|
||||
wrap_with_sgr(TERM_EFFECTS[:hide])
|
||||
end
|
||||
|
||||
def cross_out
|
||||
wrap_with_sgr(TERM_EFFECTS[:cross_out])
|
||||
end
|
||||
|
||||
alias strike cross_out
|
||||
|
||||
def black
|
||||
color(:black)
|
||||
end
|
||||
|
||||
def red
|
||||
color(:red)
|
||||
end
|
||||
|
||||
def green
|
||||
color(:green)
|
||||
end
|
||||
|
||||
def yellow
|
||||
color(:yellow)
|
||||
end
|
||||
|
||||
def blue
|
||||
color(:blue)
|
||||
end
|
||||
|
||||
def magenta
|
||||
color(:magenta)
|
||||
end
|
||||
|
||||
def cyan
|
||||
color(:cyan)
|
||||
end
|
||||
|
||||
def white
|
||||
color(:white)
|
||||
end
|
||||
|
||||
# We take care of X11 color method call here.
|
||||
# Such as #aqua, #ghostwhite.
|
||||
def method_missing(method_name, *args)
|
||||
if Color::X11Named.color_names.include?(method_name) && args.empty?
|
||||
color(method_name)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def respond_to_missing?(method_name, *args)
|
||||
Color::X11Named.color_names.include?(method_name) && args.empty? || super
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def wrap_with_sgr(codes) #:nodoc:
|
||||
self.class.new(StringUtils.wrap_with_sgr(self, [*codes]))
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'presenter'
|
||||
require_relative 'global'
|
||||
|
||||
module Rainbow
|
||||
refine String do
|
||||
Presenter.instance_methods(false).each do |method_name|
|
||||
define_method(method_name) do |*args|
|
||||
::Rainbow.global.wrap(self).send(method_name, *args)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Rainbow
|
||||
class StringUtils
|
||||
def self.wrap_with_sgr(string, codes)
|
||||
return string if codes.empty?
|
||||
|
||||
seq = "\e[" + codes.join(";") + "m"
|
||||
|
||||
string = string.sub(/^(\e\[([\d;]+)m)*/) { |m| m + seq }
|
||||
|
||||
return string if string.end_with? "\e[0m"
|
||||
|
||||
string + "\e[0m"
|
||||
end
|
||||
|
||||
def self.uncolor(string)
|
||||
# See http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
|
||||
string.gsub(/\e\[[0-9;]*m/, '')
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Rainbow
|
||||
VERSION = "3.1.1"
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'presenter'
|
||||
require_relative 'null_presenter'
|
||||
|
||||
module Rainbow
|
||||
class Wrapper
|
||||
attr_accessor :enabled
|
||||
|
||||
def initialize(enabled = true)
|
||||
@enabled = enabled
|
||||
end
|
||||
|
||||
def wrap(string)
|
||||
if enabled
|
||||
Presenter.new(string.to_s)
|
||||
else
|
||||
NullPresenter.new(string.to_s)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,153 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Rainbow
|
||||
module X11ColorNames
|
||||
NAMES = {
|
||||
aqua: [0, 255, 255],
|
||||
aquamarine: [127, 255, 212],
|
||||
mediumaquamarine: [102, 205, 170],
|
||||
azure: [240, 255, 255],
|
||||
beige: [245, 245, 220],
|
||||
bisque: [255, 228, 196],
|
||||
black: [0, 0, 0],
|
||||
blanchedalmond: [255, 235, 205],
|
||||
blue: [0, 0, 255],
|
||||
darkblue: [0, 0, 139],
|
||||
lightblue: [173, 216, 230],
|
||||
mediumblue: [0, 0, 205],
|
||||
aliceblue: [240, 248, 255],
|
||||
cadetblue: [95, 158, 160],
|
||||
dodgerblue: [30, 144, 255],
|
||||
midnightblue: [25, 25, 112],
|
||||
navyblue: [0, 0, 128],
|
||||
powderblue: [176, 224, 230],
|
||||
royalblue: [65, 105, 225],
|
||||
skyblue: [135, 206, 235],
|
||||
deepskyblue: [0, 191, 255],
|
||||
lightskyblue: [135, 206, 250],
|
||||
slateblue: [106, 90, 205],
|
||||
darkslateblue: [72, 61, 139],
|
||||
mediumslateblue: [123, 104, 238],
|
||||
steelblue: [70, 130, 180],
|
||||
lightsteelblue: [176, 196, 222],
|
||||
brown: [165, 42, 42],
|
||||
rosybrown: [188, 143, 143],
|
||||
saddlebrown: [139, 69, 19],
|
||||
sandybrown: [244, 164, 96],
|
||||
burlywood: [222, 184, 135],
|
||||
chartreuse: [127, 255, 0],
|
||||
chocolate: [210, 105, 30],
|
||||
coral: [255, 127, 80],
|
||||
lightcoral: [240, 128, 128],
|
||||
cornflower: [100, 149, 237],
|
||||
cornsilk: [255, 248, 220],
|
||||
crimson: [220, 20, 60],
|
||||
cyan: [0, 255, 255],
|
||||
darkcyan: [0, 139, 139],
|
||||
lightcyan: [224, 255, 255],
|
||||
firebrick: [178, 34, 34],
|
||||
fuchsia: [255, 0, 255],
|
||||
gainsboro: [220, 220, 220],
|
||||
gold: [255, 215, 0],
|
||||
goldenrod: [218, 165, 32],
|
||||
darkgoldenrod: [184, 134, 11],
|
||||
lightgoldenrod: [250, 250, 210],
|
||||
palegoldenrod: [238, 232, 170],
|
||||
gray: [190, 190, 190],
|
||||
darkgray: [169, 169, 169],
|
||||
dimgray: [105, 105, 105],
|
||||
lightgray: [211, 211, 211],
|
||||
slategray: [112, 128, 144],
|
||||
lightslategray: [119, 136, 153],
|
||||
webgray: [128, 128, 128],
|
||||
green: [0, 255, 0],
|
||||
darkgreen: [0, 100, 0],
|
||||
lightgreen: [144, 238, 144],
|
||||
palegreen: [152, 251, 152],
|
||||
darkolivegreen: [85, 107, 47],
|
||||
yellowgreen: [154, 205, 50],
|
||||
forestgreen: [34, 139, 34],
|
||||
lawngreen: [124, 252, 0],
|
||||
limegreen: [50, 205, 50],
|
||||
seagreen: [46, 139, 87],
|
||||
darkseagreen: [143, 188, 143],
|
||||
lightseagreen: [32, 178, 170],
|
||||
mediumseagreen: [60, 179, 113],
|
||||
springgreen: [0, 255, 127],
|
||||
mediumspringgreen: [0, 250, 154],
|
||||
webgreen: [0, 128, 0],
|
||||
honeydew: [240, 255, 240],
|
||||
indianred: [205, 92, 92],
|
||||
indigo: [75, 0, 130],
|
||||
ivory: [255, 255, 240],
|
||||
khaki: [240, 230, 140],
|
||||
darkkhaki: [189, 183, 107],
|
||||
lavender: [230, 230, 250],
|
||||
lavenderblush: [255, 240, 245],
|
||||
lemonchiffon: [255, 250, 205],
|
||||
lime: [0, 255, 0],
|
||||
linen: [250, 240, 230],
|
||||
magenta: [255, 0, 255],
|
||||
darkmagenta: [139, 0, 139],
|
||||
maroon: [176, 48, 96],
|
||||
webmaroon: [127, 0, 0],
|
||||
mintcream: [245, 255, 250],
|
||||
mistyrose: [255, 228, 225],
|
||||
moccasin: [255, 228, 181],
|
||||
oldlace: [253, 245, 230],
|
||||
olive: [128, 128, 0],
|
||||
olivedrab: [107, 142, 35],
|
||||
orange: [255, 165, 0],
|
||||
darkorange: [255, 140, 0],
|
||||
orchid: [218, 112, 214],
|
||||
darkorchid: [153, 50, 204],
|
||||
mediumorchid: [186, 85, 211],
|
||||
papayawhip: [255, 239, 213],
|
||||
peachpuff: [255, 218, 185],
|
||||
peru: [205, 133, 63],
|
||||
pink: [255, 192, 203],
|
||||
deeppink: [255, 20, 147],
|
||||
lightpink: [255, 182, 193],
|
||||
hotpink: [255, 105, 180],
|
||||
plum: [221, 160, 221],
|
||||
purple: [160, 32, 240],
|
||||
mediumpurple: [147, 112, 219],
|
||||
rebeccapurple: [102, 51, 153],
|
||||
webpurple: [127, 0, 127],
|
||||
red: [255, 0, 0],
|
||||
darkred: [139, 0, 0],
|
||||
orangered: [255, 69, 0],
|
||||
mediumvioletred: [199, 21, 133],
|
||||
palevioletred: [219, 112, 147],
|
||||
salmon: [250, 128, 114],
|
||||
darksalmon: [233, 150, 122],
|
||||
lightsalmon: [255, 160, 122],
|
||||
seashell: [255, 245, 238],
|
||||
sienna: [160, 82, 45],
|
||||
silver: [192, 192, 192],
|
||||
darkslategray: [47, 79, 79],
|
||||
snow: [255, 250, 250],
|
||||
tan: [210, 180, 140],
|
||||
teal: [0, 128, 128],
|
||||
thistle: [216, 191, 216],
|
||||
tomato: [255, 99, 71],
|
||||
turquoise: [64, 224, 208],
|
||||
darkturquoise: [0, 206, 209],
|
||||
mediumturquoise: [72, 209, 204],
|
||||
paleturquoise: [175, 238, 238],
|
||||
violet: [238, 130, 238],
|
||||
darkviolet: [148, 0, 211],
|
||||
blueviolet: [138, 43, 226],
|
||||
wheat: [245, 222, 179],
|
||||
white: [255, 255, 255],
|
||||
antiquewhite: [250, 235, 215],
|
||||
floralwhite: [255, 250, 240],
|
||||
ghostwhite: [248, 248, 255],
|
||||
navajowhite: [255, 222, 173],
|
||||
whitesmoke: [245, 245, 245],
|
||||
yellow: [255, 255, 0],
|
||||
lightyellow: [255, 255, 224],
|
||||
greenyellow: [173, 255, 47]
|
||||
}.freeze
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user