Class: Gitlab::Ci::Ansi2json::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/ci/ansi2json/parser.rb

Constant Summary collapse

COLOR =

keys represent the trailing digit in color changing command (30-37, 40-47, 90-97. 100-107)

{
  0 => 'black', # not that this is gray in the intense color table
  1 => 'red',
  2 => 'green',
  3 => 'yellow',
  4 => 'blue',
  5 => 'magenta',
  6 => 'cyan',
  7 => 'white' # not that this is gray in the dark (aka default) color table
}.freeze
STYLE_SWITCHES =
{
  bold:       0x01,
  italic:     0x02,
  underline:  0x04,
  conceal:    0x08,
  cross:      0x10
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, ansi_stack = nil) ⇒ Parser

Returns a new instance of Parser.


43
44
45
46
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 43

def initialize(command, ansi_stack = nil)
  @command = command
  @ansi_stack = ansi_stack
end

Class Method Details

.bold?(mask) ⇒ Boolean

Returns:

  • (Boolean)

30
31
32
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 30

def self.bold?(mask)
  mask & STYLE_SWITCHES[:bold] != 0
end

.matching_formats(mask) ⇒ Object


34
35
36
37
38
39
40
41
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 34

def self.matching_formats(mask)
  formats = []
  STYLE_SWITCHES.each do |text_format, flag|
    formats << "term-#{text_format}" if mask & flag != 0
  end

  formats
end

Instance Method Details

#bg_color(color_index, prefix = nil) ⇒ Object


164
165
166
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 164

def bg_color(color_index, prefix = nil)
  term_color_class(color_index, ['bg', prefix])
end

#bg_color_256(command_stack) ⇒ Object


168
169
170
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 168

def bg_color_256(command_stack)
  xterm_color_class(command_stack, 'bg')
end

#changesObject


48
49
50
51
52
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 48

def changes
  if self.respond_to?("on_#{@command}")
    send("on_#{@command}", @ansi_stack) # rubocop:disable GitlabSecurity/PublicSend
  end
end

#color_class(segments) ⇒ Object


193
194
195
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 193

def color_class(segments)
  [segments].flatten.compact.join('-')
end

#fg_color(color_index, prefix = nil) ⇒ Object

rubocop:enable Style/SingleLineMethods


156
157
158
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 156

def fg_color(color_index, prefix = nil)
  term_color_class(color_index, ['fg', prefix])
end

#fg_color_256(command_stack) ⇒ Object


160
161
162
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 160

def fg_color_256(command_stack)
  xterm_color_class(command_stack, 'fg')
end

#on_0(_) ⇒ Object

rubocop:disable Style/SingleLineMethods


55
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 55

def on_0(_) { reset: true } end

#on_1(_) ⇒ Object


57
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 57

def on_1(_) { enable: STYLE_SWITCHES[:bold] } end

#on_100(_) ⇒ Object


137
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 137

def on_100(_) { fg: bg_color(0, 'l') } end

#on_101(_) ⇒ Object


139
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 139

def on_101(_) { fg: bg_color(1, 'l') } end

#on_102(_) ⇒ Object


141
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 141

def on_102(_) { fg: bg_color(2, 'l') } end

#on_103(_) ⇒ Object


143
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 143

def on_103(_) { fg: bg_color(3, 'l') } end

#on_104(_) ⇒ Object


145
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 145

def on_104(_) { fg: bg_color(4, 'l') } end

#on_105(_) ⇒ Object


147
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 147

def on_105(_) { fg: bg_color(5, 'l') } end

#on_106(_) ⇒ Object


149
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 149

def on_106(_) { fg: bg_color(6, 'l') } end

#on_107(_) ⇒ Object


151
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 151

def on_107(_) { fg: bg_color(7, 'l') } end

#on_109(_) ⇒ Object


153
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 153

def on_109(_) { fg: bg_color(9, 'l') } end

#on_21(_) ⇒ Object


67
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 67

def on_21(_) { disable: STYLE_SWITCHES[:bold] } end

#on_22(_) ⇒ Object


69
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 69

def on_22(_) { disable: STYLE_SWITCHES[:bold] } end

#on_23(_) ⇒ Object


71
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 71

def on_23(_) { disable: STYLE_SWITCHES[:italic] } end

#on_24(_) ⇒ Object


73
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 73

def on_24(_) { disable: STYLE_SWITCHES[:underline] } end

#on_28(_) ⇒ Object


75
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 75

def on_28(_) { disable: STYLE_SWITCHES[:conceal] } end

#on_29(_) ⇒ Object


