IO ANSI Table v0.2.4 IO.ANSI.Table.Formatter View Source

Prints a table to STDOUT applying a table style to a list of key-value collections.

Link to this section Summary

Functions

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

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 six 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)

Link to this section Types

Link to this type collection() View Source
collection() :: map | Keyword.t
Link to this type collection_key() View Source
collection_key() :: String.t | atom | charlist
Link to this type column_width() View Source
column_width() :: non_neg_integer

Link to this section Functions

Link to this function column_widths(columns, max_width \\ 99) View Source
column_widths([column], non_neg_integer) :: [column_width]

Given a list of columns (string sublists), returns a list containing the 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.column_widths(data)
[6, 8]

iex> alias IO.ANSI.Table.Formatter
iex> data = [["cat", "wombat", "elk"], ["mongoose", "ant", "gnu"]]
iex> Formatter.column_widths(data, 7)
[6, 7]
Link to this function columns(collections, keys) View Source
columns([collection], [collection_key]) :: [column]

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"]]

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"
Link to this function rows(collections, keys) View Source
rows([collection], [collection_key]) :: [row]

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"]]
Link to this function titlecase(title, fixes \\ %{}) View Source
titlecase(collection_key, 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"