rainbow 1.99.0 → 2.0.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.
@@ -1,76 +1,22 @@
1
- require 'rainbow/core'
2
- require 'rainbow/string_utils'
3
- require 'rainbow/color'
1
+ require 'rainbow/presenter'
2
+ require 'rainbow/null_presenter'
4
3
 
5
4
  module Rainbow
6
5
 
7
- class Wrapper < ::String
6
+ class Wrapper
7
+ attr_accessor :enabled
8
8
 
9
- # Sets foreground color of this text.
10
- def foreground(*values)
11
- wrap_with_sgr(Color.build(:foreground, values).codes)
9
+ def initialize(enabled = true)
10
+ @enabled = enabled
12
11
  end
13
12
 
14
- alias_method :color, :foreground
15
- alias_method :colour, :foreground
16
-
17
- # Sets background color of this text.
18
- def background(*values)
19
- wrap_with_sgr(Color.build(:background, values).codes)
20
- end
21
-
22
- # Resets terminal to default colors/backgrounds.
23
- #
24
- # It shouldn't be needed to use this method because all methods
25
- # append terminal reset code to end of string.
26
- def reset
27
- wrap_with_sgr(TERM_EFFECTS[:reset])
28
- end
29
-
30
- # Turns on bright/bold for this text.
31
- def bright
32
- wrap_with_sgr(TERM_EFFECTS[:bright])
33
- end
34
-
35
- # Turns on italic style for this text (not well supported by terminal
36
- # emulators).
37
- def italic
38
- wrap_with_sgr(TERM_EFFECTS[:italic])
39
- end
40
-
41
- # Turns on underline decoration for this text.
42
- def underline
43
- wrap_with_sgr(TERM_EFFECTS[:underline])
44
- end
45
-
46
- # Turns on blinking attribute for this text (not well supported by terminal
47
- # emulators).
48
- def blink
49
- wrap_with_sgr(TERM_EFFECTS[:blink])
13
+ def wrap(string)
14
+ if enabled
15
+ Rainbow::Presenter.new(string.to_s)
16
+ else
17
+ Rainbow::NullPresenter.new(string.to_s)
18
+ end
50
19
  end
51
-
52
- # Inverses current foreground/background colors.
53
- def inverse
54
- wrap_with_sgr(TERM_EFFECTS[:inverse])
55
- end
56
-
57
- # Hides this text (set its color to the same as background).
58
- def hide
59
- wrap_with_sgr(TERM_EFFECTS[:hide])
60
- end
61
-
62
- private
63
-
64
- def wrap_with_sgr(codes) #:nodoc:
65
- return self unless Rainbow.enabled
66
-
67
- self.class.new(StringUtils.wrap_with_sgr(self, [*codes]))
68
- end
69
-
70
20
  end
71
21
 
72
22
  end
73
-
74
- def Rainbow(string)
75
- Rainbow::Wrapper.new(string.to_s)
76
- end
data/lib/rainbow.rb CHANGED
@@ -1,26 +1,24 @@
1
- require 'rainbow/core'
2
- require 'rainbow/wrapper'
3
- require 'rainbow/string'
1
+ require 'rainbow/global'
4
2
  require 'rainbow/legacy'
5
3
 
6
- unless STDOUT.tty? && STDERR.tty?
7
- Rainbow.enabled = false
8
- end
4
+ module Rainbow
9
5
 
10
- if ENV['TERM'] == 'dumb'
11
- Rainbow.enabled = false
12
- end
6
+ def self.new
7
+ Wrapper.new(global.enabled)
8
+ end
13
9
 
14
- if ENV['CLICOLOR_FORCE'] == '1'
15
- Rainbow.enabled = true
16
- end
10
+ self.enabled = false unless STDOUT.tty? && STDERR.tty?
11
+ self.enabled = false if ENV['TERM'] == 'dumb'
12
+ self.enabled = true if ENV['CLICOLOR_FORCE'] == '1'
17
13
 
18
- # On Windows systems, try to load the local ANSI support library
19
- require 'rbconfig'
20
- if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
21
- begin
22
- require 'Win32/Console/ANSI'
23
- rescue LoadError
24
- Rainbow.enabled = false
14
+ # On Windows systems, try to load the local ANSI support library
15
+ require 'rbconfig'
16
+ if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
17
+ begin
18
+ require 'Win32/Console/ANSI'
19
+ rescue LoadError
20
+ self.enabled = false
21
+ end
25
22
  end
23
+
26
24
  end
