View Source IO.ANSI.Table.Line (IO ANSI Table v1.0.34)

Formats the lines of a table.

Summary

Types

Line element

Line item

Functions

Returns an Erlang I/O format (see :io.format/2) reflecting item widths and item attributes.

Deploys the style attributes of a given line type and table spec.

Deploys the widths of elements for a given line type and table spec.

Deploys elements by interlacing them with filler and borders (left, inner and right).

Types

@type elem() :: String.t()

Line element

@type item() :: String.t()

Line item

Functions

Link to this function

format(item_widths, item_attrs, ansi_enabled? \\ true)

View Source

Returns an Erlang I/O format (see :io.format/2) reflecting item widths and item attributes.

It consists of a string with control sequences and embedded ANSI codes (escape sequences), if emitting ANSI codes is enabled.

Here are a few ANSI codes:

  • light yellow - \e[93m
  • light cyan - \e[96m
  • reset - \e[0m

Examples

iex> alias IO.ANSI.Table.Line
iex> item_widths = [2, 0, 6]
iex> item_attrs = [:light_yellow, :normal, :light_cyan]
iex> Line.format(item_widths, item_attrs, true)
"\e[93m~-2ts\e[0m~-0ts\e[96m~-6ts\e[0m~n"

Deploys the style attributes of a given line type and table spec.

Examples

iex> alias IO.ANSI.Table.Line
iex> # We use a map instead of a %Spec{} for conciseness.
iex> spec = %{style: :medium, sort_attrs: [nil, :asc, nil]}
iex> type = :header
iex> Line.item_attrs(type, spec)
[
  :normal, :gold                , :normal, # left border
  :normal, :canary              , :normal, # non-key column
  :normal, :gold                , :normal, # inner border
  :normal, [:canary, :underline], :normal, # key column
  :normal, :gold                , :normal, # inner border
  :normal, :canary              , :normal, # non-key column
  :normal, :gold                , :normal  # right border
]
Link to this function

item_widths(elems, type, spec)

View Source

Deploys the widths of elements for a given line type and table spec.

Examples

iex> alias IO.ANSI.Table.Line
iex> # We use a map instead of a %Spec{} for conciseness.
iex> spec = %{
...>   style: :medium,
...>   align_attrs: [:right, :center, nil],
...>   column_widths: [7, 13, 9]
...> }
iex> dashes = ["═══════", "═════════════", "═════════"]
iex> elems = ["Number", "Created at", "Title"]
iex> {Line.item_widths(dashes, :top, spec),
...>  Line.item_widths(elems, :header, spec)}
{[0, 2, 0,  0, 7, 0,  0, 3, 0,  0, 13, 0,  0, 3, 0,  0, 9, 0,  0, 2, 0],
 [0, 1, 1,  1, 6, 0,  1, 1, 1,  1, 10, 2,  1, 1, 1,  0, 5, 4,  1, 1, 0]}
Link to this function

items(elems, borders, filler \\ "")

View Source
@spec items([elem()], [IO.ANSI.Table.Style.border()], String.t()) :: [item()]

Deploys elements by interlacing them with filler and borders (left, inner and right).

Examples

iex> alias IO.ANSI.Table.Line
iex> elements = ["Number", "Created at", "Title"]
iex> borders = ["+-", "-+-", "-+"]
iex> Line.items(elements, borders)
[
  "", "+-"        , "", # filler, left border, filler
  "", "Number"    , "", # filler, element, filler
  "", "-+-"       , "", # filler, inner border, filler
  "", "Created at", "", # filler, element, filler
  "", "-+-"       , "", # filler, inner border, filler
  "", "Title"     , "", # filler, element, filler
  "", "-+"        , ""  # filler, right border, filler
]