Class: Gitlab::Ci::Ansi2json::Line

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

Overview

Line class is responsible for keeping the internal state of a log line and to finally serialize it as Hash.

Defined Under Namespace

Classes: Segment

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(offset:, style:, sections: []) ⇒ Line

Returns a new instance of Line.


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

def initialize(offset:, style:, sections: [])
  @offset = offset
  @segments = []
  @sections = sections
  @section_header = false
  @duration = nil
  @current_segment = Segment.new(style: style)
end

Instance Attribute Details

#current_segmentObject (readonly)

Returns the value of attribute current_segment


34
35
36
# File 'lib/gitlab/ci/ansi2json/line.rb', line 34

def current_segment
  @current_segment
end

#offsetObject (readonly)

Returns the value of attribute offset


34
35
36
# File 'lib/gitlab/ci/ansi2json/line.rb', line 34

def offset
  @offset
end

#section_durationObject (readonly)

Returns the value of attribute section_duration


34
35
36
# File 'lib/gitlab/ci/ansi2json/line.rb', line 34

def section_duration
  @section_duration
end

#section_headerObject (readonly)

Returns the value of attribute section_header


34
35
36
# File 'lib/gitlab/ci/ansi2json/line.rb', line 34

def section_header
  @section_header
end

#sectionsObject (readonly)

Returns the value of attribute sections


34
35
36
# File 'lib/gitlab/ci/ansi2json/line.rb', line 34

def sections
  @sections
end

#segmentsObject (readonly)

Returns the value of attribute segments


34
35
36
# File 'lib/gitlab/ci/ansi2json/line.rb', line 34

def segments
  @segments
end

Instance Method Details

#<<(data) ⇒ Object


46
47
48
# File 'lib/gitlab/ci/ansi2json/line.rb', line 46

def <<(data)
  @current_segment.text << data
end

#add_section(section) ⇒ Object


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

def add_section(section)
  @sections << section
end

#clear!Object


50
51
52
53
# File 'lib/gitlab/ci/ansi2json/line.rb', line 50

def clear!
  @segments.clear
  @current_segment = Segment.new(style: style)
end

#empty?Boolean

Returns:

  • (Boolean)

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

def empty?
  @segments.empty? && @current_segment.empty? && @section_duration.nil?
end

#flush_current_segment!Object


79
80
81
82
83
84
# File 'lib/gitlab/ci/ansi2json/line.rb', line 79

def flush_current_segment!
  return if @current_segment.empty?

  @segments << @current_segment.to_h
  @current_segment = Segment.new(style: @current_segment.style)
end

#set_as_section_headerObject


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

def set_as_section_header
  @section_header = true
end

#set_section_duration(duration) ⇒ Object


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

def set_section_duration(duration)
  @section_duration = Time.at(duration.to_i).strftime('%M:%S')
end

#styleObject


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

def style
  @current_segment.style
end

#to_hObject


86
87
88
89
90
91
92
93
94
# File 'lib/gitlab/ci/ansi2json/line.rb', line 86

def to_h
  flush_current_segment!

  { offset: offset, content: @segments }.tap do |result|
    result[:section] = sections.last if sections.any?
    result[:section_header] = true if @section_header
    result[:section_duration] = @section_duration if @section_duration
  end
end

#update_style(ansi_commands) ⇒ Object


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

def update_style(ansi_commands)
  @current_segment.style.update(ansi_commands)
end