View Source Owl.Table (Owl v0.11.0)
Allows drawing awesome tables.
Summary
Functions
Draws a table.
Functions
@spec new(rows :: [row :: %{required(column) => value}, ...], border_style: :solid | :solid_rounded | :none | :double, divide_body_rows: boolean(), word_wrap: :break_word | :normal, 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
: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_styleis 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. Defaults to:asc.: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.:word_wrap- sets the word wrapping mode. Can be:break_wordor:normal. Defaults to:break_word. Ignored if:truncate_linesistrue.:truncate_lines- specifies whether to truncate lines when they reach width specified by:max_content_width. Defaults tofalse.
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_chardata()
...> |> to_string()
"""
╭──────────┬──────────╮
│ \e[31m:b\e[39m │ \e[31m:a\e[39m │
├──────────┼──────────┤
│ \e[33m:asdfgh…\e[39m │ \e[33m:qwerty…\e[39m │
├──────────┼──────────┤
│ \e[33m:dcb\e[39m │ \e[33m:zxcvbnm\e[39m │
╰──────────┴──────────╯\e[0m
""" |> String.trim_trailing()