Class: Shellfie::RenderSegment
- Inherits:
-
Object
- Object
- Shellfie::RenderSegment
- 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
-
#background ⇒ Object
readonly
Returns the value of attribute background.
-
#blink ⇒ Object
readonly
Returns the value of attribute blink.
-
#bold ⇒ Object
readonly
Returns the value of attribute bold.
-
#conceal ⇒ Object
readonly
Returns the value of attribute conceal.
-
#dim ⇒ Object
readonly
Returns the value of attribute dim.
-
#foreground ⇒ Object
readonly
Returns the value of attribute foreground.
-
#italic ⇒ Object
readonly
Returns the value of attribute italic.
-
#link ⇒ Object
readonly
Returns the value of attribute link.
-
#overline ⇒ Object
readonly
Returns the value of attribute overline.
-
#reverse ⇒ Object
readonly
Returns the value of attribute reverse.
-
#strikethrough ⇒ Object
readonly
Returns the value of attribute strikethrough.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#underline ⇒ Object
readonly
Returns the value of attribute underline.
-
#underline_color ⇒ Object
readonly
Returns the value of attribute underline_color.
-
#underline_style ⇒ Object
readonly
Returns the value of attribute underline_style.
Class Method Summary collapse
- .coalesce(segments) ⇒ Object
- .copy(segment, text) ⇒ Object
- .from_segment(segment, default_color:) ⇒ Object
Instance Method Summary collapse
-
#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
constructor
A new instance of RenderSegment.
- #same_style?(other) ⇒ Boolean
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
#background ⇒ Object (readonly)
Returns the value of attribute background.
9 10 11 |
# File 'lib/shellfie/render_segment.rb', line 9 def background @background end |
#blink ⇒ Object (readonly)
Returns the value of attribute blink.
9 10 11 |
# File 'lib/shellfie/render_segment.rb', line 9 def blink @blink end |
#bold ⇒ Object (readonly)
Returns the value of attribute bold.
9 10 11 |
# File 'lib/shellfie/render_segment.rb', line 9 def bold @bold end |
#conceal ⇒ Object (readonly)
Returns the value of attribute conceal.
9 10 11 |
# File 'lib/shellfie/render_segment.rb', line 9 def conceal @conceal end |
#dim ⇒ Object (readonly)
Returns the value of attribute dim.
9 10 11 |
# File 'lib/shellfie/render_segment.rb', line 9 def dim @dim end |
#foreground ⇒ Object (readonly)
Returns the value of attribute foreground.
9 10 11 |
# File 'lib/shellfie/render_segment.rb', line 9 def foreground @foreground end |
#italic ⇒ Object (readonly)
Returns the value of attribute italic.
9 10 11 |
# File 'lib/shellfie/render_segment.rb', line 9 def italic @italic end |
#link ⇒ Object (readonly)
Returns the value of attribute link.
9 10 11 |
# File 'lib/shellfie/render_segment.rb', line 9 def link @link end |
#overline ⇒ Object (readonly)
Returns the value of attribute overline.
9 10 11 |
# File 'lib/shellfie/render_segment.rb', line 9 def overline @overline end |
#reverse ⇒ Object (readonly)
Returns the value of attribute reverse.
9 10 11 |
# File 'lib/shellfie/render_segment.rb', line 9 def reverse @reverse end |
#strikethrough ⇒ Object (readonly)
Returns the value of attribute strikethrough.
9 10 11 |
# File 'lib/shellfie/render_segment.rb', line 9 def strikethrough @strikethrough end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
9 10 11 |
# File 'lib/shellfie/render_segment.rb', line 9 def text @text end |
#underline ⇒ Object (readonly)
Returns the value of attribute underline.
9 10 11 |
# File 'lib/shellfie/render_segment.rb', line 9 def underline @underline end |
#underline_color ⇒ Object (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_style ⇒ Object (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
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 |