struct

CRT::Ansi::Style::Text

Inherits Struct < Value < Object

An array of styled text spans for rich text rendering.

Build from an array of parts — a mini-DSL for styled text:

bold = Style.new(bold: true)
red = Style.new(fg: Color.indexed(1))

text = Style::Text.new([
  "plain ", bold, "bold ", red, "bold+red",
  Style::POP, " bold again",
  Style::RESET, " back to default",
] of Style::Text::Part)

Part types:

  • String — text content, styled by the current stack top
  • Style — merge onto style stack (additive)
  • Style::Char — emit its char with its own style
  • Style::Text — splice its spans in verbatim
  • POP — pop the style stack
  • RESET — clear the stack back to default

Constructors

new(spans : Array(Span))
Source
new(parts : Array(Part), default : Style = Style.default)
Source

Instance methods

add(text : String, style : Style = Style.default) : self
Source
each_grapheme

Iterate over each grapheme with its width and style.

Source
empty?
Source
spans
Source
to_s(io : IO) : Nil

Plain text content (all spans concatenated).

Source
to_s

Returns a nicely readable and concise string representation of this object, typically intended for users.

This method should usually not be overridden. It delegates to #to_s(IO) which can be overridden for custom implementations.

Also see #inspect.

Source
width

Total display width of all spans.

Source

Nested types