Alaja.Structures.EffectInfo (Alaja v1.0.0)

Copy Markdown View Source

Structure that defines text effects applicable in terminal.

Represents a collection of visual effects (bold, italic, underline, etc.) that can be applied to text when rendering to terminal.

Examples

iex> EffectInfo.new(:bold)
%EffectInfo{bold: true, dim: false, ...}

iex> EffectInfo.new([:bold, :underline])
%EffectInfo{bold: true, underline: true, ...}

iex> EffectInfo.to_ansi(EffectInfo.new([:bold, :italic]))
"\e[1;3m"

Summary

Functions

Combines two EffectInfo structures.

Creates a new empty EffectInfo structure.

Creates a new EffectInfo from various inputs.

Calculates the optimal foreground color for a given background color.

Converts EffectInfo to ANSI escape codes.

Types

t()

@type t() :: %Alaja.Structures.EffectInfo{
  blink: boolean(),
  bold: boolean(),
  dim: boolean(),
  hidden: boolean(),
  invert: boolean(),
  italic: boolean(),
  link: String.t() | nil,
  reverse: boolean(),
  strikethrough: boolean(),
  underline: boolean()
}

Functions

combine(e1, e2)

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

Combines two EffectInfo structures.

Effects are combined using OR logic - if either has an effect enabled, the result will have it enabled.

Examples

iex> e1 = EffectInfo.new(:bold)
iex> e2 = EffectInfo.new(:italic)
iex> EffectInfo.combine(e1, e2)
%EffectInfo{bold: true, italic: true, ...}

new()

@spec new() :: t()

Creates a new empty EffectInfo structure.

Examples

iex> EffectInfo.new()
%EffectInfo{bold: false, dim: false, ...}

new(effect)

@spec new(atom() | [atom()] | String.t() | t() | map()) :: t()

Creates a new EffectInfo from various inputs.

Parameters

  • effect - Can be:
    • An atom (:bold, :italic, etc.)
    • A list of atoms
    • A URL string (creates link effect)
    • An existing EffectInfo struct
    • A map with effect fields

Examples

iex> EffectInfo.new(:bold)
%EffectInfo{bold: true, ...}

iex> EffectInfo.new([:bold, :underline])
%EffectInfo{bold: true, underline: true, ...}

iex> EffectInfo.new("https://example.com")
%EffectInfo{link: "https://example.com"}

optimal_fg_color(arg)

@spec optimal_fg_color({integer(), integer(), integer()}) ::
  {integer(), integer(), integer()}

Calculates the optimal foreground color for a given background color.

Uses brightness (luminance) to determine whether to use white or black for best contrast.

Examples

iex> EffectInfo.optimal_fg_color({255, 255, 255})
{0, 0, 0}

iex> EffectInfo.optimal_fg_color({0, 0, 0})
{255, 255, 255}

to_ansi(ei)

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

Converts EffectInfo to ANSI escape codes.

Examples

iex> EffectInfo.to_ansi(EffectInfo.new([:bold, :underline]))
"\e[1;4m"