View Source IO.ANSI (Elixir v1.14.4)

Functionality to render ANSI escape sequences.

ANSI escape sequences are characters embedded in text used to control formatting, color, and other output options on video text terminals.

ANSI escapes are typically enabled on all Unix terminals. They are also available on Windows consoles from Windows 10, although it must be explicitly enabled for the current user in the registry by running the following command:

reg add HKCU\Console /v VirtualTerminalLevel /t REG_DWORD /d 1

After running the command above, you must restart your current console.

Examples

Because the ANSI escape sequences are embedded in text, the normal usage of these functions is to concatenate their output with text.

formatted_text = IO.ANSI.blue_background() <> "Example" <> IO.ANSI.reset()
IO.puts(formatted_text)

A higher level and more convenient API is also available via IO.ANSI.format/1, where you use atoms to represent each ANSI escape sequence and by default checks if ANSI is enabled:

IO.puts(IO.ANSI.format([:blue_background, "Example"]))

In case ANSI is disabled, the ANSI escape sequences are simply discarded.

Link to this section Summary

Functions

Sets foreground color to black.

Sets background color to black.

Blink: off.

Blink: rapid. MS-DOS ANSI.SYS; 150 per minute or more; not widely supported.

Blink: slow. Less than 150 per minute.

Sets foreground color to blue.

Sets background color to blue.

Bright (increased intensity) or bold.

Clears screen.

Clears line.

Sets foreground color.

Sets the foreground color from individual RGB values.

Sets background color.

Sets the background color from individual RGB values.

Conceal. Not widely supported.

Crossed-out. Characters legible, but marked for deletion. Not widely supported.

Sends cursor to the absolute position specified by line and column.

Sends cursor lines down.

Sends cursor columns to the left.

Sends cursor columns to the right.

Sends cursor lines up.

Sets foreground color to cyan.

Sets background color to cyan.

Default background color.

Default text color.

Checks if ANSI coloring is supported and enabled on this machine.

Encircled.

Faint (decreased intensity). Not widely supported.

Sets alternative font 1.

Sets alternative font 2.

Sets alternative font 3.

Sets alternative font 4.

Sets alternative font 5.

Sets alternative font 6.

Sets alternative font 7.

Sets alternative font 8.

Sets alternative font 9.

Formats a chardata-like argument by converting named ANSI sequences into actual ANSI codes.

Formats a chardata-like argument by converting named ANSI sequences into actual ANSI codes.

Framed.

Sets foreground color to green.

Sets background color to green.

Sends cursor home.

Image: negative. Swap foreground and background.

Image: positive. Normal foreground and background.

Italic: on. Not widely supported. Sometimes treated as inverse.

Sets foreground color to light black.

Sets background color to light black.

Sets foreground color to light blue.

Sets background color to light blue.

Sets foreground color to light cyan.

Sets background color to light cyan.

Sets foreground color to light green.

Sets background color to light green.

Sets foreground color to light magenta.

Sets background color to light magenta.

Sets foreground color to light red.

Sets background color to light red.

Sets foreground color to light white.

Sets background color to light white.

Sets foreground color to light yellow.

Sets background color to light yellow.

Sets foreground color to magenta.

Sets background color to magenta.

Underline: none.

Normal color or intensity.

Not framed or encircled.

Not italic.

Not overlined.

Overlined.

Sets primary (default) font.

Sets foreground color to red.

Sets background color to red.

Resets all attributes.

Image: negative. Swap foreground and background.

Image: positive. Normal foreground and background.

Syntax colors to be used by Inspect.

Underline: single.

Sets foreground color to white.

Sets background color to white.

Sets foreground color to yellow.

Sets background color to yellow.

Link to this section Types

@type ansicode() :: atom()
@type ansidata() :: ansilist() | ansicode() | binary()
@type ansilist() ::
  maybe_improper_list(
    char() | ansicode() | binary() | ansilist(),
    binary() | ansicode() | []
  )

Link to this section Functions

@spec black() :: String.t()

