rainbow 2.2.2 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +9 -3
- data/.rubocop_todo.yml +29 -0
- data/.travis.yml +13 -11
- data/Changelog.md +52 -46
- data/Gemfile +9 -4
- data/Guardfile +0 -1
- data/README.markdown +33 -26
- data/Rakefile +5 -2
- data/appveyor.yml +40 -0
- data/lib/rainbow.rb +0 -13
- data/lib/rainbow/color.rb +22 -23
- data/lib/rainbow/ext/string.rb +2 -4
- data/lib/rainbow/global.rb +3 -1
- data/lib/rainbow/null_presenter.rb +81 -29
- data/lib/rainbow/presenter.rb +13 -13
- data/lib/rainbow/refinement.rb +12 -0
- data/lib/rainbow/string_utils.rb +5 -4
- data/lib/rainbow/version.rb +1 -1
- data/lib/rainbow/wrapper.rb +0 -2
- data/lib/rainbow/x11_color_names.rb +3 -4
- data/rainbow.gemspec +4 -8
- data/spec/integration/instance_spec.rb +1 -3
- data/spec/integration/rainbow_spec.rb +27 -30
- data/spec/integration/refinement_spec.rb +36 -0
- data/spec/integration/string_spec.rb +11 -14
- data/spec/integration/uncolor_spec.rb +14 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/support/presenter_shared_examples.rb +2 -2
- data/spec/unit/color_spec.rb +5 -10
- data/spec/unit/null_presenter_spec.rb +1 -2
- data/spec/unit/presenter_spec.rb +11 -13
- data/spec/unit/string_utils_spec.rb +34 -2
- data/spec/unit/wrapper_spec.rb +1 -3
- metadata +20 -31
- data/ext/mkrf_conf.rb +0 -22
- data/lib/rainbow/legacy.rb +0 -15
- data/spec/unit/namespace_spec.rb +0 -31
data/lib/rainbow/ext/string.rb
CHANGED
@@ -4,13 +4,12 @@ module Rainbow
|
|
4
4
|
module Ext
|
5
5
|
module String
|
6
6
|
module InstanceMethods
|
7
|
-
|
8
7
|
def foreground(*color)
|
9
8
|
Rainbow(self).foreground(*color)
|
10
9
|
end
|
11
10
|
|
12
|
-
|
13
|
-
|
11
|
+
alias color foreground
|
12
|
+
alias colour foreground
|
14
13
|
|
15
14
|
def background(*color)
|
16
15
|
Rainbow(self).background(*color)
|
@@ -47,7 +46,6 @@ module Rainbow
|
|
47
46
|
def hide
|
48
47
|
Rainbow(self).hide
|
49
48
|
end
|
50
|
-
|
51
49
|
end
|
52
50
|
end
|
53
51
|
end
|
data/lib/rainbow/global.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require_relative 'wrapper'
|
2
2
|
|
3
3
|
module Rainbow
|
4
|
-
|
5
4
|
def self.global
|
6
5
|
@global ||= Wrapper.new
|
7
6
|
end
|
@@ -14,6 +13,9 @@ module Rainbow
|
|
14
13
|
global.enabled = value
|
15
14
|
end
|
16
15
|
|
16
|
+
def self.uncolor(string)
|
17
|
+
StringUtils.uncolor(string)
|
18
|
+
end
|
17
19
|
end
|
18
20
|
|
19
21
|
def Rainbow(string)
|
@@ -1,41 +1,93 @@
|
|
1
1
|
module Rainbow
|
2
|
-
|
3
2
|
class NullPresenter < ::String
|
3
|
+
def color(*_values)
|
4
|
+
self
|
5
|
+
end
|
6
|
+
|
7
|
+
def background(*_values)
|
8
|
+
self
|
9
|
+
end
|
10
|
+
|
11
|
+
def reset
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
def bright
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def faint
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def italic
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def underline
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
def blink
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
def inverse
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def hide
|
40
|
+
self
|
41
|
+
end
|
42
|
+
|
43
|
+
def black
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def red
|
48
|
+
self
|
49
|
+
end
|
4
50
|
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
def
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
def
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
def
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
def
|
26
|
-
|
51
|
+
def green
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
def yellow
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
def blue
|
60
|
+
self
|
61
|
+
end
|
62
|
+
|
63
|
+
def magenta
|
64
|
+
self
|
65
|
+
end
|
66
|
+
|
67
|
+
def cyan
|
68
|
+
self
|
69
|
+
end
|
70
|
+
|
71
|
+
def white
|
72
|
+
self
|
73
|
+
end
|
74
|
+
|
75
|
+
def method_missing(method_name, *args)
|
76
|
+
if Color::X11Named.color_names.include?(method_name) && args.empty?
|
27
77
|
self
|
28
78
|
else
|
29
79
|
super
|
30
80
|
end
|
31
81
|
end
|
32
82
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
alias_method :bold, :bright
|
37
|
-
alias_method :dark, :faint
|
83
|
+
def respond_to_missing?(method_name, *args)
|
84
|
+
Color::X11Named.color_names.include?(method_name) && args.empty? || super
|
85
|
+
end
|
38
86
|
|
87
|
+
alias foreground color
|
88
|
+
alias fg color
|
89
|
+
alias bg background
|
90
|
+
alias bold bright
|
91
|
+
alias dark faint
|
39
92
|
end
|
40
|
-
|
41
93
|
end
|
data/lib/rainbow/presenter.rb
CHANGED
@@ -3,9 +3,7 @@ require_relative 'x11_color_names'
|
|
3
3
|
require_relative 'color'
|
4
4
|
|
5
5
|
module Rainbow
|
6
|
-
|
7
6
|
class Presenter < ::String
|
8
|
-
|
9
7
|
TERM_EFFECTS = {
|
10
8
|
reset: 0,
|
11
9
|
bright: 1,
|
@@ -14,23 +12,23 @@ module Rainbow
|
|
14
12
|
underline: 4,
|
15
13
|
blink: 5,
|
16
14
|
inverse: 7,
|
17
|
-
hide: 8
|
18
|
-
}
|
15
|
+
hide: 8
|
16
|
+
}.freeze
|
19
17
|
|
20
18
|
# Sets color of this text.
|
21
19
|
def color(*values)
|
22
20
|
wrap_with_sgr(Color.build(:foreground, values).codes)
|
23
21
|
end
|
24
22
|
|
25
|
-
|
26
|
-
|
23
|
+
alias foreground color
|
24
|
+
alias fg color
|
27
25
|
|
28
26
|
# Sets background color of this text.
|
29
27
|
def background(*values)
|
30
28
|
wrap_with_sgr(Color.build(:background, values).codes)
|
31
29
|
end
|
32
30
|
|
33
|
-
|
31
|
+
alias bg background
|
34
32
|
|
35
33
|
# Resets terminal to default colors/backgrounds.
|
36
34
|
#
|
@@ -45,7 +43,7 @@ module Rainbow
|
|
45
43
|
wrap_with_sgr(TERM_EFFECTS[:bright])
|
46
44
|
end
|
47
45
|
|
48
|
-
|
46
|
+
alias bold bright
|
49
47
|
|
50
48
|
# Turns on faint/dark for this text (not well supported by terminal
|
51
49
|
# emulators).
|
@@ -53,7 +51,7 @@ module Rainbow
|
|
53
51
|
wrap_with_sgr(TERM_EFFECTS[:faint])
|
54
52
|
end
|
55
53
|
|
56
|
-
|
54
|
+
alias dark faint
|
57
55
|
|
58
56
|
# Turns on italic style for this text (not well supported by terminal
|
59
57
|
# emulators).
|
@@ -116,20 +114,22 @@ module Rainbow
|
|
116
114
|
|
117
115
|
# We take care of X11 color method call here.
|
118
116
|
# Such as #aqua, #ghostwhite.
|
119
|
-
def method_missing(method_name
|
120
|
-
if Color::X11Named.color_names.include?
|
117
|
+
def method_missing(method_name, *args)
|
118
|
+
if Color::X11Named.color_names.include?(method_name) && args.empty?
|
121
119
|
color(method_name)
|
122
120
|
else
|
123
121
|
super
|
124
122
|
end
|
125
123
|
end
|
126
124
|
|
125
|
+
def respond_to_missing?(method_name, *args)
|
126
|
+
Color::X11Named.color_names.include?(method_name) && args.empty? || super
|
127
|
+
end
|
128
|
+
|
127
129
|
private
|
128
130
|
|
129
131
|
def wrap_with_sgr(codes) #:nodoc:
|
130
132
|
self.class.new(StringUtils.wrap_with_sgr(self, [*codes]))
|
131
133
|
end
|
132
|
-
|
133
134
|
end
|
134
|
-
|
135
135
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require_relative 'presenter'
|
2
|
+
require_relative 'global'
|
3
|
+
|
4
|
+
module Rainbow
|
5
|
+
refine String do
|
6
|
+
Presenter.instance_methods(false).each do |method_name|
|
7
|
+
define_method(method_name) do |*args|
|
8
|
+
::Rainbow.global.wrap(self).send(method_name, *args)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/rainbow/string_utils.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
module Rainbow
|
2
2
|
class StringUtils
|
3
|
-
|
4
3
|
def self.wrap_with_sgr(string, codes)
|
5
4
|
return string if codes.empty?
|
6
5
|
|
@@ -9,12 +8,14 @@ module Rainbow
|
|
9
8
|
seq_pos = match.end(0)
|
10
9
|
string = string[0...seq_pos] + seq + string[seq_pos..-1]
|
11
10
|
|
12
|
-
unless string =~ /\e\[0m$/
|
13
|
-
string = string + "\e[0m"
|
14
|
-
end
|
11
|
+
string += "\e[0m" unless string =~ /\e\[0m$/
|
15
12
|
|
16
13
|
string
|
17
14
|
end
|
18
15
|
|
16
|
+
def self.uncolor(string)
|
17
|
+
# See http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
|
18
|
+
string.gsub(/\e\[[0-9;]*m/, '')
|
19
|
+
end
|
19
20
|
end
|
20
21
|
end
|
data/lib/rainbow/version.rb
CHANGED
data/lib/rainbow/wrapper.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module Rainbow
|
2
|
-
|
3
2
|
module X11ColorNames
|
4
|
-
NAMES= {
|
3
|
+
NAMES = {
|
5
4
|
aqua: [0, 255, 255],
|
6
5
|
aquamarine: [127, 255, 212],
|
7
6
|
mediumaquamarine: [102, 205, 170],
|
@@ -147,6 +146,6 @@ module Rainbow
|
|
147
146
|
yellow: [255, 255, 0],
|
148
147
|
lightyellow: [255, 255, 224],
|
149
148
|
greenyellow: [173, 255, 47]
|
150
|
-
}
|
149
|
+
}.freeze
|
151
150
|
end
|
152
|
-
end
|
151
|
+
end
|
data/rainbow.gemspec
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# coding: utf-8
|
2
1
|
lib = File.expand_path('../lib', __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
3
|
require 'rainbow/version'
|
@@ -6,21 +5,18 @@ require 'rainbow/version'
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
6
|
spec.name = "rainbow"
|
8
7
|
spec.version = Rainbow::VERSION
|
9
|
-
spec.authors = ["Marcin Kulik"]
|
8
|
+
spec.authors = ["Marcin Kulik", "Olle Jonsson"]
|
10
9
|
spec.email = ["m@ku1ik.com"]
|
11
|
-
spec.description =
|
12
|
-
spec.summary =
|
10
|
+
spec.description = 'Colorize printed text on ANSI terminals'
|
11
|
+
spec.summary = 'Colorize printed text on ANSI terminals'
|
13
12
|
spec.homepage = "https://github.com/sickill/rainbow"
|
14
13
|
spec.license = "MIT"
|
15
|
-
spec.required_ruby_version = '>= 1.
|
14
|
+
spec.required_ruby_version = '>= 2.1.0'
|
16
15
|
|
17
16
|
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
18
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
19
|
spec.require_paths = ["lib"]
|
21
20
|
|
22
|
-
spec.add_dependency "rake"
|
23
|
-
|
24
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
-
spec.extensions = ["ext/mkrf_conf.rb"]
|
26
22
|
end
|
@@ -1,8 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'rainbow'
|
3
3
|
|
4
|
-
describe 'Custom Rainbow instance' do
|
5
|
-
|
4
|
+
RSpec.describe 'Custom Rainbow instance' do
|
6
5
|
it 'inherits enabled state from the global instance' do
|
7
6
|
Rainbow.enabled = :yep
|
8
7
|
expect(Rainbow.new.enabled).to eq(:yep)
|
@@ -31,5 +30,4 @@ describe 'Custom Rainbow instance' do
|
|
31
30
|
|
32
31
|
expect(rainbow.wrap('hello').green).to eq('hello')
|
33
32
|
end
|
34
|
-
|
35
33
|
end
|
@@ -1,8 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'rainbow'
|
3
3
|
|
4
|
-
describe 'Rainbow() wrapper' do
|
5
|
-
|
4
|
+
RSpec.describe 'Rainbow() wrapper' do
|
6
5
|
before do
|
7
6
|
Rainbow.enabled = true
|
8
7
|
end
|
@@ -113,19 +112,19 @@ describe 'Rainbow() wrapper' do
|
|
113
112
|
end
|
114
113
|
|
115
114
|
it 'allows chaining' do
|
116
|
-
result = Rainbow('hello')
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
115
|
+
result = Rainbow('hello')
|
116
|
+
.foreground(:red)
|
117
|
+
.bright
|
118
|
+
.bold
|
119
|
+
.faint
|
120
|
+
.dark
|
121
|
+
.italic
|
122
|
+
.background('#ff8040')
|
123
|
+
.underline
|
124
|
+
.color(:blue)
|
125
|
+
.blink
|
126
|
+
.inverse
|
127
|
+
.hide
|
129
128
|
|
130
129
|
expect(result).to eq(
|
131
130
|
"\e[31m\e[1m\e[1m\e[2m\e[2m\e[3m\e[48;5;215m\e[4m\e[34m\e[5m\e[7m\e[8mhello\e[0m"
|
@@ -133,28 +132,26 @@ describe 'Rainbow() wrapper' do
|
|
133
132
|
end
|
134
133
|
|
135
134
|
context "when Rainbow is disabled" do
|
136
|
-
|
137
135
|
before do
|
138
136
|
Rainbow.enabled = false
|
139
137
|
end
|
140
138
|
|
141
139
|
it "allows chaining but doesn't wrap with escape codes" do
|
142
|
-
result = Rainbow('hello')
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
140
|
+
result = Rainbow('hello')
|
141
|
+
.foreground(:red)
|
142
|
+
.bright
|
143
|
+
.bold
|
144
|
+
.faint
|
145
|
+
.dark
|
146
|
+
.italic
|
147
|
+
.background('#ff8040')
|
148
|
+
.underline
|
149
|
+
.color(:blue)
|
150
|
+
.blink
|
151
|
+
.inverse
|
152
|
+
.hide
|
155
153
|
|
156
154
|
expect(result).to eq('hello')
|
157
155
|
end
|
158
156
|
end
|
159
|
-
|
160
157
|
end
|