Module: Term::ANSIColor

Extended by:
ANSIColor
Included in:
ANSIColor
Defined in:
lib/motion-redgreen/ansiterm.rb

Overview

The ANSIColor module can be used for namespacing and mixed into your own classes.

Constant Summary collapse

ATTRIBUTES =

:stopdoc:

[
  [ :clear        ,   0 ],
  [ :reset        ,   0 ],     # synonym for :clear
  [ :bold         ,   1 ],
  [ :dark         ,   2 ],
  [ :italic       ,   3 ],     # not widely implemented
  [ :underline    ,   4 ],
  [ :underscore   ,   4 ],     # synonym for :underline
  [ :blink        ,   5 ],
  [ :rapid_blink  ,   6 ],     # not widely implemented
  [ :negative     ,   7 ],     # no reverse because of String#reverse
  [ :concealed    ,   8 ],
  [ :strikethrough,   9 ],     # not widely implemented
  [ :black        ,  30 ],
  [ :red          ,  31 ],
  [ :green        ,  32 ],
  [ :yellow       ,  33 ],
  [ :blue         ,  34 ],
  [ :magenta      ,  35 ],
  [ :cyan         ,  36 ],
  [ :white        ,  37 ],
  [ :on_black     ,  40 ],
  [ :on_red       ,  41 ],
  [ :on_green     ,  42 ],
  [ :on_yellow    ,  43 ],
  [ :on_blue      ,  44 ],
  [ :on_magenta   ,  45 ],
  [ :on_cyan      ,  46 ],
  [ :on_white     ,  47 ],
]
ATTRIBUTE_NAMES =
ATTRIBUTES.transpose.first
COLORED_REGEXP =

Regular expression that is used to scan for ANSI-sequences while uncoloring strings.

/\e\[([34][0-7]|[0-9])m/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attributesObject

Returns an array of all Term::ANSIColor attributes as symbols.



553
554
555
# File 'lib/motion-redgreen/ansiterm.rb', line 553

def attributes
  ATTRIBUTE_NAMES
end

.coloring=(val) ⇒ Object

Turns the coloring on or off globally, so you can easily do this for example: Term::ANSIColor::coloring = STDOUT.isatty



51
52
53
# File 'lib/motion-redgreen/ansiterm.rb', line 51

def self.coloring=(val)
  @coloring = val
end

.coloring?Boolean

Returns true, if the coloring function of this module is switched on, false otherwise.

Returns:

  • (Boolean)


44
45
46
# File 'lib/motion-redgreen/ansiterm.rb', line 44

def self.coloring?
  @coloring
end

Instance Method Details

#black(string = nil) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/motion-redgreen/ansiterm.rb', line 261

def black(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/motion-redgreen/ansiterm.rb', line 176

def blink(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#blue(string = nil) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/motion-redgreen/ansiterm.rb', line 329

def blue(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#bold(string = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/motion-redgreen/ansiterm.rb', line 91

def bold(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#clear(string = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/motion-redgreen/ansiterm.rb', line 57

def clear(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#concealed(string = nil) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/motion-redgreen/ansiterm.rb', line 227

def concealed(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#cyan(string = nil) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/motion-redgreen/ansiterm.rb', line 363

def cyan(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#dark(string = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/motion-redgreen/ansiterm.rb', line 108

def dark(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#green(string = nil) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/motion-redgreen/ansiterm.rb', line 295

def green(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#italic(string = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/motion-redgreen/ansiterm.rb', line 125

def italic(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#magenta(string = nil) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/motion-redgreen/ansiterm.rb', line 346

def magenta(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#negative(string = nil) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/motion-redgreen/ansiterm.rb', line 210

def negative(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#on_black(string = nil) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/motion-redgreen/ansiterm.rb', line 397

def on_black(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#on_blue(string = nil) ⇒ Object



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/motion-redgreen/ansiterm.rb', line 465

def on_blue(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#on_cyan(string = nil) ⇒ Object



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/motion-redgreen/ansiterm.rb', line 499

def on_cyan(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#on_green(string = nil) ⇒ Object



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/motion-redgreen/ansiterm.rb', line 431

def on_green(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#on_magenta(string = nil) ⇒ Object



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/motion-redgreen/ansiterm.rb', line 482

def on_magenta(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#on_red(string = nil) ⇒ Object



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/motion-redgreen/ansiterm.rb', line 414

def on_red(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#on_white(string = nil) ⇒ Object



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/motion-redgreen/ansiterm.rb', line 516

def on_white(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#on_yellow(string = nil) ⇒ Object



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/motion-redgreen/ansiterm.rb', line 448

def on_yellow(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/motion-redgreen/ansiterm.rb', line 193

def rapid_blink(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#red(string = nil) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/motion-redgreen/ansiterm.rb', line 278

def red(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#reset(string = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/motion-redgreen/ansiterm.rb', line 74

def reset(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#strikethrough(string = nil) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/motion-redgreen/ansiterm.rb', line 244

def strikethrough(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#uncolored(string = nil) ⇒ Object

Returns an uncolored version of the string, that is all ANSI-sequences are stripped from the string.



538
539
540
541
542
543
544
545
546
547
548
# File 'lib/motion-redgreen/ansiterm.rb', line 538

def uncolored(string = nil) # :yields:
  if block_given?
    yield.gsub(COLORED_REGEXP, '')
  elsif string
    string.gsub(COLORED_REGEXP, '')
  elsif respond_to?(:to_str)
    gsub(COLORED_REGEXP, '')
  else
    ''
  end
end

#underline(string = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/motion-redgreen/ansiterm.rb', line 142

def underline(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#underscore(string = nil) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/motion-redgreen/ansiterm.rb', line 159

def underscore(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#white(string = nil) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/motion-redgreen/ansiterm.rb', line 380

def white(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end

#yellow(string = nil) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/motion-redgreen/ansiterm.rb', line 312

def yellow(string = nil)
  result = ''
  result << "" if Term::ANSIColor.coloring?
  if block_given?
    result << yield
  elsif string
    result << string
  elsif respond_to?(:to_str)
    result << self
  else
    return result #only switch on
  end
  result << "" if Term::ANSIColor.coloring?
  result
end