Class: RubyTUI::Text
- Inherits:
-
Object
- Object
- RubyTUI::Text
- Defined in:
- lib/rubytui/text/text.rb
Overview
Text is a collection of Lines, representing multi-line styled text.
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#style ⇒ Object
readonly
Returns the value of attribute style.
Class Method Summary collapse
-
.from(content, style = Style::DEFAULT) ⇒ Text
Factory: from a plain string (splits on newlines).
-
.from_lines(lines) ⇒ Text
Factory: from an array of Lines.
-
.styled(content, style) ⇒ Text
Factory: styled single-line text.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Value equality: same lines and style.
-
#height ⇒ Integer
Number of lines.
-
#initialize(lines = [], style = Style::DEFAULT) ⇒ Text
constructor
A new instance of Text.
-
#to_s ⇒ String
Plain text content (no styling).
-
#width ⇒ Integer
Maximum width across all lines.
Constructor Details
#initialize(lines = [], style = Style::DEFAULT) ⇒ Text
Returns a new instance of Text.
18 19 20 21 22 23 24 25 26 |
# File 'lib/rubytui/text/text.rb', line 18 def initialize(lines = [], style = Style::DEFAULT) @lines = case lines when String then lines.split("\n").map { |l| Line.new(l) } when Line then [lines] when Array then lines.map { |l| l.is_a?(Line) ? l : Line.new(l.to_s) } else raise ArgumentError, "Text expects String, Line, or Array, got #{lines.class}" end @style = style end |
Instance Attribute Details
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
9 10 11 |
# File 'lib/rubytui/text/text.rb', line 9 def lines @lines end |
#style ⇒ Object (readonly)
Returns the value of attribute style.
9 10 11 |
# File 'lib/rubytui/text/text.rb', line 9 def style @style end |
Class Method Details
.from(content, style = Style::DEFAULT) ⇒ Text
Factory: from a plain string (splits on newlines)
35 36 37 |
# File 'lib/rubytui/text/text.rb', line 35 def self.from(content, style = Style::DEFAULT) new(content, style) end |
.from_lines(lines) ⇒ Text
Factory: from an array of Lines
43 44 45 |
# File 'lib/rubytui/text/text.rb', line 43 def self.from_lines(lines) new(lines) end |
Instance Method Details
#==(other) ⇒ Boolean
Value equality: same lines and style.
78 79 80 |
# File 'lib/rubytui/text/text.rb', line 78 def ==(other) other.is_a?(Text) && @lines == other.lines && @style == other.style end |
#height ⇒ Integer
Returns number of lines.
57 |
# File 'lib/rubytui/text/text.rb', line 57 def height = @lines.length |
#to_s ⇒ String
Plain text content (no styling).
70 71 72 |
# File 'lib/rubytui/text/text.rb', line 70 def to_s @lines.map(&:to_s).join("\n") end |
#width ⇒ Integer
Maximum width across all lines
63 64 65 |
# File 'lib/rubytui/text/text.rb', line 63 def width @lines.map(&:width).max || 0 end |