rainbow 1.1.4 → 1.99.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +11 -0
- data/Gemfile +17 -0
- data/Guardfile +9 -0
- data/README.markdown +91 -48
- data/Rakefile +6 -0
- data/lib/rainbow.rb +14 -96
- data/lib/rainbow/color.rb +109 -0
- data/lib/rainbow/core.rb +16 -0
- data/lib/rainbow/legacy.rb +14 -0
- data/lib/rainbow/string.rb +50 -0
- data/lib/rainbow/string_utils.rb +23 -0
- data/lib/rainbow/version.rb +3 -0
- data/lib/rainbow/wrapper.rb +76 -0
- data/rainbow.gemspec +25 -0
- data/spec/integration/rainbow_spec.rb +132 -0
- data/spec/integration/string_spec.rb +50 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/unit/color_spec.rb +244 -0
- data/spec/unit/namespace_spec.rb +31 -0
- data/spec/unit/string_utils_spec.rb +62 -0
- data/spec/unit/wrapper_spec.rb +149 -0
- metadata +86 -20
- data/lib/ansi_color.rb +0 -71
- data/lib/ansi_rgb.rb +0 -43
- data/test/rainbow_test.rb +0 -277
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 820cb8349ec300d38fa0b456dfec3d4fe3399314
|
|
4
|
+
data.tar.gz: bc326589c9e418bb2dc1d13989d9931b8a7d548e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7f487f330dd0cc1401f5b1172ac5e2eca94f54371f02ab79f5d12a5c23c2c3ee5938a9036875b53575721ecae9d33257676f3400f042894678d9beffe68c0df9
|
|
7
|
+
data.tar.gz: c0b10c5d741321d8743f5b0a35feb3cc9ba3128dd5798462a6b89c273a6d3b725a1062a1fe725a87a2f394b7dd34e78189b9eb0304c8776efd2806ee2ce6413f
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
source 'https://rubygems.org'
|
|
2
|
+
|
|
3
|
+
# Specify your gem's dependencies in rainbow.gemspec
|
|
4
|
+
gemspec
|
|
5
|
+
|
|
6
|
+
gem 'coveralls', :require => false
|
|
7
|
+
gem 'mime-types', '< 2.0.0', :platforms => [:ruby_18]
|
|
8
|
+
|
|
9
|
+
group :guard do
|
|
10
|
+
gem 'guard'
|
|
11
|
+
gem 'guard-rspec'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
platform :rbx do
|
|
15
|
+
gem 'rubysl'
|
|
16
|
+
gem 'rubysl-json'
|
|
17
|
+
end
|
data/Guardfile
ADDED
data/README.markdown
CHANGED
|
@@ -1,75 +1,118 @@
|
|
|
1
|
-
Rainbow
|
|
2
|
-
=======
|
|
1
|
+
# Rainbow
|
|
3
2
|
|
|
4
|
-
](https://travis-ci.org/sickill/rainbow)
|
|
4
|
+
[](https://codeclimate.com/github/sickill/rainbow)
|
|
5
|
+
[](https://coveralls.io/r/sickill/rainbow)
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
-----
|
|
7
|
+
Rainbow is a ruby gem for colorizing printed text on ANSI terminals.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
It provides a string wrapper object that allows for wrapping a string with
|
|
10
|
+
[ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code). These codes
|
|
11
|
+
when printed in a terminal change text attributes like text color, background,
|
|
12
|
+
intensity etc.
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
--------
|
|
14
|
+
## Example
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
```ruby
|
|
17
|
+
require 'rainbow'
|
|
15
18
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
* reset
|
|
19
|
-
* bright
|
|
20
|
-
* italic (not well supported by terminal emulators).
|
|
21
|
-
* underline
|
|
22
|
-
* blink
|
|
23
|
-
* inverse
|
|
24
|
-
* hide.
|
|
19
|
+
puts Rainbow("this is red").color(:red) + " and " + Rainbow("this on yellow bg").background(:yellow) + " and " + Rainbow("even bright underlined!").underline.bright
|
|
20
|
+
```
|
|
25
21
|
|
|
26
|
-
|
|
22
|
+
## API
|
|
27
23
|
|
|
28
|
-
|
|
24
|
+
Following methods are available on a string wrapper object:
|
|
29
25
|
|
|
30
|
-
|
|
26
|
+
* `foreground(color)` (with `color` and `colour` aliases)
|
|
27
|
+
* `background(color)`
|
|
28
|
+
* `bright`
|
|
29
|
+
* `underline`
|
|
30
|
+
* `blink`
|
|
31
|
+
* `inverse`
|
|
32
|
+
* `hide`
|
|
33
|
+
* `italic` (not well supported by terminal emulators).
|
|
31
34
|
|
|
32
|
-
|
|
35
|
+
All of the methods return wrapped string so you can chain method calls as in
|
|
36
|
+
the above example.
|
|
33
37
|
|
|
34
|
-
|
|
35
|
-
"Jolacz".color("#FFC482")
|
|
36
|
-
"Jolacz".color("FFC482")
|
|
38
|
+
### String mixin
|
|
37
39
|
|
|
38
|
-
|
|
40
|
+
If you prefer not to wrap every string you want to colorize with `Rainbow()`
|
|
41
|
+
you can include all the rainbow methods in a String class directly by requiring
|
|
42
|
+
`rainbow/string`:
|
|
39
43
|
|
|
40
|
-
|
|
44
|
+
```ruby
|
|
45
|
+
require 'rainbow/string'
|
|
41
46
|
|
|
42
|
-
|
|
47
|
+
puts "this is red".color(:red) + " and " + "this on yellow bg".background(:yellow) + " and " + "even bright underlined!".underline.bright
|
|
48
|
+
```
|
|
43
49
|
|
|
44
|
-
Rainbow
|
|
50
|
+
This way of using Rainbow is not recommended though as it pollutes String's
|
|
51
|
+
public interface with methods that are presentation specific.
|
|
45
52
|
|
|
46
|
-
|
|
53
|
+
NOTE: the mixin is included in String by default in rainbow 1.99 to not break
|
|
54
|
+
backwards compatibility. It won't be included by default in rainbow 2.0.
|
|
47
55
|
|
|
48
|
-
|
|
56
|
+
### Color specification
|
|
49
57
|
|
|
50
|
-
|
|
51
|
-
|
|
58
|
+
Both `foreground/color/colour` and `background` accept color specified in any
|
|
59
|
+
of the following ways:
|
|
52
60
|
|
|
53
|
-
|
|
61
|
+
* color number (where 1 is red, 2 is green and so on):
|
|
62
|
+
`Rainbow("hello").foreground(1)`
|
|
54
63
|
|
|
55
|
-
|
|
56
|
-
|
|
64
|
+
* color name as a symbol (available: :black, :red, :green, :yellow, :blue,
|
|
65
|
+
:magenta, :cyan, :white):
|
|
66
|
+
`Rainbow("hello").foreground(:yellow)`
|
|
57
67
|
|
|
58
|
-
|
|
59
|
-
|
|
68
|
+
* RGB triplet as separate values in the range 0-255:
|
|
69
|
+
`Rainbow("hello").foreground(115, 23, 98)`
|
|
60
70
|
|
|
61
|
-
|
|
62
|
-
|
|
71
|
+
* RGB triplet as a hex string:
|
|
72
|
+
`Rainbow("hello").foreground("FFC482")` or `Rainbow("hello").foreground("#FFC482")`
|
|
63
73
|
|
|
64
|
-
|
|
65
|
-
|
|
74
|
+
When you specify a color with a RGB triplet rainbow finds the nearest match
|
|
75
|
+
from 256 colors palette. Note that it requires a 256-colors capable terminal to
|
|
76
|
+
display correctly.
|
|
66
77
|
|
|
67
|
-
|
|
68
|
-
Sickill::Rainbow.enabled = true
|
|
78
|
+
## Windows support
|
|
69
79
|
|
|
70
|
-
|
|
71
|
-
-------
|
|
80
|
+
For Windows support, you should install the following gems:
|
|
72
81
|
|
|
73
|
-
|
|
74
|
-
|
|
82
|
+
```ruby
|
|
83
|
+
gem install windows-pr win32console
|
|
84
|
+
```
|
|
75
85
|
|
|
86
|
+
If the above gems aren't installed then all strings are returned unmodified.
|
|
87
|
+
|
|
88
|
+
## Configuration
|
|
89
|
+
|
|
90
|
+
Rainbow can be enabled/disabled globally by setting:
|
|
91
|
+
|
|
92
|
+
```ruby
|
|
93
|
+
Rainbow.enabled = true/false
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
When disabled all the methods return an unmodified string
|
|
97
|
+
(`Rainbow("hello").color(:red) == "hello"`).
|
|
98
|
+
|
|
99
|
+
It's enabled by default, unless STDOUT/STDERR is not a TTY or a terminal is
|
|
100
|
+
dumb.
|
|
101
|
+
|
|
102
|
+
## Installation
|
|
103
|
+
|
|
104
|
+
Add it to your Gemfile:
|
|
105
|
+
|
|
106
|
+
```ruby
|
|
107
|
+
gem 'rainbow'
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Or just install it via rubygems:
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
gem install rainbow
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Authors
|
|
117
|
+
|
|
118
|
+
[Marcin Kulik](http://ku1ik.com/) and [great open-source contributors](https://github.com/sickill/rainbow/graphs/contributors).
|
data/Rakefile
ADDED
data/lib/rainbow.rb
CHANGED
|
@@ -1,108 +1,26 @@
|
|
|
1
|
-
require '
|
|
2
|
-
require
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
module Rainbow
|
|
6
|
-
class << self; attr_accessor :enabled; end
|
|
7
|
-
@enabled = STDOUT.tty? && ENV['TERM'] != 'dumb' || ENV['CLICOLOR_FORCE'] == '1'
|
|
8
|
-
|
|
9
|
-
TERM_COLORS = {
|
|
10
|
-
:black => 0,
|
|
11
|
-
:red => 1,
|
|
12
|
-
:green => 2,
|
|
13
|
-
:yellow => 3,
|
|
14
|
-
:blue => 4,
|
|
15
|
-
:magenta => 5,
|
|
16
|
-
:cyan => 6,
|
|
17
|
-
:white => 7,
|
|
18
|
-
:default => 9,
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
TERM_EFFECTS = {
|
|
22
|
-
:reset => 0,
|
|
23
|
-
:bright => 1,
|
|
24
|
-
:italic => 3,
|
|
25
|
-
:underline => 4,
|
|
26
|
-
:blink => 5,
|
|
27
|
-
:inverse => 7,
|
|
28
|
-
:hide => 8,
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
# Sets foreground color of this text.
|
|
32
|
-
def foreground(*color)
|
|
33
|
-
wrap_with_code(AnsiColor.new(:foreground, *color).code)
|
|
34
|
-
end
|
|
35
|
-
alias_method :color, :foreground
|
|
36
|
-
alias_method :colour, :foreground
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
# Sets background color of this text.
|
|
40
|
-
def background(*color)
|
|
41
|
-
wrap_with_code(AnsiColor.new(:background, *color).code)
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
# Resets terminal to default colors/backgrounds.
|
|
45
|
-
#
|
|
46
|
-
# It shouldn't be needed to use this method because all methods
|
|
47
|
-
# append terminal reset code to end of string.
|
|
48
|
-
def reset
|
|
49
|
-
wrap_with_code(TERM_EFFECTS[:reset])
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
# Turns on bright/bold for this text.
|
|
53
|
-
def bright
|
|
54
|
-
wrap_with_code(TERM_EFFECTS[:bright])
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
# Turns on italic style for this text (not well supported by terminal
|
|
58
|
-
# emulators).
|
|
59
|
-
def italic
|
|
60
|
-
wrap_with_code(TERM_EFFECTS[:italic])
|
|
61
|
-
end
|
|
1
|
+
require 'rainbow/core'
|
|
2
|
+
require 'rainbow/wrapper'
|
|
3
|
+
require 'rainbow/string'
|
|
4
|
+
require 'rainbow/legacy'
|
|
62
5
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
# Turns on blinking attribute for this text (not well supported by terminal
|
|
69
|
-
# emulators).
|
|
70
|
-
def blink
|
|
71
|
-
wrap_with_code(TERM_EFFECTS[:blink])
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
# Inverses current foreground/background colors.
|
|
75
|
-
def inverse
|
|
76
|
-
wrap_with_code(TERM_EFFECTS[:inverse])
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
# Hides this text (set its color to the same as background).
|
|
80
|
-
def hide
|
|
81
|
-
wrap_with_code(TERM_EFFECTS[:hide])
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
private
|
|
85
|
-
|
|
86
|
-
def wrap_with_code(code) #:nodoc:
|
|
87
|
-
return self unless Sickill::Rainbow.enabled
|
|
88
|
-
|
|
89
|
-
var = self.dup
|
|
90
|
-
matched = var.match(/^(\e\[([\d;]+)m)*/)
|
|
91
|
-
var.insert(matched.end(0), "\e[#{code}m")
|
|
92
|
-
var.concat("\e[0m") unless var =~ /\e\[0m$/
|
|
93
|
-
var
|
|
94
|
-
end
|
|
6
|
+
unless STDOUT.tty? && STDERR.tty?
|
|
7
|
+
Rainbow.enabled = false
|
|
8
|
+
end
|
|
95
9
|
|
|
96
|
-
|
|
10
|
+
if ENV['TERM'] == 'dumb'
|
|
11
|
+
Rainbow.enabled = false
|
|
97
12
|
end
|
|
98
13
|
|
|
99
|
-
|
|
14
|
+
if ENV['CLICOLOR_FORCE'] == '1'
|
|
15
|
+
Rainbow.enabled = true
|
|
16
|
+
end
|
|
100
17
|
|
|
101
18
|
# On Windows systems, try to load the local ANSI support library
|
|
19
|
+
require 'rbconfig'
|
|
102
20
|
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
|
|
103
21
|
begin
|
|
104
22
|
require 'Win32/Console/ANSI'
|
|
105
23
|
rescue LoadError
|
|
106
|
-
|
|
24
|
+
Rainbow.enabled = false
|
|
107
25
|
end
|
|
108
26
|
end
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
module Rainbow
|
|
2
|
+
class Color
|
|
3
|
+
|
|
4
|
+
attr_reader :ground
|
|
5
|
+
|
|
6
|
+
def self.build(ground, values)
|
|
7
|
+
raise ArgumentError.new(
|
|
8
|
+
"Wrong number of arguments for color definition, should be 1 or 3"
|
|
9
|
+
) unless [1, 3].include?(values.size)
|
|
10
|
+
|
|
11
|
+
color = values.size == 1 ? values.first : values
|
|
12
|
+
|
|
13
|
+
case color
|
|
14
|
+
when ::Fixnum then Indexed.new(ground, color)
|
|
15
|
+
when ::Symbol then Named.new(ground, color)
|
|
16
|
+
when ::Array then RGB.new(ground, *color)
|
|
17
|
+
when ::String then RGB.new(ground, *parse_hex_color(color))
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.parse_hex_color(hex)
|
|
22
|
+
hex = hex.gsub('#', '')
|
|
23
|
+
r = hex[0..1].to_i(16)
|
|
24
|
+
g = hex[2..3].to_i(16)
|
|
25
|
+
b = hex[4..5].to_i(16)
|
|
26
|
+
|
|
27
|
+
[r, g, b]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
class Indexed < Color
|
|
31
|
+
|
|
32
|
+
attr_reader :num
|
|
33
|
+
|
|
34
|
+
def initialize(ground, num)
|
|
35
|
+
@ground = ground
|
|
36
|
+
@num = num
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def codes
|
|
40
|
+
code = num + (ground == :foreground ? 30 : 40)
|
|
41
|
+
|
|
42
|
+
[code]
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
class Named < Indexed
|
|
48
|
+
|
|
49
|
+
NAMES = {
|
|
50
|
+
:black => 0,
|
|
51
|
+
:red => 1,
|
|
52
|
+
:green => 2,
|
|
53
|
+
:yellow => 3,
|
|
54
|
+
:blue => 4,
|
|
55
|
+
:magenta => 5,
|
|
56
|
+
:cyan => 6,
|
|
57
|
+
:white => 7,
|
|
58
|
+
:default => 9,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
def initialize(ground, name)
|
|
62
|
+
raise ArgumentError.new(
|
|
63
|
+
"Unknown color name, valid names: #{color_names.join(', ')}"
|
|
64
|
+
) unless color_names.include?(name)
|
|
65
|
+
|
|
66
|
+
super(ground, NAMES[name])
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
def color_names
|
|
72
|
+
NAMES.keys
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
class RGB < Indexed
|
|
78
|
+
|
|
79
|
+
attr_reader :r, :g, :b
|
|
80
|
+
|
|
81
|
+
def self.to_ansi_domain(value)
|
|
82
|
+
(6 * (value / 256.0)).to_i
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def initialize(ground, *values)
|
|
86
|
+
raise ArgumentError.new(
|
|
87
|
+
"RGB value outside 0-255 range"
|
|
88
|
+
) if values.min < 0 or values.max > 255
|
|
89
|
+
|
|
90
|
+
super(ground, 8)
|
|
91
|
+
@r, @g, @b = values
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def codes
|
|
95
|
+
super + [5, code_from_rgb]
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
private
|
|
99
|
+
|
|
100
|
+
def code_from_rgb
|
|
101
|
+
16 + self.class.to_ansi_domain(r) * 36 +
|
|
102
|
+
self.class.to_ansi_domain(g) * 6 +
|
|
103
|
+
self.class.to_ansi_domain(b)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
end
|
|
109
|
+
end
|