ansi 1.4.3 → 1.5.0

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/Config.rb DELETED
@@ -1,13 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- #
4
- # Setup QED.
5
- #
6
- config :qed, :profile=>:cov do
7
- require 'simplecov'
8
- SimpleCov.start do
9
- coverage_dir 'log/coverage'
10
- #add_group "RSpec", "lib/assay/rspec.rb"
11
- end
12
- end
13
-
data/DEMO.rdoc DELETED
@@ -1,455 +0,0 @@
1
- = ANSI::Code
2
-
3
- Require the library.
4
-
5
- require 'ansi/code'
6
-
7
- ANSI::Code can be used as a functions module.
8
-
9
- str = ANSI::Code.red + "Hello" + ANSI::Code.blue + "World"
10
- str.assert == "\e[31mHello\e[34mWorld"
11
-
12
- If a block is supplied to each method then yielded value will
13
- be wrapped in the ANSI code and clear code.
14
-
15
- str = ANSI::Code.red{ "Hello" } + ANSI::Code.blue{ "World" }
16
- str.assert == "\e[31mHello\e[0m\e[34mWorld\e[0m"
17
-
18
- More conveniently the ANSI::Code module extends ANSI itself.
19
-
20
- str = ANSI.red + "Hello" + ANSI.blue + "World"
21
- str.assert == "\e[31mHello\e[34mWorld"
22
-
23
- str = ANSI.red{ "Hello" } + ANSI.blue{ "World" }
24
- str.assert == "\e[31mHello\e[0m\e[34mWorld\e[0m"
25
-
26
- In the appropriate context the ANSI::Code module can also be
27
- included, making its methods directly accessible.
28
-
29
- include ANSI::Code
30
-
31
- str = red + "Hello" + blue + "World"
32
- str.assert == "\e[31mHello\e[34mWorld"
33
-
34
- str = red{ "Hello" } + blue{ "World" }
35
- str.assert == "\e[31mHello\e[0m\e[34mWorld\e[0m"
36
-
37
- Along with the single font colors, the library include background colors.
38
-
39
- str = on_red + "Hello"
40
- str.assert == "\e[41mHello"
41
-
42
- As well as combined color methods.
43
-
44
- str = white_on_red + "Hello"
45
- str.assert == "\e[37m\e[41mHello"
46
-
47
- The ANSI::Code module supports most standard ANSI codes, though
48
- not all platforms support every code, so YMMV.
49
-
50
-
51
- = String Extensions
52
-
53
- In addition the library offers an extension to String class
54
- called #ansi, which allows some of the ANSI::Code methods
55
- to be called in a more object-oriented fashion.
56
-
57
- require 'ansi/core'
58
-
59
- str = "Hello".ansi(:red) + "World".ansi(:blue)
60
- str.assert == "\e[31mHello\e[0m\e[34mWorld\e[0m"
61
-
62
-
63
- = ANSI::Logger
64
-
65
- Require the ANSI::Logger library.
66
-
67
- require 'ansi/logger'
68
-
69
- Create a new ANSI::Logger
70
-
71
- log = ANSI::Logger.new(STDOUT)
72
-
73
- Info logging appears normal.
74
-
75
- log.info{"Info logs are green.\n"}
76
-
77
- Warn logging appears yellow.
78
-
79
- log.warn{"Warn logs are yellow.\n"}
80
-
81
- Debug logging appears cyan.
82
-
83
- log.debug{"Debug logs are cyan.\n"}
84
-
85
- Error logging appears red.
86
-
87
- log.error{"Error logs are red.\n"}
88
-
89
- Fatal logging appears bright red.
90
-
91
- log.fatal{"Fatal logs are bold red!\n"}
92
-
93
- QED.
94
-
95
- = ANSI::Progressbar
96
-
97
- Pretty progress bars are easy to construct.
98
-
99
- require 'ansi/progressbar'
100
-
101
- pbar = ANSI::Progressbar.new("Test Bar", 100)
102
-
103
- Running the bar simply requires calling the #inc method during
104
- a loop and calling #finish when done.
105
-
106
- 100.times do |i|
107
- sleep 0.01
108
- pbar.inc
109
- end
110
- pbar.finish
111
-
112
- We will use this same rountine in all the examples below, so lets
113
- make a quick macro for it. Notice we have to use #reset first
114
- before reusing the same progress bar.
115
-
116
- def run(pbar)
117
- pbar.reset
118
- 100.times do |i|
119
- sleep 0.01
120
- pbar.inc
121
- end
122
- pbar.finish
123
- puts
124
- end
125
-
126
- The progress bar can be stylized in almost any way.
127
- The #format setter provides control over the parts
128
- that appear on the line. For example, by default the
129
- format is:
130
-
131
- pbar.format("%-14s %3d%% %s %s", :title, :percentage, :bar, :stat)
132
-
133
- So lets vary it up to demonstrate the case.
134
-
135
- pbar.format("%-14s %3d%% %s %s", :title, :percentage, :stat, :bar)
136
- run(pbar)
137
-
138
- The progress bar has an extra build in format intended for use with
139
- file downloads called #transer_mode.
140
-
141
- pbar.transfer_mode
142
- run(pbar)
143
-
144
- Calling this methods is the same as calling:
145
-
146
- pbar.format("%-14s %3d%% %s %s",:title, :percentage, :bar, :stat_for_file_transfer)
147
- run(pbar)
148
-
149
- The #style setter allows each part of the line be modified with ANSI codes. And the
150
- #bar_mark writer can be used to change the character used to make the bar.
151
-
152
- pbar.standard_mode
153
- pbar.style(:title => [:red], :bar=>[:blue])
154
- pbar.bar_mark = "="
155
- run(pbar)
156
-
157
- QED.
158
-
159
- = ANSI::Mixin
160
-
161
- The ANSI::Mixin module is design for including into
162
- String-like classes. It will support any class that defines
163
- a #to_s method.
164
-
165
- require 'ansi/mixin'
166
-
167
- In this demonstration we will simply include it in the
168
- core String class.
169
-
170
- class ::String
171
- include ANSI::Mixin
172
- end
173
-
174
- Now all strings will have access to ANSI's style and color
175
- codes via simple method calls.
176
-
177
- "roses".red.assert == "\e[31mroses\e[0m"
178
-
179
- "violets".blue.assert == "\e[34mviolets\e[0m"
180
-
181
- "sugar".italic.assert == "\e[3msugar\e[0m"
182
-
183
- The method can be combined, of course.
184
-
185
- "you".italic.bold.assert == "\e[1m\e[3myou\e[0m\e[0m"
186
-
187
- The mixin also supports background methods.
188
-
189
- "envy".on_green.assert == "\e[42menvy\e[0m"
190
-
191
- And it also supports the combined foreground-on-background
192
- methods.
193
-
194
- "b&w".white_on_black.assert == "\e[37m\e[40mb&w\e[0m"
195
-
196
-
197
- = ANSI::String
198
-
199
- The ANSI::String class is a very sophisticated implementation
200
- of Ruby's standard String class, but one that can handle
201
- ANSI codes seamlessly.
202
-
203
- require 'ansi/string'
204
-
205
- flower1 = ANSI::String.new("Roses")
206
- flower2 = ANSI::String.new("Violets")
207
-
208
- Like any other string.
209
-
210
- flower1.to_s.assert == "Roses"
211
- flower2.to_s.assert == "Violets"
212
-
213
- Bet now we can add color.
214
-
215
- flower1.red!
216
- flower2.blue!
217
-
218
- flower1.to_s.assert == "\e[31mRoses\e[0m"
219
- flower2.to_s.assert == "\e[34mViolets\e[0m"
220
-
221
- Despite that the string representation now contains ANSI codes,
222
- we can still manipulate the string in much the same way that
223
- we manipulate an ordinary string.
224
-
225
- flower1.size.assert == 5
226
- flower2.size.assert == 7
227
-
228
- Like ordinary strings we can concatenate the two strings
229
-
230
- flowers = flower1 + ' ' + flower2
231
- flowers.to_s.assert == "\e[31mRoses\e[0m \e[34mViolets\e[0m"
232
-
233
- flowers.size.assert == 13
234
-
235
- Standard case conversion such as #upcase and #downcase work.
236
-
237
- flower1.upcase.to_s.assert == "\e[31mROSES\e[0m"
238
- flower1.downcase.to_s.assert == "\e[31mroses\e[0m"
239
-
240
- Some of the most difficult methods to re-implement were the
241
- substitution methods such as #sub and #gsub. They are still
242
- somewhat more limited than the original string methods, but
243
- their primary functionality should work.
244
-
245
- flower1.gsub('s', 'z').to_s.assert == "\e[31mRozez\e[0m"
246
-
247
- There are still a number of methods that need implementation.
248
- ANSI::String is currently a very partial implementation. But
249
- as you can see from the methods it does currently support,
250
- is it already useful.
251
-
252
-
253
-
254
- = ANSI::Columns
255
-
256
- The +Columns+ class makes it easy to create nice looking text columns,
257
- sorted from top to bottom, right to left (as opposed to the other way
258
- around).
259
-
260
- require 'ansi/columns'
261
-
262
- list = %w{a b c d e f g h i j k l}
263
-
264
- columns = ANSI::Columns.new(list)
265
-
266
- columns.to_s(4)
267
-
268
- The output will be:
269
-
270
- a d g j
271
- b e h k
272
- c f i l
273
-
274
- Besides an array of elements, Columns.new can take a string in which
275
- the elements are divided by newlines characters. The default column
276
- size can also be given to the initializer.
277
-
278
- list = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl"
279
-
280
- columns = ANSI::Columns.new(list, :columns=>6)
281
-
282
- columns.to_s
283
-
284
- The output will be:
285
-
286
- a c e g i k
287
- b d f h j l
288
-
289
- If the column count is +nil+, then the number of columns will be calculated
290
- as a best fit for the current terminal window.
291
-
292
- == Padding
293
-
294
- Columns can adjust the padding between cells.
295
-
296
- list = %w{a b c d e f g h i j k l}
297
-
298
- columns = ANSI::Columns.new(list, :padding=>2)
299
-
300
- columns.to_s(4)
301
-
302
- The output will be:
303
-
304
- a d g j
305
- b e h k
306
- c f i l
307
-
308
- == Alignment
309
-
310
- Columns can also be aligned either left or right.
311
-
312
- list = %w{xx xx xx yy y yy z zz z}
313
-
314
- columns = ANSI::Columns.new(list, :align=>:right)
315
-
316
- columns.to_s(3)
317
-
318
- The output will be:
319
-
320
- xx yy z
321
- xx y zz
322
- xx yy z
323
-
324
- == Format
325
-
326
- Lastly, columns can be augmented with ANSI codes. This is done through
327
- a formatting block. The block can take up to three parameters, the cell
328
- content, the column and row numbers, or the cell and the column and row
329
- numbers.
330
-
331
- list = %w{a b c d e f g h i j k l}
332
-
333
- columns = ANSI::Columns.new(list){ |c,r| r % 2 == 0 ? :red : :blue }
334
-
335
- out = columns.to_s(4)
336
-
337
- out.assert == (
338
- "\e[31ma \e[0m\e[31md \e[0m\e[31mg \e[0m\e[31mj \e[0m\n" +
339
- "\e[34mb \e[0m\e[34me \e[0m\e[34mh \e[0m\e[34mk \e[0m\n" +
340
- "\e[31mc \e[0m\e[31mf \e[0m\e[31mi \e[0m\e[31ml \e[0m\n"
341
- )
342
-
343
- QED.
344
-
345
- = ANSI::Table
346
-
347
- The ANSI::Table class can be used to output tabular data with nicely
348
- formated ASCII cell borders.
349
-
350
- require 'ansi/table'
351
-
352
- The constructor takes an 2-dimensional array.
353
-
354
- data = [
355
- [ 10, 20, 30 ],
356
- [ 20, 10, 20 ],
357
- [ 50, 40, 20 ]
358
- ]
359
-
360
- table = ANSI::Table.new(data)
361
-
362
- table.to_s
363
-
364
- The output will be:
365
-
366
- +----+----+----+
367
- | 10 | 20 | 30 |
368
- | 20 | 10 | 20 |
369
- | 50 | 40 | 20 |
370
- +----+----+----+
371
-
372
-
373
-
374
- = ANSI::Diff
375
-
376
- require 'ansi/diff'
377
-
378
- a = 'abcYefg'
379
- b = 'abcXefg'
380
-
381
- diff = ANSI::Diff.new(a,b)
382
-
383
- diff.to_s.assert == "\e[31mabc\e[0m\e[33mYefg\e[0m\n\e[31mabc\e[0mXefg"
384
-
385
- Try another.
386
-
387
- a = 'abc'
388
- b = 'abcdef'
389
-
390
- diff = ANSI::Diff.new(a,b)
391
-
392
- diff.to_s.assert == "\e[31mabc\e[0m\n\e[31mabc\e[0mdef"
393
-
394
- And another.
395
-
396
- a = 'abcXXXghi'
397
- b = 'abcdefghi'
398
-
399
- diff = ANSI::Diff.new(a,b)
400
-
401
- diff.to_s.assert == "\e[31mabc\e[0m\e[33mXXXghi\e[0m\n\e[31mabc\e[0mdefghi"
402
-
403
- And another.
404
-
405
- a = 'abcXXXdefghi'
406
- b = 'abcdefghi'
407
-
408
- diff = ANSI::Diff.new(a,b)
409
-
410
- diff.to_s.assert == "\e[31mabc\e[0m\e[33mXXX\e[0m\e[35mdefghi\e[0m\n\e[31mabc\e[0m\e[35mdefghi\e[0m"
411
-
412
- Comparison that is mostly different.
413
-
414
- a = 'abcpppz123'
415
- b = 'abcxyzzz43'
416
-
417
- diff = ANSI::Diff.new(a,b)
418
-
419
- diff.to_s.assert == "\e[31mabc\e[0m\e[33mpppz123\e[0m\n\e[31mabc\e[0mxyzzz43"
420
-
421
-
422
- = ANSI::BBCode
423
-
424
- The BBCode module provides methods for converting between
425
- BBCodes, basic HTML and ANSI codes.
426
-
427
- require 'ansi/bbcode'
428
-
429
- BBCodes are color and style codes in square brackets, quite
430
- popular with on line forums.
431
-
432
- bbcode = "this is [COLOR=red]red[/COLOR], this is [B]bold[/B]"
433
-
434
- We can convert this to ANSI code simply enough:
435
-
436
- ansi = ANSI::BBCode.bbcode_to_ansi(bbcode)
437
-
438
- ansi.assert == "this is \e[0;31mred\e[0m, this is \e[1mbold\e[0m\n"
439
-
440
- In addition the BBCode module supports conversion to simple HTML.
441
-
442
- html = ANSI::BBCode.bbcode_to_html(bbcode)
443
-
444
- html.assert == "this is <font color=\"red\">red</font>, this is <strong>bold</strong><br />\n"
445
-
446
-
447
- = ANSI::Terminal
448
-
449
- We should be ables to get the terminal width via the `terminal_width` method.
450
-
451
- width = ANSI::Terminal.terminal_width
452
-
453
- Fixnum.assert === width
454
-
455
-