IO ANSI Table v0.1.19 IO.ANSI.Table.Formatter.Helper

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

Summary

Functions

Returns the list of attributes for a given table style and line/row type

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

Takes a list of widths and a list of corresponding attributes

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

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

Takes a list of column widths, a list of elements and a tuple of 3 multipart border widths (left, inner and right)

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

Types

t()
t() :: %IO.ANSI.Table.Formatter.Helper{header_fixes: map | nil, headers: [IO.ANSI.Table.Formatter.collection_key] | nil, key_headers: [IO.ANSI.Table.Formatter.collection_key] | nil, margin: String.t | nil, rows: [IO.ANSI.Table.Formatter.row] | nil, style: IO.ANSI.Table.Style.t | nil, widths: [IO.ANSI.Table.Formatter.column_width] | nil}

Functions

Returns the list of attributes for a given table style and line/row type.

Examples

iex> alias IO.ANSI.Table.Formatter.Helper
iex> headers = ["Number", "Created At", "Title"]
iex> key_headers = ["Created At"]
iex> style = :dark
iex> type = :header
iex> Helper.attrs(headers, key_headers, style, type)
[ :light_green, :normal,                          # left border
  :light_red, :normal,                            # non key column
  :normal, :light_green, :normal,                 # inner border
  [:light_white, :light_red_background], :normal, # key column
  :normal, :light_green, :normal,                 # inner border
  :light_red, :normal,                            # non key column
  :normal, :light_green                           # right border
]

iex> alias IO.ANSI.Table.Formatter.Helper
iex> headers = ["Number", "Created At", "Title"]
iex> key_headers = ["Created At"]
iex> style = :dark
iex> type = :row
iex> Helper.attrs(headers, key_headers, style, type)
[ :light_green, :normal,          # left border
  :light_green, :normal,          # non key column
  :normal, :light_green, :normal, # inner border
  :light_magenta, :normal,        # key column
  :normal, :light_green, :normal, # inner border
  :light_green, :normal,          # non key column
  :normal, :light_green           # right border
]
expand(elems, arg)
expand([any], {any, any, any}) :: [any]

Takes a list of elements and a tuple of 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], [IO.ANSI.Table.Style.attr]) :: String.t

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

Returns an Erlang io format reflecting these widths and attributes. It consists of a string with embedded ANSI color codes (escape sequences).

Here are a few ANSI color codes:

  • 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"
items(elems, arg)

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

Will expand the list of elements by combining “fillers” and borders.

Examples

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

Creates a table formatter helper (struct).

widths(widths, elems, arg)
widths([IO.ANSI.Table.Formatter.column_width], [String.t], {[...], [...], [...]}) :: [non_neg_integer]

Takes a list of column widths, a list of elements and a tuple of 3 multipart border widths (left, inner and right):

  • left - widths of left border and “filler”
  • inner - widths of “filler”, inner border and “filler”
  • right - widths of “filler” and right border

Returns the widths of elements and their “fillers” combined with border widths.

Examples

iex> alias IO.ANSI.Table.Formatter.Helper
iex> widths = [6, 13, 11]
iex> elements = ["Number", "Created At", "Title"]
iex> border_widths = {[1, 1], [1, 1, 1], [1, 1]}
iex> Helper.widths(widths, elements, border_widths)
[1, 1, 6, 0, 1, 1, 1, 10, 3, 1, 1, 1, 5, 6, 1, 1]

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

Line types

  • :top - top line
  • :header - line of table header(s)
  • :separator - separator line
  • row types - list of related row type(s)
  • :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

alias IO.ANSI.Table.Formatter.Helper
helper = %Helper{
  rows: [
    ["Ottawa", "Canada" , "1,142,700"],
    ["Zagreb", "Croatia", "  685,500"],
    ["Paris" , "France" , "9,854,000"]
  ],
  headers: ["city", "country", "population"],
  key_headers: ["country"],
  header_fixes: %{},
  margin: "   ",
  widths: [6, 7, 10],
  style: :pretty_alt
}
Enum.each [:top, :header, :separator], &Helper.write(helper, &1)

write_header

Helper.write helper, [:even_row, :odd_row]

write_rows