TermUI.SGR (TermUI v1.0.0)
View SourceSGR (Select Graphic Rendition) sequence generation for terminal styling.
This module provides centralized generation of SGR parameters and sequences for terminal text styling, including colors and text attributes.
Overview
SGR sequences control text appearance (colors, bold, italic, etc.) in terminals.
They follow the format ESC[<params>m where params are semicolon-separated numbers.
Two Modes of Operation
Parameter mode - Returns parameter strings for combining into sequences
- Use when building combined sequences like
ESC[1;31;4m - Functions:
color_param/2,attr_param/1
- Use when building combined sequences like
Sequence mode - Returns complete escape sequences
- Use for direct terminal output
- Functions:
color_sequence/2,attr_sequence/1
Color Types
- Named colors:
:red,:green,:blue,:cyan,:magenta,:yellow,:black,:white - Bright variants:
:bright_red,:bright_green, etc. - 256-color palette: Integer 0-255
- True color RGB:
{r, g, b}tuple - Default:
:defaultto reset to terminal default
Attributes
Supported: :bold, :dim, :italic, :underline, :blink, :reverse,
:hidden, :strikethrough
Examples
# Parameter mode for combining
iex> SGR.color_param(:fg, :red)
"31"
iex> SGR.color_param(:fg, {255, 128, 0})
"38;2;255;128;0"
iex> SGR.attr_param(:bold)
"1"
# Building combined sequence
iex> params = [SGR.attr_param(:bold), SGR.color_param(:fg, :red)]
iex> SGR.build_sequence(params)
["\e[", ["1", ";", "31"], "m"]
# Sequence mode for direct output
iex> SGR.color_sequence(:fg, :red) |> IO.iodata_to_binary()
"\e[31m"
iex> SGR.attr_sequence(:bold) |> IO.iodata_to_binary()
"\e[1m"
Summary
Functions
Returns SGR parameter string to turn off an attribute.
Returns SGR parameter string for an attribute.
Returns complete SGR escape sequence for an attribute.
Builds a combined SGR sequence from a list of parameters.
Returns SGR parameter string for a color.
Returns complete SGR escape sequence for a color.
Returns all supported named colors.
Returns SGR reset sequence.
Returns all supported attributes.
Checks if an attribute is valid.
Checks if a color value is valid.
Functions
Returns SGR parameter string to turn off an attribute.
Used when removing specific attributes without full reset.
Examples
iex> SGR.attr_off_param(:bold)
"22"
iex> SGR.attr_off_param(:underline)
"24"
Returns SGR parameter string for an attribute.
Used when building combined sequences.
Examples
iex> SGR.attr_param(:bold)
"1"
iex> SGR.attr_param(:underline)
"4"
Returns complete SGR escape sequence for an attribute.
Used for direct terminal output.
Examples
iex> SGR.attr_sequence(:bold) |> IO.iodata_to_binary()
"\e[1m"
Builds a combined SGR sequence from a list of parameters.
Examples
iex> SGR.build_sequence(["1", "31"])
["\e[", ["1", ";", "31"], "m"]
iex> SGR.build_sequence([])
[]
Returns SGR parameter string for a color.
Used when building combined sequences like ESC[1;31;4m.
Examples
iex> SGR.color_param(:fg, :red)
"31"
iex> SGR.color_param(:bg, :blue)
"44"
iex> SGR.color_param(:fg, 196)
"38;5;196"
iex> SGR.color_param(:bg, {0, 255, 128})
"48;2;0;255;128"
Returns complete SGR escape sequence for a color.
Used for direct terminal output.
Examples
iex> SGR.color_sequence(:fg, :red) |> IO.iodata_to_binary()
"\e[31m"
iex> SGR.color_sequence(:fg, :default) |> IO.iodata_to_binary()
"\e[39m"
@spec named_colors() :: [atom()]
Returns all supported named colors.
@spec reset() :: iolist()
Returns SGR reset sequence.
Resets all attributes and colors to terminal defaults.
@spec supported_attrs() :: [atom()]
Returns all supported attributes.
Checks if an attribute is valid.
Checks if a color value is valid.