Vtex.Output.ANSI (Vtex v0.1.0)

Copy Markdown View Source

A drop-in superset of Elixir's IO.ANSI.

Every IO.ANSI function is mirrored here byte-for-byte — the test suite asserts parity against IO.ANSI itself — so you can swap IO.ANSI for Vtex.Output.ANSI and keep the same calls: Vtex.Output.ANSI.red(), Vtex.Output.ANSI.cursor(2, 3), Vtex.Output.ANSI.format([:red, "hi"]).

On top of that it adds what IO.ANSI cannot express — notably 24-bit truecolor via true_color/3 and true_color_background/3. Richer screen and cursor control (alternate buffer, scroll regions, save/restore, hide/show) lives in Vtex.Output.Screen and Vtex.Output.Cursor; mouse/paste/focus toggles in Vtex.Mouse, Vtex.Paste, Vtex.Focus.

Like the rest of Vtex, every function returns iodata for you to write; nothing here performs IO.

Difference from IO.ANSI

format/1 and format_fragment/1 emit by default (emit? = true), because you call this module specifically to produce sequences for a terminal. (IO.ANSI instead defaults to enabled?/0, which is off unless configured.) Pass an explicit boolean as the second argument to override.

Summary

Functions

Returns "\e[30m".

Returns "\e[40m".

Returns "\e[25m".

Returns "\e[6m".

Returns "\e[5m".

Returns "\e[34m".

Returns "\e[44m".

Returns "\e[1m".

Returns "\e[2J".

Returns "\e[2K".

256-colour foreground: a palette index (color/1) or a 6×6×6 cube point (color/3, each component 0..5). Mirrors IO.ANSI.

256-colour background; see color/1 and color/3.

Returns "\e[8m".

Returns "\e[9m".

Move the cursor to line, column (1-based). Mirrors IO.ANSI.cursor/2.

Move the cursor down n lines (default 1).

Move the cursor left n columns (default 1).

Move the cursor right n columns (default 1).

Move the cursor up n lines (default 1).

Returns "\e[36m".

Returns "\e[46m".

Returns "\e[49m".

Returns "\e[39m".

Whether ANSI output should be emitted. Always true for Vtex.

Returns "\e[52m".

Returns "\e[2m".

Returns "\e[11m".

Returns "\e[12m".

Returns "\e[13m".

Returns "\e[14m".

Returns "\e[15m".

Returns "\e[16m".

Returns "\e[17m".

Returns "\e[18m".

Returns "\e[19m".

Format chardata with embedded ANSI atoms, appending a reset if anything was emitted. Mirrors IO.ANSI.format/2, but emit? defaults to true.

Like format/2, but never appends a trailing reset. Mirrors IO.ANSI.format_fragment/2, with emit? defaulting to true.

Returns "\e[51m".

Returns "\e[32m".

Returns "\e[42m".

Returns "\e[H".

Returns "\e[7m".

Returns "\e[27m".

Returns "\e[3m".

Returns "\e[90m".

Returns "\e[100m".

Returns "\e[94m".

Returns "\e[104m".

Returns "\e[96m".

Returns "\e[106m".

Returns "\e[92m".

Returns "\e[102m".

Returns "\e[95m".

Returns "\e[105m".

Returns "\e[91m".

Returns "\e[101m".

Returns "\e[97m".

Returns "\e[107m".

Returns "\e[93m".

Returns "\e[103m".

Returns "\e[35m".

Returns "\e[45m".

Returns "\e[24m".

Returns "\e[22m".

Returns "\e[54m".

Returns "\e[23m".

Returns "\e[55m".

Returns "\e[53m".

Returns "\e[10m".

Returns "\e[31m".

Returns "\e[41m".

Returns "\e[0m".

Returns "\e[7m".

Returns "\e[27m".

24-bit truecolor foreground (each component 0..255). Not available in IO.ANSI.

24-bit truecolor background (each component 0..255). Not in IO.ANSI.