Sets foreground color to black.

@spec black_background() :: String.t()

Sets background color to black.

@spec blue() :: String.t()

Sets foreground color to blue.

@spec blue_background() :: String.t()

Sets background color to blue.

@spec bright() :: String.t()

Bright (increased intensity) or bold.

@spec clear() :: String.t()

Clears screen.

@spec clear_line() :: String.t()

Clears line.

@spec color(0..255) :: String.t()

Sets foreground color.

@spec color(0..5, 0..5, 0..5) :: String.t()

Sets the foreground color from individual RGB values.

Valid values for each color are in the range 0 to 5.

@spec color_background(0..255) :: String.t()

Sets background color.

Link to this function

color_background(r, g, b)

View Source
@spec color_background(0..5, 0..5, 0..5) :: String.t()

Sets the background color from individual RGB values.

Valid values for each color are in the range 0 to 5.

@spec conceal() :: String.t()

Conceal. Not widely supported.

@spec crossed_out() :: String.t()

Crossed-out. Characters legible, but marked for deletion. Not widely supported.

@spec cursor(non_neg_integer(), non_neg_integer()) :: String.t()

Sends cursor to the absolute position specified by line and column.

Line 0 and column 0 would mean the top left corner.

@spec cursor_down(pos_integer()) :: String.t()

Sends cursor lines down.

Link to this function

cursor_left(columns \\ 1)

View Source
@spec cursor_left(pos_integer()) :: String.t()

Sends cursor columns to the left.

Link to this function

cursor_right(columns \\ 1)

View Source
@spec cursor_right(pos_integer()) :: String.t()

Sends cursor columns to the right.

@spec cursor_up(pos_integer()) :: String.t()

Sends cursor lines up.

@spec cyan() :: String.t()

Sets foreground color to cyan.

@spec cyan_background() :: String.t()

Sets background color to cyan.

@spec default_background() :: String.t()

Default background color.

@spec default_color() :: String.t()

Default text color.

@spec enabled?() :: boolean()

Checks if ANSI coloring is supported and enabled on this machine.

This function simply reads the configuration value for :ansi_enabled in the :elixir application. The value is by default false unless Elixir can detect during startup that both stdout and stderr are terminals.

@spec encircled() :: String.t()

Encircled.

@spec faint() :: String.t()

Faint (decreased intensity). Not widely supported.

@spec font_1() :: String.t()

Sets alternative font 1.

@spec font_2() :: String.t()

Sets alternative font 2.

@spec font_3() :: String.t()

Sets alternative font 3.

@spec font_4() :: String.t()

Sets alternative font 4.

@spec font_5() :: String.t()

Sets alternative font 5.

@spec font_6() :: String.t()

Sets alternative font 6.

@spec font_7() :: String.t()

Sets alternative font 7.

@spec font_8() :: String.t()

Sets alternative font 8.

@spec font_9() :: String.t()

Sets alternative font 9.

Link to this function

format(ansidata, emit? \\ enabled?())

View Source
@spec format(ansidata(), boolean()) :: IO.chardata()

Formats a chardata-like argument by converting named ANSI sequences into actual ANSI codes.

The named sequences are represented by atoms.

It will also append an IO.ANSI.reset/0 to the chardata when a conversion is performed. If you don't want this behaviour, use format_fragment/2.

An optional boolean parameter can be passed to enable or disable emitting actual ANSI codes. When false, no ANSI codes will be emitted. By default checks if ANSI is enabled using the enabled?/0 function.

Examples

iex> IO.ANSI.format(["Hello, ", :red, :bright, "world!"], true)
[[[[[[], "Hello, "] | "\e[31m"] | "\e[1m"], "world!"] | "\e[0m"]
Link to this function

format_fragment(ansidata, emit? \\ enabled?())

View Source
@spec format_fragment(ansidata(), boolean()) :: IO.chardata()

Formats a chardata-like argument by converting named ANSI sequences into actual ANSI codes.

The named sequences are represented by atoms.

