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