spruce/table

Bordered, ANSI-aware data table rendering.

Types

Horizontal alignment for a table column.

pub type Align {
  Left
  Center
  Right
}

Constructors

  • Left
  • Center
  • Right

Identifies the row kind passed to a style_fn.

pub type RowContext {
  Header
  Body(index: Int)
}

Constructors

  • Header

    The header row.

  • Body(index: Int)

    A zero-based body row.

A table of string cells with an optional header row. Build one with new and the configuration functions in this module, then render it with render.

pub opaque type Table

Values

pub fn border(table: Table, border: border.Border) -> Table

Set the table border style.

Junctions are exact for Normal, Rounded, Thick, and Double. Other border styles approximate junctions from their edge characters.

pub fn column_alignments(
  table: Table,
  alignments: List(Align),
) -> Table

Set horizontal alignment by column.

Columns without a corresponding alignment are left-aligned.

pub fn column_widths(table: Table, widths: List(Int)) -> Table

Constrain columns to maximum visual widths.

Widths less than one are ignored. Columns without a corresponding configured width use their natural content width.

pub fn headers(table: Table, headers: List(String)) -> Table

Set the optional header row.

pub fn new() -> Table

Create an empty table.

pub fn render(context: spruce.Spruce, table: Table) -> String

Render a table as a bordered grid.

pub fn row_separators(table: Table, enabled: Bool) -> Table

Toggle separator lines between body rows.

pub fn rows(table: Table, rows: List(List(String))) -> Table

Set the body rows. Short rows are padded with empty cells at render time.

pub fn style_fn(
  table: Table,
  style_fn: fn(RowContext, Int) -> style.Style,
) -> Table

Set a per-cell style function.

The RowContext argument is Header for header cells and Body(index) for zero-based body row cells.

pub fn width(table: Table, width: Int) -> Table

Constrain the overall table to a maximum visual width.

The available width after visible borders and cell padding is distributed evenly across columns. Cells that exceed their column width are wrapped with spruce/align.wrap.

✨ Search Document