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

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

Overview

Line::Segment is a portion of a line that has its own style and text. Multiple segments make the line content.

Constant Summary

Constants included from EncodingHelper

EncodingHelper::ENCODING_CONFIDENCE_THRESHOLD, EncodingHelper::ESCAPED_CHARS, EncodingHelper::UNICODE_REPLACEMENT_CHARACTER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EncodingHelper

#binary_io, #detect_binary?, #detect_encoding, #detect_libgit2_binary?, #encode!, #encode_binary, #encode_utf8, #encode_utf8_no_detect, #encode_utf8_with_replacement_character, #unquote_path

Constructor Details

#initialize(style:) ⇒ Segment

Returns a new instance of Segment.


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

def initialize(style:)
  @text = +''
  @style = style
end

Instance Attribute Details

#styleObject

Returns the value of attribute style.


14
15
16
# File 'lib/gitlab/ci/ansi2json/line.rb', line 14

def style
  @style
end

#textObject

Returns the value of attribute text.


14
15
16
# File 'lib/gitlab/ci/ansi2json/line.rb', line 14

def text
  @text
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)

21
22
23
# File 'lib/gitlab/ci/ansi2json/line.rb', line 21

def empty?
  text.empty?
end

#to_hObject


25
26
27
28
29
30
31
32
33
34
# File 'lib/gitlab/ci/ansi2json/line.rb', line 25

def to_h
  # Without forcing the encoding to UTF-8 and then replacing
  # invalid UTF-8 sequences we can get an error when serializing
  # the Hash to JSON.
  # Encoding::UndefinedConversionError:
  #   "\xE2" from ASCII-8BIT to UTF-8
  { text: encode_utf8_no_detect(text) }.tap do |result|
    result[:style] = style.to_s if style.set?
  end
end