Module: Rib::Color
- Extended by:
- Plugin
- Defined in:
- lib/rib/more/color.rb
Instance Attribute Summary
Attributes included from Plugin
#disabled
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Plugin
const_missing, disable, disabled?, enable, enabled?, extended
Class Method Details
.black(&block) ⇒ Object
109
|
# File 'lib/rib/more/color.rb', line 109
def black █ color(30, &block); end
|
.blue(&block) ⇒ Object
113
|
# File 'lib/rib/more/color.rb', line 113
def blue █ color(34, &block); end
|
.color(rgb) ⇒ Object
105
106
107
|
# File 'lib/rib/more/color.rb', line 105
def color rgb
"\e[#{rgb}m" + if block_given? then "#{yield}#{reset}" else '' end
end
|
.colorize_backtrace(backtrace) ⇒ Object
86
87
88
89
90
91
92
93
94
|
# File 'lib/rib/more/color.rb', line 86
def colorize_backtrace backtrace
backtrace.map{ |b|
path, msgs = b.split(':', 2)
dir, sep, file = path.rpartition('/')
msg = msgs.sub(/(\d+):/){red{$1}+':'}.sub(/`.+?'/){green{$&}}
"#{dir+sep}#{yellow{file}}:#{msg}"
}
end
|
.cyan(&block) ⇒ Object
115
|
# File 'lib/rib/more/color.rb', line 115
def cyan █ color(36, &block); end
|
.find_color(colors, value) ⇒ Object
96
97
98
99
100
101
102
103
|
# File 'lib/rib/more/color.rb', line 96
def find_color colors, value
(colors.sort{ |(k1, _), (k2, _)|
if k1 < k2 then -1
elsif k1 > k2 then 1
else 0
end}.find{ |(klass, _)| value.kind_of?(klass) } || []).last
end
|
.green(&block) ⇒ Object
111
|
# File 'lib/rib/more/color.rb', line 111
def green █ color(32, &block); end
|
.magenta(&block) ⇒ Object
114
|
# File 'lib/rib/more/color.rb', line 114
def magenta █ color(35, &block); end
|
.red(&block) ⇒ Object
110
|
# File 'lib/rib/more/color.rb', line 110
def red █ color(31, &block); end
|
.reset(&block) ⇒ Object
117
|
# File 'lib/rib/more/color.rb', line 117
def reset █ color( 0, &block); end
|
.white(&block) ⇒ Object
116
|
# File 'lib/rib/more/color.rb', line 116
def white █ color(37, &block); end
|
.yellow(&block) ⇒ Object
112
|
# File 'lib/rib/more/color.rb', line 112
def yellow █ color(33, &block); end
|
Instance Method Details
#before_loop ⇒ Object
10
11
12
13
|
# File 'lib/rib/more/color.rb', line 10
def before_loop
colors
super
end
|
#colors ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/rib/more/color.rb', line 33
def colors
config[:color] ||= {
Numeric => :red ,
String => :green ,
Symbol => :cyan ,
Array => :blue ,
Hash => :blue ,
NilClass => :magenta,
TrueClass => :magenta,
FalseClass => :magenta,
Exception => :magenta,
Object => :yellow }
end
|
79
80
81
|
# File 'lib/rib/more/color.rb', line 79
def format_backtrace backtrace
colorize_backtrace(super(backtrace))
end
|
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
73
74
75
76
77
|
# File 'lib/rib/more/color.rb', line 47
def format_color result, display=inspect_result(result)
case result
when String ; send(colors[String ]){ display }
when Numeric; send(colors[Numeric]){ display }
when Symbol ; send(colors[Symbol ]){ display }
when Array ; send(colors[Array ]){ '[' } +
result.map{ |e | if e.object_id == result.object_id
send(colors[Array]){'[...]'}
else
format_color(e); end }.
join(send(colors[Array ]){ ', ' }) +
send(colors[Array ]){ ']' }
when Hash ; send(colors[Hash ]){ '{' } +
result.map{ |k, v| format_color(k) +
send(colors[Hash ]){ '=>' } +
if v.object_id == result.object_id
send(colors[Hash]){'{...}'}
else
format_color(v); end }.
join(send(colors[Hash ]){ ', ' }) +
send(colors[Hash ]){ '}' }
else ; if color = find_color(colors, result)
send(color){ display }
else
send(colors[Object]){ display }
end
end
end
|
15
16
17
18
|
# File 'lib/rib/more/color.rb', line 15
def format_result result
return super if Color.disabled?
"#{result_prompt}#{format_color(result)}"
end
|
#get_error(err) ⇒ Object
20
21
22
23
24
|
# File 'lib/rib/more/color.rb', line 20
def get_error err
return super if Color.disabled?
message, backtrace = super
[format_color(err, message), format_backtrace(backtrace)]
end
|
#warn(message) ⇒ Object
26
27
28
29
|
# File 'lib/rib/more/color.rb', line 26
def warn message
return super if Color.disabled?
super(red{message})
end
|