IO.ANSI.Table.Column (IO ANSI Table v1.0.40)

View Source

Functions related to the columns of a table.

Summary

Types

A list of 3 contiguous widths: left width, inner width and right width

t()

Table column

Column width

Functions

Spreads a width given an element and its align attribute.

Returns a list of column widths capped by maximum width.

Types

spread()

@type spread() :: [width()]

A list of 3 contiguous widths: left width, inner width and right width

t()

@type t() :: [String.t()]

Table column

width()

@type width() :: non_neg_integer()

Column width

Functions

spread(width, elem, align_attr)

Spreads a width given an element and its align attribute.

Examples

iex> alias IO.ANSI.Table.Column
iex> {
...>   Column.spread(7, "name", :left  ),
...>   Column.spread(7, "name", :center),
...>   Column.spread(7, "name", :right )
...> }
{[0, 4, 3], [1, 4, 2], [3, 4, 0]}

iex> alias IO.ANSI.Table.Column
iex> {
...>   Column.spread(3, "name", :left  ),
...>   Column.spread(3, "name", :center),
...>   Column.spread(3, "name", :right )
...> }
{[0, 3, 0], [0, 3, 0], [0, 3, 0]}

iex> alias IO.ANSI.Table.Column
iex> {
...>   Column.spread(10, "\e[32m\e[42mCHEETAH\e[0m", :left  ),
...>   Column.spread(10, "\e[32m\e[42mCHEETAH\e[0m", :center),
...>   Column.spread(10, "\e[32m\e[42mCHEETAH\e[0m", :right )
...> }
{[0, 21, 3], [1, 21, 2], [3, 21, 0]}

iex> alias IO.ANSI.Table.Column
iex> {
...>   Column.spread(7, "\e[32m\e[42mCHEETAH\e[0m", :left  ),
...>   Column.spread(7, "\e[32m\e[42mCHEETAH\e[0m", :center),
...>   Column.spread(7, "\e[32m\e[42mCHEETAH\e[0m", :right )
...> }
{[0, 21, 0], [0, 21, 0], [0, 21, 0]}

widths(columns, max_width)

@spec widths([t()], width()) :: [width()]

Returns a list of column widths capped by maximum width.

Examples

iex> alias IO.ANSI.Table.Column
iex> columns = [["cat", "wombat", "elk"], ["mongoose", "ant", "gnu"]]
iex> Column.widths(columns, 99)
[6, 8]

iex> alias IO.ANSI.Table.Column
iex> columns = [["cat", "wombat", "elk"], ["mongoose", "ant", "gnu"]]
iex> Column.widths(columns, 7)
[6, 7]

iex> alias IO.ANSI.Table.Column
iex> columns = [["\e[32m\e[42mCHEETAH\e[0m", "elk"], ["mongoose", "ant"]]
iex> Column.widths(columns, 99)
[7, 8]

iex> alias IO.ANSI.Table.Column
iex> columns = [["\e[32m\e[42mCHEETAH\e[0m", "elk"], ["mongoose", "ant"]]
iex> Column.widths(columns, 6)
[6, 6]