TermUI.Renderer.Style (TermUI v0.2.0)
View SourceRepresents visual styling for text and cells.
Styles encapsulate colors and text attributes, providing a fluent builder API and support for style merging (cascading). Styles can be converted to cells for rendering.
Fluent Builder API
Style.new()
|> Style.fg(:red)
|> Style.bg(:black)
|> Style.bold()
|> Style.underline()Style Merging
Styles can be merged with later styles overriding earlier values:
base = Style.new() |> Style.fg(:white)
override = Style.new() |> Style.fg(:red) |> Style.bold()
merged = Style.merge(base, override)
# fg: :red, attrs: [:bold]
Summary
Functions
Adds an attribute to the style.
Applies a style to an existing cell, returning a new cell.
Sets the background color.
Adds the blink attribute.
Adds the bold attribute.
Adds the dim attribute.
Checks if the style has any properties set.
Checks if two styles are visually equal.
Sets the foreground color.
Adds the hidden attribute.
Adds the italic attribute.
Merges two styles, with the second style overriding the first.
Creates a new empty style.
Creates a style with initial values.
Removes an attribute from the style.
Resets style to default (empty).
Adds the reverse attribute.
Adds the strikethrough attribute.
Converts a style to a cell with the given character.
Adds the underline attribute.
Types
Functions
Adds an attribute to the style.
@spec apply_to_cell(t(), TermUI.Renderer.Cell.t()) :: TermUI.Renderer.Cell.t()
Applies a style to an existing cell, returning a new cell.
The style's values override the cell's values where set.
Examples
iex> cell = Cell.new("A", fg: :white)
iex> style = Style.new() |> Style.fg(:red)
iex> new_cell = Style.apply_to_cell(style, cell)
iex> new_cell.fg
:red
Sets the background color.
Examples
iex> Style.new() |> Style.bg(:blue)
%Style{fg: nil, bg: :blue, attrs: MapSet.new()}
Adds the blink attribute.
Adds the bold attribute.
Adds the dim attribute.
Checks if the style has any properties set.
Checks if two styles are visually equal.
Compares foreground color, background color, and all attributes.
Examples
iex> s1 = Style.new(fg: :red, attrs: [:bold])
iex> s2 = Style.new(fg: :red, attrs: [:bold])
iex> Style.equal?(s1, s2)
true
Sets the foreground color.
Examples
iex> Style.new() |> Style.fg(:red)
%Style{fg: :red, bg: nil, attrs: MapSet.new()}
Adds the italic attribute.
Merges two styles, with the second style overriding the first.
Only non-nil values from the override style replace values in the base. Attributes are combined (union of both sets).
Examples
iex> base = Style.new(fg: :white, bg: :black)
iex> override = Style.new(fg: :red, attrs: [:bold])
iex> merged = Style.merge(base, override)
iex> merged.fg
:red
iex> merged.bg
:black
iex> :bold in merged.attrs
true
@spec new() :: t()
Creates a new empty style.
Examples
iex> Style.new()
%Style{fg: nil, bg: nil, attrs: MapSet.new()}
Creates a style with initial values.
Examples
iex> Style.new(fg: :red, attrs: [:bold])
%Style{fg: :red, bg: nil, attrs: MapSet.new([:bold])}
Removes an attribute from the style.
Resets style to default (empty).
Adds the reverse attribute.
Adds the strikethrough attribute.
@spec to_cell(t(), String.t()) :: TermUI.Renderer.Cell.t()
Converts a style to a cell with the given character.
Applies the style's colors and attributes to create a new cell.
Uses :default for any unset colors.
Examples
iex> style = Style.new() |> Style.fg(:red) |> Style.bold()
iex> cell = Style.to_cell(style, "X")
iex> cell.char
"X"
iex> cell.fg
:red
iex> cell.bg
:default
Adds the underline attribute.