Module: Hilighter::Methods
- Included in:
- String
- Defined in:
- lib/hilighter/methods.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/hilighter/methods.rb', line 12
def method_missing(method_name, *args)
method_name.to_s.match(/^([A-Fa-f0-9]{6})$/) do |m|
return self.hex_color(m[1])
end
method_name.to_s.match(/^on_([A-Fa-f0-9]{6})$/) do |m|
return self.on_hex_color(m[1])
end
method_name.to_s.match(/^wrap(_(\d+))?$/) do |m|
width = nil
width = m[2].to_i if (!m[1].nil?)
return self.wrap(width)
end
super
end
|
Instance Method Details
#hex_color(hex) ⇒ Object
8
9
10
|
# File 'lib/hilighter/methods.rb', line 8
def hex_color(hex)
return self.send(Hilighter.hex_to_x256(hex))
end
|
#on_hex_color(hex) ⇒ Object
27
28
29
|
# File 'lib/hilighter/methods.rb', line 27
def on_hex_color(hex)
return self.send("on_#{Hilighter.hex_to_x256(hex)}")
end
|
#on_rainbow ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/hilighter/methods.rb', line 31
def on_rainbow
return self.plain if (Hilighter.disable?)
clrs = rainbow_colors
out = Array.new
self.plain_bg.each_line do |line|
line.chomp!
colorized = line.scan(
/((#{color_regex})*[^\e](#{color_regex})*)/
).map.with_index do |c, i|
"\e[#{clrs[i % clrs.length] + 10}m#{c[0]}"
end.join
out.push("#{colorized}\e[49m\n")
end
return out.join.gsub(/^(#{color_regex})+$/, "")
end
|
#plain ⇒ Object
50
51
52
|
# File 'lib/hilighter/methods.rb', line 50
def plain
return self.gsub(color_regex, "")
end
|
#plain_bg ⇒ Object
54
55
56
|
# File 'lib/hilighter/methods.rb', line 54
def plain_bg
return self.gsub(/\e\[(4|10)[0-9;]+m/, "")
end
|
#plain_fg ⇒ Object
58
59
60
|
# File 'lib/hilighter/methods.rb', line 58
def plain_fg
return self.gsub(/\e\[[39][0-9;]+m/, "")
end
|
#rainbow ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/hilighter/methods.rb', line 67
def rainbow
return self.plain if (Hilighter.disable?)
clrs = rainbow_colors
out = Array.new
self.plain_fg.each_line do |line|
line.chomp!
colorized = line.scan(
/((#{color_regex})*[^\e](#{color_regex})*)/
).map.with_index do |c, i|
"\e[#{clrs[i % clrs.length]}m#{c[0]}"
end.join
out.push("#{colorized}\n")
end
return out.join.gsub(/^(#{color_regex})+$/, "")
end
|
#wrap(width = 80) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/hilighter/methods.rb', line 86
def wrap(width = 80)
line = ""
lines = Array.new
self.split(/\s+/).each do |word|
if (line.empty?)
line = word
elsif ((line.plain.size + word.plain.size) + 1 > width)
lines.push(line)
line = word
else
line += " #{word}"
end
end
lines.push(line) if (!line.empty?)
lines.push("") if (self.end_with?("\n"))
return lines.join("\n")
end
|