Class: RubyTUI::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytui/text/text.rb

Overview

Text is a collection of Lines, representing multi-line styled text.

Examples:

Text.new("first\nsecond").height  # => 2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines = [], style = Style::DEFAULT) ⇒ Text

Returns a new instance of Text.

Parameters:

  • lines (String, Line, Array<Line, #to_s>) (defaults to: []) —

    content; a String is split on newlines into unstyled Lines (trailing empty lines are dropped), a single Line is wrapped in an Array, and Array elements that are not Lines become unstyled Lines of their to_s (default: empty)

  • style (Style) (defaults to: Style::DEFAULT) —

    text-level style, stored as #style (default: Style::DEFAULT)

Raises:

  • (ArgumentError) —

    if lines is not a String, Line or Array



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)

Parameters:

  • content (String) —

    text; see #initialize for the other accepted types

  • style (Style) (defaults to: Style::DEFAULT) —

    text-level style stored as #style; not applied to the spans (default: Style::DEFAULT)

Returns:

Raises:

  • (ArgumentError) —

    if content is not a String, Line or Array



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

Parameters:

  • lines (Array<Line>) —

    the lines

Returns:



43
44
45
# File 'lib/rubytui/text/text.rb', line 43

def self.from_lines(lines)
  new(lines)
end

.styled(content, style) ⇒ Text

Factory: styled single-line text

Parameters:

  • content (String) —

    text of the line (not split on newlines)

  • style (Style) —

    style of the single Span

Returns:

  • (Text) —

    a Text with one Line containing one Span



52
53
54
# File 'lib/rubytui/text/text.rb', line 52

def self.styled(content, style)
  new([Line.styled(content, style)])
end

Instance Method Details

#==(other) ⇒ Boolean

Value equality: same lines and style.

Parameters:

  • other (Object) —

    object to compare

Returns:

  • (Boolean) —

    true if other is a Text with equal #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.

Returns:

  • (Integer) —

    number of lines



57
# File 'lib/rubytui/text/text.rb', line 57

def height = @lines.length

#to_s ⇒ String

Plain text content (no styling).

Returns:

  • (String) —

    line texts joined with newlines



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

Returns:

  • (Integer) —

    largest Line#width in terminal columns (0 if there are no lines)



63
64
65
# File 'lib/rubytui/text/text.rb', line 63

def width
  @lines.map(&:width).max || 0
end