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 underline_style underline_color dim reverse strikethrough overline blink conceal link
].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, underline_style: nil, underline_color: nil, dim: false, reverse: false, strikethrough: false, overline: false, blink: false, conceal: false, link: nil) ⇒ RenderSegment

Returns a new instance of RenderSegment.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/shellfie/render_segment.rb', line 46

def initialize(text:, foreground: nil, background: nil, bold: false, italic: false, underline: false,
               underline_style: nil, underline_color: nil, dim: false, reverse: false, strikethrough: false,
               overline: false, blink: false, conceal: false, link: nil)
  @text = text
  @foreground = foreground
  @background = background
  @bold = bold
  @italic = italic
  @underline = underline
  @underline_style = underline_style
  @underline_color = underline_color
  @dim = dim
  @reverse = reverse
  @strikethrough = strikethrough
  @overline = overline
  @blink = blink
  @conceal = conceal
  @link = link
  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

Returns the value of attribute blink.



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

def blink
  @blink
end

#boldObject (readonly)

Returns the value of attribute bold.



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

def bold
  @bold
end

#concealObject (readonly)

Returns the value of attribute conceal.



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

def conceal
  @conceal
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

Returns the value of attribute link.



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

def link
  @link
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

#underline_colorObject (readonly)

Returns the value of attribute underline_color.



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

def underline_color
  @underline_color
end

#underline_styleObject (readonly)

Returns the value of attribute underline_style.



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

def underline_style
  @underline_style
end

Class Method Details

.coalesce(segments) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/shellfie/render_segment.rb', line 36

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



32
33
34
# File 'lib/shellfie/render_segment.rb', line 32

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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/shellfie/render_segment.rb', line 12

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,
    underline_style: segment.underline_style,
    underline_color: segment.underline_color,
    dim: segment.dim,
    reverse: segment.reverse,
    strikethrough: segment.strikethrough,
    overline: segment.overline,
    blink: segment.blink,
    conceal: segment.conceal,
    link: segment.link
  )
end

Instance Method Details

#same_style?(other) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/shellfie/render_segment.rb', line 67

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