TermUI.Style (TermUI v1.0.0)

View Source

Style system for consistent visual presentation.

Styles define colors, text attributes, and visual properties for components. Styles are immutable—modifications return new styles.

This is an older standalone style vocabulary retained for compatibility. The integrated 1.0 render tree, themes, and backends use TermUI.Renderer.Style. The two structs are not interchangeable: this module uses tagged {:indexed, n} and {:rgb, r, g, b} colors, while the renderer style uses integer palette indexes and untagged {r, g, b} tuples.

Color Types

  • Named colors: :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white
  • Bright variants: :bright_black, :bright_red, etc.
  • Indexed (256): {:indexed, 0..255}
  • RGB (true color): {:rgb, r, g, b}

Examples

# Build a style
style = Style.new()
  |> Style.fg(:blue)
  |> Style.bg(:white)
  |> Style.bold()
  |> Style.underline()

# Merge styles
merged = Style.merge(base, override)

# Inherit from parent
effective = Style.inherit(child, parent)

# Variants
variants = %{
  normal: Style.new() |> Style.fg(:white),
  focused: Style.new() |> Style.fg(:blue) |> Style.bold()
}
style = Style.get_variant(variants, :focused)

Summary

Functions

Sets the background color.

Adds blink attribute.

Adds bold attribute.

Builds a complete variant map from partial definitions.

Clears all attributes.

Converts color for a specific terminal capability.

Creates a variant that inherits from the normal variant.

Adds dim attribute.

Sets the foreground color.

Creates a style from a keyword list or map.

Gets a variant style from a variant map.

Checks if style has an attribute.

Adds hidden attribute.

Inherits unset properties from parent style.

Adds italic attribute.

Merges two styles, with the second overriding the first.

Creates a new style with default values.

Removes an attribute from the style.

Resets style to defaults, breaking inheritance.

Adds reverse attribute.

Converts RGB to nearest 256-color palette index.

Returns a semantic color.

Adds strikethrough attribute.

Converts any color to nearest 16-color.

Converts any color to RGB tuple.

Adds underline attribute.

Types

attr()

@type attr() ::
  :bold
  | :dim
  | :italic
  | :underline
  | :blink
  | :reverse
  | :hidden
  | :strikethrough

color()

@type color() :: named_color() | indexed_color() | rgb_color()

indexed_color()

@type indexed_color() :: {:indexed, 0..255}

named_color()

@type named_color() ::
  :black
  | :red
  | :green
  | :yellow
  | :blue
  | :magenta
  | :cyan
  | :white
  | :bright_black
  | :bright_red
  | :bright_green
  | :bright_yellow
  | :bright_blue
  | :bright_magenta
  | :bright_cyan
  | :bright_white
  | :default

rgb_color()

@type rgb_color() :: {:rgb, 0..255, 0..255, 0..255}

t()

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

Functions

bg(style, color)

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

Sets the background color.

blink(style)

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

Adds blink attribute.

bold(style)

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

Adds bold attribute.

build_variants(variants)

@spec build_variants(map()) :: map()

Builds a complete variant map from partial definitions.

Each variant inherits from :normal.

clear_attrs(style)

@spec clear_attrs(t()) :: t()

Clears all attributes.

convert_for_terminal(color, atom)

@spec convert_for_terminal(color(), :true_color | :color_256 | :color_16) :: color()

Converts color for a specific terminal capability.

  • :true_color - returns as-is
  • :color_256 - converts to indexed
  • :color_16 - converts to named

create_variant(normal, variant)

@spec create_variant(t(), t()) :: t()

Creates a variant that inherits from the normal variant.

Only non-nil values in the variant override the normal style.

dim(style)

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

Adds dim attribute.

fg(style, color)

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

Sets the foreground color.

from(opts)

@spec from(keyword() | map()) :: t()

Creates a style from a keyword list or map.

Examples

Style.from(fg: :blue, bg: :white, bold: true)

get_variant(variants, state)

@spec get_variant(map(), atom()) :: t()

Gets a variant style from a variant map.

Falls back to :normal if variant not found.

has_attr?(style, attr)

@spec has_attr?(t(), attr()) :: boolean()

Checks if style has an attribute.

hidden(style)

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

Adds hidden attribute.

inherit(child, parent)

@spec inherit(t(), t()) :: t()

Inherits unset properties from parent style.

Unlike merge, this only fills in nil values from parent.

italic(style)

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

Adds italic attribute.

merge(base, override)

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

Merges two styles, with the second overriding the first.

Only non-nil values from the override style replace base values. Attributes are combined.

new()

@spec new() :: t()

Creates a new style with default values.

remove_attr(style, attr)

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

Removes an attribute from the style.

reset(style)

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

Resets style to defaults, breaking inheritance.

reverse(style)

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

Adds reverse attribute.

rgb_to_indexed(arg)

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

Converts RGB to nearest 256-color palette index.

semantic(arg1)

@spec semantic(atom()) :: named_color()

Returns a semantic color.

These can be overridden by themes.

strikethrough(style)

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

Adds strikethrough attribute.

to_named(color)

@spec to_named(color()) :: named_color()

Converts any color to nearest 16-color.

to_rgb(named)

@spec to_rgb(color()) :: {integer(), integer(), integer()}

Converts any color to RGB tuple.

underline(style)

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

Adds underline attribute.