View Source grid (grid v0.2.0)

Formatting library for tabular data.

Link to this section Summary

Types

A cell containing text contents and its desired display width.

Column display specification. An integer Integer is equivalent to #{index => Integer}, an atom Atom is equivalent to #{key => Atom}.

Ordered list of columns to print.
A format specification, either as a shortcut atom or a custom fun of type format_fun().
Function that takes a cell value and return its textual representation. Either as printable IO-data or a cell().
Enable headers by setting to true or supplying a specific format of type format(). Setting it to false disables printing of headers.

Options to customize printing.

Functions

Returns a cell with the specified text contents and width.

Format a list of items as a table.

Link to this section Types

-opaque cell()
A cell containing text contents and its desired display width.
-type column() ::
    pos_integer() |
    atom() |
    #{index => pos_integer(), key => atom(), name => iodata(), format => format()}.

Column display specification. An integer Integer is equivalent to #{index => Integer}, an atom Atom is equivalent to #{key => Atom}.

  • index is which cell index should be rendered in this column
  • key is which cell key should be rendered in this column
  • name is the display name to use for the column header
  • format is the format specification that will be applied to each cell
-type columns() :: [column()].
Ordered list of columns to print.
-type format() :: uppercase | lowercase | titlecase | format_fun().
A format specification, either as a shortcut atom or a custom fun of type format_fun().
-type format_fun() :: fun((term()) -> iodata() | cell()).
Function that takes a cell value and return its textual representation. Either as printable IO-data or a cell().
-type header_opt() :: boolean() | format().
Enable headers by setting to true or supplying a specific format of type format(). Setting it to false disables printing of headers.
-type opts() :: #{header => header_opt(), spacer => iodata(), columns => columns()}.

Options to customize printing.

  • spacer defaults to " "
  • header defaults to false
  • columns defaults to to printing all columns as-is

Link to this section Functions

-spec cell(iodata(), non_neg_integer()) -> cell().

Returns a cell with the specified text contents and width.

Should only be called from format functions.

This function is useful when the text returned does not correspond to the resulting visual width, e.g. when returning colored terminal text using ANSI escape codes (that don't have a width when printed to the terminal).

Equivalent to format(Items, #{}).

-spec format(Items :: [term()], Opts :: opts()) -> iodata().

Format a list of items as a table.

Returns a nested IO-data that needs to be printed manually (e.g. with io:format(Result)).