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.



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

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.



37
38
39
# File 'lib/gitlab/ci/ansi2json/line.rb', line 37

def current_segment
  @current_segment
end

#offsetObject (readonly)

Returns the value of attribute offset.



37
38
39
# File 'lib/gitlab/ci/ansi2json/line.rb', line 37

def offset
  @offset
end

#section_durationObject (readonly)

Returns the value of attribute section_duration.



37
38
39
# File 'lib/gitlab/ci/ansi2json/line.rb', line 37

def section_duration
  @section_duration
end

#section_headerObject (readonly)

Returns the value of attribute section_header.



37
38
39
# File 'lib/gitlab/ci/ansi2json/line.rb', line 37

def section_header
  @section_header
end

#section_optionsObject (readonly)

Returns the value of attribute section_options.



37
38
39
# File 'lib/gitlab/ci/ansi2json/line.rb', line 37

def section_options
  @section_options
end

#sectionsObject (readonly)

Returns the value of attribute sections.



37
38
39
# File 'lib/gitlab/ci/ansi2json/line.rb', line 37

def sections
  @sections
end

#segmentsObject (readonly)

Returns the value of attribute segments.



37
38
39
# File 'lib/gitlab/ci/ansi2json/line.rb', line 37

def segments
  @segments
end

Instance Method Details

#<<(data) ⇒ Object



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

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

#add_section(section) ⇒ Object



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

def add_section(section)
  @sections << section
end

#clear!Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

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

#flush_current_segment!Object



93
94
95
96
97
98
# File 'lib/gitlab/ci/ansi2json/line.rb', line 93

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



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

def set_as_section_header
  @section_header = true
end

#set_section_duration(duration_in_seconds) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/gitlab/ci/ansi2json/line.rb', line 82

def set_section_duration(duration_in_seconds)
  normalized_duration_in_seconds = duration_in_seconds.to_i.clamp(0, 1.year)
  duration = ActiveSupport::Duration.build(normalized_duration_in_seconds)
  hours = duration.in_hours.floor
  hours = hours > 0 ? "%02d" % hours : nil
  minutes = "%02d" % duration.parts[:minutes].to_i
  seconds = "%02d" % duration.parts[:seconds].to_i

  @section_duration = [hours, minutes, seconds].compact.join(':')
end

#set_section_options(options) ⇒ Object



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

def set_section_options(options)
  @section_options = options
end

#styleObject



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

def style
  @current_segment.style
end

#to_hObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/gitlab/ci/ansi2json/line.rb', line 100

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
    result[:section_options] = @section_options if @section_options
  end
end

#update_style(ansi_commands) ⇒ Object



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

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