forked from theforeman/foreman_remote_execution
-
Notifications
You must be signed in to change notification settings - Fork 1
/
job_invocation_output_helper.rb
42 lines (40 loc) · 1.08 KB
/
job_invocation_output_helper.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
module JobInvocationOutputHelper
COLOR_PATTERN = /\e\[.*?m/
CONSOLE_COLOR = {
'31' => 'red',
'32' => 'lightgreen',
'33' => 'orange',
'34' => 'deepskyblue',
'35' => 'mediumpurple',
'36' => 'cyan',
'37' => 'grey',
'91' => 'red',
'92' => 'lightgreen',
'93' => 'yellow',
'94' => 'lightblue',
'95' => 'violet',
'96' => 'turquoise',
'0' => 'default',
}.tap { |h| h.default = 'default' }.freeze
def colorize_line(line)
line = line.gsub(COLOR_PATTERN) do |seq|
color = seq[/(\d+)m/,1]
"{{{format color:#{color}}}}"
end
@current_color ||= 'default'
out = %{<span style="color: #{@current_color}">}
parts = line.split(/({{{format.*?}}})/)
parts.each do |console_line|
if console_line.include?('{{{format')
if (color_index = console_line[/color:(\d+)/, 1]).present?
@current_color = CONSOLE_COLOR[color_index]
out << %{</span><span style="color: #{@current_color}">}
end
else
out << h(console_line)
end
end
out << %{</span>}
out
end
end