/
colorize.rb
73 lines (55 loc) · 1.41 KB
/
colorize.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
begin
require 'ansi/code'
rescue LoadError
begin
require 'rubygems'
require 'ansi/code'
rescue LoadError
end
end
module Turn
module Colorize
COLORLESS_TERMINALS = ['dumb']
def colorize?
defined?(::ANSI::Code) &&
ENV.has_key?('TERM') &&
!COLORLESS_TERMINALS.include?(ENV['TERM']) &&
$stdout.tty?
end
module_function :colorize?
def self.red(string)
colorize? ? ::ANSI::Code.red{ string } : string
end
def self.green(string)
colorize? ? ::ANSI::Code.green{ string } : string
end
def self.blue(string)
colorize? ? ::ANSI::Code.blue{ string } : string
end
def self.magenta(string)
colorize? ? ::ANSI::Code.magenta{ string } : string
end
def self.bold(string)
colorize? ? ::ANSI::Code.bold{ string } : string
end
def self.pass(string)
colorize? ? ::ANSI::Code.green{ string } : string
end
def self.fail(string)
colorize? ? ::ANSI::Code.red{ string } : string
end
#def self.error(string)
# colorize? ? ::ANSI::Code.white{ ::ANSI::Code.on_red{ string } } : string
#end
def self.error(string)
colorize? ? ::ANSI::Code.yellow{ string } : string
end
def self.skip(string)
colorize? ? ::ANSI::Code.cyan{ string } : string
end
PASS = pass('PASS')
FAIL = fail('FAIL')
ERROR = error('ERROR')
SKIP = skip('SKIP')
end
end