Class: TuiTui::Line
- Inherits:
-
Object
- Object
- TuiTui::Line
- 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
-
#spans ⇒ Object
readonly
Returns the value of attribute spans.
Class Method Summary collapse
-
.[](*spans) ⇒ Object
Convenience constructor: Line[Span[“a”, s1], Span[“b”, s2]].
-
.coerce(content, style = nil) ⇒ Object
Coerce loose content into a Line: a Line passes through, a Span or an Array of Spans is wrapped, and anything else is one Span (in ‘style`, when given).
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#initialize(spans = []) ⇒ Line
constructor
A new instance of Line.
- #to_s ⇒ Object
-
#truncate(max, marker: "...") ⇒ Object
Truncate to ‘max` columns, keeping span styles.
- #width ⇒ Object
Constructor Details
#initialize(spans = []) ⇒ Line
Returns a new instance of Line.
24 25 26 |
# File 'lib/tui_tui/line.rb', line 24 def initialize(spans = []) @spans = spans end |
Instance Attribute Details
#spans ⇒ Object (readonly)
Returns the value of attribute spans.
28 29 30 |
# File 'lib/tui_tui/line.rb', line 28 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) |
.coerce(content, style = nil) ⇒ Object
Coerce loose content into a Line: a Line passes through, a Span or an Array of Spans is wrapped, and anything else is one Span (in ‘style`, when given).
15 16 17 18 19 20 21 22 |
# File 'lib/tui_tui/line.rb', line 15 def self.coerce(content, style = nil) case content when Line then content when Span then new([content]) when Array then new(content) else Line[Span[content.to_s, style]] end end |
Instance Method Details
#each(&block) ⇒ Object
31 |
# File 'lib/tui_tui/line.rb', line 31 def each(&block) = @spans.each(&block) |
#to_s ⇒ Object
32 |
# File 'lib/tui_tui/line.rb', line 32 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`.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tui_tui/line.rb', line 37 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 |
#width ⇒ Object
30 |
# File 'lib/tui_tui/line.rb', line 30 def width = @spans.sum(&:width) |