TermUI.Widgets.Table.Column (TermUI v0.2.0)

View Source

Column specification for Table widget.

Defines how a column is displayed and how data is extracted from rows.

Usage

Column.new(:name, "Name")
Column.new(:age, "Age", width: Constraint.length(10))
Column.new(:status, "Status", render: &format_status/1)

Width Constraints

Columns support the full constraint system:

  • Constraint.length(20) - Fixed 20 cells
  • Constraint.ratio(2) - Proportional share
  • Constraint.percentage(50) - 50% of available
  • Constraint.fill() - Take remaining space

Custom Renderers

The render function transforms the cell value to a string:

Column.new(:date, "Date", render: fn date ->
  Calendar.strftime(date, "%Y-%m-%d")
end)

Summary

Functions

Aligns text within a given width.

Creates a new column specification.

Extracts and renders the cell value from a row.

Types

t()

@type t() :: %TermUI.Widgets.Table.Column{
  align: :left | :center | :right,
  header: String.t(),
  key: atom(),
  render: (term() -> String.t()) | nil,
  sortable: boolean(),
  width: TermUI.Layout.Constraint.t()
}

Functions

align_text(text, width, align)

@spec align_text(String.t(), non_neg_integer(), :left | :center | :right) ::
  String.t()

Aligns text within a given width.

Parameters

  • text - The text to align
  • width - The available width
  • align - Alignment (:left, :center, :right)

Returns

The aligned text, padded to width.

new(key, header, opts \\ [])

@spec new(atom(), String.t(), keyword()) :: t()

Creates a new column specification.

Parameters

  • key - The map key to extract from row data
  • header - The header text to display
  • opts - Additional options

Options

  • :width - Width constraint (default: Constraint.fill())
  • :render - Custom render function (default: to_string/1)
  • :sortable - Whether column can be sorted (default: true)
  • :align - Text alignment :left, :center, :right (default: :left)

Examples

Column.new(:name, "Name")
Column.new(:age, "Age", width: Constraint.length(10), align: :right)

render_cell(column, row)

@spec render_cell(t(), map()) :: String.t()

Extracts and renders the cell value from a row.

Parameters

  • column - The column specification
  • row - The row data (map or struct)

Returns

The rendered string value for the cell.

Examples

column = Column.new(:name, "Name")
Column.render_cell(column, %{name: "Alice"})
# => "Alice"