rainbow 2.2.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- alias_method :color, :foreground
13
- alias_method :colour, :foreground
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
@@ -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 color(*values); self; end
6
- def background(*values); self; end
7
- def reset; self; end
8
- def bright; self; end
9
- def faint; self; end
10
- def italic; self; end
11
- def underline; self; end
12
- def blink; self; end
13
- def inverse; self; end
14
- def hide; self; end
15
-
16
- def black; self; end
17
- def red; self; end
18
- def green; self; end
19
- def yellow; self; end
20
- def blue; self; end
21
- def magenta; self; end
22
- def cyan; self; end
23
- def white; self; end
24
-
25
- def method_missing(method_name,*args)
26
- if Color::X11Named.color_names.include? method_name and args.empty? then
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
- alias_method :foreground, :color
34
- alias_method :fg, :color
35
- alias_method :bg, :background
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
@@ -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
- alias_method :foreground, :color
26
- alias_method :fg, :color
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
- alias_method :bg, :background
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
- alias_method :bold, :bright
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
- alias_method :dark, :faint
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,*args)
120
- if Color::X11Named.color_names.include? method_name and args.empty? then
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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Rainbow
2
- VERSION = "2.2.2".freeze
2
+ VERSION = "3.0.0".freeze
3
3
  end
@@ -2,7 +2,6 @@ require_relative 'presenter'
2
2
  require_relative 'null_presenter'
3
3
 
4
4
  module Rainbow
5
-
6
5
  class Wrapper
7
6
  attr_accessor :enabled
8
7
 
@@ -18,5 +17,4 @@ module Rainbow
18
17
  end
19
18
  end
20
19
  end
21
-
22
20
  end
@@ -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
@@ -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 = %q(Colorize printed text on ANSI terminals)
12
- spec.summary = %q(Colorize printed text on ANSI terminals)
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.9.2'
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
- foreground(:red).
118
- bright.
119
- bold.
120
- faint.
121
- dark.
122
- italic.
123
- background('#ff8040').
124
- underline.
125
- color(:blue).
126
- blink.
127
- inverse.
128
- hide
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
- foreground(:red).
144
- bright.
145
- bold.
146
- faint.
147
- dark.
148
- italic.
149
- background('#ff8040').
150
- underline.
151
- color(:blue).
152
- blink.
153
- inverse.
154
- hide
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