IO.ANSI

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.

Summary

Types

Functions

black()

Sets foreground color to black.

black_background()

Sets background color to black.

blink_off()

Blink: off.

blink_rapid()

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

blink_slow()

Blink: slow. Less than 150 per minute.

blue()

Sets foreground color to blue.

blue_background()

Sets background color to blue.

bright()

Bright (increased intensity) or bold.

clear()

Clears screen.

clear_line()

Clears line.

color(code)

Sets foreground color.

color(r, g, b)

Sets the foreground color from individual RGB values.

color_background(code)

Sets background color.

color_background(r, g, b)

Sets the background color from individual RGB values.

conceal()

Conceal. Not widely supported.

crossed_out()

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

cursor(line, column)

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

cursor_down(lines \\ 1)

Sends cursor lines down.

cursor_left(columns \\ 1)

Sends cursor columns to the left.

cursor_right(columns \\ 1)

Sends cursor columns to the right.

cursor_up(lines \\ 1)

Sends cursor lines up.

cyan()

Sets foreground color to cyan.

cyan_background()

Sets background color to cyan.

default_background()

Default background color.

default_color()

Default text color.

enabled?()

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

encircled()

Encircled.

faint()

Faint (decreased intensity). Not widely supported.

font_1()

Sets alternative font 1.

font_2()

Sets alternative font 2.

font_3()

Sets alternative font 3.

font_4()

Sets alternative font 4.

font_5()

Sets alternative font 5.

font_6()

Sets alternative font 6.

font_7()

Sets alternative font 7.

font_8()

Sets alternative font 8.

font_9()

Sets alternative font 9.

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

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

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

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

framed()

Framed.

green()

Sets foreground color to green.

green_background()

Sets background color to green.

home()

Sends cursor home.

inverse()

Image: negative. Swap foreground and background.

inverse_off()

Image: positive. Normal foreground and background.

italic()

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

light_black()

Sets foreground color to light black.

light_black_background()

Sets background color to light black.

light_blue()

Sets foreground color to light blue.

light_blue_background()

Sets background color to light blue.

light_cyan()

Sets foreground color to light cyan.

light_cyan_background()

Sets background color to light cyan.

light_green()

Sets foreground color to light green.

light_green_background()

Sets background color to light green.

light_magenta()

Sets foreground color to light magenta.

light_magenta_background()

Sets background color to light magenta.

light_red()

Sets foreground color to light red.

light_red_background()

Sets background color to light red.

light_white()

Sets foreground color to light white.

light_white_background()

Sets background color to light white.

light_yellow()

Sets foreground color to light yellow.

light_yellow_background()

Sets background color to light yellow.

magenta()

Sets foreground color to magenta.

magenta_background()

Sets background color to magenta.

no_underline()

Underline: none.

normal()

Normal color or intensity.

not_framed_encircled()

Not framed or encircled.

not_italic()

Not italic.

not_overlined()

Not overlined.

overlined()

Overlined.

primary_font()

Sets primary (default) font.

red()

Sets foreground color to red.

red_background()

Sets background color to red.

reset()

Resets all attributes.

reverse()

Image: negative. Swap foreground and background.

reverse_off()

Image: positive. Normal foreground and background.

underline()

Underline: single.

white()

Sets foreground color to white.

white_background()

Sets background color to white.

yellow()

Sets foreground color to yellow.

yellow_background()

Sets background color to yellow.

Types

ansicode()Source

Specs

ansicode() :: atom()

ansidata()Source

Specs

ansidata() :: ansilist() | ansicode() | binary()

ansilist()Source

Specs

ansilist() ::
  maybe_improper_list(
    char() | ansicode() | binary() | ansilist(),
    binary() | ansicode() | []
  )

Functions

black()Source

Sets foreground color to black.

black_background()Source

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.

blue()Source

Sets foreground color to blue.

blue_background()Source

Sets background color to blue.

bright()Source

Bright (increased intensity) or bold.

clear()Source

Clears screen.

clear_line()Source

Clears line.

color(code)Source

Specs

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

Sets foreground color.

color(r, g, b)Source

Specs

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.

color_background(code)Source

Specs

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

Sets background color.

color_background(r, g, b)Source

Specs

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.

conceal()Source

Conceal. Not widely supported.

crossed_out()Source

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

cursor(line, column)Source

Specs

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.

cursor_down(lines \\ 1)Source

Specs

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

Sends cursor lines down.

cursor_left(columns \\ 1)Source

Specs

cursor_left(pos_integer()) :: String.t()

Sends cursor columns to the left.

cursor_right(columns \\ 1)Source

Specs

cursor_right(pos_integer()) :: String.t()

Sends cursor columns to the right.

cursor_up(lines \\ 1)Source

Specs

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

Sends cursor lines up.

cyan()Source

Sets foreground color to cyan.

cyan_background()Source

Sets background color to cyan.

default_background()Source

Default background color.

default_color()Source

Default text color.

enabled?()Source

Specs

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.

encircled()Source

Encircled.

faint()Source

Faint (decreased intensity). Not widely supported.

font_1()Source

Sets alternative font 1.

font_2()Source

Sets alternative font 2.

font_3()Source

Sets alternative font 3.

font_4()Source

Sets alternative font 4.

font_5()Source

Sets alternative font 5.

font_6()Source

Sets alternative font 6.

font_7()Source

Sets alternative font 7.

font_8()Source

Sets alternative font 8.

font_9()Source

Sets alternative font 9.

format(chardata, emit? \\ enabled?())Source

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"]

format_fragment(chardata, emit? \\ enabled?())Source

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]

framed()Source

Framed.

green()Source

Sets foreground color to green.

green_background()Source

Sets background color to green.

home()Source

Sends cursor home.

inverse()Source

Image: negative. Swap foreground and background.

inverse_off()Source

Image: positive. Normal foreground and background.

italic()Source

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

light_black()Source

Sets foreground color to light black.

light_black_background()Source

Sets background color to light black.

light_blue()Source

Sets foreground color to light blue.

light_blue_background()Source

Sets background color to light blue.

light_cyan()Source

Sets foreground color to light cyan.

light_cyan_background()Source

Sets background color to light cyan.

light_green()Source

Sets foreground color to light green.

light_green_background()Source

Sets background color to light green.

light_magenta()Source

Sets foreground color to light magenta.

light_magenta_background()Source

Sets background color to light magenta.

light_red()Source

Sets foreground color to light red.

light_red_background()Source

Sets background color to light red.

light_white()Source

Sets foreground color to light white.

light_white_background()Source

Sets background color to light white.

light_yellow()Source

Sets foreground color to light yellow.

light_yellow_background()Source

Sets background color to light yellow.

magenta()Source

Sets foreground color to magenta.

magenta_background()Source

Sets background color to magenta.

no_underline()Source

Underline: none.

normal()Source

Normal color or intensity.

not_framed_encircled()Source

Not framed or encircled.

not_italic()Source

Not italic.

not_overlined()Source

Not overlined.

overlined()Source

Overlined.

primary_font()Source

Sets primary (default) font.

red()Source

Sets foreground color to red.

red_background()Source

Sets background color to red.

reset()Source

Resets all attributes.

reverse()Source

Image: negative. Swap foreground and background.

reverse_off()Source

Image: positive. Normal foreground and background.

underline()Source

Underline: single.

white()Source

Sets foreground color to white.

white_background()Source

Sets background color to white.

yellow()Source

Sets foreground color to yellow.

yellow_background()Source

Sets background color to yellow.

© 2012 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/elixir/1.12.0/IO.ANSI.html