Returns "\e[4m".

Returns "\e[37m".

Returns "\e[47m".

Returns "\e[33m".

Returns "\e[43m".

Functions

black()

@spec black() :: binary()

Returns "\e[30m".

black_background()

@spec black_background() :: binary()

Returns "\e[40m".

blue()

@spec blue() :: binary()

Returns "\e[34m".

blue_background()

@spec blue_background() :: binary()

Returns "\e[44m".

bright()

@spec bright() :: binary()

Returns "\e[1m".

clear()

@spec clear() :: binary()

Returns "\e[2J".

clear_line()

@spec clear_line() :: binary()

Returns "\e[2K".

color(code)

@spec color(0..255) :: binary()

256-colour foreground: a palette index (color/1) or a 6×6×6 cube point (color/3, each component 0..5). Mirrors IO.ANSI.

Examples

iex> Vtex.Output.ANSI.color(196)
"\e[38;5;196m"

iex> Vtex.Output.ANSI.color(5, 0, 0)
"\e[38;5;196m"

color(r, g, b)

@spec color(0..5, 0..5, 0..5) :: binary()

color_background(code)

@spec color_background(0..255) :: binary()

256-colour background; see color/1 and color/3.

color_background(r, g, b)

@spec color_background(0..5, 0..5, 0..5) :: binary()

conceal()

@spec conceal() :: binary()

Returns "\e[8m".

crossed_out()

@spec crossed_out() :: binary()

Returns "\e[9m".

cursor(line, column)

@spec cursor(integer(), integer()) :: binary()

Move the cursor to line, column (1-based). Mirrors IO.ANSI.cursor/2.

cursor_down(n \\ 1)

@spec cursor_down(integer()) :: binary()

Move the cursor down n lines (default 1).

cursor_left(n \\ 1)

@spec cursor_left(integer()) :: binary()

Move the cursor left n columns (default 1).

cursor_right(n \\ 1)

@spec cursor_right(integer()) :: binary()

Move the cursor right n columns (default 1).

cursor_up(n \\ 1)

@spec cursor_up(integer()) :: binary()

Move the cursor up n lines (default 1).

cyan()

@spec cyan() :: binary()

Returns "\e[36m".

cyan_background()

@spec cyan_background() :: binary()

Returns "\e[46m".

default_background()

@spec default_background() :: binary()

Returns "\e[49m".

default_color()

@spec default_color() :: binary()

Returns "\e[39m".

enabled?()

@spec enabled?() :: boolean()

Whether ANSI output should be emitted. Always true for Vtex.

encircled()

@spec encircled() :: binary()

Returns "\e[52m".

faint()

@spec faint() :: binary()

Returns "\e[2m".

font_1()

@spec font_1() :: binary()

Returns "\e[11m".

font_2()

@spec font_2() :: binary()

Returns "\e[12m".

font_3()

@spec font_3() :: binary()

Returns "\e[13m".

font_4()

@spec font_4() :: binary()

Returns "\e[14m".

font_5()

@spec font_5() :: binary()

Returns "\e[15m".

font_6()

@spec font_6() :: binary()

Returns "\e[16m".

font_7()

@spec font_7() :: binary()

Returns "\e[17m".

font_8()

@spec font_8() :: binary()

Returns "\e[18m".

font_9()

@spec font_9() :: binary()

Returns "\e[19m".

format(chardata, emit? \\ true)

@spec format(IO.chardata(), boolean()) :: IO.chardata()

Format chardata with embedded ANSI atoms, appending a reset if anything was emitted. Mirrors IO.ANSI.format/2, but emit? defaults to true.

Examples

iex> Vtex.Output.ANSI.format([:red, :bright, "hi"]) |> IO.iodata_to_binary()
"\e[31m\e[1mhi\e[0m"

iex> Vtex.Output.ANSI.format([:red, "hi"], false) |> IO.iodata_to_binary()
"hi"

format_fragment(chardata, emit? \\ true)

