rainbow 1.1.1 → 1.1.2

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.
data/LICENSE CHANGED
@@ -0,0 +1,20 @@
1
+ Copyright (c) Marcin Kulik
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -64,3 +64,10 @@ To make things easy, create the file `config/initializers/rainbow.rb` and includ
64
64
 
65
65
  require 'rainbow'
66
66
  Sickill::Rainbow.enabled = true
67
+
68
+ Authors
69
+ -------
70
+
71
+ * Marcin Kulik
72
+ * Xavier Nayrac
73
+
@@ -0,0 +1,71 @@
1
+ require File.join(File.dirname(__FILE__), 'ansi_rgb')
2
+
3
+ module Sickill
4
+ module Rainbow
5
+
6
+ # Retrieve ANSI color code from a color name, an html color
7
+ # or an RGB color
8
+ class AnsiColor
9
+
10
+ # +ground+ is one of :foreground, :background
11
+ # +color+ is one of this 3 formats: name, html, rgb
12
+ def initialize(ground, *color)
13
+ @ground = ground
14
+
15
+ if color.size == 1
16
+ @color = color.first
17
+ else
18
+ @color = color
19
+ end
20
+ end
21
+
22
+ # Get the ANSI color code.
23
+ def code
24
+ case @color
25
+ when Symbol then code_from_name
26
+ when String then code_from_html
27
+ when Array then code_from_rgb
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def code_from_name #:nodoc:
34
+ validate_color_name
35
+
36
+ TERM_COLORS[@color] + (@ground == :foreground ? 30 : 40)
37
+ end
38
+
39
+ def code_from_html #:nodoc:
40
+ @color = @color.gsub("#", "")
41
+ AnsiRgb.new(@ground, rgb_from_html).code
42
+ end
43
+
44
+ def rgb_from_html #:nodoc:
45
+ red = @color[0..1].to_i(16)
46
+ green = @color[2..3].to_i(16)
47
+ blue = @color[4..5].to_i(16)
48
+ [red, green, blue]
49
+ end
50
+
51
+ def code_from_rgb #:nodoc:
52
+ unless @color.size == 3
53
+ raise ArgumentError.new \
54
+ "Bad number of arguments for RGB color definition, should be 3"
55
+ end
56
+
57
+ AnsiRgb.new(@ground, @color).code
58
+ end
59
+
60
+ def validate_color_name #:nodoc:
61
+ color_names = TERM_COLORS.keys
62
+
63
+ unless color_names.include?(@color)
64
+ raise ArgumentError.new \
65
+ "Unknown color name, valid names: #{color_names.join(', ')}"
66
+ end
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,43 @@
1
+ module Sickill
2
+ module Rainbow
3
+
4
+ # Retrieve ANSI color code from RGB color.
5
+ class AnsiRgb
6
+
7
+ # +ground+ is one of :foreground, :background
8
+ # +rgb+ is an array of 3 values between 0 and 255.
9
+ def initialize(ground, rgb)
10
+ if RGB.outside_range?(rgb)
11
+ raise ArgumentError.new("RGB value outside 0-255 range")
12
+ end
13
+
14
+ @ground_code = { :foreground => 38, :background => 48 }[ground]
15
+ @red, @green, @blue = rgb[0], rgb[1], rgb[2]
16
+ end
17
+
18
+ # Get the ANSI color code for this RGB color.
19
+ def code
20
+ index = 16 +
21
+ RGB.to_ansi_domain(@red) * 36 +
22
+ RGB.to_ansi_domain(@green) * 6 +
23
+ RGB.to_ansi_domain(@blue)
24
+
25
+ "#{@ground_code};5;#{index}"
26
+ end
27
+
28
+ end
29
+
30
+ # Helper class for RGB color format.
31
+ class RGB
32
+ def self.outside_range?(rgb)
33
+ rgb.min < 0 or rgb.max > 255
34
+ end
35
+
36
+ # Change domain of color value from 0-255 to 0-5
37
+ def self.to_ansi_domain(value)
38
+ (6 * (value / 256.0)).to_i
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -1,4 +1,5 @@
1
1
  require 'rbconfig'
2
+ require File.join(File.dirname(__FILE__), 'ansi_color')
2
3
 
3
4
  module Sickill
4
5
  module Rainbow
@@ -29,8 +30,7 @@ module Sickill
29
30
 
30
31
  # Sets foreground color of this text.
31
32
  def foreground(*color)
32
- color = color.first if color.size == 1
33
- wrap_with_code(get_color_code(color, :foreground))
33
+ wrap_with_code(AnsiColor.new(:foreground, *color).code)
34
34
  end
35
35
  alias_method :color, :foreground
