epitron-colorize 0.6.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.
- data/README.rdoc +84 -0
- data/lib/colorize.rb +267 -0
- data/test/test_colorize.rb +11 -0
- data/test/test_helper.rb +2 -0
- metadata +57 -0
data/README.rdoc
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
= colorize
|
2
|
+
|
3
|
+
originally by Michal Kalbarczyk (fazibear)
|
4
|
+
http://fazibear.prv.pl
|
5
|
+
|
6
|
+
extended by Chris Gahan (epitron)
|
7
|
+
http://github.com/epitron
|
8
|
+
|
9
|
+
== DESCRIPTION:
|
10
|
+
|
11
|
+
Ruby String class extension. It adds some methods to set color, background color and text effect on console easier. It works by inserting ANSI escape sequences into your strings.
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
=== Colorizing an entire string:
|
16
|
+
|
17
|
+
puts "This is blue".colorize( :blue )
|
18
|
+
puts "This is light blue".colorize( :light_blue )
|
19
|
+
puts "This is also blue".colorize( :color => :blue )
|
20
|
+
puts "This is blue with red background".colorize( :color => :light_blue, :background => :red )
|
21
|
+
puts "This is blue with red background".colorize( :light_blue ).colorize( :background => :red )
|
22
|
+
puts "This is blue text on red".blue.on_red
|
23
|
+
puts "This is red on blue".colorize( :red ).on_blue
|
24
|
+
puts "This is uncolorized".blue.on_red.uncolorize
|
25
|
+
puts "This is red on blue and underline".colorize( :red ).on_blue.underline
|
26
|
+
puts "This is blue text on red".blue.on_red.blink
|
27
|
+
|
28
|
+
=== Using HTML-like color tags:
|
29
|
+
|
30
|
+
puts "<blue>This is blue <red>followed by red.</red></blue>".colorize
|
31
|
+
puts "<10>This is light green <14>with light yellow</14> in the middle.</10>".colorize
|
32
|
+
|
33
|
+
Note: Closing the tags is optional; this also works:
|
34
|
+
|
35
|
+
puts "<green>hi <blue>there <yellow>everyone!".colorize
|
36
|
+
|
37
|
+
The numeric color tags are based on the old BBS-style color codes. Here's how they map to colors:
|
38
|
+
|
39
|
+
BBS_COLOR_TABLE = {
|
40
|
+
0 => :black,
|
41
|
+
1 => :blue,
|
42
|
+
2 => :green,
|
43
|
+
3 => :cyan,
|
44
|
+
4 => :red,
|
45
|
+
5 => :magenta,
|
46
|
+
6 => :yellow,
|
47
|
+
7 => :white,
|
48
|
+
8 => :light_black,
|
49
|
+
9 => :light_blue,
|
50
|
+
10 => :light_green,
|
51
|
+
11 => :light_cyan,
|
52
|
+
12 => :light_red,
|
53
|
+
13 => :light_magenta,
|
54
|
+
14 => :light_yellow,
|
55
|
+
15 => :light_white,
|
56
|
+
}
|
57
|
+
|
58
|
+
=== Class methods:
|
59
|
+
|
60
|
+
* String.colors - return array of all possible colors names
|
61
|
+
* String.modes - return array of all possible modes
|
62
|
+
* String.color_matrix - displays color matrix with color names
|
63
|
+
* String.color_matrix( "FOO" ) - display color matrix with "FOO" text
|
64
|
+
|
65
|
+
== REQUIREMENTS:
|
66
|
+
|
67
|
+
* Win32/Console/ANSI (for Windows)
|
68
|
+
|
69
|
+
== INSTALL:
|
70
|
+
|
71
|
+
* sudo gem install colorize
|
72
|
+
|
73
|
+
== LICENSE:
|
74
|
+
|
75
|
+
colorize - Ruby string class extension.
|
76
|
+
|
77
|
+
Copyright (C) 2007 Michal Kalbarczyk,
|
78
|
+
Copyright (C) 2009 Chris Gahan
|
79
|
+
|
80
|
+
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
|
81
|
+
|
82
|
+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
83
|
+
|
84
|
+
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
data/lib/colorize.rb
ADDED
@@ -0,0 +1,267 @@
|
|
1
|
+
#
|
2
|
+
# Colorize String class extension.
|
3
|
+
#
|
4
|
+
class String
|
5
|
+
|
6
|
+
#
|
7
|
+
# Colors Hash
|
8
|
+
#
|
9
|
+
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
|
+
:light_black => 10,
|
21
|
+
:light_red => 11,
|
22
|
+
:light_green => 12,
|
23
|
+
:light_yellow => 13,
|
24
|
+
:light_blue => 14,
|
25
|
+
:light_magenta => 15,
|
26
|
+
:light_cyan => 16,
|
27
|
+
:light_white => 17
|
28
|
+
}
|
29
|
+
|
30
|
+
#
|
31
|
+
# Modes Hash
|
32
|
+
#
|
33
|
+
MODES = {
|
34
|
+
:default => 0, # Turn off all attributes
|
35
|
+
#:bright => 1, # Set bright mode
|
36
|
+
:underline => 4, # Set underline mode
|
37
|
+
:blink => 5, # Set blink mode
|
38
|
+
:swap => 7, # Exchange foreground and background colors
|
39
|
+
:hide => 8 # Hide text (foreground color would be the same as background)
|
40
|
+
}
|
41
|
+
|
42
|
+
protected
|
43
|
+
|
44
|
+
#
|
45
|
+
# Set color values in new string intance
|
46
|
+
#
|
47
|
+
def set_color_parameters( params )
|
48
|
+
if (params.instance_of?(Hash))
|
49
|
+
@color = params[:color]
|
50
|
+
@background = params[:background]
|
51
|
+
@mode = params[:mode]
|
52
|
+
@uncolorized = params[:uncolorized]
|
53
|
+
self
|
54
|
+
else
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
public
|
60
|
+
|
61
|
+
#
|
62
|
+
# Change color of string
|
63
|
+
#
|
64
|
+
# Examples:
|
65
|
+
#
|
66
|
+
# puts "This is blue".colorize( :blue )
|
67
|
+
# puts "This is light blue".colorize( :light_blue )
|
68
|
+
# puts "This is also blue".colorize( :color => :blue )
|
69
|
+
# puts "This is blue with red background".colorize( :color => :light_blue, :background => :red )
|
70
|
+
# puts "This is blue with red background".colorize( :light_blue ).colorize( :background => :red )
|
71
|
+
# puts "This is blue text on red".blue.on_red
|
72
|
+
# puts "This is red on blue".colorize( :red ).on_blue
|
73
|
+
# puts "This is red on blue and underline".colorize( :red ).on_blue.underline
|
74
|
+
# puts "This is blue text on red".blue.on_red.blink
|
75
|
+
#
|
76
|
+
def colorize( params=nil )
|
77
|
+
|
78
|
+
return self unless STDOUT.isatty
|
79
|
+
|
80
|
+
begin
|
81
|
+
require 'Win32/Console/ANSI' if PLATFORM =~ /win32/
|
82
|
+
rescue LoadError
|
83
|
+
raise 'You must gem install win32console to use color on Windows'
|
84
|
+
end
|
85
|
+
|
86
|
+
return self.tagged_colors unless params
|
87
|
+
|
88
|
+
color_parameters = {}
|
89
|
+
|
90
|
+
if (params.instance_of?(Hash))
|
91
|
+
color_parameters[:color] = COLORS[params[:color]]
|
92
|
+
color_parameters[:background] = COLORS[params[:background]]
|
93
|
+
color_parameters[:mode] = MODES[params[:mode]]
|
94
|
+
elsif (params.instance_of?(Symbol))
|
95
|
+
color_parameters[:color] = COLORS[params]
|
96
|
+
end
|
97
|
+
|
98
|
+
color_parameters[:color] ||= @color || 9
|
99
|
+
color_parameters[:background] ||= @background || 9
|
100
|
+
color_parameters[:mode] ||= @mode || 0
|
101
|
+
|
102
|
+
color_parameters[:uncolorized] ||= @uncolorized || self.dup
|
103
|
+
|
104
|
+
# calculate bright mode
|
105
|
+
color_parameters[:color] += 50 if color_parameters[:color] > 10
|
106
|
+
|
107
|
+
color_parameters[:background] += 50 if color_parameters[:background] > 10
|
108
|
+
|
109
|
+
return "\033[#{color_parameters[:mode]};#{color_parameters[:color]+30};#{color_parameters[:background]+40}m#{color_parameters[:uncolorized]}\033[0m".set_color_parameters( color_parameters )
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
#
|
114
|
+
# Return uncolorized string
|
115
|
+
#
|
116
|
+
def uncolorize
|
117
|
+
return @uncolorized || self
|
118
|
+
end
|
119
|
+
|
120
|
+
#
|
121
|
+
# Return true if sting is colorized
|
122
|
+
#
|
123
|
+
def colorized?
|
124
|
+
return !@uncolorized.nil?
|
125
|
+
end
|
126
|
+
|
127
|
+
#
|
128
|
+
# Make some color and on_color methods
|
129
|
+
#
|
130
|
+
COLORS.each_key do | key |
|
131
|
+
eval <<-"end_eval"
|
132
|
+
def #{key.to_s}
|
133
|
+
return self.colorize( :color => :#{key.to_s} )
|
134
|
+
end
|
135
|
+
def on_#{key.to_s}
|
136
|
+
return self.colorize( :background => :#{key.to_s} )
|
137
|
+
end
|
138
|
+
end_eval
|
139
|
+
end
|
140
|
+
|
141
|
+
#
|
142
|
+
# Methods for modes
|
143
|
+
#
|
144
|
+
MODES.each_key do | key |
|
145
|
+
eval <<-"end_eval"
|
146
|
+
def #{key.to_s}
|
147
|
+
return self.colorize( :mode => :#{key.to_s} )
|
148
|
+
end
|
149
|
+
end_eval
|
150
|
+
end
|
151
|
+
|
152
|
+
class << self
|
153
|
+
|
154
|
+
#
|
155
|
+
# Return array of available modes used by colorize method
|
156
|
+
#
|
157
|
+
def modes
|
158
|
+
keys = []
|
159
|
+
MODES.each_key do | key |
|
160
|
+
keys << key
|
161
|
+
end
|
162
|
+
keys
|
163
|
+
end
|
164
|
+
|
165
|
+
#
|
166
|
+
# Return array of available colors used by colorize method
|
167
|
+
#
|
168
|
+
def colors
|
169
|
+
keys = []
|
170
|
+
COLORS.each_key do | key |
|
171
|
+
keys << key
|
172
|
+
end
|
173
|
+
keys
|
174
|
+
end
|
175
|
+
|
176
|
+
#
|
177
|
+
# Display color matrix with color names.
|
178
|
+
#
|
179
|
+
def color_matrix( txt = "[X]" )
|
180
|
+
size = String.colors.length
|
181
|
+
String.colors.each do | color |
|
182
|
+
String.colors.each do | back |
|
183
|
+
print txt.colorize( :color => color, :background => back )
|
184
|
+
end
|
185
|
+
puts " < #{color}"
|
186
|
+
end
|
187
|
+
String.colors.reverse.each_with_index do | back, index |
|
188
|
+
puts "#{"|".rjust(txt.length)*(size-index)} < #{back}"
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
puts
|
193
|
+
|
194
|
+
#
|
195
|
+
# BBS-style numeric color codes.
|
196
|
+
#
|
197
|
+
BBS_COLOR_TABLE = {
|
198
|
+
0 => :black,
|
199
|
+
1 => :blue,
|
200
|
+
2 => :green,
|
201
|
+
3 => :cyan,
|
202
|
+
4 => :red,
|
203
|
+
5 => :magenta,
|
204
|
+
6 => :yellow,
|
205
|
+
7 => :white,
|
206
|
+
8 => :light_black,
|
207
|
+
9 => :light_blue,
|
208
|
+
10 => :light_green,
|
209
|
+
11 => :light_cyan,
|
210
|
+
12 => :light_red,
|
211
|
+
13 => :light_magenta,
|
212
|
+
14 => :light_yellow,
|
213
|
+
15 => :light_white,
|
214
|
+
}
|
215
|
+
|
216
|
+
|
217
|
+
#
|
218
|
+
# Colorize a string that has "color tags".
|
219
|
+
#
|
220
|
+
# Examples:
|
221
|
+
#
|
222
|
+
# Colors as words:
|
223
|
+
# puts "<light_yellow><light_white>*</light_white> Hey mom! I am <light_green>SO</light_green> colourized right now.</light_yellow>".colorize
|
224
|
+
#
|
225
|
+
# Numeric ANSI colors (from the BBS days):
|
226
|
+
# puts "<10><5>*</5> Hey mom! I am <9>SO</9> colourized right now.</10>".colorize
|
227
|
+
#
|
228
|
+
def tagged_colors
|
229
|
+
stack = []
|
230
|
+
|
231
|
+
open_tag_re = /<([\w\d_]+)>/
|
232
|
+
close_tag_re = /<\/([\w\d_]+)>/
|
233
|
+
tokens = self.scan(/(<\/?[\w\d_]+>|[^<>]+)/).flatten
|
234
|
+
|
235
|
+
result = ""
|
236
|
+
|
237
|
+
tokens.each do |token|
|
238
|
+
|
239
|
+
if open_tag_re =~ token
|
240
|
+
|
241
|
+
stack.push $1
|
242
|
+
|
243
|
+
elsif close_tag_re =~ token
|
244
|
+
|
245
|
+
# if this color is on the stack somwehere...
|
246
|
+
if pos = stack.rindex($1)
|
247
|
+
# close the tag by removing it from the stack
|
248
|
+
stack.delete_at pos
|
249
|
+
else
|
250
|
+
raise "Error: tried to close an unopened color tag -- #{token}"
|
251
|
+
end
|
252
|
+
|
253
|
+
else
|
254
|
+
|
255
|
+
color = (stack.last || "white")
|
256
|
+
color = BBS_COLOR_TABLE[color.to_i] if color =~ /^\d+$/
|
257
|
+
result << token.colorize(color.to_sym)
|
258
|
+
|
259
|
+
end
|
260
|
+
|
261
|
+
end
|
262
|
+
|
263
|
+
result
|
264
|
+
end
|
265
|
+
|
266
|
+
end
|
267
|
+
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: epitron-colorize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.6.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- fazibear
|
8
|
+
- epitron
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-09-11 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: "Extends the String class to allow you to do things like: { \"Hi mom\".red.on_blue.underline }. (Works under Win32 with \"Win32/Console/ANSI\".)"
|
18
|
+
email:
|
19
|
+
- chris@ill-logic.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files:
|
25
|
+
- README.rdoc
|
26
|
+
files:
|
27
|
+
- lib/colorize.rb
|
28
|
+
- README.rdoc
|
29
|
+
has_rdoc: false
|
30
|
+
homepage: http://github.com/epitron/colorize
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options:
|
33
|
+
- --charset=UTF-8
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.2.0
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Colorize your console output (via extensions to the String class).
|
55
|
+
test_files:
|
56
|
+
- test/test_colorize.rb
|
57
|
+
- test/test_helper.rb
|