Raxol.Protocols.Styleable protocol (Raxol v2.0.1)
View SourceProtocol 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
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
@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() }
@type t() :: term()
All the types that implement this protocol.
Functions
Applies a style to the data structure.
Parameters
data- The data structure to stylestyle- The style map to apply
Returns
The data structure with the style applied.
Gets the current style of the data structure.
Returns
The current style map, or an empty map if no style is set.
Merges new styles with existing styles.
New styles override existing ones for the same keys.
Parameters
data- The data structure with existing stylesnew_style- The new style map to merge
Returns
The data structure with merged styles.
Resets all styles to default.
Returns
The data structure with all styles removed.
Converts the style to ANSI escape codes.
Returns
A string containing the ANSI escape codes for the style.