36
36
  alias_method :colour, :foreground
@@ -38,13 +38,13 @@ module Sickill
38
38
 
39
39
  # Sets background color of this text.
40
40
  def background(*color)
41
- color = color.first if color.size == 1
42
- wrap_with_code(get_color_code(color, :background))
41
+ wrap_with_code(AnsiColor.new(:background, *color).code)
43
42
  end
44
43
 
45
44
  # Resets terminal to default colors/backgrounds.
46
45
  #
47
- # It shouldn't be needed to use this method because all methods append terminal reset code to end of string.
46
+ # It shouldn't be needed to use this method because all methods
47
+ # append terminal reset code to end of string.
48
48
  def reset
49
49
  wrap_with_code(TERM_EFFECTS[:reset])
50
50
  end
@@ -54,7 +54,8 @@ module Sickill
54
54
  wrap_with_code(TERM_EFFECTS[:bright])
55
55
  end
56
56
 
57
- # Turns on italic style for this text (not well supported by terminal emulators).
57
+ # Turns on italic style for this text (not well supported by terminal
58
+ # emulators).
58
59
  def italic
59
60
  wrap_with_code(TERM_EFFECTS[:italic])
60
61
  end
@@ -64,7 +65,8 @@ module Sickill
64
65
  wrap_with_code(TERM_EFFECTS[:underline])
65
66
  end
66
67
 
67
- # Turns on blinking attribute for this text (not well supported by terminal emulators).
68
+ # Turns on blinking attribute for this text (not well supported by terminal
69
+ # emulators).
68
70
  def blink
69
71
  wrap_with_code(TERM_EFFECTS[:blink])
70
72
  end
@@ -79,49 +81,24 @@ module Sickill
79
81
  wrap_with_code(TERM_EFFECTS[:hide])
80
82
  end
81
83
 
82
- protected
84
+ private
85
+
83
86
  def wrap_with_code(code) #:nodoc:
84
87
  return self unless Sickill::Rainbow.enabled
85
88
 