@spec format_fragment(IO.chardata(), boolean()) :: IO.chardata()

Like format/2, but never appends a trailing reset. Mirrors IO.ANSI.format_fragment/2, with emit? defaulting to true.

framed()

@spec framed() :: binary()

Returns "\e[51m".

green()

@spec green() :: binary()

Returns "\e[32m".

green_background()

@spec green_background() :: binary()

Returns "\e[42m".

home()

@spec home() :: binary()

Returns "\e[H".

inverse()

@spec inverse() :: binary()

Returns "\e[7m".

inverse_off()

@spec inverse_off() :: binary()

Returns "\e[27m".

italic()

@spec italic() :: binary()

Returns "\e[3m".

light_black()

@spec light_black() :: binary()

Returns "\e[90m".

light_black_background()

@spec light_black_background() :: binary()

Returns "\e[100m".

light_blue()

@spec light_blue() :: binary()

Returns "\e[94m".

light_blue_background()

@spec light_blue_background() :: binary()

Returns "\e[104m".

light_cyan()

@spec light_cyan() :: binary()

Returns "\e[96m".

light_cyan_background()

@spec light_cyan_background() :: binary()

Returns "\e[106m".

light_green()

@spec light_green() :: binary()

Returns "\e[92m".

light_green_background()

@spec light_green_background() :: binary()

Returns "\e[102m".

light_magenta()

@spec light_magenta() :: binary()

Returns "\e[95m".

light_magenta_background()

@spec light_magenta_background() :: binary()

Returns "\e[105m".

light_red()

@spec light_red() :: binary()

Returns "\e[91m".

light_red_background()

@spec light_red_background() :: binary()

Returns "\e[101m".

light_white()

@spec light_white() :: binary()

Returns "\e[97m".

light_white_background()

@spec light_white_background() :: binary()

Returns "\e[107m".

light_yellow()

@spec light_yellow() :: binary()

Returns "\e[93m".

light_yellow_background()

@spec light_yellow_background() :: binary()

Returns "\e[103m".

magenta()

@spec magenta() :: binary()

Returns "\e[35m".

magenta_background()

@spec magenta_background() :: binary()

Returns "\e[45m".

no_underline()

@spec no_underline() :: binary()

Returns "\e[24m".

normal()

@spec normal() :: binary()

Returns "\e[22m".

not_framed_encircled()

@spec not_framed_encircled() :: binary()

Returns "\e[54m".

not_italic()

@spec not_italic() :: binary()

Returns "\e[23m".

not_overlined()

@spec not_overlined() :: binary()

Returns "\e[55m".

overlined()

@spec overlined() :: binary()

Returns "\e[53m".

primary_font()

@spec primary_font() :: binary()

Returns "\e[10m".

red()

@spec red() :: binary()

Returns "\e[31m".

red_background()

@spec red_background() :: binary()

Returns "\e[41m".

reset()

@spec reset() :: binary()

Returns "\e[0m".

reverse()

@spec reverse() :: binary()

Returns "\e[7m".

reverse_off()

@spec reverse_off() :: binary()

Returns "\e[27m".

true_color(r, g, b)

@spec true_color(0..255, 0..255, 0..255) :: binary()

24-bit truecolor foreground (each component 0..255). Not available in IO.ANSI.

Examples

iex> Vtex.Output.ANSI.true_color(255, 128, 0)
"\e[38;2;255;128;0m"

true_color_background(r, g, b)

@spec true_color_background(0..255, 0..255, 0..255) :: binary()

24-bit truecolor background (each component 0..255). Not in IO.ANSI.

underline()

@spec underline() :: binary()

Returns "\e[4m".

white()

@spec white() :: binary()

Returns "\e[37m".

white_background()

@spec white_background() :: binary()

Returns "\e[47m".

yellow()

@spec yellow() :: binary()

Returns "\e[33m".

yellow_background()

@spec yellow_background() :: binary()

Returns "\e[43m".