IO ANSI Table v0.1.0 IO.ANSI.Table.Formatter.Helper
Prints a table of rows with headers, applying a table style (configurable).
Summary
Functions
Takes a list of elements
and 3 delimiters
(left
, inner
and right
)
Takes a list of widths
and a list of corresponding attributes
Creates a new table formatter helper (struct)
Takes a list of rows
(string sublists), a list of headers
,
a key
header
, 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 (atom)
Functions
Takes a list of elements
and 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
/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]
Takes a list of widths
and a list of corresponding attributes
.
Returns an Erlang io format reflecting these widths
and attributes
.
For details of ANSI color codes (attributes
) see
ANSI color codes.
Here are a few examples:
- 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)
#~S" \e[93m~-2ts\e[0m~-0ts\e[96m~-6ts\e[0m~n"
"\e[93m~-2ts\e[0m~-0ts\e[96m~-6ts\e[0m~n"
new([[String.t]], [any], any, [non_neg_integer], atom) :: %IO.ANSI.Table.Formatter.Helper{headers: term, key: term, rows: term, style: term, widths: term}
Creates a new table formatter helper (struct).
print_table([[String.t]], [any], any, [non_neg_integer], atom, boolean) :: :ok
Takes a list of rows
(string sublists), a list of headers
,
a key
header
, 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
:medium
:dark
: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:dashed
- no colors:plain
- slightly colored:test
- no colors:bare
- no colors:barish
- like bare but colored
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_header = 'country'
widths = [6, 7, 10]
table_style = :medium
bell = true
Helper.print_table(
capitals, headers, key_header, widths, table_style, bell
)
iex> alias IO.ANSI.Table.Formatter.Helper
iex> alias ExUnit.CaptureIO
iex> capitals = [
...> ["Ottawa", "Canada" , "1,142,700"],
...> ["Zagreb", "Croatia", " 685,500"],
...> ["Paris" , "France" , "9,854,000"]
...> ]
iex> headers = ['city', :country, "population"]
iex> key_header = :country
iex> widths = [6, 7, 10]
iex> table_style = :dashed
iex> bell = false
iex> CaptureIO.capture_io fn ->
...> Helper.print_table(
...> capitals, headers, key_header, widths, table_style, bell
...> )
...> end
"""
+--------+---------+------------+
| City | Country | Population |
+--------+---------+------------+
| Ottawa | Canada | 1,142,700 |
| Zagreb | Croatia | 685,500 |
| Paris | France | 9,854,000 |
+--------+---------+------------+
"""
write(%IO.ANSI.Table.Formatter.Helper{headers: term, key: term, rows: term, style: term, widths: term}, atom) :: :ok
Writes one or many table lines depending on the line type
given (atom).
Line types
:top
- top line:header
- line of header(s) between top & separator:separator
- separator line between header & data:data
- line(s) of data item(s) between separator & bottom:bottom
- bottom line
Examples
# Evaluate rows, headers, key, widths and style...
alias IO.ANSI.Table.Formatter.Helper
helper = Helper.new(rows, headers, key, widths, style)
Helper.write(helper, :data)