optics-ansi 0.3.7
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/History.txt +87 -0
- data/Manifest.txt +11 -0
- data/README.txt +305 -0
- data/Rakefile +30 -0
- data/bin/optics +6 -0
- data/lib/ansi.rb +138 -0
- data/lib/optics.rb +127 -0
- data/lib/version.rb +39 -0
- data/spec/optics_spec.rb +11 -0
- data/spec/spec_helper.rb +16 -0
- data/test/test_optics.rb +0 -0
- metadata +76 -0
data/History.txt
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
|
2
|
+
0.3.7 / 2009-06-19
|
3
|
+
|
4
|
+
Finish Hoefile
|
5
|
+
* Create Manifest
|
6
|
+
|
7
|
+
0.3.6 / 2009-06-19
|
8
|
+
|
9
|
+
Create Rakefile
|
10
|
+
* Add lib/dir methods to /lib/version.rb
|
11
|
+
|
12
|
+
0.3.5 / 2009-06-19
|
13
|
+
|
14
|
+
Finish documentation for Optics.methods
|
15
|
+
* Add :unhide alias for Optics.normal
|
16
|
+
|
17
|
+
0.3.4 / 2009-06-19
|
18
|
+
|
19
|
+
Add documentation for String methods
|
20
|
+
|
21
|
+
0.3.3 / 2009-06-19
|
22
|
+
|
23
|
+
Clean up clerical files
|
24
|
+
|
25
|
+
0.3.2 / 2009-06-19
|
26
|
+
|
27
|
+
Add C_STRUCTURE Constant in ANSI
|
28
|
+
* Use to Meta-program COLOR functions
|
29
|
+
* Condense color.to_s into one string in Optics.method
|
30
|
+
|
31
|
+
0.3.1 / 2009-06-19
|
32
|
+
|
33
|
+
Restructure EFFECTS constant
|
34
|
+
* Move codes and aliases into a seperate hash
|
35
|
+
* Represent aliases as an Array
|
36
|
+
* Print aliases from EFFECTS[:e][:alias] Array
|
37
|
+
|
38
|
+
0.3.0 / 2009-06-19
|
39
|
+
|
40
|
+
Add aliases for dk_color and lt_color
|
41
|
+
* all exising methods and aliases working well
|
42
|
+
|
43
|
+
== 0.2.6 / 2009-06-19
|
44
|
+
|
45
|
+
Condense all effects methods into meta-prog functions
|
46
|
+
* condense ansi.rb and optics.rb
|
47
|
+
* add ANSI::COLOR_CLASSES Constant
|
48
|
+
|
49
|
+
== 0.2.4 / 2009-06-19
|
50
|
+
|
51
|
+
Move all Optics.methods into class << self
|
52
|
+
* add aliases for most methods
|
53
|
+
* add Optics.bold alias
|
54
|
+
* add Optics.dark_color shorthands
|
55
|
+
* add Optics.color alias
|
56
|
+
|
57
|
+
== 0.2.3 / 2009-06-19
|
58
|
+
|
59
|
+
Add Optics.method shorthands for all colors
|
60
|
+
|
61
|
+
== 0.2.2 / 2009-06-19
|
62
|
+
|
63
|
+
Fix bugs in the color method blocks
|
64
|
+
* add 'clear' alias for 'reset' in Optics.methods
|
65
|
+
|
66
|
+
== 0.2.1 / 2009-06-19
|
67
|
+
|
68
|
+
Allow Optics.color methods to accept blocks
|
69
|
+
* Divide ANSI into seperate file
|
70
|
+
|
71
|
+
== 0.2.0 / 2009-06-19
|
72
|
+
|
73
|
+
Add Optics.methods to the Optics class
|
74
|
+
* allow open, unclosed optics
|
75
|
+
* add no_underline, blink_off, normalize
|
76
|
+
* need to add blocks to Optics.methods
|
77
|
+
|
78
|
+
== 0.1.2 / 2009-06-19
|
79
|
+
|
80
|
+
Add light-colored foreground and background
|
81
|
+
* rename 'foreground' to 'dark'
|
82
|
+
* add additional effects
|
83
|
+
* clean up syntax
|
84
|
+
|
85
|
+
== 0.1.1 / 2009-06-18
|
86
|
+
|
87
|
+
Add Version module
|
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,305 @@
|
|
1
|
+
=Optics::ANSI
|
2
|
+
* by Mike Zazaian
|
3
|
+
* http://optics-ansi.rubyforge.org
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
The most flexible, syntactically elegant library for the colorization
|
8
|
+
of text in ANSI terminals. Has more options and better support for
|
9
|
+
adding effects to entire areas than the Rainbow gem, and with a more
|
10
|
+
elegant syntax than the ssoroka-ansi gem.
|
11
|
+
|
12
|
+
== FEATURES/PROBLEMS:
|
13
|
+
|
14
|
+
* Adds colors and text effects to instances of the String object.
|
15
|
+
* Allows user to toggle colors and effects for all text in the
|
16
|
+
terminal through the Optics module.
|
17
|
+
|
18
|
+
== SYNOPSIS:
|
19
|
+
|
20
|
+
=== Optics::ANSI String methods
|
21
|
+
|
22
|
+
==== Font Color
|
23
|
+
|
24
|
+
If you want to apply effects to the text only in a single string,
|
25
|
+
then you can access the Optics::ANSI methods included in the
|
26
|
+
String class, like this:
|
27
|
+
|
28
|
+
"this will be blue".dark(:blue)
|
29
|
+
"this will be blue".color(:blue) # alias
|
30
|
+
"this will be blue".dark_color(:blue) # alias
|
31
|
+
|
32
|
+
or, if you want a nice, lighter color:
|
33
|
+
|
34
|
+
"this will be yellow".light(:yellow)
|
35
|
+
"this will be yellow".light_color(:yellow) # alias
|
36
|
+
"this will be yellow".pale(:yellow) # alias
|
37
|
+
|
38
|
+
Optics supports all of the standard ANSI colors :
|
39
|
+
|
40
|
+
:black
|
41
|
+
:red
|
42
|
+
:green
|
43
|
+
:yellow
|
44
|
+
:blue
|
45
|
+
:magenta
|
46
|
+
:cyan
|
47
|
+
:white
|
48
|
+
:default
|
49
|
+
|
50
|
+
==== Background Color
|
51
|
+
|
52
|
+
Works just like the text color methods. To call a dark background:
|
53
|
+
|
54
|
+
"this has a dark cyan background".dark_bg(:cyan)
|
55
|
+
|
56
|
+
And a light one:
|
57
|
+
|
58
|
+
"this has a light magenta background".light_bg(:magenta)
|
59
|
+
|
60
|
+
==== Effects
|
61
|
+
|
62
|
+
There are also a number of methods that Optics::ANSI includes in the
|
63
|
+
String class for adding other effects. For the most part, they're all
|
64
|
+
pretty self-explanatory:
|
65
|
+
|
66
|
+
# Bold
|
67
|
+
"this will be bold".bold
|
68
|
+
"this will be bold".bright # alias
|
69
|
+
|
70
|
+
# Italic
|
71
|
+
"this will be italicized".italic
|
72
|
+
"this will be italicized".italics # alias
|
73
|
+
"this will be italicized".italicize # alias
|
74
|
+
|
75
|
+
# Underline
|
76
|
+
"this will be underlined".underline
|
77
|
+
|
78
|
+
# Blink: makes the text blink
|
79
|
+
"this will blink".blink
|
80
|
+
|
81
|
+
# Inverse: swaps the text and background colors
|
82
|
+
"this will appear inverted".inverse
|
83
|
+
"this will appear inverted".invert #alias
|
84
|
+
|
85
|
+
# Conceal: hides the text
|
86
|
+
"you won't be able to see this".conceal
|
87
|
+
"you won't be able to see this".hide # alias
|
88
|
+
|
89
|
+
Dark colors will always be converted to their lighter variant when
|
90
|
+
the bold or bright methods are applied.
|
91
|
+
|
92
|
+
==== Stacking effects and colors
|
93
|
+
|
94
|
+
You can also stack effects on a string. This will print text to the
|
95
|
+
terminal in a bolded, light blue font with an underline:
|
96
|
+
|
97
|
+
puts "this will be very pretty".light(:blue).bold.underline
|
98
|
+
|
99
|
+
or
|
100
|
+
|
101
|
+
puts "this will also be pretty".dark(:green).light_bg(:green).italics.blink
|
102
|
+
|
103
|
+
==== A note on 'puts'
|
104
|
+
|
105
|
+
If you're testing this in irb, all of the methods above will only
|
106
|
+
return the encoded string. To actually see the effects, you'll need
|
107
|
+
to print them to the terminal like this:
|
108
|
+
|
109
|
+
puts "this actually looks pretty".color(:white).light_bg(:yellow).bright
|
110
|
+
|
111
|
+
|
112
|
+
=== Methods in the Optics module
|
113
|
+
|
114
|
+
Calling these effects directly from the Optics module works exactly
|
115
|
+
as it does on String objects, but with a few exceptions, being:
|
116
|
+
|
117
|
+
* When you call a method from the Optics module, it doesn't close
|
118
|
+
itself.
|
119
|
+
|
120
|
+
* Some methods in the Optics module take blocks {}.
|
121
|
+
|
122
|
+
* There are more aliases available for colors.
|
123
|
+
|
124
|
+
* There are methods for cancelling colors or effects:
|
125
|
+
* Optics.cancel
|
126
|
+
* Optics.normal
|
127
|
+
* Optics.no_underline
|
128
|
+
* Optics.blink_off
|
129
|
+
* Optics.reveal
|
130
|
+
|
131
|
+
==== Optics.color
|
132
|
+
|
133
|
+
The Optics.color methods initialize the use of a color in the
|
134
|
+
terminal until cancelled or overwritten. This code will make
|
135
|
+
everything in the terminal dark blue until Optics.cancel is called:
|
136
|
+
|
137
|
+
puts Optics.dark(:blue)
|
138
|
+
|
139
|
+
The Optics module also has method aliases for each color, so all of
|
140
|
+
following are valid ways to call Optics.dark(:blue):
|
141
|
+
|
142
|
+
puts Optics.blue # alias
|
143
|
+
puts Optics.dk_blue # alias
|
144
|
+
puts Optics.dark_blue # alias
|
145
|
+
|
146
|
+
The lighter variant of blue can be initialized with:
|
147
|
+
|
148
|
+
puts Optics.light(:blue)
|
149
|
+
puts Optics.lt_blue # alias
|
150
|
+
puts Optics.light_blue # alias
|
151
|
+
|
152
|
+
Background colors can be initialized with:
|
153
|
+
|
154
|
+
puts Optics.dark_bg(:yellow)
|
155
|
+
puts Optics.light_bg(:red)
|
156
|
+
|
157
|
+
When you want things to stop being blue, you can reset them with:
|
158
|
+
|
159
|
+
puts Optics.reset
|
160
|
+
|
161
|
+
or:
|
162
|
+
|
163
|
+
puts Optics.cancel
|
164
|
+
|
165
|
+
Note that these will also cancel ALL other effects that have been
|
166
|
+
activated in the terminal, including bolding, underline and italics.
|
167
|
+
|
168
|
+
==== Optics.color {"string"}
|
169
|
+
|
170
|
+
The base, non-alias Optics.color methods can also take blocks in
|
171
|
+
order to act like the "string".color methods.
|
172
|
+
|
173
|
+
For example, this will print a green "snaaaaaaaaaaaaaaake" in the terminal:
|
174
|
+
|
175
|
+
puts Optics.light(:green) { "snnaaaaaaaaaaaake" }
|
176
|
+
|
177
|
+
This will also work with:
|
178
|
+
|
179
|
+
puts Optics.dark(:green) { "snnaaaaaaaaaaaaake" }
|
180
|
+
puts Optics.dark_bg(:green) { "snnaaaaaaaaaaaaake" }
|
181
|
+
puts Optics.light_bg(:green) { "snnaaaaaaaaaaaaake" }
|
182
|
+
|
183
|
+
But because none of the aliases can take blocks, the following
|
184
|
+
examples WILL NOT WORK:
|
185
|
+
|
186
|
+
puts Optics.dark_green { "snaaaaaaaaaaake" }
|
187
|
+
puts Optics.light_yellow { "canary" }
|
188
|
+
puts Optics.black { "crow" }
|
189
|
+
|
190
|
+
However, because these { blocks } call the Optics.reset method to
|
191
|
+
stop the color at the end of the word, calling one of these WILL
|
192
|
+
ALSO CANCEL ALL OTHER EFFECTS IN THE TERMINAL.
|
193
|
+
|
194
|
+
==== Optics.effects
|
195
|
+
|
196
|
+
Because effects called by Optics.effects don't automatically close
|
197
|
+
themselves, they're a little more flexible.
|
198
|
+
|
199
|
+
You can, for example, turn on an underline (in addition to any
|
200
|
+
existing colors or effects), then turn it off later without
|
201
|
+
cancelling the other active effects, like this:
|
202
|
+
|
203
|
+
puts Optics.underline
|
204
|
+
|
205
|
+
Then, to turn it back off:
|
206
|
+
|
207
|
+
puts Optics.no_underline.
|
208
|
+
|
209
|
+
The following pairs work in the same way:
|
210
|
+
|
211
|
+
*# Blink*
|
212
|
+
puts Optics.blink
|
213
|
+
|
214
|
+
*# No Blink*
|
215
|
+
puts Optics.blink_off
|
216
|
+
puts Optics.no_blink # alias
|
217
|
+
|
218
|
+
*# Bright*
|
219
|
+
puts Optics.bright
|
220
|
+
puts Optics.bold # alias
|
221
|
+
|
222
|
+
*# No Bright*
|
223
|
+
puts Optics.normal
|
224
|
+
puts Optics.normalize # alias
|
225
|
+
puts Optics.unbold # alias
|
226
|
+
|
227
|
+
*# Conceal*
|
228
|
+
puts Optics.conceal
|
229
|
+
puts Optics.hide # alias
|
230
|
+
|
231
|
+
*# No Conceal (Reveal)*
|
232
|
+
puts Optics.reveal
|
233
|
+
puts Optics.unhide # alias
|
234
|
+
|
235
|
+
There are also a couple of Optics methods that don't have their own
|
236
|
+
closing methods, and can only be cleared with Optics.reset:
|
237
|
+
|
238
|
+
# Italic
|
239
|
+
Optics.italic
|
240
|
+
Optics.italics # alias
|
241
|
+
Optics.italicize # alias
|
242
|
+
|
243
|
+
# Inverse
|
244
|
+
Optics.inverse
|
245
|
+
Optics.invert # alias
|
246
|
+
|
247
|
+
|
248
|
+
== REQUIREMENTS:
|
249
|
+
|
250
|
+
You must have rubygems installed to use Optics as a gem. To install
|
251
|
+
Rubygems on a debian or ubuntu linux system, do:
|
252
|
+
|
253
|
+
sudo aptitude update && sudo aptitude install rubygems
|
254
|
+
|
255
|
+
== INSTALL:
|
256
|
+
|
257
|
+
=== As a Rubygem
|
258
|
+
|
259
|
+
sudo gem install --remote optics-ansi
|
260
|
+
|
261
|
+
Then, add Optics to your script with:
|
262
|
+
|
263
|
+
require 'rubygems'
|
264
|
+
require 'optics'
|
265
|
+
|
266
|
+
=== Directly into your script
|
267
|
+
|
268
|
+
If you want to include Optics directly into your script, you can
|
269
|
+
download Optics as a .tgz or .zip package from the Optics homepage at:
|
270
|
+
|
271
|
+
http://optics-ansi.rubygems.org
|
272
|
+
|
273
|
+
Once you have the package, add Optics to your script's directory, add
|
274
|
+
the optics directory to your $LOAD_PATH variable...
|
275
|
+
|
276
|
+
$LOAD_PATH << "./optics"
|
277
|
+
|
278
|
+
.. and require Optics at the top of your script:
|
279
|
+
|
280
|
+
require 'optics'
|
281
|
+
|
282
|
+
== LICENSE:
|
283
|
+
|
284
|
+
(The MIT License)
|
285
|
+
|
286
|
+
Copyright (c) 2009 Mike Zazaian
|
287
|
+
|
288
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
289
|
+
a copy of this software and associated documentation files (the
|
290
|
+
'Software'), to deal in the Software without restriction, including
|
291
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
292
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
293
|
+
permit persons to whom the Software is furnished to do so, subject to
|
294
|
+
the following conditions:
|
295
|
+
|
296
|
+
The above copyright notice and this permission notice shall be
|
297
|
+
included in all copies or substantial portions of the Software.
|
298
|
+
|
299
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
300
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
301
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
302
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
303
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
304
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
305
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
require 'rubygems'
|
3
|
+
require 'hoe'
|
4
|
+
$:.unshift(File.dirname(__FILE__) + "/lib")
|
5
|
+
require 'optics'
|
6
|
+
|
7
|
+
Hoe.new('optics', Optics::VERSION.pretty) do |p|
|
8
|
+
p.name = "optics-ansi"
|
9
|
+
p.developer "Mike Zazaian", 'zazaian@gmail.com'
|
10
|
+
p.url = "http://optics-ansi.rubyforge.org"
|
11
|
+
|
12
|
+
p.rubyforge_name = "optics-ansi"
|
13
|
+
p.summary = "Flexible, easy colorization for terminal output."
|
14
|
+
p.description = <<-EOL
|
15
|
+
A flexible, easy-to-use module for adding color and effects to
|
16
|
+
text in ANSI terminals.
|
17
|
+
EOL
|
18
|
+
|
19
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
20
|
+
p.remote_rdoc_dir = '' # Release to root
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Simple test on packaged files to make sure they are all there"
|
24
|
+
task :verify => :package do
|
25
|
+
# An error message will be displayed if files are missing
|
26
|
+
if system %(ruby -e "require 'pkg/optics-#{Optics::VERSION}/lib/optics'")
|
27
|
+
puts "\nThe library files are present"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
data/bin/optics
ADDED
data/lib/ansi.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
|
2
|
+
module Optics
|
3
|
+
module Optics::ANSI
|
4
|
+
|
5
|
+
COLORS = {
|
6
|
+
:black => 0,
|
7
|
+
:red => 1,
|
8
|
+
:green => 2,
|
9
|
+
:yellow => 3,
|
10
|
+
:blue => 4,
|
11
|
+
:magenta => 5,
|
12
|
+
:cyan => 6,
|
13
|
+
:white => 7,
|
14
|
+
:default => 9
|
15
|
+
}
|
16
|
+
|
17
|
+
EFFECTS = {
|
18
|
+
:reset => {
|
19
|
+
:code => 0,
|
20
|
+
:alias => [:clear]
|
21
|
+
},
|
22
|
+
:bright => {
|
23
|
+
:code => 1,
|
24
|
+
:alias => [:bold]
|
25
|
+
},
|
26
|
+
:italic => {
|
27
|
+
:code => 3,
|
28
|
+
:alias => [:italicize, :italics]
|
29
|
+
},
|
30
|
+
:underline => {
|
31
|
+
:code => 4
|
32
|
+
},
|
33
|
+
:blink => {
|
34
|
+
:code => 5
|
35
|
+
},
|
36
|
+
:inverse => {
|
37
|
+
:code => 7,
|
38
|
+
:alias => [:invert]
|
39
|
+
},
|
40
|
+
:conceal => {
|
41
|
+
:code => 8,
|
42
|
+
:alias => [:hide]
|
43
|
+
},
|
44
|
+
:normal => {
|
45
|
+
:code => 22,
|
46
|
+
:alias => [:normalize, :unbold]
|
47
|
+
},
|
48
|
+
:no_underline => {
|
49
|
+
:code => 24
|
50
|
+
},
|
51
|
+
:blink_off => {
|
52
|
+
:code => 25,
|
53
|
+
:alias => [:no_blink]
|
54
|
+
},
|
55
|
+
:reveal => {
|
56
|
+
:code => 28,
|
57
|
+
:alias => [:unhide]
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
CLASSES = {
|
62
|
+
:dark => 30,
|
63
|
+
:light => 90,
|
64
|
+
:dark_bg => 40,
|
65
|
+
:light_bg => 100
|
66
|
+
}
|
67
|
+
|
68
|
+
C_STRUCTURE = {
|
69
|
+
:dark => {
|
70
|
+
:text => 30,
|
71
|
+
:bg => 40
|
72
|
+
},
|
73
|
+
:light => {
|
74
|
+
:text => 90,
|
75
|
+
:bg => 100
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
# Dark-colored text
|
80
|
+
def dark(color)
|
81
|
+
base(color, 3)
|
82
|
+
end
|
83
|
+
alias_method :dark_color, :dark
|
84
|
+
alias_method :color, :dark
|
85
|
+
|
86
|
+
# Light-colored text
|
87
|
+
def light(color)
|
88
|
+
base(color, 9)
|
89
|
+
end
|
90
|
+
alias_method :light_color, :light
|
91
|
+
alias_method :pale, :light
|
92
|
+
|
93
|
+
# Dark-colored background
|
94
|
+
def dark_bg(color)
|
95
|
+
base(color,4)
|
96
|
+
end
|
97
|
+
|
98
|
+
# Light-colored background
|
99
|
+
def light_bg(color)
|
100
|
+
base(color,10)
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
# Define all text EFFECTS
|
105
|
+
EFFECTS.keys.each do |e|
|
106
|
+
send :define_method, e do
|
107
|
+
encode(EFFECTS[e][:code])
|
108
|
+
end
|
109
|
+
|
110
|
+
# Add each alias listed in the EFFECTS Constant
|
111
|
+
if EFFECTS[e][:alias]
|
112
|
+
EFFECTS[e][:alias].each do |a|
|
113
|
+
alias_method a, e
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
protected
|
119
|
+
def encode(code) #:nodoc:
|
120
|
+
out = self
|
121
|
+
match = out.match(/^(\e\[([\d;]+)m)*/)
|
122
|
+
out.insert(match.end(0), "\e[#{code}m")
|
123
|
+
out.concat("\e[0m") unless out =~ /\e\[0m$/
|
124
|
+
out
|
125
|
+
end
|
126
|
+
|
127
|
+
def base(color, tens) #:nodoc:
|
128
|
+
color = color.to_sym
|
129
|
+
ANSI.validate(color)
|
130
|
+
encode(COLORS[color] + tens * 10)
|
131
|
+
end
|
132
|
+
|
133
|
+
def ANSI.validate(color) #:nodoc:
|
134
|
+
raise ArgumentError.new("Unknown color, valid colors: #{COLORS.keys.join(', ')}") \
|
135
|
+
unless COLORS.keys.include?(color)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
data/lib/optics.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
#= Optics
|
2
|
+
#== Written by Mike Zazaian
|
3
|
+
#== zazaian (at) gmail.com
|
4
|
+
|
5
|
+
$LOAD_PATH << "./"
|
6
|
+
require 'ansi'
|
7
|
+
require 'version'
|
8
|
+
|
9
|
+
module Optics
|
10
|
+
|
11
|
+
include Optics::VERSION
|
12
|
+
include Optics::ANSI
|
13
|
+
|
14
|
+
class << self # Optics.methods
|
15
|
+
|
16
|
+
#= COLOR-SPECIFIC-METHODS
|
17
|
+
|
18
|
+
#== Define shorthand methods for each color
|
19
|
+
Optics::ANSI::COLORS.keys.each do |c|
|
20
|
+
s = c.to_s # Color as String
|
21
|
+
light = "light_#{s}".to_sym
|
22
|
+
lt = "lt_#{s}".to_sym
|
23
|
+
dark = "dark_#{s}".to_sym
|
24
|
+
dk = "dk_#{s}".to_sym
|
25
|
+
|
26
|
+
#=== Dark
|
27
|
+
send :define_method, c do
|
28
|
+
encode(ANSI::COLORS[c] + 30)
|
29
|
+
end
|
30
|
+
alias_method dark, c
|
31
|
+
alias_method dk, c
|
32
|
+
|
33
|
+
#=== Light
|
34
|
+
next if [:default].member?(c)
|
35
|
+
send :define_method, light do
|
36
|
+
encode(ANSI::COLORS[c] + 90)
|
37
|
+
end
|
38
|
+
alias_method lt, light
|
39
|
+
end
|
40
|
+
|
41
|
+
#=== Dark-colored text
|
42
|
+
def dark(color, &block)
|
43
|
+
o = base(color, 3)
|
44
|
+
if block_given?
|
45
|
+
o += yield
|
46
|
+
o += reset
|
47
|
+
end
|
48
|
+
o
|
49
|
+
end
|
50
|
+
alias_method :dark_color, :dark
|
51
|
+
alias_method :color, :dark
|
52
|
+
|
53
|
+
#=== Light-colored text
|
54
|
+
def light(color, &block)
|
55
|
+
o = base(color, 9)
|
56
|
+
if block_given?
|
57
|
+
o += yield
|
58
|
+
o += reset
|
59
|
+
end
|
60
|
+
o
|
61
|
+
end
|
62
|
+
alias_method :light_color, :light
|
63
|
+
alias_method :pale, :light
|
64
|
+
|
65
|
+
#=== Dark-colored background
|
66
|
+
def dark_bg(color, &block)
|
67
|
+
o = base(color, 4)
|
68
|
+
if block_given?
|
69
|
+
o += yield
|
70
|
+
o += reset
|
71
|
+
end
|
72
|
+
o
|
73
|
+
end
|
74
|
+
|
75
|
+
#=== Light-colored background
|
76
|
+
def light_bg(color, &block)
|
77
|
+
o = base(color, 10)
|
78
|
+
if block_given?
|
79
|
+
o += yield
|
80
|
+
o += reset
|
81
|
+
end
|
82
|
+
o
|
83
|
+
end
|
84
|
+
|
85
|
+
#= Define shorthand methods for each EFFECT
|
86
|
+
Optics::ANSI::EFFECTS.keys.each do |e|
|
87
|
+
send :define_method, e do
|
88
|
+
encode(ANSI::EFFECTS[e][:code])
|
89
|
+
end
|
90
|
+
# Define aliases for each Optics.effect
|
91
|
+
if ANSI::EFFECTS[e][:alias]
|
92
|
+
ANSI::EFFECTS[e][:alias].each do |a|
|
93
|
+
alias_method a, e
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
protected
|
99
|
+
def encode(code) #:nodoc:
|
100
|
+
"\e[#{code}m"
|
101
|
+
end
|
102
|
+
|
103
|
+
def base(color, tens) #:nodoc:
|
104
|
+
color = color.to_sym
|
105
|
+
ANSI.validate(color)
|
106
|
+
encode(ANSI::COLORS[color] + tens * 10)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end # module Optics
|
110
|
+
|
111
|
+
|
112
|
+
begin
|
113
|
+
require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
|
114
|
+
rescue LoadError
|
115
|
+
# raise 'You must gem install win32console to use color on Windows'
|
116
|
+
module Optics::ANSI
|
117
|
+
protected
|
118
|
+
def encode(code)
|
119
|
+
self
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
Optics.require_all_libs_relative_to(__FILE__)
|
125
|
+
String.send(:include, Optics::ANSI)
|
126
|
+
|
127
|
+
# EOF
|
data/lib/version.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
# :stopdoc:
|
3
|
+
module Optics
|
4
|
+
module VERSION
|
5
|
+
MAJOR = 0
|
6
|
+
MINOR = 3
|
7
|
+
TINY = 7
|
8
|
+
REVISION = 19
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def pretty
|
12
|
+
"#{MAJOR}.#{MINOR}.#{TINY}"
|
13
|
+
end
|
14
|
+
alias_method :print, :pretty
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
|
19
|
+
PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
|
20
|
+
# :startdoc:
|
21
|
+
|
22
|
+
|
23
|
+
def self.libpath( *args )
|
24
|
+
args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.path( *args )
|
28
|
+
args.empty? ? PATH : ::File.join(PATH, args.flatten)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.require_all_libs_relative_to( fname, dir = nil )
|
32
|
+
dir ||= ::File.basename(fname, '.*')
|
33
|
+
search_me = ::File.expand_path(
|
34
|
+
::File.join(::File.dirname(fname), dir, '**', '*.rb'))
|
35
|
+
|
36
|
+
Dir.glob(search_me).sort.each {|rb| require rb}
|
37
|
+
end
|
38
|
+
|
39
|
+
end # module Optics
|
data/spec/optics_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
require File.expand_path(
|
3
|
+
File.join(File.dirname(__FILE__), %w[.. lib prism]))
|
4
|
+
|
5
|
+
Spec::Runner.configure do |config|
|
6
|
+
# == Mock Framework
|
7
|
+
#
|
8
|
+
# RSpec uses it's own mocking framework by default. If you prefer to
|
9
|
+
# use mocha, flexmock or RR, uncomment the appropriate line:
|
10
|
+
#
|
11
|
+
# config.mock_with :mocha
|
12
|
+
# config.mock_with :flexmock
|
13
|
+
# config.mock_with :rr
|
14
|
+
end
|
15
|
+
|
16
|
+
# EOF
|
data/test/test_optics.rb
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: optics-ansi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.7
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Zazaian
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-20 00:00:00 +10:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.12.2
|
24
|
+
version:
|
25
|
+
description: A flexible, easy-to-use module for adding color and effects to text in ANSI terminals.
|
26
|
+
email:
|
27
|
+
- zazaian@gmail.com
|
28
|
+
executables:
|
29
|
+
- optics
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- History.txt
|
34
|
+
- Manifest.txt
|
35
|
+
- README.txt
|
36
|
+
files:
|
37
|
+
- History.txt
|
38
|
+
- Manifest.txt
|
39
|
+
- README.txt
|
40
|
+
- Rakefile
|
41
|
+
- bin/optics
|
42
|
+
- lib/ansi.rb
|
43
|
+
- lib/optics.rb
|
44
|
+
- lib/version.rb
|
45
|
+
- spec/optics_spec.rb
|
46
|
+
- spec/spec_helper.rb
|
47
|
+
- test/test_optics.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://optics-ansi.rubyforge.org
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options:
|
52
|
+
- --main
|
53
|
+
- README.txt
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project: optics-ansi
|
71
|
+
rubygems_version: 1.2.0
|
72
|
+
signing_key:
|
73
|
+
specification_version: 2
|
74
|
+
summary: Flexible, easy colorization for terminal output.
|
75
|
+
test_files:
|
76
|
+
- test/test_optics.rb
|