Raxol.Protocols.Styleable protocol (Raxol v2.0.1)

View Source

Protocol for applying styles to data structures.

This protocol provides a unified interface for applying visual styles (colors, formatting, effects) to different types of data in the terminal.

Style Attributes

Styles are represented as maps with the following optional keys:

  • :foreground - Foreground color (RGB tuple or color name)
  • :background - Background color (RGB tuple or color name)
  • :bold - Bold text (boolean)
  • :italic - Italic text (boolean)
  • :underline - Underlined text (boolean)
  • :blink - Blinking text (boolean)
  • :reverse - Reverse video (boolean)
  • :hidden - Hidden/invisible text (boolean)
  • :strikethrough - Strikethrough text (boolean)

Examples

defimpl Raxol.Protocols.Styleable, for: MyComponent do
  def apply_style(component, style) do
    %{component | style: merge_styles(component.style, style)}
  end

  def get_style(component) do
    component.style || %{}
  end

  def merge_styles(component, new_style) do
    %{component | style: Map.merge(get_style(component), new_style)}
  end

  def reset_style(component) do
    %{component | style: %{}}
  end
end

Summary

Types

t()

All the types that implement this protocol.

Functions

Applies a style to the data structure.

Gets the current style of the data structure.

Merges new styles with existing styles.

Resets all styles to default.

Converts the style to ANSI escape codes.

Types

style()

@type style() :: %{
  optional(:foreground) =>
    {non_neg_integer(), non_neg_integer(), non_neg_integer()} | atom(),
  optional(:background) =>
    {non_neg_integer(), non_neg_integer(), non_neg_integer()} | atom(),
  optional(:bold) => boolean(),
  optional(:italic) => boolean(),
  optional(:underline) => boolean(),
  optional(:blink) => boolean(),
  optional(:reverse) => boolean(),
  optional(:hidden) => boolean(),
  optional(:strikethrough) => boolean()
}

t()

@type t() :: term()

All the types that implement this protocol.

Functions

apply_style(data, style)

@spec apply_style(t(), style()) :: t()

Applies a style to the data structure.

Parameters

  • data - The data structure to style
  • style - The style map to apply

Returns

The data structure with the style applied.

get_style(data)

@spec get_style(t()) :: style()

Gets the current style of the data structure.

Returns

The current style map, or an empty map if no style is set.

merge_styles(data, new_style)

@spec merge_styles(t(), style()) :: t()

Merges new styles with existing styles.

New styles override existing ones for the same keys.

Parameters

  • data - The data structure with existing styles
  • new_style - The new style map to merge

Returns

The data structure with merged styles.

reset_style(data)

@spec reset_style(t()) :: t()

Resets all styles to default.

Returns

The data structure with all styles removed.

to_ansi(data)

@spec to_ansi(t()) :: binary()

Converts the style to ANSI escape codes.

Returns

A string containing the ANSI escape codes for the style.