Alaja.Structures.ChunkText (Alaja v1.0.0)

Copy Markdown View Source

Minimal text chunk with formatting.

Represents a text fragment with its color and effects information. This is the basic composition unit for formatted text rendering.

Examples

iex> ChunkText.new("Hello world")
%ChunkText{text: "Hello world", color: nil, bg_color: nil, effects: nil}

iex> ChunkText.new("Hello", color: :red, effects: [:bold])
%ChunkText{text: "Hello", color: %ColorInfo{...}, effects: %EffectInfo{...}}

Summary

Functions

Combines two ChunkTexts (useful for concatenating formatted text).

Creates a new ChunkText.

Renders the ChunkText with ANSI codes.

Types

t()

@type t() :: %Alaja.Structures.ChunkText{
  bg_color: Pote.ColorInfo.t() | nil,
  color: Pote.ColorInfo.t() | nil,
  effects: Alaja.Structures.EffectInfo.t() | nil,
  text: String.t()
}

Functions

combine(chunk_text1, chunk_text2)

@spec combine(any(), any()) :: any()

Combines two ChunkTexts (useful for concatenating formatted text).

The second chunk's properties take precedence over the first's.

Examples

iex> c1 = ChunkText.new("Hello", color: :red)
iex> c2 = ChunkText.new(" world", color: :green)
iex> ChunkText.combine(c1, c2)
%ChunkText{text: "Hello world", color: %ColorInfo{...}}

new(text, opts \\ [])

@spec new(
  String.t(),
  keyword()
) :: t()

Creates a new ChunkText.

Parameters

  • text - The text to display
  • opts - Optional options:
    • :color - ColorInfo or convertible value (atom, hex, rgb, etc.)
    • :bg_color - Background ColorInfo or convertible value
    • :effects - EffectInfo or list of effects

Examples

iex> ChunkText.new("Hello")
%ChunkText{text: "Hello"}

iex> ChunkText.new("Hello", color: :red)
%ChunkText{text: "Hello", color: %ColorInfo{...}}

iex> ChunkText.new("Hello", color: "#FF0000", effects: [:bold, :underline])
%ChunkText{text: "Hello", color: %ColorInfo{...}, effects: %EffectInfo{...}}

render(chunk_text)

@spec render(t()) :: String.t()

Renders the ChunkText with ANSI codes.

Produces true-color (24-bit) ANSI sequences with effects. Always resets formatting after the text.

Examples

iex> ChunkText.render(ChunkText.new("Hello", color: :red, effects: [:bold]))
"\e[38;2;255;0;0m\e[1mHello\e[0m"