TermUI.Renderer.Style (TermUI v0.2.0)

View Source

Represents 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

attribute()

@type attribute() :: TermUI.Renderer.Cell.attribute()

color()

@type color() :: TermUI.Renderer.Cell.color()

t()

@type t() :: %TermUI.Renderer.Style{
  attrs: MapSet.t(attribute()),
  bg: color() | nil,
  fg: color() | nil
}

Functions

add_attr(style, attr)

@spec add_attr(t(), attribute()) :: t()

Adds an attribute to the style.

apply_to_cell(style, cell)

@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

bg(style, color)

@spec bg(t(), color()) :: t()

Sets the background color.

Examples

iex> Style.new() |> Style.bg(:blue)
%Style{fg: nil, bg: :blue, attrs: MapSet.new()}

blink(style)

@spec blink(t()) :: t()

Adds the blink attribute.

bold(style)

@spec bold(t()) :: t()

Adds the bold attribute.

dim(style)

@spec dim(t()) :: t()

Adds the dim attribute.

empty?(style)

@spec empty?(t()) :: boolean()

Checks if the style has any properties set.

equal?(a, b)

@spec equal?(t(), t()) :: boolean()

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

fg(style, color)

@spec fg(t(), color()) :: t()

Sets the foreground color.

Examples

iex> Style.new() |> Style.fg(:red)
%Style{fg: :red, bg: nil, attrs: MapSet.new()}

hidden(style)

@spec hidden(t()) :: t()

Adds the hidden attribute.

italic(style)

@spec italic(t()) :: t()

Adds the italic attribute.

merge(base, override)

@spec merge(t(), t()) :: t()

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

new()

@spec new() :: t()

Creates a new empty style.

Examples

iex> Style.new()
%Style{fg: nil, bg: nil, attrs: MapSet.new()}

new(opts)

@spec new(keyword()) :: t()

Creates a style with initial values.

Examples

iex> Style.new(fg: :red, attrs: [:bold])
%Style{fg: :red, bg: nil, attrs: MapSet.new([:bold])}

remove_attr(style, attr)

@spec remove_attr(t(), attribute()) :: t()

Removes an attribute from the style.

reset(style)

@spec reset(t()) :: t()

Resets style to default (empty).

reverse(style)

@spec reverse(t()) :: t()

Adds the reverse attribute.

strikethrough(style)

@spec strikethrough(t()) :: t()

Adds the strikethrough attribute.

to_cell(style, char)

@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

underline(style)

@spec underline(t()) :: t()

Adds the underline attribute.