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 dim reverse strikethrough overline ].freeze
Instance Attribute Summary collapse
-
#background ⇒ Object
readonly
Returns the value of attribute background.
-
#bold ⇒ Object
readonly
Returns the value of attribute bold.
-
#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.
-
#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.
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, dim: false, reverse: false, strikethrough: false, overline: false) ⇒ 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, 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
#background ⇒ Object (readonly)
Returns the value of attribute background.
9 10 11 |
# File 'lib/shellfie/render_segment.rb', line 9 def background @background 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 |
#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 |
#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 |
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
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 |