View Source TableRex.Table (table_rex v4.1.0)

A set of functions for working with tables.

The Table is represented internally as a struct though the fields are private and must not be accessed directly. Instead, use the functions in this module.

Summary

Functions

Adds a single row to the table.

Adds multiple rows to the table.

Removes column meta for all columns, effectively resetting column meta back to the default options across the board.

Removes all row data from the table, keeping everything else.

Retrieves the value of a column meta option at the specified col_index. If no value has been set, default values are returned.

Returns a boolean detailing if the passed table has a header row set.

Creates a new blank table.

Creates a new table with an initial set of rows and an optional header and title.

Sets cell level information such as alignment.

Sets column level information such as padding and alignment.

Sets a list as the optional header row. Set to nil or [] to remove an already set header from renders.

Sets cell level information for the header cells.

Sets a string as the optional table title. Set to nil or "" to remove an already set title from renders.

Renders the current table state to string, ready for display via IO.puts/2 or other means.

Renders the current table state to string, ready for display via IO.puts/2 or other means.

Sorts the table rows by using the values in a specified column.

Types

@type t() :: %TableRex.Table{
  columns: term(),
  default_column: term(),
  header_row: term(),
  rows: term(),
  title: term()
}

Functions

@spec add_row(t(), list()) :: t()

Adds a single row to the table.

@spec add_rows(t(), list()) :: t()

Adds multiple rows to the table.

Link to this function

clear_all_column_meta(table)

View Source
@spec clear_all_column_meta(t()) :: t()

Removes column meta for all columns, effectively resetting column meta back to the default options across the board.

@spec clear_rows(t()) :: t()

Removes all row data from the table, keeping everything else.

Link to this function

get_column_meta(table, col_index, key)

View Source
@spec get_column_meta(t(), integer(), atom()) :: any()

Retrieves the value of a column meta option at the specified col_index. If no value has been set, default values are returned.

@spec has_header?(t()) :: boolean()

Returns a boolean detailing if the passed table has a header row set.

@spec new() :: t()

Creates a new blank table.

The table created will not be able to be rendered until it has some row data.

Examples

iex> Table.new
%TableRex.Table{}
Link to this function

new(rows, header_row \\ [], title \\ nil)

View Source
@spec new(list(), list(), String.t() | nil) :: t()

Creates a new table with an initial set of rows and an optional header and title.

Link to this function

put_cell_meta(table, col_index, row_index, cell_meta)

View Source
@spec put_cell_meta(t(), integer(), integer(), Keyword.t()) :: t()

Sets cell level information such as alignment.

Link to this function

put_column_meta(table, col_index, col_meta)

View Source
@spec put_column_meta(t(), integer() | atom() | Enum.t(), Keyword.t()) :: t()

Sets column level information such as padding and alignment.

Link to this function

put_header(table, header_row)

View Source
@spec put_header(t(), list() | nil) :: t()

Sets a list as the optional header row. Set to nil or [] to remove an already set header from renders.

Link to this function

put_header_meta(table, col_index, cell_meta)

View Source
@spec put_header_meta(t(), integer() | Enum.t(), Keyword.t()) :: t()

Sets cell level information for the header cells.

@spec put_title(t(), String.t() | nil) :: t()

Sets a string as the optional table title. Set to nil or "" to remove an already set title from renders.

Link to this function

render(table, opts \\ [])

View Source
@spec render(t(), list()) :: TableRex.Renderer.render_return()

Renders the current table state to string, ready for display via IO.puts/2 or other means.

At least one row must have been added before rendering.

Returns {:ok, rendered_string} on success and {:error, reason} on failure.

Link to this function

render!(table, opts \\ [])

View Source
@spec render!(t(), list()) :: String.t() | no_return()

Renders the current table state to string, ready for display via IO.puts/2 or other means.

At least one row must have been added before rendering.

Returns the rendered string on success, or raises TableRex.Error on failure.

Link to this function

row_colors(result, colors)

View Source
Link to this function

sort(table, column_index, order \\ :desc)

View Source
@spec sort(t(), integer(), atom()) :: t()

Sorts the table rows by using the values in a specified column.

This is very much a simple sorting function and relies on Elixir's built-in comparison operators & types to cover the basic cases.

As each cell retains the original value it was created with, we use that value to sort on as this allows us to sort on many built-in types in the most obvious fashions.

Remember that rows are stored internally in reverse order that they will be output in, to allow for fast insertion.

Parameters:

`column_index`: the 0-indexed column number to sort by
`order`: supply :desc or :asc for sort direction.

Returns a new Table, with sorted rows.