data/rainbow.gemspec CHANGED
@@ -4,14 +4,15 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'rainbow/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "rainbow"
8
- spec.version = Rainbow::VERSION
9
- spec.authors = ["Marcin Kulik"]
10
- spec.email = ["m@ku1ik.com"]
11
- spec.description = %q{Colorize printed text on ANSI terminals}
12
- spec.summary = %q{Colorize printed text on ANSI terminals}
13
- spec.homepage = "https://github.com/sickill/rainbow"
14
- spec.license = "MIT"
7
+ spec.name = "rainbow"
8
+ spec.version = Rainbow::VERSION
9
+ spec.authors = ["Marcin Kulik"]
10
+ spec.email = ["m@ku1ik.com"]
11
+ spec.description = %q{Colorize printed text on ANSI terminals}
12
+ spec.summary = %q{Colorize printed text on ANSI terminals}
13
+ spec.homepage = "https://github.com/sickill/rainbow"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = '>= 1.9.2'
15
16
 
16
17
  spec.files = `git ls-files`.split($/)
17
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
@@ -21,5 +22,4 @@ Gem::Specification.new do |spec|
21
22
  spec.add_development_dependency "bundler", "~> 1.3"
22
23
  spec.add_development_dependency "rake"
23
24
  spec.add_development_dependency "rspec"
24
- spec.add_development_dependency 'mime-types', '< 2.0.0' if RUBY_VERSION < "1.9.0"
25
25
  end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'rainbow'
3
+
4
+ describe 'Custom Rainbow instance' do
5
+
6
+ it 'inherits enabled state from the global instance' do
7
+ Rainbow.enabled = :yep
8
+ expect(Rainbow.new.enabled).to eq(:yep)
9
+ end
10
+
11
+ it 'tracks its own state separately from the global instance' do
12
+ Rainbow.enabled = :yep
13
+ rainbow = Rainbow.new
14
+ expect(Rainbow.new.enabled).to eq(:yep)
15
+
16
+ rainbow.enabled = :nope
17
+ expect(Rainbow.enabled).to eq(:yep)
18
+ expect(rainbow.enabled).to eq(:nope)
19
+ end
20
+
21
+ it 'wraps string with escape codes when enabled' do
22
+ rainbow = Rainbow.new
23
+ rainbow.enabled = true
24
+
25
+ expect(rainbow.wrap('hello').green).to eq("\e[32mhello\e[0m")
26
+ end
27
+
28
+ it "doesn't wrap string with any escape code when disabled" do
29
+ rainbow = Rainbow.new
30
+ rainbow.enabled = false
31
+
32
+ expect(rainbow.wrap('hello').green).to eq('hello')
33
+ end
34
+
35
+ end
@@ -3,6 +3,10 @@ require 'rainbow'
3
3
 
4
4
  describe 'Rainbow() wrapper' do
5
5
 
6
+ before do
7
+ Rainbow.enabled = true
8
+ end
9
+
6
10
  it 'allows foreground coloring by color number' do
7
11
  result = Rainbow('hello').foreground(5)
8
12
  expect(result).to eq("\e[35mhello\e[0m")
@@ -13,6 +17,11 @@ describe 'Rainbow() wrapper' do
13
17
  expect(result).to eq("\e[31mhello\e[0m")
14
18
  end
15
19
 
20
+ it 'allows foreground coloring directly by method name' do
21
+ result = Rainbow('hello').red
22
+ expect(result).to eq("\e[31mhello\e[0m")
23
+ end
24
+
16
25
  it 'allows foreground coloring by color name (color alias)' do
17
26
  result = Rainbow('hello').color(:red)
18
27
  expect(result).to eq("\e[31mhello\e[0m")
@@ -100,19 +109,17 @@ describe 'Rainbow() wrapper' do
100
109
  inverse.
101
110
  hide
102
111
 
103
- expect(result).to eq("\e[31m\e[1m\e[3m\e[48;5;215m\e[4m\e[34m\e[5m\e[7m\e[8mhello\e[0m")
112
+ expect(result).to eq(
113
+ "\e[31m\e[1m\e[3m\e[48;5;215m\e[4m\e[34m\e[5m\e[7m\e[8mhello\e[0m"
114
+ )
104
115
  end
105
116
 
106
117
  context "when Rainbow is disabled" do
118
+
107
119
  before do
108
- @enabled = Rainbow.enabled
109
120
  Rainbow.enabled = false
110
121
  end
111
122
 
112
- after do
113
- Rainbow.enabled = @enabled
114
- end
115
-
116
123
  it "allows chaining but doesn't wrap with escape codes" do
117
124
  result = Rainbow('hello').
118
125
  foreground(:red).
@@ -1,8 +1,12 @@
1
1
  require 'spec_helper'
2
- require 'rainbow/string'
2
+ require 'rainbow/ext/string'
3
3
 
4
4
  describe 'String mixin' do
5
5
 
6
+ before do
7
+ Rainbow.enabled = true
8
+ end
9
+
6
10
  it 'proxies foreground to Rainbow().foreground' do
