Class: RubyTUI::Span

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

Overview

A Span is a styled fragment of text. It is the atomic unit for rich text rendering.

Examples:

Span.new("error", Style.new.fg(Color::RED).bold)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, style = Style::DEFAULT) ⇒ Span

Returns a new instance of Span.

Parameters:

  • content (String) —

    text of the span

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

    style applied to the text (default: RubyTUI::Style::DEFAULT)



14
15
16
17
# File 'lib/rubytui/text/span.rb', line 14

def initialize(content, style = Style::DEFAULT)
  @content = content
  @style = style
end

Instance Attribute Details

#content ⇒ Object (readonly)

Returns the value of attribute content.



10
11
12
# File 'lib/rubytui/text/span.rb', line 10

def content
  @content
end

#style ⇒ Object (readonly)

Returns the value of attribute style.



10
11
12
# File 'lib/rubytui/text/span.rb', line 10

def style
  @style
end

Instance Method Details

#==(other) ⇒ Boolean

Value equality: same content and style.

Parameters:

  • other (Object) —

    object to compare

Returns:

  • (Boolean) —

    true if other is a Span with equal #content and #style



27
28
29
# File 'lib/rubytui/text/span.rb', line 27

def ==(other)
  other.is_a?(Span) && @content == other.content && @style == other.style
end

#to_s ⇒ String

Returns the unstyled text (#content).

Returns:

  • (String) —

    the unstyled text (#content)



32
# File 'lib/rubytui/text/span.rb', line 32

def to_s = @content

#width ⇒ Integer

Returns display width of #content in terminal columns (see Unicode.string_width).

Returns:



21
# File 'lib/rubytui/text/span.rb', line 21

def width = Unicode.string_width(@content)