Class: TuiTui::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/tui_tui/line.rb

Overview

An ordered list of styled Spans. Width-aware truncation preserves each span’s style.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spans = []) ⇒ Line

Returns a new instance of Line.



13
14
15
# File 'lib/tui_tui/line.rb', line 13

def initialize(spans = [])
  @spans = spans
end

Instance Attribute Details

#spansObject (readonly)

Returns the value of attribute spans.



17
18
19
# File 'lib/tui_tui/line.rb', line 17

def spans
  @spans
end

Class Method Details

.[](*spans) ⇒ Object

Convenience constructor: Line[Span[“a”, s1], Span[“b”, s2]].



11
# File 'lib/tui_tui/line.rb', line 11

def self.[](*spans) = new(spans)

Instance Method Details

#each(&block) ⇒ Object



20
# File 'lib/tui_tui/line.rb', line 20

def each(&block) = @spans.each(&block)

#to_sObject



21
# File 'lib/tui_tui/line.rb', line 21

def to_s = @spans.map(&:text).join

#truncate(max, marker: "...") ⇒ Object

Truncate to ‘max` columns, keeping span styles. When content is dropped, `marker` is appended in the style of the span it cut into; the marker’s own width is reserved so the result never exceeds ‘max`.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tui_tui/line.rb', line 26

def truncate(max, marker: "...")
  return self if width <= max

  budget = max - DisplayText.new(marker).width
  if budget <= 0
    clipped = DisplayText.new(marker).truncate(max, marker: "").to_s
    return self.class.new([Span[clipped, @spans.first&.style]])
  end

  take_until(budget, marker)
end

#widthObject



19
# File 'lib/tui_tui/line.rb', line 19

def width = @spans.sum(&:width)