IO ANSI Table v0.2.5 IO.ANSI.Table.Formatter.Helper View Source
Prints a table of rows with headers, applying a table style (configurable).
Link to this section Summary
Functions
Takes an enumerable and a tuple of 3 delimiters (left, inner and right)
Takes a list of item widths and a list of corresponding
item attributes
Returns a list of attributes based on the given column types,
table style and line/row type
Takes a list of column widths, a list of elements, a list of
align attributes and a tuple of 3 multipart border widths
(left, inner and right)
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 map of align attributes,
a keyword 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
Link to this section Types
t() :: %IO.ANSI.Table.Formatter.Helper{align_attrs: [align_attr] | nil, column_types: [column_type] | nil, column_widths: [IO.ANSI.Table.Formatter.column_width] | nil, headers: [header] | nil, left_margin: String.t | nil, rows: [IO.ANSI.Table.Formatter.row] | nil, style: IO.ANSI.Table.Style.t | nil}
Link to this section Functions
Takes an enumerable and a tuple of 3 delimiters (left, inner and right).
Expands the elements in the enumerable 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([item_width], [IO.ANSI.Table.Style.attr]) :: String.t
Takes a list of item widths and a list of corresponding
item attributes.
Returns an Erlang io format reflecting these item widths and
item 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> item_widths = [2, 0, 6]
iex> item_attrs = [:light_yellow, :normal, :light_cyan]
iex> Helper.format(item_widths, item_attrs)
"\e[93m~-2ts\e[0m~-0ts\e[96m~-6ts\e[0m~n"
Returns a list of attributes based on the given column types,
table style and line/row type.
Examples
iex> alias IO.ANSI.Table.Formatter.Helper
iex> column_types = [:non_key, :key, :non_key]
iex> style = :medium
iex> type = :header
iex> Helper.item_attrs(column_types, style, type)
[ :light_yellow, :normal, # left border
:normal, :light_green, :normal, # non key column
:normal, :light_yellow, :normal, # inner border
:normal, [:light_green, :underline], :normal, # key column
:normal, :light_yellow, :normal, # inner border
:normal, :light_green, :normal, # non key column
:normal, :light_yellow # right border
]
iex> alias IO.ANSI.Table.Formatter.Helper
iex> column_types = [:non_key, :key, :non_key]
iex> style = :dark
iex> type = :row
iex> Helper.item_attrs(column_types, style, type)
[ :light_green, :normal, # left border
:normal, :light_green, :normal, # non key column
:normal, :light_green, :normal, # inner border
:normal, :light_white, :normal, # key column
:normal, :light_green, :normal, # inner border
:normal, :light_green, :normal, # non key column
:normal, :light_green # right border
]
item_widths([IO.ANSI.Table.Formatter.column_width], [elem], [align_attr], IO.ANSI.Table.Style.border_widths) :: [item_width]
Takes a list of column widths, a list of elements, a list of
align attributes 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> column_widths = [7, 13, 11]
iex> elems = ["Number", "Created At", "Title"]
iex> align_attrs = [:right, :center, :left]
iex> border_widths = {[1, 1], [1, 1, 1], [1, 1]}
iex> Helper.item_widths(column_widths, elems, align_attrs, border_widths)
[1, 1, 1, 6, 0, 1, 1, 1, 1, 10, 2, 1, 1, 1, 0, 5, 6, 1, 1]
items([elem], IO.ANSI.Table.Style.borders) :: [item]
Takes a list of elements and a tuple of 3 borders (left, inner
and right).
Expands 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)
[ "<", "", # left border, filler
"", "Number", "", # filler, element, filler
"", "~", "", # filler, inner border, filler
"", "Created At", "", # filler, element, filler
"", "~", "", # filler, inner border, filler
"", "Title", "", # filler, element, filler
"", ">" # filler, right border
]
print_table([IO.ANSI.Table.Formatter.row], [IO.ANSI.Table.Formatter.collection_key], [IO.ANSI.Table.Formatter.collection_key], map, map, Keyword.t, [IO.ANSI.Table.Formatter.column_width], IO.ANSI.Table.Style.t, boolean) :: :ok
Takes a list of rows (string sublists), a list of headers, a list
of key headers, a map of header fixes, a map of align attributes,
a keyword list of margins, a list of column widths, a table style
and whether to ring the bell.
Prints a table to STDOUT of the strings in each row.
The columns are identified by successive headers.
Table styles
:light- light colors:light_alt- light colors, alternating row colors:light_mult- light colors, 3 repeating row colors:medium- medium colors:medium_alt- medium colors, alternating row colors:medium_mult- medium colors, 3 repeating row colors:dark- dark colors:dark_alt- dark colors, alternating row colors:dark_mult- dark colors, 3 repeating row colors:pretty- multicolored:pretty_alt- multicolored, alternating row colors:pretty_mult- multicolored, 3 repeating row colors:cyan- light cyan background:yellow- light yellow background:green- light green background:CYAN- light cyan border:YELLOW- light yellow border:GREEN- light green border:mixed- fillers revealed:dotted- slightly colored:dotted_alt- slightly colored, alternating row colors:dotted_mult- slightly colored, 3 repeating row colors:dashed- no colors:plain- slightly colored:test- no colors:bare- no colors:barish- like bare but colored:green_padded- like green but with extra padding:green_unpadded- like green but without padding:GREEN_PADDED- like GREEN but with extra padding:GREEN_UNPADDED- like GREEN but without padding:cyan_alt- cyan header, alternating row colors:cyan_mult- cyan header, 3 repeating row colors:green_alt- green header, alternating row colors:green_mult- green header, 3 repeating row colors
Examples
alias IO.ANSI.Table.Formatter.Helper
capitals = [
["Ottawa", "Canada" , "1,142,700"],
["Zagreb", "Croatia", "685,500"],
["Paris" , "France" , "9,854,000"]
]
headers = ['city', 'country', 'population']
key_headers = ['country']
header_fixes = %{}
align_attrs = %{'population' => :right}
margins = [top: 2, bottom: 2, left: 2]
column_widths = [6, 7, 10]
style = :medium
bell = true
Helper.print_table(
capitals, headers, key_headers,
header_fixes, align_attrs, margins, column_widths, style, bell
)
iex> alias ExUnit.CaptureIO
iex> alias IO.ANSI.Table.Formatter.Helper
iex> capitals = [
...> ["Ottawa", "Canada" , "1,142,700"],
...> ["Zagreb", "Croatia", "685,500"],
...> ["Paris" , "France" , "9,854,000"]
...> ]
iex> headers = ['city', :country, "population"]
iex> key_headers = [:country]
iex> header_fixes = %{}
iex> align_attrs = %{"population" => :right}
iex> margins = [top: 2, bottom: 2, left: 3]
iex> column_widths = [6, 7, 10]
iex> style = :dashed
iex> bell = false
iex> CaptureIO.capture_io fn ->
...> Helper.print_table(
...> capitals, headers, key_headers,
...> header_fixes, align_attrs, margins, column_widths, style, bell
...> )
...> end
"\n\n" <> """
+--------+---------+------------+
| City | Country | Population |
+--------+---------+------------+
| Ottawa | Canada | 1,142,700 |
| Zagreb | Croatia | 685,500 |
| Paris | France | 9,854,000 |
+--------+---------+------------+
""" <> "\n\n"
write(t, IO.ANSI.Table.Style.line_type | [IO.ANSI.Table.Style.row_type]) :: :ok
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 linerow 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"],
column_types: [:non_key, :key, :non_key],
align_attrs: [:left, :left, :right],
left_margin: " ",
column_widths: [6, 7, 10],
style: :pretty_alt
}
Enum.each [:top, :header, :separator], &Helper.write(helper, &1)
Helper.write helper, [:even_row, :odd_row]


