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
73
74
|
# File 'lib/colored2.rb', line 9
def self.included(from_class)
from_class.class_eval do
def surround_with_color(color_or_effect, color_self, string = nil, &block)
color_type = if Colored2.background_next?
Colored2.foreground_next!
:background
else
:foreground
end
if color_self then
opts = { beginning: :on, end: :off, color_type => color_or_effect, effect: color_or_effect }
colored = Colored2::AsciiDecorator.new(self).decorate(opts)
if string || block
arg = "#{string}#{block.call if block}"
colored << Colored2::AsciiDecorator.new(arg).decorate(opts) if arg.length > 0
end
else
opts = { end: :on, color_type => color_or_effect, effect: color_or_effect}
colored = Colored2::AsciiDecorator.new(self).decorate(opts)
if string || block
arg = "#{string}#{block.call if block}"
colored << Colored2::AsciiDecorator.new(arg).decorate(opts.merge(end: :off)) if arg.length > 0
end
end
colored
end
def on
Colored2.background_next!
self
end
end
from_class.instance_eval do
COLORS.keys.each do |color|
define_method(color) do |string = nil, &block|
surround_with_color(color, true, string, &block)
end
define_method("#{color}!".to_sym) do |string = nil, &block|
surround_with_color(color, false, string, &block)
end
end
EFFECTS.keys.each do |effect|
next if effect == 'clear'
define_method(effect) do |string = nil, &block|
surround_with_color(effect, true, string, &block)
end
define_method("#{effect}!".to_sym) do |string = nil, &block|
surround_with_color(effect, false, string, &block)
end
end
define_method(:to_eol) do
tmp = sub(/^(\e\[[\[\e0-9;m]+m)/, "\\1\e[2K")
if tmp == self
return "\e[2K" << self
end
tmp
end
end
end
|