View Source Owl.Table (Owl v0.8.0)
Allows drawing awesome tables.
Link to this section Summary
Functions
Draws a table.
Link to this section Functions
@spec new(rows :: [row :: %{required(column) => value}, ...], border_style: :solid | :solid_rounded | :none | :double, divide_body_rows: boolean(), truncate_lines: boolean(), filter_columns: (column -> as_boolean(term())), padding_x: non_neg_integer(), max_column_widths: (column -> pos_integer() | :infinity), max_width: pos_integer() | :infinity, render_cell: [ header: (column -> Owl.Data.t()), body: (value -> Owl.Data.t()) | (column, value -> Owl.Data.t()) ] | (value | column -> Owl.Data.t()), sort_columns: (column, column -> boolean()) | :asc | :desc | module() | {:asc | :desc, module()} ) :: Owl.Data.t() when column: any(), value: any()
Draws a table.
Accepts a list of maps, where each map represents a row.
The keys and values of maps should have the type Owl.Data.t/0
, otherwise use :render_cell
option to make values printable.
options
Options
:border_style
- sets the border style. Defaults to:solid
.:divide_body_rows
- specifies whether to show divider between rows in body. It is better to use it if cells have multiline values. Ignored, if:border_style
is set to:none
. Defaults tofalse
.:filter_columns
- sets a function which filters column (second argument forEnum.filter/2
). No filter function by default.:padding_x
- sets horizontal padding. Defaults to0
.:render_cell
- sets how to render header and body cells. Accepts either a function or a keyword list. Defaults to&Function.identity/1
. Options in case of a keyword list::header
- sets a function to render header cell. Defaults to&Function.identity/1
.:body
- sets a function to render body cell. Defaults to&Function.identity/1
.
:sort_columns
- sets a sorter (second argument forEnum.sort/2
) for columns. No sorter by default.:max_column_widths
- sets max width for columns in symbols. Accepts a function that returns an inner width (content + padding) for each column. Defaults tofn _ -> :infinity end
.:max_width
- sets a maximum width of of the table in symbols including borders. Defaults to width of the terminal or:infinity
, if a terminal is not available.:truncate_lines
- specifies whether to truncate lines when they reach width specified by:max_content_width
. Defaults tofalse
.
examples
Examples
# render as is without options
iex> [
...> %{"id" => "1", "name" => "Yaroslav"},
...> %{"id" => "2", "name" => "Volodymyr"}
...> ] |> Owl.Table.new() |> to_string()
"""
┌──┬─────────┐
│id│name │
├──┼─────────┤
│1 │Yaroslav │
│2 │Volodymyr│
└──┴─────────┘
""" |> String.trim_trailing()
# ...and more complex example with a bunch of options
iex> [
...> %{a: :qwertyuiop, b: :asdfghjkl},
...> %{a: :zxcvbnm, b: :dcb}
...> ]
...> |> Owl.Table.new(
...> render_cell: [
...> header: &(&1 |> inspect() |> Owl.Data.tag(:red)),
...> body: &(&1 |> inspect() |> Owl.Data.truncate(8) |> Owl.Data.tag(:yellow))
...> ],
...> divide_body_rows: true,
...> border_style: :solid_rounded,
...> padding_x: 1,
...> sort_columns: :desc
...> )
...> |> Owl.Data.to_ansidata()
...> |> to_string()
"""
╭──────────┬──────────╮
│ [31m:b[39m │ [31m:a[39m │
├──────────┼──────────┤
│ [33m:asdfgh…[39m │ [33m:qwerty…[39m │
├──────────┼──────────┤
│ [33m:dcb[39m │ [33m:zxcvbnm[39m │
╰──────────┴──────────╯[0m
""" |> String.trim_trailing()