86
- out = "#{self}"
87
- match = out.match(/^(\e\[([\d;]+)m)*/)
88
- out.insert(match.end(0), "\e[#{code}m")
89
- out.concat("\e[0m") unless out =~ /\e\[0m$/
90
- out
91
- end
92
-
93
- def get_color_code(color, type) #:nodoc:
94
- case color
95
- when Symbol
96
- validate_color(color)
97
- TERM_COLORS[color] + (type == :foreground ? 30 : 40)
98
- when String
99
- color = color.gsub("#", "")
100
- r, g, b = color[0..1].to_i(16), color[2..3].to_i(16), color[4..5].to_i(16)
101
- get_rgb_code(r, g, b, type)
102
- when Array
103
- raise ArgumentError.new("Bad number of arguments for RGB color definition, should be 3") unless color.size == 3
104
- get_rgb_code(color[0], color[1], color[2], type)
105
- end
89
+ matched = match(/^(\e\[([\d;]+)m)*/)
90
+ insert(matched.end(0), "\e[#{code}m")
91
+ concat("\e[0m") unless self =~ /\e\[0m$/
92
+ self
106
93
  end
107
94
 
108
- def get_rgb_code(r, g, b, type) #:nodoc:
109
- raise ArgumentError.new("RGB value outside 0-255 range") if [r, g, b].min < 0 || [r, g, b].max > 255
110
- code = { :foreground => 38, :background => 48 }[type]
111
- index = 16 + (6 * (r / 256.0)).to_i * 36 + (6 * (g / 256.0)).to_i * 6 + (6 * (b / 256.0)).to_i
112
- "#{code};5;#{index}"
113
- end
114
-
115
- def validate_color(color) #:nodoc:
116
- raise ArgumentError.new("Unknown color, valid colors: #{TERM_COLORS.keys.join(', ')}") unless TERM_COLORS.keys.include?(color)
117
- end
118
95
  end
119
96
  end
120
97
 
121
98
  String.send(:include, Sickill::Rainbow)
122
99
 
123
100
  # On Windows systems, try to load the local ANSI support library
124
- if Config::CONFIG['host_os'] =~ /mswin|mingw/
101
+ if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
125
102
  begin
126
103
  require 'Win32/Console/ANSI'
127
104
  rescue LoadError
@@ -5,11 +5,11 @@ class RainbowTest < Test::Unit::TestCase #:nodoc:
5
5
  def test_color_by_name
6
6
  assert_equal "\e[31mhello\e[0m", "hello".color(:red)
7
7
  end
8
-
8
+
9
9
  def test_color_by_rgb
10
10
  assert_equal "\e[38;5;196mhello\e[0m", "hello".color(255, 0, 0)
11
11
  end
12
-
12
+
13
13
  def test_foreground_alias
14
14
  assert_equal "hello".color(:red), "hello".foreground(:red)
15
15
  end
@@ -17,7 +17,7 @@ class RainbowTest < Test::Unit::TestCase #:nodoc:
17
17
  def test_colour_alias
18
18
  assert_equal "hello".color(:red), "hello".colour(:red)
19
19
  end
20
-
20
+
21
21
  def test_background_by_name
22
22
  assert_equal "\e[42mhello\e[0m", "hello".background(:green)
23
23
  end
@@ -29,7 +29,7 @@ class RainbowTest < Test::Unit::TestCase #:nodoc:
29
29
  def test_color_and_background
30
30
  assert_equal "\e[31m\e[42mhello\e[0m", "hello".color(:red).background(:green)
31
31
  end
32
-
32
+
33
33
  def test_hex_color
34
34
  assert_equal "\e[48;5;46mhello\e[0m", "hello".background("#00FF00")
35
35
  assert_equal "\e[48;5;46mhello\e[0m", "hello".background("00FF00")
@@ -81,4 +81,185 @@ class RainbowTest < Test::Unit::TestCase #:nodoc:
81
81
  def test_color_override
82
82
  assert_equal "\e[31m\e[34m\e[33mhello\e[0m", "hello".color(:red).color(:blue).color(:yellow)
83
83
  end
84
+
85
+ def test_reset
86
+ assert_equal "\e[0mhello\e[0m", "hello".reset
87
+ end
88
+
89
+ def test_italic
90
+ assert_equal "\e[3mhello\e[0m", "hello".italic
91
+ end
92
+
93
+ def test_underline
94
+ assert_equal "\e[4mhello\e[0m", "hello".underline
95
+ end
96
+
97
+ def test_blink
98
+ assert_equal "\e[5mhello\e[0m", "hello".blink
99
+ end
100
+
101
+ def test_inverse
102
+ assert_equal "\e[7mhello\e[0m", "hello".inverse
103
+ end
104
+
105
+ def test_hide
106
+ assert_equal "\e[8mhello\e[0m", "hello".hide
107
+ end
108
+
109
+ class MyString < String
110
+ end
111
+
112
+ def test_inheritance
113
+ my_string = MyString.new "hello"
114
+ assert_equal "\e[31mhello\e[0m", my_string.color(:red)
115
+ end
116
+
117
+ def test_disabled
118
+ Sickill::Rainbow.enabled = false
119
+ assert_equal "hello", "hello".color(:red)
120
+ Sickill::Rainbow.enabled = true
121
+ end
122
+
123
+ end
124
+
125
+ class AnsiColorTest < Test::Unit::TestCase
126
+ include Sickill::Rainbow
127
+
128
+ ### Foreground
129
+
130
+ def test_bad_foreground_name
131
+ assert_raises ArgumentError do
132
+ AnsiColor.new(:foreground, :azerty).code
133
+ end
134
+ end
135
+
136
+ def test_by_name_black_foreground
137
+ assert_equal 30, AnsiColor.new(:foreground, :black).code
138
+ end
139
+
140
+ def test_by_name_red_foreground
141
+ assert_equal 31, AnsiColor.new(:foreground, :red).code
142
+ end
143
+
144
+ def test_by_name_green_foreground
145
+ assert_equal 32, AnsiColor.new(:foreground, :green).code
146
+ end
147
+
148
+ def test_by_name_yellow_foreground
149
+ assert_equal 33, AnsiColor.new(:foreground, :yellow).code
150
+ end
151
+
152
+ def test_by_name_blue_foreground
153
+ assert_equal 34, AnsiColor.new(:foreground, :blue).code
154
+ end
155
+
156
+ def test_by_name_magenta_foreground
157
+ assert_equal 35, AnsiColor.new(:foreground, :magenta).code
158
+ end
159
+
160
+ def test_by_name_cyan_foreground
161
+ assert_equal 36, AnsiColor.new(:foreground, :cyan).code
162
+ end
163
+
164
+ def test_by_name_white_foreground
165
+ assert_equal 37, AnsiColor.new(:foreground, :white).code
166
+ end
167
+
168
+ ### Background
169
+
170
+ def test_bad_background_name
171
+ assert_raises ArgumentError do
172
+ AnsiColor.new(:background, :azerty).code
173
+ end
174
+ end
175
+
176
+ def test_by_name_black_background
177
+ assert_equal 40, AnsiColor.new(:background, :black).code
178
+ end
179
+
180
+ def test_by_name_red_background
181
+ assert_equal 41, AnsiColor.new(:background, :red).code
182
+ end
183
+
184
+ def test_by_name_green_background
185
+ assert_equal 42, AnsiColor.new(:background, :green).code
186
+ end
187
+
188
+ def test_by_name_yellow_background
189
+ assert_equal 43, AnsiColor.new(:background, :yellow).code
190
+ end
191
+
192
+ def test_by_name_blue_background
193
+ assert_equal 44, AnsiColor.new(:background, :blue).code
194
+ end
195
+
196
+ def test_by_name_magenta_background
197
+ assert_equal 45, AnsiColor.new(:background, :magenta).code
198
+ end
199
+
200
+ def test_by_name_cyan_background
201
+ assert_equal 46, AnsiColor.new(:background, :cyan).code
202
+ end
203
+
204
+ def test_by_name_white_background
205
+ assert_equal 47, AnsiColor.new(:background, :white).code
206
+ end
207
+
208
+ ### Hex color
209
+
210
+ def test_by_hex_maj
211
+ assert_equal "38;5;46", AnsiColor.new(:foreground, "00FF00").code
212
+ end
213
+
214
+ def test_by_hex_min
215
+ assert_equal "38;5;46", AnsiColor.new(:foreground, "00ff00").code
216
+ end
217
+
218
+ def test_by_hex_maj_with_sharp
219
+ assert_equal "38;5;46", AnsiColor.new(:foreground, "#00FF00").code
220
+ end
221
+
222
+ def test_by_hex_min_with_sharp
223
+ assert_equal "38;5;46", AnsiColor.new(:foreground, "#00ff00").code
224
+ end
225
+
226
+ ### RGB color
227
+
228
+ def test_too_few_colors
229
+ assert_raises ArgumentError do
230
+ AnsiColor.new(:foreground, 255, 0).code
231
+ end
232
+ end
233
+
234
+ def test_too_much_colors
235
+ assert_raises ArgumentError do
236
+ AnsiColor.new(:foreground, 255, 0, 0, 0).code
237
+ end
238
+ end
239
+
240
+ def test_by_rgb_red_foreground
241
+ assert_equal "38;5;196", AnsiColor.new(:foreground, 255, 0, 0).code
242
+ end
243
+
244
+ end
245
+
246
+ class AnsiRgbTest < Test::Unit::TestCase
247
+ include Sickill::Rainbow
248
+
249
+ def test_red
250
+ assert_equal "38;5;196", AnsiRgb.new(:foreground, [255, 0, 0]).code
251
+ end
252
+
253
+ def test_rgb_color_with_values_below_zero
254
+ assert_raises ArgumentError do
255
+ AnsiRgb.new(:foreground, [-1, 0, 0])
256
+ end
257
+ end
258
+
259
+ def test_rgb_color_with_values_above_255
260
+ assert_raises ArgumentError do
261
+ AnsiRgb.new(:foreground, [256, 0, 0])
262
+ end
263
+ end
264
+
84
265
  end
metadata CHANGED
@@ -1,68 +1,51 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rainbow
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 1
8
- - 1
9
- version: 1.1.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.2
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Marcin Kulik
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-02-06 00:00:00 +01:00
18
- default_executable:
12
+ date: 2011-11-13 00:00:00.000000000Z
19
13
  dependencies: []
20
-
21
14
  description:
22
15
  email: marcin.kulik@gmail.com
23
16
  executables: []
24
-
25
17
  extensions: []
26
-
27
18
  extra_rdoc_files: []
28
-
29
- files:
19
+ files:
30
20
  - README.markdown
31
21
  - Changelog
32
22
  - LICENSE
33
23
  - lib/rainbow.rb
24
+ - lib/ansi_color.rb
25
+ - lib/ansi_rgb.rb
34
26
  - test/rainbow_test.rb
35
- has_rdoc: true
36
- homepage: http://sickill.net
27
+ homepage: http://ku1ik.com/
37
28
  licenses: []
38
-
39
29
  post_install_message:
40
30
  rdoc_options: []
41
-
42
- require_paths:
31
+ require_paths:
43
32
  - lib
44
- required_ruby_version: !ruby/object:Gem::Requirement
33
+ required_ruby_version: !ruby/object:Gem::Requirement
45
34
  none: false
46
- requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- segments:
50
- - 0
51
- version: "0"
52
- required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
40
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- segments:
58
- - 0
59
- version: "0"
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
60
45
  requirements: []
61
-
62
46
  rubyforge_project:
63
- rubygems_version: 1.3.7
47
+ rubygems_version: 1.8.10
64
48
  signing_key:
65
49
  specification_version: 3
66
50
  summary: Rainbow extends ruby String class enabling coloring text on ANSI terminals
67
51
  test_files: []
68
-