rainbow 1.99.0 → 1.99.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +14 -0
- data/.travis.yml +0 -1
- data/Changelog.md +59 -0
- data/Gemfile +2 -2
- data/Guardfile +1 -1
- data/README.markdown +91 -35
- data/lib/rainbow.rb +18 -19
- data/lib/rainbow/color.rb +28 -22
- data/lib/rainbow/ext/string.rb +52 -0
- data/lib/rainbow/global.rb +21 -0
- data/lib/rainbow/legacy.rb +2 -1
- data/lib/rainbow/null_presenter.rb +30 -0
- data/lib/rainbow/presenter.rb +113 -0
- data/lib/rainbow/version.rb +1 -1
- data/lib/rainbow/wrapper.rb +12 -66
- data/rainbow.gemspec +9 -9
- data/spec/integration/instance_spec.rb +35 -0
- data/spec/integration/rainbow_spec.rb +13 -6
- data/spec/integration/string_spec.rb +27 -1
- data/spec/spec_helper.rb +1 -5
- data/spec/support/presenter_shared_examples.rb +9 -0
- data/spec/unit/null_presenter_spec.rb +93 -0
- data/spec/unit/presenter_spec.rb +168 -0
- data/spec/unit/wrapper_spec.rb +11 -132
- metadata +17 -6
- data/Changelog +0 -3
- data/lib/rainbow/core.rb +0 -16
- data/lib/rainbow/string.rb +0 -50
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0bc1801f960efaed6eabd94cd757e0cbe722652
|
4
|
+
data.tar.gz: 2dad299611e1e8624548ff9392edbf47e8387261
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0eeb4d8862a768369d6e3e957a9cffad58aa1e5bd259a6e04fdead2387f4ebc649e35181e4ce4fff2670849aa4a995819bd5dcb1d411f19e66bf534c04730863
|
7
|
+
data.tar.gz: 6fbcd166b88afa5ff0ade32320a9e8e3d0c381adbd56581452bdb31b5c19be21cc220d5b01a994e710deb6bbb78b07b82f51d07033daa04b1de1f97a0bdaf54f
|
data/.rubocop.yml
ADDED
data/.travis.yml
CHANGED
data/Changelog.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Rainbow changelog
|
2
|
+
|
3
|
+
## 1.0.0 (2008-7-21)
|
4
|
+
|
5
|
+
* initial version
|
6
|
+
|
7
|
+
## 1.0.1 (2009-3-19)
|
8
|
+
|
9
|
+
* Windows support
|
10
|
+
|
11
|
+
## 1.0.2 (2009-5-15)
|
12
|
+
|
13
|
+
* improved support for ruby 1.8.6 and 1.9.1
|
14
|
+
|
15
|
+
## 1.0.3 (2009-7-26)
|
16
|
+
|
17
|
+
* rainbow methods don't mutate the original string object anymore
|
18
|
+
|
19
|
+
## 1.0.4 (2009-11-27)
|
20
|
+
|
21
|
+
* support for 256 colors
|
22
|
+
|
23
|
+
## 1.1 (2010-6-7)
|
24
|
+
|
25
|
+
* option for enabling/disabling of escape code wrapping
|
26
|
+
* auto-disabling when STDOUT is not a TTY
|
27
|
+
|
28
|
+
## 1.1.1 (2011-2-7)
|
29
|
+
|
30
|
+
* improved Windows support
|
31
|
+
|
32
|
+
## 1.1.2 (2011-11-13)
|
33
|
+
|
34
|
+
* improved compatibility with MRI 1.9.3
|
35
|
+
|
36
|
+
## 1.1.3 (2011-12-6)
|
37
|
+
|
38
|
+
* improved compatibility with MRI 1.8.7
|
39
|
+
* fix for regression with regards to original string mutation
|
40
|
+
|
41
|
+
## 1.1.4 (2012-4-28)
|
42
|
+
|
43
|
+
* option for forcing coloring even when STDOUT is not a TTY (CLICOLOR_FORCE env var)
|
44
|
+
* fix for frozen strings
|
45
|
+
|
46
|
+
## 1.99.0 (2013-12-26)
|
47
|
+
|
48
|
+
* preparation for dropping String monkey patching
|
49
|
+
* `require "rainbow/string"` if you want to use monkey patched String
|
50
|
+
* introduction of Rainbow() wrapper
|
51
|
+
* support for MRI 1.8.7, 1.9.2, 1.9.3, 2.0 and 2.1, JRuby and Rubinius
|
52
|
+
* deprecation of Sickill::Rainbow namespace (use Rainbow.enabled = true instead)
|
53
|
+
|
54
|
+
## 1.99.1 (2013-12-28)
|
55
|
+
|
56
|
+
* drop support for ruby 1.8
|
57
|
+
* `require "rainbow/string"` -> `require "rainbow/ext/string"`
|
58
|
+
* custom rainbow wrapper instances (with separate enabled/disabled state)
|
59
|
+
* shortcut methods for changing text color (`Rainbow("foo").red`)
|
data/Gemfile
CHANGED
@@ -3,8 +3,8 @@ source 'https://rubygems.org'
|
|
3
3
|
# Specify your gem's dependencies in rainbow.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
-
gem 'coveralls', :
|
7
|
-
gem 'mime-types', '< 2.0.0', :
|
6
|
+
gem 'coveralls', require: false
|
7
|
+
gem 'mime-types', '< 2.0.0', platforms: [:ruby_18]
|
8
8
|
|
9
9
|
group :guard do
|
10
10
|
gem 'guard'
|
data/Guardfile
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# A sample Guardfile
|
2
2
|
# More info at https://github.com/guard/guard#readme
|
3
3
|
|
4
|
-
guard :rspec, :
|
4
|
+
guard :rspec, cmd: 'rspec --color' do
|
5
5
|
watch(%r{^spec/.+_spec\.rb$})
|
6
6
|
watch(%r{^lib/(.+)\.rb$}) { "spec" }
|
7
7
|
watch('spec/spec_helper.rb') { "spec" }
|
data/README.markdown
CHANGED
@@ -6,25 +6,33 @@
|
|
6
6
|
|
7
7
|
Rainbow is a ruby gem for colorizing printed text on ANSI terminals.
|
8
8
|
|
9
|
-
It provides a string
|
10
|
-
[ANSI escape
|
11
|
-
|
9
|
+
It provides a string presenter object, which adds several methods to your
|
10
|
+
strings for wrapping them in [ANSI escape
|
11
|
+
codes](http://en.wikipedia.org/wiki/ANSI_escape_code). These codes when printed
|
12
|
+
in a terminal change text attributes like text color, background color,
|
12
13
|
intensity etc.
|
13
14
|
|
14
|
-
##
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
To make your string colored wrap it with `Rainbow()` presenter and call
|
18
|
+
`.color(<color name>)` on it.
|
19
|
+
|
20
|
+
### Example
|
15
21
|
|
16
22
|
```ruby
|
17
23
|
require 'rainbow'
|
18
24
|
|
19
|
-
|
25
|
+
p Rainbow("this is red").red + " and " + Rainbow("this on yellow bg").bg(:yellow) + " and " + Rainbow("even bright underlined!").underline.bright
|
26
|
+
|
27
|
+
# => "\e[31mthis is red\e[0m and \e[43mthis on yellow bg\e[0m and \e[4m\e[1meven bright underlined!\e[0m"
|
20
28
|
```
|
21
29
|
|
22
|
-
|
30
|
+
### Rainbow presenter API
|
23
31
|
|
24
|
-
|
32
|
+
Rainbow presenter adds the following methods to presented string:
|
25
33
|
|
26
|
-
* `
|
27
|
-
* `background(
|
34
|
+
* `color(c)` (with `foreground`, and `fg` aliases)
|
35
|
+
* `background(c)` (with `bg` alias)
|
28
36
|
* `bright`
|
29
37
|
* `underline`
|
30
38
|
* `blink`
|
@@ -32,17 +40,32 @@ Following methods are available on a string wrapper object:
|
|
32
40
|
* `hide`
|
33
41
|
* `italic` (not well supported by terminal emulators).
|
34
42
|
|
35
|
-
|
36
|
-
|
43
|
+
Text color can also be changed by calling a method named by a color:
|
44
|
+
|
45
|
+
* `black`
|
46
|
+
* `red`
|
47
|
+
* `green`
|
48
|
+
* `yellow`
|
49
|
+
* `blue`
|
50
|
+
* `magenta`
|
51
|
+
* `cyan`
|
52
|
+
* `white`
|
53
|
+
|
54
|
+
All of the methods return `self` (the presenter object) so you can chain method
|
55
|
+
calls:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
Rainbow("hola!").blue.bright.underline
|
59
|
+
```
|
37
60
|
|
38
61
|
### String mixin
|
39
62
|
|
40
|
-
If you
|
41
|
-
you can include all the rainbow methods in a String class
|
42
|
-
`rainbow/string`:
|
63
|
+
If you don't like wrapping every string you want to colorize with `Rainbow()`
|
64
|
+
you can include all the rainbow presenter methods directly in a String class by
|
65
|
+
requiring `rainbow/ext/string`:
|
43
66
|
|
44
67
|
```ruby
|
45
|
-
require 'rainbow/string'
|
68
|
+
require 'rainbow/ext/string'
|
46
69
|
|
47
70
|
puts "this is red".color(:red) + " and " + "this on yellow bg".background(:yellow) + " and " + "even bright underlined!".underline.bright
|
48
71
|
```
|
@@ -50,54 +73,87 @@ puts "this is red".color(:red) + " and " + "this on yellow bg".background(:yello
|
|
50
73
|
This way of using Rainbow is not recommended though as it pollutes String's
|
51
74
|
public interface with methods that are presentation specific.
|
52
75
|
|
53
|
-
NOTE: the
|
54
|
-
|
76
|
+
NOTE: the mixing doesn't include shortcut methods for changing text color, you
|
77
|
+
should use "string".color(:blue) instead of "string".blue
|
78
|
+
|
79
|
+
NOTE: the mixin is included in String by default in rainbow versions up to (and
|
80
|
+
including) 1.99.x to not break backwards compatibility. It won't be included by
|
81
|
+
default in rainbow 2.0.
|
55
82
|
|
56
83
|
### Color specification
|
57
84
|
|
58
|
-
Both `
|
85
|
+
Both `color` and `background` accept color specified in any
|
59
86
|
of the following ways:
|
60
87
|
|
61
|
-
* color number (where 1 is red, 2 is green and so on):
|
62
|
-
`Rainbow("hello").
|
88
|
+
* color number (where 0 is black, 1 is red, 2 is green and so on):
|
89
|
+
`Rainbow("hello").color(1)`
|
63
90
|
|
64
|
-
* color name as a symbol (
|
91
|
+
* color name as a symbol (:black, :red, :green, :yellow, :blue,
|
65
92
|
:magenta, :cyan, :white):
|
66
|
-
`Rainbow("hello").
|
93
|
+
`Rainbow("hello").color(:yellow)`.
|
94
|
+
This can be simplified to `Rainbow("hello").yellow`
|
67
95
|
|
68
96
|
* RGB triplet as separate values in the range 0-255:
|
69
|
-
`Rainbow("hello").
|
97
|
+
`Rainbow("hello").color(115, 23, 98)`
|
70
98
|
|
71
99
|
* RGB triplet as a hex string:
|
72
|
-
`Rainbow("hello").
|
100
|
+
`Rainbow("hello").color("FFC482")` or `Rainbow("hello").color("#FFC482")`
|
73
101
|
|
74
102
|
When you specify a color with a RGB triplet rainbow finds the nearest match
|
75
103
|
from 256 colors palette. Note that it requires a 256-colors capable terminal to
|
76
104
|
display correctly.
|
77
105
|
|
78
|
-
|
106
|
+
### Configuration
|
79
107
|
|
80
|
-
|
108
|
+
Rainbow can be enabled/disabled globally by setting:
|
81
109
|
|
82
110
|
```ruby
|
83
|
-
|
111
|
+
Rainbow.enabled = true/false
|
84
112
|
```
|
85
113
|
|
86
|
-
|
114
|
+
When disabled all the methods return an unmodified string
|
115
|
+
(`Rainbow("hello").red == "hello"`).
|
116
|
+
|
117
|
+
It's enabled by default, unless STDOUT/STDERR is not a TTY or a terminal is
|
118
|
+
dumb.
|
87
119
|
|
88
|
-
|
120
|
+
### Advanced usage
|
89
121
|
|
90
|
-
Rainbow
|
122
|
+
`Rainbow()` and `Rainbow.enabled` operate on the global Rainbow wrapper
|
123
|
+
instance. If you would like to selectively enable/disable coloring in separate
|
124
|
+
parts of your application you can get a new Rainbow wrapper instance for each
|
125
|
+
of them and control the state of coloring during the runtime.
|
91
126
|
|
92
127
|
```ruby
|
93
|
-
|
128
|
+
rainbow_one = Rainbow.new
|
129
|
+
rainbow_two = Rainbow.new
|
130
|
+
|
131
|
+
rainbow_one.enabled = false
|
132
|
+
|
133
|
+
Rainbow("hello").red # => "\e[31mhello\e[0m" ("hello" if not on TTY)
|
134
|
+
rainbow_one.wrap("hello").red # => "hello"
|
135
|
+
rainbow_two.wrap("hello").red # => "\e[31mhello\e[0m" ("hello" if not on TTY)
|
94
136
|
```
|
95
137
|
|
96
|
-
|
97
|
-
|
138
|
+
By default each new instance inherits enabled/disabled state from the global
|
139
|
+
`Rainbow.enabled`.
|
98
140
|
|
99
|
-
|
100
|
-
|
141
|
+
This feature comes handy for example when you have multiple output formatters
|
142
|
+
in your application and some of them print to a terminal but others write to a
|
143
|
+
file. Normally rainbow would detect that STDIN/STDERR is a TTY and would
|
144
|
+
colorize all the strings, even the ones that go through file writing
|
145
|
+
formatters. You can easily solve that by disabling coloring for the Rainbow
|
146
|
+
instances that are used by formatters with file output.
|
147
|
+
|
148
|
+
## Windows support
|
149
|
+
|
150
|
+
For Windows support, you should install the following gems:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
gem install windows-pr win32console
|
154
|
+
```
|
155
|
+
|
156
|
+
If the above gems aren't installed then all strings are returned unmodified.
|
101
157
|
|
102
158
|
## Installation
|
103
159
|
|
data/lib/rainbow.rb
CHANGED
@@ -1,26 +1,25 @@
|
|
1
|
-
require 'rainbow/
|
2
|
-
require 'rainbow/wrapper'
|
3
|
-
require 'rainbow/string'
|
1
|
+
require 'rainbow/global'
|
4
2
|
require 'rainbow/legacy'
|
3
|
+
require 'rainbow/ext/string'
|
5
4
|
|
6
|
-
|
7
|
-
Rainbow.enabled = false
|
8
|
-
end
|
5
|
+
module Rainbow
|
9
6
|
|
10
|
-
|
11
|
-
|
12
|
-
end
|
7
|
+
def self.new
|
8
|
+
Wrapper.new(global.enabled)
|
9
|
+
end
|
13
10
|
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
self.enabled = false unless STDOUT.tty? && STDERR.tty?
|
12
|
+
self.enabled = false if ENV['TERM'] == 'dumb'
|
13
|
+
self.enabled = true if ENV['CLICOLOR_FORCE'] == '1'
|
17
14
|
|
18
|
-
# On Windows systems, try to load the local ANSI support library
|
19
|
-
require 'rbconfig'
|
20
|
-
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
15
|
+
# On Windows systems, try to load the local ANSI support library
|
16
|
+
require 'rbconfig'
|
17
|
+
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
|
18
|
+
begin
|
19
|
+
require 'Win32/Console/ANSI'
|
20
|
+
rescue LoadError
|
21
|
+
self.enabled = false
|
22
|
+
end
|
25
23
|
end
|
24
|
+
|
26
25
|
end
|
data/lib/rainbow/color.rb
CHANGED
@@ -4,17 +4,22 @@ module Rainbow
|
|
4
4
|
attr_reader :ground
|
5
5
|
|
6
6
|
def self.build(ground, values)
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
unless [1, 3].include?(values.size)
|
8
|
+
fail ArgumentError,
|
9
|
+
"Wrong number of arguments for color definition, should be 1 or 3"
|
10
|
+
end
|
10
11
|
|
11
12
|
color = values.size == 1 ? values.first : values
|
12
13
|
|
13
14
|
case color
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
when ::Fixnum
|
16
|
+
Indexed.new(ground, color)
|
17
|
+
when ::Symbol
|
18
|
+
Named.new(ground, color)
|
19
|
+
when ::Array
|
20
|
+
RGB.new(ground, *color)
|
21
|
+
when ::String
|
22
|
+
RGB.new(ground, *parse_hex_color(color))
|
18
23
|
end
|
19
24
|
end
|
20
25
|
|
@@ -47,21 +52,22 @@ module Rainbow
|
|
47
52
|
class Named < Indexed
|
48
53
|
|
49
54
|
NAMES = {
|
50
|
-
:
|
51
|
-
:
|
52
|
-
:
|
53
|
-
:
|
54
|
-
:
|
55
|
-
:
|
56
|
-
:
|
57
|
-
:
|
58
|
-
:
|
55
|
+
black: 0,
|
56
|
+
red: 1,
|
57
|
+
green: 2,
|
58
|
+
yellow: 3,
|
59
|
+
blue: 4,
|
60
|
+
magenta: 5,
|
61
|
+
cyan: 6,
|
62
|
+
white: 7,
|
63
|
+
default: 9,
|
59
64
|
}
|
60
65
|
|
61
66
|
def initialize(ground, name)
|
62
|
-
|
63
|
-
|
64
|
-
|
67
|
+
unless color_names.include?(name)
|
68
|
+
fail ArgumentError,
|
69
|
+
"Unknown color name, valid names: #{color_names.join(', ')}"
|
70
|
+
end
|
65
71
|
|
66
72
|
super(ground, NAMES[name])
|
67
73
|
end
|
@@ -83,9 +89,9 @@ module Rainbow
|
|
83
89
|
end
|
84
90
|
|
85
91
|
def initialize(ground, *values)
|
86
|
-
|
87
|
-
"RGB value outside 0-255 range"
|
88
|
-
|
92
|
+
if values.min < 0 || values.max > 255
|
93
|
+
fail ArgumentError, "RGB value outside 0-255 range"
|
94
|
+
end
|
89
95
|
|
90
96
|
super(ground, 8)
|
91
97
|
@r, @g, @b = values
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rainbow'
|
2
|
+
|
3
|
+
module Rainbow
|
4
|
+
module Ext
|
5
|
+
module String
|
6
|
+
module InstanceMethods
|
7
|
+
|
8
|
+
def foreground(*color)
|
9
|
+
Rainbow(self).foreground(*color)
|
10
|
+
end
|
11
|
+
|
12
|
+
alias_method :color, :foreground
|
13
|
+
alias_method :colour, :foreground
|
14
|
+
|
15
|
+
def background(*color)
|
16
|
+
Rainbow(self).background(*color)
|
17
|
+
end
|
18
|
+
|
19
|
+
def reset
|
20
|
+
Rainbow(self).reset
|
21
|
+
end
|
22
|
+
|
23
|
+
def bright
|
24
|
+
Rainbow(self).bright
|
25
|
+
end
|
26
|
+
|
27
|
+
def italic
|
28
|
+
Rainbow(self).italic
|
29
|
+
end
|
30
|
+
|
31
|
+
def underline
|
32
|
+
Rainbow(self).underline
|
33
|
+
end
|
34
|
+
|
35
|
+
def blink
|
36
|
+
Rainbow(self).blink
|
37
|
+
end
|
38
|
+
|
39
|
+
def inverse
|
40
|
+
Rainbow(self).inverse
|
41
|
+
end
|
42
|
+
|
43
|
+
def hide
|
44
|
+
Rainbow(self).hide
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
::String.send(:include, Rainbow::Ext::String::InstanceMethods)
|