IO ANSI Table v0.1.5 IO.ANSI.Table.Formatter.Helper

Prints a table of rows with headers, applying a table style (configurable).

Summary

Functions

Takes a list of elements and 3 delimiters (left, inner and right)

Takes a list of widths and a list of corresponding attributes

Creates a new table formatter helper (struct)

Takes a list of rows (string sublists), a list of headers, a list of key headers, a list of margins, a list of column widths, a table style and whether to ring the bell

Writes one or many table lines depending on the line type given

Functions

expand(elems, arg)
expand([any], {any, any, any}) :: [any]

Takes a list of elements and 3 delimiters (left, inner and right).

Expands the list of elements by combining the delimiters.

The inner delimiter is inserted between all elements and the result is then surrounded by the left and right delimiters.

Returns a flattened list in case any element or delimiter is a list.

Examples

iex> alias IO.ANSI.Table.Formatter.Helper
iex> elements = ["Number", "Created At", "Title"]
iex> delimiters = {"<", "~", ">"}
iex> Helper.expand(elements, delimiters)
["<", "Number", "~", "Created At", "~", "Title", ">"]

iex> alias IO.ANSI.Table.Formatter.Helper
iex> elements = ["Number", "Created At", "Title"]
iex> delimiters = {["<", " "], [" ", "~", " "], [" ", ">"]}
iex> Helper.expand(elements, delimiters)
[
  "<", " ",
  "Number",
  " ", "~", " ",
  "Created At",
  " ", "~", " ",
  "Title",
  " ", ">"
]

iex> alias IO.ANSI.Table.Formatter.Helper
iex> elements = [6, 10, 5]
iex> delimiters = {[1, 1], [1, 1, 1], [1, 1]}
iex> Helper.expand(elements, delimiters)
[1, 1, 6, 1, 1, 1, 10, 1, 1, 1, 5, 1, 1]
format(widths, attrs)
format([non_neg_integer], [[atom] | atom]) :: String.t

Takes a list of widths and a list of corresponding attributes.

Returns an Erlang io format reflecting these widths and attributes.

For details of ANSI color codes (attributes) see ANSI color codes.

Here are a few examples:

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

Examples

iex> alias IO.ANSI.Table.Formatter.Helper
iex> widths = [2, 0, 6]
iex> attrs = [:light_yellow, :normal, :light_cyan]
iex> Helper.format(widths, attrs)
"\e[93m~-2ts\e[0m~-0ts\e[96m~-6ts\e[0m~n"
new(rows, headers, key_headers, header_terms, margin, widths, style)
new([[String.t]], [any], [any], [String.t], String.t, [non_neg_integer], atom) :: %IO.ANSI.Table.Formatter.Helper{header_terms: term, headers: term, key_headers: term, margin: term, rows: term, style: term, widths: term}

Creates a new table formatter helper (struct).

write(helper, type)
write(%IO.ANSI.Table.Formatter.Helper{header_terms: term, headers: term, key_headers: term, margin: term, rows: term, style: term, widths: term}, atom) :: :ok

Writes one or many table lines depending on the line type given.

Line types

  • :top - top line
  • :header - line of header(s) between top & separator
  • :separator - separator line between header & data rows
  • row types - list of related row type(s) listed below
  • :bottom - bottom line

Row types

  • :row - single row type
  • :even_row - first alternating row type
  • :odd_row - second alternating row type
  • :row_1 - first row type of repeating group of 3
  • :row_2 - second row type of repeating group of 3
  • :row_3 - third row type of repeating group of 3

Examples

# Evaluate rows, headers, key headers, margin, widths and style...
alias IO.ANSI.Table.Formatter.Helper
helper = Helper.new(rows, headers, key_headers, margin, widths, style)
Helper.write(helper, :top)
...
Helper.write(helper, [:row])
...