webbynode-rainbow 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog ADDED
@@ -0,0 +1,3 @@
1
+ 1.0.1
2
+ - added Windows support
3
+
data/LICENSE ADDED
@@ -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.
data/README.markdown ADDED
@@ -0,0 +1,75 @@
1
+ Rainbow
2
+ =======
3
+
4
+ ![build status](https://secure.travis-ci.org/sickill/rainbow.png)
5
+
6
+ About
7
+ -----
8
+
9
+ Rainbow extends ruby String class adding methods to wrap the string with [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code).
10
+
11
+ Features
12
+ --------
13
+
14
+ Rainbow adds following methods to String class:
15
+
16
+ * foreground(color) (with _color_ and _colour_ aliases)
17
+ * background(color)
18
+ * reset
19
+ * bright
20
+ * italic (not well supported by terminal emulators).
21
+ * underline
22
+ * blink
23
+ * inverse
24
+ * hide.
25
+
26
+ Each of those methods returns string wrapped with some ANSI codes so you can chain calls as in example above.
27
+
28
+ Color can be one of following symbols:
29
+
30
+ :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, :default
31
+
32
+ If you have 256-colors capable terminal you can also specify color in RGB which will find the nearest match from 256 colors palette:
33
+
34
+ "Jolacz".color(115, 23, 98)
35
+ "Jolacz".color("#FFC482")
36
+ "Jolacz".color("FFC482")
37
+
38
+ For support on Windows, you should install the following gems:
39
+
40
+ gem install windows-pr win32console
41
+
42
+ If the gems aren't installed strings are simply returned unaltered.
43
+
44
+ Rainbow can be disabled globally by setting:
45
+
46
+ Sickill::Rainbow.enabled = false
47
+
48
+ It will be disabled by default if it detects that STDOUT is not a TTY.
49
+
50
+ Installation
51
+ ------------
52
+
53
+ gem install rainbow
54
+
55
+ Usage
56
+ -----
57
+
58
+ require 'rainbow'
59
+ puts "this is red".foreground(:red) + " and " + "this on yellow bg".background(:yellow) + " and " + "even bright underlined!".underline.bright
60
+
61
+ Rails Usage
62
+ -----------
63
+
64
+ You're probably wanting to add colour to your logs. To do so you must explicity enable rainbow because it will detect that STDOUT (ie: the log file) is not a TTY.
65
+ To make things easy, create the file `config/initializers/rainbow.rb` and include the following:
66
+
67
+ require 'rainbow'
68
+ Sickill::Rainbow.enabled = true
69
+
70
+ Authors
71
+ -------
72
+
73
+ * Marcin Kulik
74
+ * Xavier Nayrac
75
+
data/lib/ansi_color.rb ADDED
@@ -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
data/lib/ansi_rgb.rb ADDED
@@ -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
data/lib/rainbow.rb ADDED
@@ -0,0 +1,108 @@
1
+ require 'rbconfig'
2
+ require File.join(File.dirname(__FILE__), 'ansi_color')
3
+
4
+ module Sickill
5
+ module Rainbow
6
+ class << self; attr_accessor :enabled; end
7
+ @enabled = STDOUT.tty? && ENV['TERM'] != 'dumb' || ENV['CLICOLOR_FORCE'] == '1'
8
+
9
+ TERM_COLORS = {
10
+ :black => 0,
11
+ :red => 1,
12
+ :green => 2,
13
+ :yellow => 3,
14
+ :blue => 4,
15
+ :magenta => 5,
16
+ :cyan => 6,
17
+ :white => 7,
18
+ :default => 9,
19
+ }
20
+
21
+ TERM_EFFECTS = {
22
+ :reset => 0,
23
+ :bright => 1,
24
+ :italic => 3,
25
+ :underline => 4,
26
+ :blink => 5,
27
+ :inverse => 7,
28
+ :hide => 8,
29
+ }
30
+
31
+ # Sets foreground color of this text.
32
+ def foreground(*color)
33
+ wrap_with_code(AnsiColor.new(:foreground, *color).code)
34
+ end
35
+ alias_method :color, :foreground
36
+ alias_method :colour, :foreground
37
+
38
+
39
+ # Sets background color of this text.
40
+ def background(*color)
41
+ wrap_with_code(AnsiColor.new(:background, *color).code)
42
+ end
43
+
44
+ # Resets terminal to default colors/backgrounds.
45
+ #
46
+ # It shouldn't be needed to use this method because all methods
47
+ # append terminal reset code to end of string.
48
+ def reset
49
+ wrap_with_code(TERM_EFFECTS[:reset])
50
+ end
51
+
52
+ # Turns on bright/bold for this text.
53
+ def bright
54
+ wrap_with_code(TERM_EFFECTS[:bright])
55
+ end
56
+
57
+ # Turns on italic style for this text (not well supported by terminal
58
+ # emulators).
59
+ def italic
60
+ wrap_with_code(TERM_EFFECTS[:italic])
61
+ end
62
+
63
+ # Turns on underline decoration for this text.
64
+ def underline
65
+ wrap_with_code(TERM_EFFECTS[:underline])
66
+ end
67
+
68
+ # Turns on blinking attribute for this text (not well supported by terminal
69
+ # emulators).
70
+ def blink
71
+ wrap_with_code(TERM_EFFECTS[:blink])
72
+ end
73
+
74
+ # Inverses current foreground/background colors.
75
+ def inverse
76
+ wrap_with_code(TERM_EFFECTS[:inverse])
77
+ end
78
+
79
+ # Hides this text (set its color to the same as background).
80
+ def hide
81
+ wrap_with_code(TERM_EFFECTS[:hide])
82
+ end
83
+
84
+ private
85
+
86
+ def wrap_with_code(code) #:nodoc:
87
+ return self unless Sickill::Rainbow.enabled
88
+
89
+ var = self.dup
90
+ matched = var.match(/^(\e\[([\d;]+)m)*/)
91
+ var.insert(matched.end(0), "\e[#{code}m")
92
+ var.concat("\e[0m") unless var =~ /\e\[0m$/
93
+ var
94
+ end
95
+
96
+ end
97
+ end
98
+
99
+ String.send(:include, Sickill::Rainbow)
100
+
101
+ # On Windows systems, try to load the local ANSI support library
102
+ if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
103
+ begin
104
+ require 'Win32/Console/ANSI'
105
+ rescue LoadError
106
+ Sickill::Rainbow.enabled = false
107
+ end
108
+ end
@@ -0,0 +1,277 @@
1
+ require 'test/unit'
2
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/rainbow')
3
+
4
+ class RainbowTest < Test::Unit::TestCase #:nodoc:
5
+ def test_color_by_name
6
+ assert_equal "\e[31mhello\e[0m", "hello".color(:red)
7
+ end
8
+
9
+ def test_color_by_rgb
10
+ assert_equal "\e[38;5;196mhello\e[0m", "hello".color(255, 0, 0)
11
+ end
12
+
13
+ def test_foreground_alias
14
+ assert_equal "hello".color(:red), "hello".foreground(:red)
15
+ end
16
+
17
+ def test_colour_alias
18
+ assert_equal "hello".color(:red), "hello".colour(:red)
19
+ end
20
+
21
+ def test_background_by_name
22
+ assert_equal "\e[42mhello\e[0m", "hello".background(:green)
23
+ end
24
+
25
+ def test_background_by_rgb
26
+ assert_equal "\e[48;5;46mhello\e[0m", "hello".background(0, 255, 0)
27
+ end
28
+
29
+ def test_color_and_background
30
+ assert_equal "\e[31m\e[42mhello\e[0m", "hello".color(:red).background(:green)
31
+ end
32
+
33
+ def test_hex_color
34
+ assert_equal "\e[48;5;46mhello\e[0m", "hello".background("#00FF00")
35
+ assert_equal "\e[48;5;46mhello\e[0m", "hello".background("00FF00")
36
+ assert_equal "\e[48;5;46mhello\e[0m", "hello".background("00ff00")
37
+ end
38
+
39
+ def test_bad_color_name
40
+ assert_raises ArgumentError do
41
+ "hello".background(:baaaad)
42
+ end
43
+ end
44
+
45
+ def test_rgb_color_with_2_args
46
+ assert_raises ArgumentError do
47
+ "hello".background(1, 2)
48
+ end
49
+ end
50
+
51
+ def test_rgb_color_with_4_args
52
+ assert_raises ArgumentError do
53
+ "hello".background(1, 2, 3, 4)
54
+ end
55
+ end
56
+
57
+ def test_rgb_color_with_values_below_zero
58
+ assert_raises ArgumentError do
59
+ "hello".background(-3, 2, 3)
60
+ end
61
+ end
62
+
63
+ def test_rgb_color_with_values_above_255
64
+ assert_raises ArgumentError do
65
+ "hello".background(256, 2, 3)
66
+ end
67
+ end
68
+
69
+ def test_bright
70
+ assert_equal "\e[1mhello\e[0m", "hello".bright
71
+ end
72
+
73
+ def test_bright_and_color
74
+ assert_equal "\e[1m\e[31mhello\e[0m", "hello".bright.color(:red)
75
+ end
76
+
77
+ def test_bright_and_background
78
+ assert_equal "\e[1m\e[44mhello\e[0m", "hello".bright.background(:blue)
79
+ end
80
+
81
+ def test_color_override
82
+ assert_equal "\e[31m\e[34m\e[33mhello\e[0m", "hello".color(:red).color(:blue).color(:yellow)
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
+ def test_immutability
110
+ string = "hello"
111
+ string.color(:red)
112
+ assert_equal string, "hello"
113
+ end
114
+
115
+ def test_frozen
116
+ string = "frozen"
117
+ string.freeze
118
+ string.color(:red)
119
+ end
120
+
121
+ class MyString < String
122
+ end
123
+
124
+ def test_inheritance
125
+ my_string = MyString.new "hello"
126
+ assert_equal "\e[31mhello\e[0m", my_string.color(:red)
127
+ end
128
+
129
+ def test_disabled
130
+ Sickill::Rainbow.enabled = false
131
+ assert_equal "hello", "hello".color(:red)
132
+ Sickill::Rainbow.enabled = true
133
+ end
134
+
135
+ end
136
+
137
+ class AnsiColorTest < Test::Unit::TestCase
138
+ include Sickill::Rainbow
139
+
140
+ ### Foreground
141
+
142
+ def test_bad_foreground_name
143
+ assert_raises ArgumentError do
144
+ AnsiColor.new(:foreground, :azerty).code
145
+ end
146
+ end
147
+
148
+ def test_by_name_black_foreground
149
+ assert_equal 30, AnsiColor.new(:foreground, :black).code
150
+ end
151
+
152
+ def test_by_name_red_foreground
153
+ assert_equal 31, AnsiColor.new(:foreground, :red).code
154
+ end
155
+
156
+ def test_by_name_green_foreground
157
+ assert_equal 32, AnsiColor.new(:foreground, :green).code
158
+ end
159
+
160
+ def test_by_name_yellow_foreground
161
+ assert_equal 33, AnsiColor.new(:foreground, :yellow).code
162
+ end
163
+
164
+ def test_by_name_blue_foreground
165
+ assert_equal 34, AnsiColor.new(:foreground, :blue).code
166
+ end
167
+
168
+ def test_by_name_magenta_foreground
169
+ assert_equal 35, AnsiColor.new(:foreground, :magenta).code
170
+ end
171
+
172
+ def test_by_name_cyan_foreground
173
+ assert_equal 36, AnsiColor.new(:foreground, :cyan).code
174
+ end
175
+
176
+ def test_by_name_white_foreground
177
+ assert_equal 37, AnsiColor.new(:foreground, :white).code
178
+ end
179
+
180
+ ### Background
181
+
182
+ def test_bad_background_name
183
+ assert_raises ArgumentError do
184
+ AnsiColor.new(:background, :azerty).code
185
+ end
186
+ end
187
+
188
+ def test_by_name_black_background
189
+ assert_equal 40, AnsiColor.new(:background, :black).code
190
+ end
191
+
192
+ def test_by_name_red_background
193
+ assert_equal 41, AnsiColor.new(:background, :red).code
194
+ end
195
+
196
+ def test_by_name_green_background
197
+ assert_equal 42, AnsiColor.new(:background, :green).code
198
+ end
199
+
200
+ def test_by_name_yellow_background
201
+ assert_equal 43, AnsiColor.new(:background, :yellow).code
202
+ end
203
+
204
+ def test_by_name_blue_background
205
+ assert_equal 44, AnsiColor.new(:background, :blue).code
206
+ end
207
+
208
+ def test_by_name_magenta_background
209
+ assert_equal 45, AnsiColor.new(:background, :magenta).code
210
+ end
211
+
212
+ def test_by_name_cyan_background
213
+ assert_equal 46, AnsiColor.new(:background, :cyan).code
214
+ end
215
+
216
+ def test_by_name_white_background
217
+ assert_equal 47, AnsiColor.new(:background, :white).code
218
+ end
219
+
220
+ ### Hex color
221
+
222
+ def test_by_hex_maj
223
+ assert_equal "38;5;46", AnsiColor.new(:foreground, "00FF00").code
224
+ end
225
+
226
+ def test_by_hex_min
227
+ assert_equal "38;5;46", AnsiColor.new(:foreground, "00ff00").code
228
+ end
229
+
230
+ def test_by_hex_maj_with_sharp
231
+ assert_equal "38;5;46", AnsiColor.new(:foreground, "#00FF00").code
232
+ end
233
+
234
+ def test_by_hex_min_with_sharp
235
+ assert_equal "38;5;46", AnsiColor.new(:foreground, "#00ff00").code
236
+ end
237
+
238
+ ### RGB color
239
+
240
+ def test_too_few_colors
241
+ assert_raises ArgumentError do
242
+ AnsiColor.new(:foreground, 255, 0).code
243
+ end
244
+ end
245
+
246
+ def test_too_much_colors
247
+ assert_raises ArgumentError do
248
+ AnsiColor.new(:foreground, 255, 0, 0, 0).code
249
+ end
250
+ end
251
+
252
+ def test_by_rgb_red_foreground
253
+ assert_equal "38;5;196", AnsiColor.new(:foreground, 255, 0, 0).code
254
+ end
255
+
256
+ end
257
+
258
+ class AnsiRgbTest < Test::Unit::TestCase
259
+ include Sickill::Rainbow
260
+
261
+ def test_red
262
+ assert_equal "38;5;196", AnsiRgb.new(:foreground, [255, 0, 0]).code
263
+ end
264
+
265
+ def test_rgb_color_with_values_below_zero
266
+ assert_raises ArgumentError do
267
+ AnsiRgb.new(:foreground, [-1, 0, 0])
268
+ end
269
+ end
270
+
271
+ def test_rgb_color_with_values_above_255
272
+ assert_raises ArgumentError do
273
+ AnsiRgb.new(:foreground, [256, 0, 0])
274
+ end
275
+ end
276
+
277
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webbynode-rainbow
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marcin Kulik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: marcin.kulik@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.markdown
21
+ - Changelog
22
+ - LICENSE
23
+ - lib/rainbow.rb
24
+ - lib/ansi_color.rb
25
+ - lib/ansi_rgb.rb
26
+ - test/rainbow_test.rb
27
+ homepage: http://ku1ik.com/
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.15
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: Rainbow extends ruby String class enabling coloring text on ANSI terminals
51
+ test_files: []