Class: Shellfie::RenderSegment

Inherits:
Object
  • Object
show all
Defined in:
lib/shellfie/render_segment.rb

Constant Summary collapse

ATTRIBUTES =
%i[
  foreground background bold italic underline dim reverse strikethrough overline
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text:, foreground: nil, background: nil, bold: false, italic: false, underline: false, dim: false, reverse: false, strikethrough: false, overline: false) ⇒ RenderSegment

Returns a new instance of RenderSegment.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shellfie/render_segment.rb', line 40

def initialize(text:, foreground: nil, background: nil, bold: false, italic: false, underline: false, dim: false,
               reverse: false, strikethrough: false, overline: false)
  @text = text
  @foreground = foreground
  @background = background
  @bold = bold
  @italic = italic
  @underline = underline
  @dim = dim
  @reverse = reverse
  @strikethrough = strikethrough
  @overline = overline
  freeze
end

Instance Attribute Details

#backgroundObject (readonly)

Returns the value of attribute background.



9
10
11
# File 'lib/shellfie/render_segment.rb', line 9

def background
  @background
end

#boldObject (readonly)

Returns the value of attribute bold.



9
10
11
# File 'lib/shellfie/render_segment.rb', line 9

def bold
  @bold
end

#dimObject (readonly)

Returns the value of attribute dim.



9
10
11
# File 'lib/shellfie/render_segment.rb', line 9

def dim
  @dim
end

#foregroundObject (readonly)

Returns the value of attribute foreground.



9
10
11
# File 'lib/shellfie/render_segment.rb', line 9

def foreground
  @foreground
end

#italicObject (readonly)

Returns the value of attribute italic.



9
10
11
# File 'lib/shellfie/render_segment.rb', line 9

def italic
  @italic
end

#overlineObject (readonly)

Returns the value of attribute overline.



9
10
11
# File 'lib/shellfie/render_segment.rb', line 9

def overline
  @overline
end

#reverseObject (readonly)

Returns the value of attribute reverse.



9
10
11
# File 'lib/shellfie/render_segment.rb', line 9

def reverse
  @reverse
end

#strikethroughObject (readonly)

Returns the value of attribute strikethrough.



9
10
11
# File 'lib/shellfie/render_segment.rb', line 9

def strikethrough
  @strikethrough
end

#textObject (readonly)

Returns the value of attribute text.



9
10
11
# File 'lib/shellfie/render_segment.rb', line 9

def text
  @text
end

#underlineObject (readonly)

Returns the value of attribute underline.



9
10
11
# File 'lib/shellfie/render_segment.rb', line 9

def underline
  @underline
end

Class Method Details

.coalesce(segments) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/shellfie/render_segment.rb', line 30

def self.coalesce(segments)
  segments.each_with_object([]) do |segment, result|
    if result.last&.same_style?(segment)
      result[-1] = copy(result.last, result.last.text + segment.text)
    else
      result << segment
    end
  end
end

.copy(segment, text) ⇒ Object



26
27
28
# File 'lib/shellfie/render_segment.rb', line 26

def self.copy(segment, text)
  new(**ATTRIBUTES.each_with_object(text: text.dup) { |attribute, values| values[attribute] = segment.public_send(attribute) })
end

.from_segment(segment, default_color:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/shellfie/render_segment.rb', line 11

def self.from_segment(segment, default_color:)
  new(
    text: segment.text.to_s,
    foreground: segment.foreground || default_color,
    background: segment.background,
    bold: segment.bold,
    italic: segment.italic,
    underline: segment.underline,
    dim: segment.dim,
    reverse: segment.reverse,
    strikethrough: segment.strikethrough,
    overline: segment.overline
  )
end

Instance Method Details

#same_style?(other) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/shellfie/render_segment.rb', line 55

def same_style?(other)
  ATTRIBUTES.all? { |attribute| public_send(attribute) == other.public_send(attribute) }
end