IO ANSI Table v0.1.16 IO.ANSI.Table.Formatter

Prints a table applying a table style. Each row is a list of values identified by successive headers.

Summary

Functions

Takes a list of key-value collections and a list of keys

Takes a key-value collection and a list of keys

Takes a list of key-value collections, the (maximum) number of collections to format, whether to ring the bell, a table style and up to five options

Takes a list of key-value collections and a list of keys

Uppercases the first letter of every “word” of a title (must be convertible to a string)

Given a list of columns (string sublists), returns a list containing the maximum width of each column capped by maximum width

Functions

columns(collections, keys)
columns([map | Keyword.t], [any]) :: [[String.t]]

Takes a list of key-value collections and a list of keys.

Returns a list of columns, each column being a list of string values selected from each collection on a given key and repeatedly on successive keys in order.

Examples

iex> alias IO.ANSI.Table.Formatter
iex> list = [
...>   %{"a" => "1", "b" => "2", "c" => "3"},
...>   %{"a" => "4", "b" => "5", "c" => "6"}
...> ]
iex> Formatter.columns(list, ["c", "a"])
[["3", "6"], ["1", "4"]]

iex> alias IO.ANSI.Table.Formatter
iex> list = [
...>   %{:one => "1", '2' => 2.0, 3 => :three},
...>   %{:one => '4', '2' => "5", 3 => 000006}
...> ]
iex> Formatter.columns(list, [3, :one, '2'])
[["three", "6"], ["1", "4"], ["2.0", "5"]]
key_for(collection, keys)
key_for([map | Keyword.t], [any]) :: String.t

Takes a key-value collection and a list of keys.

Returns a sort key for the collection.

Examples

iex> alias IO.ANSI.Table.Formatter
iex> collection = [one: '1', two: "2", three: 3.0, four: 4]
iex> keys = [:three, :one, :four]
iex> Formatter.key_for(collection, keys)
"3.014"
rows(collections, keys)
rows([map | Keyword.t], [any]) :: [[String.t]]

Takes a list of key-value collections and a list of keys.

Returns a list of rows, each row being a list of string values orderly selected on successive keys from a given collection and repeatedly for each collection in turn.

Examples

iex> alias IO.ANSI.Table.Formatter
iex> list = [
...>   %{"a" => "1", "b" => "2", "c" => "3"},
...>   %{"a" => "4", "b" => "5", "c" => "6"}
...> ]
iex> Formatter.rows(list, ["c", "a"])
[["3", "1"], ["6", "4"]]

iex> alias IO.ANSI.Table.Formatter
iex> list = [
...>   %{:one => "1", '2' => 2.0, 3 => :three},
...>   %{:one => '4', '2' => "5", 3 => 000006}
...> ]
iex> Formatter.rows(list, [3, :one, '2'])
[["three", "1", "2.0"], ["6", "4", "5"]]
titlecase(title, fixes \\ %{})
titlecase(any, map) :: String.t

Uppercases the first letter of every “word” of a title (must be convertible to a string).

Any underscore is considered a space and consecutive whitespace characters are treated as a single occurrence. Leading and trailing whitespace characters are removed.

If a map of fixes is given, all occurrences of each fix key in the title will be replaced with the corresponding fix value.

Examples

iex> alias IO.ANSI.Table.Formatter
iex> Formatter.titlecase(" son   of a gun ", %{
...>   ~r[ of ]i => " of ",
...>   ~r[ a ]i  => " a "
...> })
"Son of a Gun"

iex> alias IO.ANSI.Table.Formatter
iex> Formatter.titlecase("_date___of_birth_", %{
...>   ~r[ of ]i => " of "
...> })
"Date of Birth"

iex> alias IO.ANSI.Table.Formatter
iex> Formatter.titlecase(:" _an_ _oDD case_ ")
"An ODD Case"
widths(columns, max_width \\ 99)
widths([[String.t]], non_neg_integer) :: [non_neg_integer]

Given a list of columns (string sublists), returns a list containing the maximum width of each column capped by maximum width.

Examples

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

iex> alias IO.ANSI.Table.Formatter
iex> data = [[":cat", "{1, 2}", "007"], ["mongoose", ":ant", "3.1416"]]
iex> Formatter.widths(data)
[6, 8]

iex> alias IO.ANSI.Table.Formatter
iex> data = [[":cat", "{1, 2}", "007"], ["mongoose", ":ant", "3.1416"]]
iex> Formatter.widths(data, 7)
[6, 7]