ansi 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,19 +18,17 @@ Website:: http://rubyworks.github.com/ansi
18
18
  of conditions and the following disclaimer in the documentation and/or other materials
19
19
  provided with the distribution.
20
20
 
21
- THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS OR IMPLIED
21
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS OR IMPLIED
22
22
  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
23
- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
24
- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
24
+ OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
25
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
26
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27
27
  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
28
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29
29
  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
30
 
31
- The views and conclusions contained in the software and documentation are those of the
32
- authors and should not be interpreted as representing official policies, either expressed
33
- or implied, of <copyright holder>.
31
+ (http://spdx.org/licenses/BSD-2-Clause)
34
32
 
35
33
 
36
34
  == ProgressBar
@@ -117,14 +115,42 @@ support into the ANSI::Code module.
117
115
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
118
116
 
119
117
 
120
- = ACKNOWLEDGEMENTS
118
+ == Paint
119
+
120
+ Copyright:: (c) 2011 Jan Lelis
121
+ Website:: https://github.com/janlelis/paint
122
+
123
+ Some of the latest ANSI code names, and inspiration to check out Rainbow
124
+ and include XTerm 256 color codes, came from Paint.
125
+
126
+ Copyright (c) 2011 Jan Lelis
127
+
128
+ Permission is hereby granted, free of charge, to any person obtaining
129
+ a copy of this software and associated documentation files (the
130
+ "Software"), to deal in the Software without restriction, including
131
+ without limitation the rights to use, copy, modify, merge, publish,
132
+ distribute, sublicense, and/or sell copies of the Software, and to
133
+ permit persons to whom the Software is furnished to do so, subject to
134
+ the following conditions:
135
+
136
+ The above copyright notice and this permission notice shall be
137
+ included in all copies or substantial portions of the Software.
138
+
139
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
140
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
141
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
142
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
143
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
144
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
145
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
146
+
121
147
 
122
148
  == ANSIColor
123
149
 
124
150
  Copyright:: (c) 2002 Florian Frank
125
151
  Website:: http://flori.github.com/term-ansicolor
126
152
 
127
- Albeit the code no long bares any resemblance to it, the ANSI Code module
128
- (and subsequently the Constants module) originated with the ANSIColor
129
- library by Florian Frank.
153
+ Albeit the code no long bares much if any resemblance to it, the ANSI Code
154
+ module (and subsequently the Constants module) originated with the
155
+ ANSIColor library by Florian Frank.
130
156
 
@@ -0,0 +1,398 @@
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
+ = ANSI::BBCode
52
+
53
+ The BBCode module provides methods for converting between
54
+ BBCodes, basic HTML and ANSI codes.
55
+
56
+ require 'ansi/bbcode'
57
+
58
+ BBCodes are color and style codes in square brackets, quite
59
+ popular with on line forums.
60
+
61
+ bbcode = "this is [COLOR=red]red[/COLOR], this is [B]bold[/B]"
62
+
63
+ We can convert this to ANSI code simply enough:
64
+
65
+ ansi = ANSI::BBCode.bbcode_to_ansi(bbcode)
66
+
67
+ ansi.assert == "this is \e[0;31mred\e[0m, this is \e[1mbold\e[0m\n"
68
+
69
+ In addition the BBCode module supports conversion to simple HTML.
70
+
71
+ html = ANSI::BBCode.bbcode_to_html(bbcode)
72
+
73
+ html.assert == "this is <font color=\"red\">red</font>, this is <strong>bold</strong><br />\n"
74
+
75
+
76
+ = ANSI::Logger
77
+
78
+ Require the ANSI::Logger library.
79
+
80
+ require 'ansi/logger'
81
+
82
+ Create a new ANSI::Logger
83
+
84
+ log = ANSI::Logger.new(STDOUT)
85
+
86
+ Info logging appears normal.
87
+
88
+ log.info{"Info logs are green.\n"}
89
+
90
+ Warn logging appears yellow.
91
+
92
+ log.warn{"Warn logs are yellow.\n"}
93
+
94
+ Debug logging appears cyan.
95
+
96
+ log.debug{"Debug logs are cyan.\n"}
97
+
98
+ Error logging appears red.
99
+
100
+ log.error{"Error logs are red.\n"}
101
+
102
+ Fatal logging appears bright red.
103
+
104
+ log.fatal{"Fatal logs are bold red!\n"}
105
+
106
+ QED.
107
+
108
+ = ANSI::Progressbar
109
+
110
+ Pretty progress bars are easy to construct.
111
+
112
+ require 'ansi/progressbar'
113
+
114
+ pbar = ANSI::Progressbar.new("Test Bar", 100)
115
+
116
+ Running the bar simply requires calling the #inc method during
117
+ a loop and calling #finish when done.
118
+
119
+ 100.times do |i|
120
+ sleep 0.01
121
+ pbar.inc
122
+ end
123
+ pbar.finish
124
+
125
+ We will use this same rountine in all the examples below, so lets
126
+ make a quick macro for it. Notice we have to use #reset first
127
+ before reusing the same progress bar.
128
+
129
+ def run(pbar)
130
+ pbar.reset
131
+ 100.times do |i|
132
+ sleep 0.01
133
+ pbar.inc
134
+ end
135
+ pbar.finish
136
+ puts
137
+ end
138
+
139
+ The progress bar can be stylized in almost any way.
140
+ The #format setter provides control over the parts
141
+ that appear on the line. For example, by default the
142
+ format is:
143
+
144
+ pbar.format("%-14s %3d%% %s %s", :title, :percentage, :bar, :stat)
145
+
146
+ So lets vary it up to demonstrate the case.
147
+
148
+ pbar.format("%-14s %3d%% %s %s", :title, :percentage, :stat, :bar)
149
+ run(pbar)
150
+
151
+ The progress bar has an extra build in format intended for use with
152
+ file downloads called #transer_mode.
153
+
154
+ pbar.transfer_mode
155
+ run(pbar)
156
+
157
+ Calling this methods is the same as calling:
158
+
159
+ pbar.format("%-14s %3d%% %s %s",:title, :percentage, :bar, :stat_for_file_transfer)
160
+ run(pbar)
161
+
162
+ The #style setter allows each part of the line be modified with ANSI codes. And the
163
+ #bar_mark writer can be used to change the character used to make the bar.
164
+
165
+ pbar.standard_mode
166
+ pbar.style(:title => [:red], :bar=>[:blue])
167
+ pbar.bar_mark = "="
168
+ run(pbar)
169
+
170
+ QED.
171
+
172
+ = ANSI::Mixin
173
+
174
+ The ANSI::Mixin module is design for including into
175
+ String-like classes. It will support any class that defines
176
+ a #to_s method.
177
+
178
+ require 'ansi/mixin'
179
+
180
+ In this demonstration we will simply include it in the
181
+ core String class.
182
+
183
+ class ::String
184
+ include ANSI::Mixin
185
+ end
186
+
187
+ Now all strings will have access to ANSI's style and color
188
+ codes via simple method calls.
189
+
190
+ "roses".red.assert == "\e[31mroses\e[0m"
191
+
192
+ "violets".blue.assert == "\e[34mviolets\e[0m"
193
+
194
+ "sugar".italic.assert == "\e[3msugar\e[0m"
195
+
196
+ The method can be combined, of course.
197
+
198
+ "you".italic.bold.assert == "\e[1m\e[3myou\e[0m\e[0m"
199
+
200
+ The mixin also supports background methods.
201
+
202
+ "envy".on_green.assert == "\e[42menvy\e[0m"
203
+
204
+ And it also supports the combined foreground-on-background
205
+ methods.
206
+
207
+ "b&w".white_on_black.assert == "\e[37m\e[40mb&w\e[0m"
208
+
209
+
210
+ = ANSI::String
211
+
212
+ The ANSI::String class is a very sophisticated implementation
213
+ of Ruby's standard String class, but one that can handle
214
+ ANSI codes seamlessly.
215
+
216
+ require 'ansi/string'
217
+
218
+ flower1 = ANSI::String.new("Roses")
219
+ flower2 = ANSI::String.new("Violets")
220
+
221
+ Like any other string.
222
+
223
+ flower1.to_s.assert == "Roses"
224
+ flower2.to_s.assert == "Violets"
225
+
226
+ Bet now we can add color.
227
+
228
+ flower1.red!
229
+ flower2.blue!
230
+
231
+ flower1.to_s.assert == "\e[31mRoses\e[0m"
232
+ flower2.to_s.assert == "\e[34mViolets\e[0m"
233
+
234
+ Despite that the string representation now contains ANSI codes,
235
+ we can still manipulate the string in much the same way that
236
+ we manipulate an ordinary string.
237
+
238
+ flower1.size.assert == 5
239
+ flower2.size.assert == 7
240
+
241
+ Like ordinary strings we can concatenate the two strings
242
+
243
+ flowers = flower1 + ' ' + flower2
244
+ flowers.to_s.assert == "\e[31mRoses\e[0m \e[34mViolets\e[0m"
245
+
246
+ flowers.size.assert == 13
247
+
248
+ Standard case conversion such as #upcase and #downcase work.
249
+
250
+ flower1.upcase.to_s.assert == "\e[31mROSES\e[0m"
251
+ flower1.downcase.to_s.assert == "\e[31mroses\e[0m"
252
+
253
+ Some of the most difficult methods to re-implement were the
254
+ substitution methods such as #sub and #gsub. They are still
255
+ somewhat more limited than the original string methods, but
256
+ their primary functionality should work.
257
+
258
+ flower1.gsub('s', 'z').to_s.assert == "\e[31mRozez\e[0m"
259
+
260
+ There are still a number of methods that need implementation.
261
+ ANSI::String is currently a very partial implementation. But
262
+ as you can see from the methods it does currently support,
263
+ is it already useful.
264
+
265
+
266
+
267
+ = ANSI::Columns
268
+
269
+ The +Columns+ class makes it easy to create nice looking text columns,
270
+ sorted from top to bottom, right to left (as opposed to the other way
271
+ around).
272
+
273
+ require 'ansi/columns'
274
+
275
+ list = %w{a b c d e f g h i j k l}
276
+
277
+ columns = ANSI::Columns.new(list)
278
+
279
+ columns.to_s(4)
280
+
281
+ The output will be:
282
+
283
+ a d g j
284
+ b e h k
285
+ c f i l
286
+
287
+ Besides an array of elements, Columns.new can take a string in which
288
+ the elements are divided by newlines characters. The default column
289
+ size can also be given to the initializer.
290
+
291
+ list = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl"
292
+
293
+ columns = ANSI::Columns.new(list, :columns=>6)
294
+
295
+ columns.to_s
296
+
297
+ The output will be:
298
+
299
+ a c e g i k
300
+ b d f h j l
301
+
302
+ If the column count is +nil+, then the number of columns will be calculated
303
+ as a best fit for the current terminal window.
304
+
305
+ == Padding
306
+
307
+ Columns can adjust the padding between cells.
308
+
309
+ list = %w{a b c d e f g h i j k l}
310
+
311
+ columns = ANSI::Columns.new(list, :padding=>2)
312
+
313
+ columns.to_s(4)
314
+
315
+ The output will be:
316
+
317
+ a d g j
318
+ b e h k
319
+ c f i l
320
+
321
+ == Alignment
322
+
323
+ Columns can also be aligned either left or right.
324
+
325
+ list = %w{xx xx xx yy y yy z zz z}
326
+
327
+ columns = ANSI::Columns.new(list, :align=>:right)
328
+
329
+ columns.to_s(3)
330
+
331
+ The output will be:
332
+
333
+ xx yy z
334
+ xx y zz
335
+ xx yy z
336
+
337
+ == Format
338
+
339
+ Lastly, columns can be augmented with ANSI codes. This is done through
340
+ a formatting block. The block can take up to three parameters, the cell
341
+ content, the column and row numbers, or the cell and the column and row
342
+ numbers.
343
+
344
+ list = %w{a b c d e f g h i j k l}
345
+
346
+ columns = ANSI::Columns.new(list){ |c,r| r % 2 == 0 ? :red : :blue }
347
+
348
+ out = columns.to_s(4)
349
+
350
+ out.assert == (
351
+ "\e[31ma \e[0m\e[31md \e[0m\e[31mg \e[0m\e[31mj \e[0m\n" +
352
+ "\e[34mb \e[0m\e[34me \e[0m\e[34mh \e[0m\e[34mk \e[0m\n" +
353
+ "\e[31mc \e[0m\e[31mf \e[0m\e[31mi \e[0m\e[31ml \e[0m\n"
354
+ )
355
+
356
+ QED.
357
+
358
+ = ANSI::Table
359
+
360
+ The ANSI::Table class can be used to output tabular data with nicely
361
+ formated ASCII cell borders.
362
+
363
+ require 'ansi/table'
364
+
365
+ The constructor takes an 2-dimensional array.
366
+
367
+ data = [
368
+ [ 10, 20, 30 ],
369
+ [ 20, 10, 20 ],
370
+ [ 50, 40, 20 ]
371
+ ]
372
+
373
+ table = ANSI::Table.new(data)
374
+
375
+ table.to_s
376
+
377
+ The output will be:
378
+
379
+ +----+----+----+
380
+ | 10 | 20 | 30 |
381
+ | 20 | 10 | 20 |
382
+ | 50 | 40 | 20 |
383
+ +----+----+----+
384
+
385
+
386
+
387
+ = String Extensions
388
+
389
+ In addition the library offers an extension to String class
390
+ called #ansi, which allows some of the ANSI::Code methods
391
+ to be called in a more object-oriented fashion.
392
+
393
+ require 'ansi/core'
394
+
395
+ str = "Hello".ansi(:red) + "World".ansi(:blue)
396
+ str.assert == "\e[31mHello\e[0m\e[34mWorld\e[0m"
397
+
398
+