rainbow 1.99.0 → 1.99.1

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.
@@ -0,0 +1,21 @@
1
+ require 'rainbow/wrapper'
2
+
3
+ module Rainbow
4
+
5
+ def self.global
6
+ @global ||= Wrapper.new
7
+ end
8
+
9
+ def self.enabled
10
+ global.enabled
11
+ end
12
+
13
+ def self.enabled=(value)
14
+ global.enabled = value
15
+ end
16
+
17
+ end
18
+
19
+ def Rainbow(string)
20
+ Rainbow.global.wrap(string.to_s)
21
+ end
@@ -2,7 +2,8 @@ module Sickill
2
2
  module Rainbow
3
3
 
4
4
  def self.enabled=(value)
5
- STDERR.puts "Rainbow gem notice: Sickill::Rainbow.enabled= is deprecated, use Rainbow.enabled= instead."
5
+ STDERR.puts("Rainbow gem notice: Sickill::Rainbow.enabled= is " \
6
+ "deprecated, use Rainbow.enabled= instead.")
6
7
  ::Rainbow.enabled = value
7
8
  end
8
9
 
@@ -0,0 +1,30 @@
1
+ module Rainbow
2
+
3
+ class NullPresenter < ::String
4
+
5
+ def color(*values); self; end
6
+ def background(*values); self; end
7
+ def reset; self; end
8
+ def bright; self; end
9
+ def italic; self; end
10
+ def underline; self; end
11
+ def blink; self; end
12
+ def inverse; self; end
13
+ def hide; self; end
14
+
15
+ def black; self; end
16
+ def red; self; end
17
+ def green; self; end
18
+ def yellow; self; end
19
+ def blue; self; end
20
+ def magenta; self; end
21
+ def cyan; self; end
22
+ def white; self; end
23
+
24
+ alias_method :foreground, :color
25
+ alias_method :fg, :color
26
+ alias_method :bg, :background
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,113 @@
1
+ require 'rainbow/string_utils'
2
+ require 'rainbow/color'
3
+
4
+ module Rainbow
5
+
6
+ class Presenter < ::String
7
+
8
+ TERM_EFFECTS = {
9
+ reset: 0,
10
+ bright: 1,
11
+ italic: 3,
12
+ underline: 4,
13
+ blink: 5,
14
+ inverse: 7,
15
+ hide: 8,
16
+ }
17
+
18
+ # Sets color of this text.
19
+ def color(*values)
20
+ wrap_with_sgr(Color.build(:foreground, values).codes)
21
+ end
22
+
23
+ alias_method :foreground, :color
24
+ alias_method :fg, :color
25
+
26
+ # Sets background color of this text.
27
+ def background(*values)
28
+ wrap_with_sgr(Color.build(:background, values).codes)
29
+ end
30
+
31
+ alias_method :bg, :background
32
+
33
+ # Resets terminal to default colors/backgrounds.
34
+ #
35
+ # It shouldn't be needed to use this method because all methods
36
+ # append terminal reset code to end of string.
37
+ def reset
38
+ wrap_with_sgr(TERM_EFFECTS[:reset])
39
+ end
40
+
41
+ # Turns on bright/bold for this text.
42
+ def bright
43
+ wrap_with_sgr(TERM_EFFECTS[:bright])
44
+ end
45
+
46
+ # Turns on italic style for this text (not well supported by terminal
47
+ # emulators).
48
+ def italic
49
+ wrap_with_sgr(TERM_EFFECTS[:italic])
50
+ end
51
+
52
+ # Turns on underline decoration for this text.
53
+ def underline
54
+ wrap_with_sgr(TERM_EFFECTS[:underline])
55
+ end
56
+
57
+ # Turns on blinking attribute for this text (not well supported by terminal
58
+ # emulators).
59
+ def blink
60
+ wrap_with_sgr(TERM_EFFECTS[:blink])
61
+ end
62
+
63
+ # Inverses current foreground/background colors.
64
+ def inverse
65
+ wrap_with_sgr(TERM_EFFECTS[:inverse])
66
+ end
67
+
68
+ # Hides this text (set its color to the same as background).
69
+ def hide
70
+ wrap_with_sgr(TERM_EFFECTS[:hide])
71
+ end
72
+
73
+ def black
74
+ color(:black)
75
+ end
76
+
77
+ def red
78
+ color(:red)
79
+ end
80
+
81
+ def green
82
+ color(:green)
83
+ end
84
+
85
+ def yellow
86
+ color(:yellow)
87
+ end
88
+
89
+ def blue
90
+ color(:blue)
91
+ end
92
+
93
+ def magenta
94
+ color(:magenta)
95
+ end
96
+
97
+ def cyan
98
+ color(:cyan)
99
+ end
100
+
101
+ def white
102
+ color(:white)
103
+ end
104
+
105
+ private
106
+
107
+ def wrap_with_sgr(codes) #:nodoc:
108
+ self.class.new(StringUtils.wrap_with_sgr(self, [*codes]))
109
+ end
110
+
111
+ end
112
+
113
+ end
@@ -1,3 +1,3 @@
1
1
  module Rainbow
2
- VERSION = "1.99.0".freeze
2
+ VERSION = "1.99.1".freeze
3
3
  end
@@ -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/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