Class: Gitlab::Ci::Ansi2json::State

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/ci/ansi2json/state.rb

Constant Summary collapse

SIGNATURE_KEY_SALT =
'gitlab-ci-ansi2json-state'
SEPARATOR =
'--'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_state, stream_size) ⇒ State

Returns a new instance of State.



18
19
20
21
22
23
24
25
# File 'lib/gitlab/ci/ansi2json/state.rb', line 18

def initialize(new_state, stream_size)
  @offset = 0
  @inherited_style = {}
  @open_sections = {}
  @stream_size = stream_size

  restore_state!(new_state)
end

Instance Attribute Details

#current_lineObject

Returns the value of attribute current_line.



16
17
18
# File 'lib/gitlab/ci/ansi2json/state.rb', line 16

def current_line
  @current_line
end

#inherited_styleObject

Returns the value of attribute inherited_style.



16
17
18
# File 'lib/gitlab/ci/ansi2json/state.rb', line 16

def inherited_style
  @inherited_style
end

#last_line_offsetObject

Returns the value of attribute last_line_offset.



16
17
18
# File 'lib/gitlab/ci/ansi2json/state.rb', line 16

def last_line_offset
  @last_line_offset
end

#offsetObject

Returns the value of attribute offset.



16
17
18
# File 'lib/gitlab/ci/ansi2json/state.rb', line 16

def offset
  @offset
end

#open_sectionsObject

Returns the value of attribute open_sections.



16
17
18
# File 'lib/gitlab/ci/ansi2json/state.rb', line 16

def open_sections
  @open_sections
end

Instance Method Details

#close_section(section, timestamp) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/gitlab/ci/ansi2json/state.rb', line 47

def close_section(section, timestamp)
  return unless section_open?(section)

  duration = timestamp.to_i - @open_sections[section].to_i
  @current_line.set_section_duration(duration)

  @open_sections.delete(section)
end

#encodeObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gitlab/ci/ansi2json/state.rb', line 27

def encode
  json = {
    offset: @last_line_offset,
    style: @current_line.style.to_h,
    open_sections: @open_sections
  }.to_json

  encoded = Base64.urlsafe_encode64(json, padding: false)

  encoded + SEPARATOR + sign(encoded)
end

#new_line!(style: nil) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/gitlab/ci/ansi2json/state.rb', line 60

def new_line!(style: nil)
  new_line = Line.new(
    offset: @offset,
    style: style || @current_line.style,
    sections: @open_sections.keys
  )
  @current_line = new_line
end

#open_section(section, timestamp, options) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/gitlab/ci/ansi2json/state.rb', line 39

def open_section(section, timestamp, options)
  @open_sections[section] = timestamp

  @current_line.add_section(section)
  @current_line.set_section_options(options)
  @current_line.set_as_section_header
end

#section_open?(section) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/gitlab/ci/ansi2json/state.rb', line 56

def section_open?(section)
  @open_sections.key?(section)
end

#set_last_line_offsetObject



69
70
71
# File 'lib/gitlab/ci/ansi2json/state.rb', line 69

def set_last_line_offset
  @last_line_offset = @current_line.offset
end

#update_style(commands) ⇒ Object



73
74
75
76
# File 'lib/gitlab/ci/ansi2json/state.rb', line 73

def update_style(commands)
  @current_line.flush_current_segment!
  @current_line.update_style(commands)
end