text-highlight 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,28 @@
1
+ Text::Highlight
2
+
3
+ Description:
4
+
5
+ A Ruby module for highlighting text, using ANSI escape sequences or HTML.
6
+
7
+ Installation:
8
+
9
+ # ruby install.rb
10
+
11
+ Uninstallation:
12
+
13
+ # ruby install.rb uninstall
14
+
15
+ Test:
16
+
17
+ % cd text-highlight-X.Y.Z/t
18
+ % ./runtests.rb
19
+
20
+ Reference:
21
+
22
+ http://text-highlight.rubyforge.org
23
+
24
+ Contact:
25
+
26
+ jpace@incava.org
27
+
28
+
@@ -0,0 +1,16 @@
1
+ require "rbconfig"
2
+ require "ftools"
3
+
4
+ include Config
5
+
6
+ version = CONFIG["MAJOR"] + "." + CONFIG["MINOR"]
7
+ sitedir = CONFIG["sitedir"]
8
+ tgtdir = (ENV['DESTDIR'] || '') + "#{sitedir}/#{version}"
9
+ dest = tgtdir + File::SEPARATOR + "text"
10
+
11
+ if ARGV[0] == "uninstall"
12
+ File.safe_unlink(dest + File::SEPARATOR + "highlight.rb")
13
+ else
14
+ File.makedirs(dest)
15
+ File.install("text/highlight.rb", dest)
16
+ end
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ $:.unshift(`pwd`.chomp + "/..")
5
+
6
+ require 'text/highlight'
7
+
8
+ $HAVELOG = true
9
+ begin
10
+ require 'log'
11
+ Log.level = Log::DEBUG
12
+ rescue LoadError => e
13
+ $HAVELOG = false
14
+ end
15
+
16
+ String.highlighter = Text::Highlighter::ANSI
17
+
18
+ # Runs all test_*.rb files, capturing whether they succeeded or failed. If they
19
+ # failed, the entire output of the test is displayed.
20
+
21
+ # Note that the last line is checked for "OK", displayed by rubyunit. So
22
+ # unbuffer stderr and stdout ($stderr.sync = true; $stdout.sync = true).
23
+
24
+ class TestSuite
25
+ $HAVELOG && (include Loggable)
26
+
27
+ def initialize
28
+ line = " %6d %6d %s\n"
29
+ @success_line = line.green
30
+ @failure_line = line.red
31
+
32
+ @failed_tests = 0
33
+ @successful_tests = 0
34
+ @total_assertions = 0
35
+ @total_tests = 0
36
+ end
37
+
38
+ def run(testname)
39
+ $HAVELOG && (debug "testing: #{testname}")
40
+
41
+ testname = testname
42
+ lines = IO.popen("#{testname} 2>&1").readlines
43
+
44
+ test_assertions = 0
45
+ tests = 0
46
+ if lines[-1].index(/^OK/) && lines[-1].index(/(\d+)\s+tests\s+(\d+)\s+asserts/)
47
+ tests, test_assertions = $1.to_i, $2.to_i
48
+ end
49
+
50
+ if lines[-1].index(/^OK/)
51
+ @successful_tests += 1
52
+ printf @success_line, tests, test_assertions, testname
53
+ else
54
+ @failed_tests += 1
55
+ printf @failure_line, tests, test_assertions, testname
56
+ puts lines
57
+ puts
58
+ end
59
+
60
+ @total_tests += tests
61
+ @total_assertions += test_assertions
62
+ end
63
+
64
+ def show_summary_line
65
+ fmt = "%6d".red + " " + "%6d".green + " " + "%6d %6d TOTAL".bold + "\n"
66
+ printf fmt, @failed_tests, @successful_tests, @total_tests, @total_assertions
67
+ end
68
+ end
69
+
70
+ pat = ARGV[0] ? ARGV[0] : ""
71
+
72
+ $tests = Dir["test_*#{pat}*.rb"].sort
73
+
74
+ suite = TestSuite.new
75
+
76
+ $tests.each do |test|
77
+ suite.run(test)
78
+ end
79
+
80
+ suite.show_summary_line
@@ -0,0 +1,367 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'testcase'
5
+ require 'text/highlight'
6
+
7
+
8
+ class ANSIHighlightTest < My::TestCase
9
+
10
+ def test_ansi
11
+ debug ""
12
+ hl = Text::ANSIHighlighter.new
13
+ String.highlighter = hl
14
+
15
+ # foregrounds
16
+
17
+ # none
18
+
19
+ ios = IOString.new
20
+ ios.puts hl.none + "testing" + hl.reset
21
+ assert_equal("\e[0mtesting\e[0m", ios)
22
+
23
+ ios = IOString.new
24
+ ios.puts hl.none { "testing" }
25
+ assert_equal("\e[0mtesting\e[0m", ios)
26
+
27
+ ios = IOString.new
28
+ ios.puts "testing".none
29
+ assert_equal("\e[0mtesting\e[0m", ios)
30
+
31
+ # reset
32
+
33
+ ios = IOString.new
34
+ ios.puts hl.reset + "testing" + hl.reset
35
+ assert_equal("\e[0mtesting\e[0m", ios)
36
+
37
+ ios = IOString.new
38
+ ios.puts hl.reset { "testing" }
39
+ assert_equal("\e[0mtesting\e[0m", ios)
40
+
41
+ ios = IOString.new
42
+ ios.puts "testing".reset
43
+ assert_equal("\e[0mtesting\e[0m", ios)
44
+
45
+ # bold
46
+
47
+ ios = IOString.new
48
+ ios.puts hl.bold + "testing" + hl.reset
49
+ assert_equal("\e[1mtesting\e[0m", ios)
50
+
51
+ ios = IOString.new
52
+ ios.puts hl.bold { "testing" }
53
+ assert_equal("\e[1mtesting\e[0m", ios)
54
+
55
+ ios = IOString.new
56
+ ios.puts "testing".bold
57
+ assert_equal("\e[1mtesting\e[0m", ios)
58
+
59
+ # underscore
60
+
61
+ ios = IOString.new
62
+ ios.puts hl.underscore + "testing" + hl.reset
63
+ assert_equal("\e[4mtesting\e[0m", ios)
64
+
65
+ ios = IOString.new
66
+ ios.puts hl.underscore { "testing" }
67
+ assert_equal("\e[4mtesting\e[0m", ios)
68
+
69
+ ios = IOString.new
70
+ ios.puts "testing".underscore
71
+ assert_equal("\e[4mtesting\e[0m", ios)
72
+
73
+ # underline
74
+
75
+ ios = IOString.new
76
+ ios.puts hl.underline + "testing" + hl.reset
77
+ assert_equal("\e[4mtesting\e[0m", ios)
78
+
79
+ ios = IOString.new
80
+ ios.puts hl.underline { "testing" }
81
+ assert_equal("\e[4mtesting\e[0m", ios)
82
+
83
+ ios = IOString.new
84
+ ios.puts "testing".underline
85
+ assert_equal("\e[4mtesting\e[0m", ios)
86
+
87
+ # blink
88
+
89
+ ios = IOString.new
90
+ ios.puts hl.blink + "testing" + hl.reset
91
+ assert_equal("\e[5mtesting\e[0m", ios)
92
+
93
+ ios = IOString.new
94
+ ios.puts hl.blink { "testing" }
95
+ assert_equal("\e[5mtesting\e[0m", ios)
96
+
97
+ ios = IOString.new
98
+ ios.puts "testing".blink
99
+ assert_equal("\e[5mtesting\e[0m", ios)
100
+
101
+ # reverse
102
+
103
+ ios = IOString.new
104
+ ios.puts hl.reverse + "testing" + hl.reset
105
+ assert_equal("\e[7mtesting\e[0m", ios)
106
+
107
+ ios = IOString.new
108
+ ios.puts hl.reverse { "testing" }
109
+ assert_equal("\e[7mtesting\e[0m", ios)
110
+
111
+ ios = IOString.new
112
+ ios.puts "testing".reverse
113
+ assert_equal("gnitset", ios)
114
+
115
+ ios = IOString.new
116
+ ios.puts "testing".negative
117
+ assert_equal("\e[7mtesting\e[0m", ios)
118
+
119
+ # concealed
120
+
121
+ ios = IOString.new
122
+ ios.puts hl.concealed + "testing" + hl.reset
123
+ assert_equal("\e[8mtesting\e[0m", ios)
124
+
125
+ ios = IOString.new
126
+ ios.puts hl.concealed { "testing" }
127
+ assert_equal("\e[8mtesting\e[0m", ios)
128
+
129
+ ios = IOString.new
130
+ ios.puts "testing".concealed
131
+ assert_equal("\e[8mtesting\e[0m", ios)
132
+
133
+ # black
134
+
135
+ ios = IOString.new
136
+ ios.puts hl.black + "testing" + hl.reset
137
+ assert_equal("\e[30mtesting\e[0m", ios)
138
+
139
+ ios = IOString.new
140
+ ios.puts hl.black { "testing" }
141
+ assert_equal("\e[30mtesting\e[0m", ios)
142
+
143
+ ios = IOString.new
144
+ ios.puts "testing".black
145
+ assert_equal("\e[30mtesting\e[0m", ios)
146
+
147
+ # red
148
+
149
+ ios = IOString.new
150
+ ios.puts hl.red + "testing" + hl.reset
151
+ assert_equal("\e[31mtesting\e[0m", ios)
152
+
153
+ ios = IOString.new
154
+ ios.puts hl.red { "testing" }
155
+ assert_equal("\e[31mtesting\e[0m", ios)
156
+
157
+ ios = IOString.new
158
+ ios.puts "testing".red
159
+ assert_equal("\e[31mtesting\e[0m", ios)
160
+
161
+ # green
162
+
163
+ ios = IOString.new
164
+ ios.puts hl.green + "testing" + hl.reset
165
+ assert_equal("\e[32mtesting\e[0m", ios)
166
+
167
+ ios = IOString.new
168
+ ios.puts hl.green { "testing" }
169
+ assert_equal("\e[32mtesting\e[0m", ios)
170
+
171
+ ios = IOString.new
172
+ ios.puts "testing".green
173
+ assert_equal("\e[32mtesting\e[0m", ios)
174
+
175
+ # yellow
176
+
177
+ ios = IOString.new
178
+ ios.puts hl.yellow + "testing" + hl.reset
179
+ assert_equal("\e[33mtesting\e[0m", ios)
180
+
181
+ ios = IOString.new
182
+ ios.puts hl.yellow { "testing" }
183
+ assert_equal("\e[33mtesting\e[0m", ios)
184
+
185
+ ios = IOString.new
186
+ ios.puts "testing".yellow
187
+ assert_equal("\e[33mtesting\e[0m", ios)
188
+
189
+ # blue
190
+
191
+ ios = IOString.new
192
+ ios.puts hl.blue + "testing" + hl.reset
193
+ assert_equal("\e[34mtesting\e[0m", ios)
194
+
195
+ ios = IOString.new
196
+ ios.puts hl.blue { "testing" }
197
+ assert_equal("\e[34mtesting\e[0m", ios)
198
+
199
+ ios = IOString.new
200
+ ios.puts "testing".blue
201
+ assert_equal("\e[34mtesting\e[0m", ios)
202
+
203
+ # magenta
204
+
205
+ ios = IOString.new
206
+ ios.puts hl.magenta + "testing" + hl.reset
207
+ assert_equal("\e[35mtesting\e[0m", ios)
208
+
209
+ ios = IOString.new
210
+ ios.puts hl.magenta { "testing" }
211
+ assert_equal("\e[35mtesting\e[0m", ios)
212
+
213
+ ios = IOString.new
214
+ ios.puts "testing".magenta
215
+ assert_equal("\e[35mtesting\e[0m", ios)
216
+
217
+ # cyan
218
+
219
+ ios = IOString.new
220
+ ios.puts hl.cyan + "testing" + hl.reset
221
+ assert_equal("\e[36mtesting\e[0m", ios)
222
+
223
+ ios = IOString.new
224
+ ios.puts hl.cyan { "testing" }
225
+ assert_equal("\e[36mtesting\e[0m", ios)
226
+
227
+ ios = IOString.new
228
+ ios.puts "testing".cyan
229
+ assert_equal("\e[36mtesting\e[0m", ios)
230
+
231
+ # white
232
+
233
+ ios = IOString.new
234
+ ios.puts hl.white + "testing" + hl.reset
235
+ assert_equal("\e[37mtesting\e[0m", ios)
236
+
237
+ ios = IOString.new
238
+ ios.puts hl.white { "testing" }
239
+ assert_equal("\e[37mtesting\e[0m", ios)
240
+
241
+ ios = IOString.new
242
+ ios.puts "testing".white
243
+ assert_equal("\e[37mtesting\e[0m", ios)
244
+
245
+ # backgrounds
246
+
247
+ # on_black
248
+
249
+ ios = IOString.new
250
+ ios.puts hl.on_black + "testing" + hl.reset
251
+ assert_equals("\e[40mtesting\e[0m", ios)
252
+
253
+ ios = IOString.new
254
+ ios.puts hl.on_black { "testing" }
255
+ assert_equals("\e[40mtesting\e[0m", ios)
256
+
257
+ ios = IOString.new
258
+ ios.puts "testing".on_black
259
+ assert_equals("\e[40mtesting\e[0m", ios)
260
+
261
+ # on_red
262
+
263
+ ios = IOString.new
264
+ ios.puts hl.on_red + "testing" + hl.reset
265
+ assert_equals("\e[41mtesting\e[0m", ios)
266
+
267
+ ios = IOString.new
268
+ ios.puts hl.on_red { "testing" }
269
+ assert_equals("\e[41mtesting\e[0m", ios)
270
+
271
+ ios = IOString.new
272
+ ios.puts "testing".on_red
273
+ assert_equals("\e[41mtesting\e[0m", ios)
274
+
275
+ # on_green
276
+
277
+ ios = IOString.new
278
+ ios.puts hl.on_green + "testing" + hl.reset
279
+ assert_equals("\e[42mtesting\e[0m", ios)
280
+
281
+ ios = IOString.new
282
+ ios.puts hl.on_green { "testing" }
283
+ assert_equals("\e[42mtesting\e[0m", ios)
284
+
285
+ ios = IOString.new
286
+ ios.puts "testing".on_green
287
+ assert_equals("\e[42mtesting\e[0m", ios)
288
+
289
+ # on_yellow
290
+
291
+ ios = IOString.new
292
+ ios.puts hl.on_yellow + "testing" + hl.reset
293
+ assert_equals("\e[43mtesting\e[0m", ios)
294
+
295
+ ios = IOString.new
296
+ ios.puts hl.on_yellow { "testing" }
297
+ assert_equals("\e[43mtesting\e[0m", ios)
298
+
299
+ ios = IOString.new
300
+ ios.puts "testing".on_yellow
301
+ assert_equals("\e[43mtesting\e[0m", ios)
302
+
303
+ # on_blue
304
+
305
+ ios = IOString.new
306
+ ios.puts hl.on_blue + "testing" + hl.reset
307
+ assert_equals("\e[44mtesting\e[0m", ios)
308
+
309
+ ios = IOString.new
310
+ ios.puts hl.on_blue { "testing" }
311
+ assert_equals("\e[44mtesting\e[0m", ios)
312
+
313
+ ios = IOString.new
314
+ ios.puts "testing".on_blue
315
+ assert_equals("\e[44mtesting\e[0m", ios)
316
+
317
+ # on_magenta
318
+
319
+ ios = IOString.new
320
+ ios.puts hl.on_magenta + "testing" + hl.reset
321
+ assert_equals("\e[45mtesting\e[0m", ios)
322
+
323
+ ios = IOString.new
324
+ ios.puts hl.on_magenta { "testing" }
325
+ assert_equals("\e[45mtesting\e[0m", ios)
326
+
327
+ ios = IOString.new
328
+ ios.puts "testing".on_magenta
329
+ assert_equals("\e[45mtesting\e[0m", ios)
330
+
331
+ # on_cyan
332
+
333
+ ios = IOString.new
334
+ ios.puts hl.on_cyan + "testing" + hl.reset
335
+ assert_equals("\e[46mtesting\e[0m", ios)
336
+
337
+ ios = IOString.new
338
+ ios.puts hl.on_cyan { "testing" }
339
+ assert_equals("\e[46mtesting\e[0m", ios)
340
+
341
+ ios = IOString.new
342
+ ios.puts "testing".on_cyan
343
+ assert_equals("\e[46mtesting\e[0m", ios)
344
+
345
+ # on_white
346
+
347
+ ios = IOString.new
348
+ ios.puts hl.on_white + "testing" + hl.reset
349
+ assert_equals("\e[47mtesting\e[0m", ios)
350
+
351
+ ios = IOString.new
352
+ ios.puts hl.on_white { "testing" }
353
+ assert_equals("\e[47mtesting\e[0m", ios)
354
+
355
+ ios = IOString.new
356
+ ios.puts "testing".on_white
357
+ assert_equals("\e[47mtesting\e[0m", ios)
358
+
359
+ # combos
360
+
361
+ debug "testing HTML red on_blue"
362
+ ios = IOString.new
363
+ ios.puts hl.red + hl.on_blue + "this is red on blue" + hl.reset
364
+ assert_equal("\e[31m\e[44mthis is red on blue\e[0m", ios)
365
+ end
366
+
367
+ end