Raxol.Terminal.Format (Raxol Terminal v2.6.0)

Copy Markdown View Source

Unified terminal text formatting and styling operations.

This module manages formatting state (bold, italic, colors, etc.) and provides functions for applying ANSI escape codes to text. It consolidates the previously separate FormattingManager and Formatting.Manager modules.

Usage

iex> format = Format.new()
iex> format = Format.set_foreground(format, 196)
iex> format = Format.toggle_bold(format)
iex> Format.apply_formatting(format, "Hello")
"Hello"

Summary

Types

Terminal text attributes

t()

Format state with current and saved formats

Functions

Applies a map of format updates to the current state.

Applies the current formatting to a string, wrapping it with ANSI escape codes.

Checks if the specified attribute is set.

Gets the current background color.

Gets the current foreground color.

Gets the current formatting state.

Returns a list of all attributes that are currently set to true.

Creates a new formatting state with default values.

Resets the specified attribute to false.

Resets the current format to default values.

Restores the previously saved format state.

Saves the current format state for later restoration.

Sets the specified attribute to true.

Sets the background color.

Sets the font number (0-9 for standard ANSI fonts).

Sets the foreground color.

Generates the ANSI escape sequence for the current format without text.

Toggles blink formatting.

Toggles bold formatting.

Toggles conceal formatting.

Toggles faint formatting.

Toggles italic formatting.

Toggles reverse video formatting.

Toggles strikethrough formatting.

Toggles underline formatting.

Types

format()

@type format() :: %{
  bold: boolean(),
  faint: boolean(),
  italic: boolean(),
  underline: boolean(),
  blink: boolean(),
  reverse: boolean(),
  conceal: boolean(),
  strikethrough: boolean(),
  foreground: term() | nil,
  background: term() | nil,
  font: non_neg_integer()
}

Terminal text attributes

t()

@type t() :: %Raxol.Terminal.Format{
  current_format: format(),
  saved_format: format() | nil
}

Format state with current and saved formats

Functions

apply_format(state, format)

@spec apply_format(t(), map()) :: t()

Applies a map of format updates to the current state.

apply_formatting(state, text)

@spec apply_formatting(t(), String.t()) :: String.t()

Applies the current formatting to a string, wrapping it with ANSI escape codes.

Each attribute that is enabled will add the appropriate SGR codes around the text.

attribute_set?(state, attribute)

@spec attribute_set?(t(), atom()) :: boolean()

Checks if the specified attribute is set.

get_background(state)

@spec get_background(t()) :: term() | nil

Gets the current background color.

get_foreground(state)

@spec get_foreground(t()) :: term() | nil

Gets the current foreground color.

get_format(state)

@spec get_format(t()) :: format()

Gets the current formatting state.

get_set_attributes(state)

@spec get_set_attributes(t()) :: [{atom(), true}]

Returns a list of all attributes that are currently set to true.

new()

@spec new() :: %Raxol.Terminal.Format{
  current_format: %{
    bold: false,
    faint: false,
    italic: false,
    underline: false,
    blink: false,
    reverse: false,
    conceal: false,
    strikethrough: false,
    foreground: nil,
    background: nil,
    font: 0
  },
  saved_format: nil
}

Creates a new formatting state with default values.

reset_attribute(state, attribute)

@spec reset_attribute(t(), atom()) :: t()

Resets the specified attribute to false.

reset_format(state)

@spec reset_format(t()) :: t()

Resets the current format to default values.

restore_format(state)

@spec restore_format(t()) :: t()

Restores the previously saved format state.

Returns unchanged state if no format was saved.

save_format(state)

@spec save_format(t()) :: t()

Saves the current format state for later restoration.

set_attribute(state, attribute)

@spec set_attribute(t(), atom()) :: t()

Sets the specified attribute to true.

set_background(state, color)

@spec set_background(t(), term() | nil) :: t()

Sets the background color.

Color can be an 8-bit color code (0-255) or nil for default.

set_font(state, font)

@spec set_font(t(), non_neg_integer()) :: t()

Sets the font number (0-9 for standard ANSI fonts).

set_foreground(state, color)

@spec set_foreground(t(), term() | nil) :: t()

Sets the foreground color.

Color can be an 8-bit color code (0-255) or nil for default.

to_ansi_sequences(state)

@spec to_ansi_sequences(t()) :: {String.t(), String.t()}

Generates the ANSI escape sequence for the current format without text.

Returns a tuple of {start_sequence, end_sequence} that can be used to wrap text.

toggle_blink(state)

@spec toggle_blink(t()) :: t()

Toggles blink formatting.

toggle_bold(state)

@spec toggle_bold(t()) :: t()

Toggles bold formatting.

toggle_conceal(state)

@spec toggle_conceal(t()) :: t()

Toggles conceal formatting.

toggle_faint(state)

@spec toggle_faint(t()) :: t()

Toggles faint formatting.

toggle_italic(state)

@spec toggle_italic(t()) :: t()

Toggles italic formatting.

toggle_reverse(state)

@spec toggle_reverse(t()) :: t()

Toggles reverse video formatting.

toggle_strikethrough(state)

@spec toggle_strikethrough(t()) :: t()

Toggles strikethrough formatting.

toggle_underline(state)

@spec toggle_underline(t()) :: t()

Toggles underline formatting.