7
11
  expect('hello'.foreground(:red)).to eq(Rainbow('hello').foreground(:red))
8
12
  end
@@ -47,4 +51,26 @@ describe 'String mixin' do
47
51
  expect('hello'.reset).to eq(Rainbow('hello').reset)
48
52
  end
49
53
 
54
+ context "when Rainbow is disabled" do
55
+
56
+ before do
57
+ Rainbow.enabled = false
58
+ end
59
+
60
+ it "allows chaining but doesn't wrap with escape codes" do
61
+ result = 'hello'.
62
+ foreground(:red).
63
+ bright.
64
+ italic.
65
+ background('#ff8040').
66
+ underline.
67
+ color(:blue).
68
+ blink.
69
+ inverse.
70
+ hide
71
+
72
+ expect(result).to eq('hello')
73
+ end
74
+ end
75
+
50
76
  end
data/spec/spec_helper.rb CHANGED
@@ -3,8 +3,4 @@ if ENV["CI"] && (!defined?(RUBY_ENGINE) || RUBY_ENGINE == "ruby")
3
3
  Coveralls.wear!
4
4
  end
5
5
 
6
- RSpec.configure do |config|
7
- config.before(:each) do
8
- Rainbow.enabled = true
9
- end
10
- end
6
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |file| require file }
@@ -0,0 +1,9 @@
1
+ shared_examples_for "presenter with shortcut color methods" do
2
+ [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white].each do |name|
3
+ describe "##{name}" do
4
+ subject { presenter.public_send(name) }
5
+
6
+ it { should eq(presenter.color(name)) }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+ require 'rainbow/null_presenter'
3
+
4
+ module Rainbow
5
+ describe NullPresenter do
6
+
7
+ let(:presenter) { described_class.new('hello') }
8
+
9
+ shared_examples_for "rainbow null string method" do
10
+ it "doesn't wrap the text with any sgr sequence" do
11
+ expect(subject).to eq('hello')
12
+ end
13
+
14
+ it "returns an instance of Rainbow::NullPresenter" do
15
+ expect(subject).to be_kind_of(Rainbow::NullPresenter)
16
+ end
17
+ end
18
+
19
+ describe '#color' do
20
+ subject { presenter.color(:arg1, 'arg2') }
21
+
22
+ it_behaves_like "rainbow null string method"
23
+ end
24
+
25
+ describe '#foreground' do
26
+ subject { presenter.foreground(:arg1, 'arg2') }
27
+
28
+ it_behaves_like "rainbow null string method"
29
+ end
30
+
31
+ describe '#fg' do
32
+ subject { presenter.fg(:arg1, 'arg2') }
33
+
34
+ it_behaves_like "rainbow null string method"
35
+ end
36
+
37
+ describe '#background' do
38
+ subject { presenter.background(:arg1, 'arg2') }
39
+
40
+ it_behaves_like "rainbow null string method"
41
+ end
42
+
43
+ describe '#bg' do
44
+ subject { presenter.bg(:arg1, 'arg2') }
45
+
46
+ it_behaves_like "rainbow null string method"
47
+ end
48
+
49
+ describe '#reset' do
50
+ subject { presenter.reset }
51
+
52
+ it_behaves_like "rainbow null string method"
53
+ end
54
+
55
+ describe '#bright' do
56
+ subject { presenter.bright }
57
+
58
+ it_behaves_like "rainbow null string method"
59
+ end
60
+
61
+ describe '#italic' do
62
+ subject { presenter.italic }
63
+
64
+ it_behaves_like "rainbow null string method"
65
+ end
66
+
67
+ describe '#underline' do
68
+ subject { presenter.underline }
69
+
70
+ it_behaves_like "rainbow null string method"
71
+ end
72
+
73
+ describe '#blink' do
74
+ subject { presenter.blink }
75
+
76
+ it_behaves_like "rainbow null string method"
77
+ end
78
+
79
+ describe '#inverse' do
80
+ subject { presenter.inverse }
81
+
82
+ it_behaves_like "rainbow null string method"
83
+ end
84
+
85
+ describe '#hide' do
86
+ subject { presenter.hide }
87
+
88
+ it_behaves_like "rainbow null string method"
89
+ end
90
+
91
+ it_behaves_like "presenter with shortcut color methods"
92
+ end
93
+ end
@@ -0,0 +1,168 @@
1
+ require 'spec_helper'
2
+ require 'rainbow/presenter'
3
+
4
+ module Rainbow
5
+ describe Presenter do
6
+
7
+ let(:presenter) { described_class.new('hello') }
8
+
9
+ shared_examples_for "rainbow string method" do
10
+ it "wraps the text with a sgr sequence" do
11
+ expect(subject).to eq('[hello]')
12
+ end
13
+
14
+ it "returns an instance of Rainbow::Presenter" do
15
+ expect(subject).to be_kind_of(Rainbow::Presenter)
16
+ end
17
+ end
18
+
19
+ shared_examples_for "text color method" do
20
+ let(:color) { double('color', codes: [1, 2]) }
21
+
22
+ before do
23
+ allow(Color).to receive(:build).
24
+ with(:foreground, [:arg1, 'arg2']) { color }
25
+ end
26
+
27
+ it_behaves_like "rainbow string method"
28
+
29
+ it 'wraps with color codes' do
30
+ subject
31
+ expect(StringUtils).to have_received(:wrap_with_sgr).
32
+ with('hello', [1, 2])
33
+ end
34
+ end
35
+
36
+ shared_examples_for "text background method" do
37
+ let(:color) { double('color', codes: [1, 2]) }
38
+
39
+ before do
40
+ allow(Color).to receive(:build).
41
+ with(:background, [:arg1, 'arg2']) { color }
42
+ end
43
+
44
+ it_behaves_like "rainbow string method"
45
+
46
+ it 'wraps with color codes' do
47
+ subject
48
+ expect(StringUtils).to have_received(:wrap_with_sgr).
49
+ with('hello', [1, 2])
50
+ end
51
+ end
52
+
53
+ before do
54
+ allow(StringUtils).to receive(:wrap_with_sgr) { '[hello]' }
55
+ end
56
+
57
+ describe '#color' do
58
+ subject { presenter.color(:arg1, 'arg2') }
59
+
60
+ it_behaves_like "text color method"
61
+ end
62
+
63
+ describe '#foreground' do
64
+ subject { presenter.foreground(:arg1, 'arg2') }
65
+
66
+ it_behaves_like "text color method"
67
+ end
68
+
69
+ describe '#fg' do
70
+ subject { presenter.fg(:arg1, 'arg2') }
71
+
72
+ it_behaves_like "text color method"
73
+ end
74
+
75
+ describe '#background' do
76
+ subject { presenter.background(:arg1, 'arg2') }
77
+
78
+ it_behaves_like "text background method"
79
+ end
80
+
81
+ describe '#bg' do
82
+ subject { presenter.bg(:arg1, 'arg2') }
83
+
84
+ it_behaves_like "text background method"
85
+ end
86
+
87
+ describe '#reset' do
88
+ subject { presenter.reset }
89
+
90
+ it_behaves_like "rainbow string method"
91
+
92
+ it 'wraps with 0 code' do
93
+ subject
94
+ expect(StringUtils).to have_received(:wrap_with_sgr).with('hello', [0])
95
+ end
96
+ end
97
+
98
+ describe '#bright' do
99
+ subject { presenter.bright }
100
+
101
+ it_behaves_like "rainbow string method"
102
+
103
+ it 'wraps with 1 code' do
104
+ subject
105
+ expect(StringUtils).to have_received(:wrap_with_sgr).with('hello', [1])
106
+ end
107
+ end
108
+
109
+ describe '#italic' do
110
+ subject { presenter.italic }
111
+
112
+ it_behaves_like "rainbow string method"
113
+
114
+ it 'wraps with 3 code' do
115
+ subject
116
+ expect(StringUtils).to have_received(:wrap_with_sgr).
117
+ with('hello', [3])
118
+ end
119
+ end
120
+
121
+ describe '#underline' do
122
+ subject { presenter.underline }
123
+
124
+ it_behaves_like "rainbow string method"
125
+
126
+ it 'wraps with 4 code' do
127
+ subject
128
+ expect(StringUtils).to have_received(:wrap_with_sgr).with('hello', [4])
129
+ end
130
+ end
131
+
132
+ describe '#blink' do
133
+ subject { presenter.blink }
134
+
135
+ it_behaves_like "rainbow string method"
136
+
137
+ it 'wraps with 5 code' do
138
+ subject
139
+ expect(StringUtils).to have_received(:wrap_with_sgr).with('hello', [5])
140
+ end
141
+ end
142
+
143
+ describe '#inverse' do
144
+ subject { presenter.inverse }
145
+
146
+ it_behaves_like "rainbow string method"
147
+
148
+ it 'wraps with 7 code' do
149
+ subject
150
+ expect(StringUtils).to have_received(:wrap_with_sgr).with('hello', [7])
151
+ end
152
+ end
153
+
154
+ describe '#hide' do
155
+ subject { presenter.hide }
156
+
157
+ it_behaves_like "rainbow string method"
158
+
159
+ it 'wraps with 8 code' do
160
+ subject
161
+ expect(StringUtils).to have_received(:wrap_with_sgr).with('hello', [8])
162
+ end
163
+ end
164
+
165
+ it_behaves_like "presenter with shortcut color methods"
166
+
167
+ end
168
+ end