77
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 77

def on_29(_) { disable: STYLE_SWITCHES[:cross] } end

#on_3(_) ⇒ Object


59
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 59

def on_3(_) { enable: STYLE_SWITCHES[:italic] } end

#on_30(_) ⇒ Object


79
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 79

def on_30(_) { fg: fg_color(0) } end

#on_31(_) ⇒ Object


81
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 81

def on_31(_) { fg: fg_color(1) } end

#on_32(_) ⇒ Object


83
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 83

def on_32(_) { fg: fg_color(2) } end

#on_33(_) ⇒ Object


85
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 85

def on_33(_) { fg: fg_color(3) } end

#on_34(_) ⇒ Object


87
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 87

def on_34(_) { fg: fg_color(4) } end

#on_35(_) ⇒ Object


89
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 89

def on_35(_) { fg: fg_color(5) } end

#on_36(_) ⇒ Object


91
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 91

def on_36(_) { fg: fg_color(6) } end

#on_37(_) ⇒ Object


93
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 93

def on_37(_) { fg: fg_color(7) } end

#on_38(stack) ⇒ Object


95
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 95

def on_38(stack) { fg: fg_color_256(stack) } end

#on_39(_) ⇒ Object


97
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 97

def on_39(_) { fg: nil } end

#on_4(_) ⇒ Object


61
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 61

def on_4(_) { enable: STYLE_SWITCHES[:underline] } end

#on_40(_) ⇒ Object


99
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 99

def on_40(_) { bg: bg_color(0) } end

#on_41(_) ⇒ Object


101
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 101

def on_41(_) { bg: bg_color(1) } end

#on_42(_) ⇒ Object


103
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 103

def on_42(_) { bg: bg_color(2) } end

#on_43(_) ⇒ Object


105
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 105

def on_43(_) { bg: bg_color(3) } end

#on_44(_) ⇒ Object


107
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 107

def on_44(_) { bg: bg_color(4) } end

#on_45(_) ⇒ Object


109
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 109

def on_45(_) { bg: bg_color(5) } end

#on_46(_) ⇒ Object


111
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 111

def on_46(_) { bg: bg_color(6) } end

#on_47(_) ⇒ Object


113
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 113

def on_47(_) { bg: bg_color(7) } end

#on_48(stack) ⇒ Object


115
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 115

def on_48(stack) { bg: bg_color_256(stack) } end

#on_49(_) ⇒ Object


117
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 117

def on_49(_) { bg: nil } end

#on_8(_) ⇒ Object


63
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 63

def on_8(_) { enable: STYLE_SWITCHES[:conceal] } end

#on_9(_) ⇒ Object


65
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 65

def on_9(_) { enable: STYLE_SWITCHES[:cross] } end

#on_90(_) ⇒ Object


119
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 119

def on_90(_) { fg: fg_color(0, 'l') } end

#on_91(_) ⇒ Object


121
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 121

def on_91(_) { fg: fg_color(1, 'l') } end

#on_92(_) ⇒ Object


123
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 123

def on_92(_) { fg: fg_color(2, 'l') } end

#on_93(_) ⇒ Object


125
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 125

def on_93(_) { fg: fg_color(3, 'l') } end

#on_94(_) ⇒ Object


127
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 127

def on_94(_) { fg: fg_color(4, 'l') } end

#on_95(_) ⇒ Object


129
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 129

def on_95(_) { fg: fg_color(5, 'l') } end

#on_96(_) ⇒ Object


131
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 131

def on_96(_) { fg: fg_color(6, 'l') } end

#on_97(_) ⇒ Object


133
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 133

def on_97(_) { fg: fg_color(7, 'l') } end

#on_99(_) ⇒ Object


135
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 135

def on_99(_) { fg: fg_color(9, 'l') } end

#term_color_class(color_index, prefix) ⇒ Object


172
173
174
175
176
177
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 172

def term_color_class(color_index, prefix)
  color_name = COLOR[color_index]
  return if color_name.nil?

  color_class(['term', prefix, color_name])
end

#xterm_color_class(command_stack, prefix) ⇒ Object


179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/gitlab/ci/ansi2json/parser.rb', line 179

def xterm_color_class(command_stack, prefix)
  # the 38 and 48 commands have to be followed by "5" and the color index
  return unless command_stack.length >= 2
  return unless command_stack[0] == "5"

  command_stack.shift # ignore the "5" command
  color_index = command_stack.shift.to_i

  return unless color_index >= 0
  return unless color_index <= 255

  color_class(["xterm", prefix, color_index])
end