An optional boolean parameter can be passed to enable or disable emitting actual ANSI codes. When false, no ANSI codes will be emitted. By default checks if ANSI is enabled using the enabled?/0 function.

Examples

iex> IO.ANSI.format_fragment([:bright, 'Word'], true)
[[[[[[] | "\e[1m"], 87], 111], 114], 100]
@spec framed() :: String.t()

Framed.

@spec green() :: String.t()

Sets foreground color to green.

@spec green_background() :: String.t()

Sets background color to green.

@spec home() :: String.t()

Sends cursor home.

@spec inverse() :: String.t()

Image: negative. Swap foreground and background.

@spec inverse_off() :: String.t()

Image: positive. Normal foreground and background.

@spec italic() :: String.t()

Italic: on. Not widely supported. Sometimes treated as inverse.

@spec light_black() :: String.t()

Sets foreground color to light black.

Link to this function

light_black_background()

View Source
@spec light_black_background() :: String.t()

Sets background color to light black.

@spec light_blue() :: String.t()

Sets foreground color to light blue.

@spec light_blue_background() :: String.t()

Sets background color to light blue.

@spec light_cyan() :: String.t()

Sets foreground color to light cyan.

@spec light_cyan_background() :: String.t()

Sets background color to light cyan.

@spec light_green() :: String.t()

Sets foreground color to light green.

Link to this function

light_green_background()

View Source
@spec light_green_background() :: String.t()

Sets background color to light green.

@spec light_magenta() :: String.t()

Sets foreground color to light magenta.

Link to this function

light_magenta_background()

View Source
@spec light_magenta_background() :: String.t()

Sets background color to light magenta.

@spec light_red() :: String.t()

Sets foreground color to light red.

@spec light_red_background() :: String.t()

Sets background color to light red.

@spec light_white() :: String.t()

Sets foreground color to light white.

Link to this function

light_white_background()

View Source
@spec light_white_background() :: String.t()

Sets background color to light white.

@spec light_yellow() :: String.t()

Sets foreground color to light yellow.

Link to this function

light_yellow_background()

View Source
@spec light_yellow_background() :: String.t()

Sets background color to light yellow.

@spec magenta() :: String.t()

Sets foreground color to magenta.

@spec magenta_background() :: String.t()

Sets background color to magenta.

@spec no_underline() :: String.t()

Underline: none.

@spec normal() :: String.t()

Normal color or intensity.

@spec not_framed_encircled() :: String.t()

Not framed or encircled.

@spec not_italic() :: String.t()

Not italic.

@spec not_overlined() :: String.t()

Not overlined.

@spec overlined() :: String.t()

Overlined.

@spec primary_font() :: String.t()

Sets primary (default) font.

@spec red() :: String.t()

Sets foreground color to red.

@spec red_background() :: String.t()

Sets background color to red.

@spec reset() :: String.t()

Resets all attributes.

@spec reverse() :: String.t()

Image: negative. Swap foreground and background.

@spec reverse_off() :: String.t()

Image: positive. Normal foreground and background.

Link to this function

syntax_colors()

View Source (since 1.14.0)
@spec syntax_colors() :: Keyword.t(ansidata())

Syntax colors to be used by Inspect.

Those colors are used throughout Elixir's standard library, such as dbg/2 and IEx.

The colors can be changed by setting the :ansi_syntax_colors in the :elixir application configuration. Configuration for most built-in data types are supported: :atom, :binary, :boolean, :charlist, :list, :map, :nil, :number, :string, and :tuple. The default is:

[
  atom: :cyan
  boolean: :magenta,
  charlist: :yellow,
  nil: :magenta,
  number: :yellow,
  string: :green
]
@spec underline() :: String.t()

Underline: single.

@spec white() :: String.t()

Sets foreground color to white.

@spec white_background() :: String.t()

Sets background color to white.

@spec yellow() :: String.t()

Sets foreground color to yellow.

@spec yellow_background() :: String.t()

Sets background color to yellow.