On this page:
str.tabular
str.Cell
str.Cell.cont
str.Cell  Property
str.Cell  Properties
str.Vertical  Align
str.Vertical  Align.top
str.Vertical  Align.vcenter
str.Vertical  Align.bottom
str.Border
str.Border.border
str.Border.top_  border
str.Border.left_  border
str.Border.right_  border
str.Border.bottom_  border
str.Cell  Padding
str.Cell  Padding.left
str.Cell  Padding.top
str.Cell  Padding.right
str.Cell  Padding.bottom
9.0.0.10

12.2 Table Formatting🔗ℹ

 import: rhombus/tabular package: rhombus-lib

The rhombus/tabular module extends the str namespace with str.tabular for formatting a two-dimensional table of strings.

function

fun str.tabular(

  [[cell :: str.Cell, ...], ...],

  ~pad: pad :: str.CellPadding || Nat || [Nat, Nat] || [Nat, Nat, Nat, Nat]

          = 0,

  ~column_properties: colum_properties :: List.of(str.CellProperties)

                        = [],

  ~row_properties: row_properties :: List.of(str.CellProperties)

                     = [],

  ~cell_properties: cell_properties :: List.of(List.of(str.CellProperties))

                      = []

) :: String

 

enumeration

enum str.Cell:

  ~is_a String

  cont

Returns a string that formats the given cells into a table. Each inner list of cells represents a row, so the argument to str.tabular is a list of rows. The width and height (in characters) of each row and column is inferred, while pad, cell_properties, column_properties, and row_properties are merged to determine padding space around a cell, horizontal alignment, vertical alignment, and where borders are drawn around or between cells.

> println(str.tabular([["apple", "banana"],

                       ["cherry", "durian"]]))

apple banana

cherrydurian

> println(str.tabular([["apple", "banana"],

                       ["cherry", "durian"]],

                      ~pad: [1, 0],

                      ~cell_properties: [[#'border]]))

+--------+--------+

| apple  | banana |

+--------+--------+

| cherry | durian |

+--------+--------+

The resulting string has newlines for a table that has multiple rows, but it does not end in a newline after the last row. Each cell string can itself contain newlines, in which case the corresponding cell will span multiple lines, and the resulting string will have newlines even if it formats just one row.

> println(str.tabular(

            [["fruits", str.tabular([["apple", "banana"],

                                     ["cherry", "durian"]],

                                    ~pad: [1, 0],

                                    ~cell_properties: [[#'border]])],

             ["veggies", str.tabular([["eggplant"]],

                                     ~cell_properties: [[#'border]])]],

            ~pad: [1, 0],

            ~cell_properties: [[#'vcenter]]

          ))

          +--------+--------+

          | apple  | banana |

 fruits   +--------+--------+

          | cherry | durian |

          +--------+--------+

          +--------+          

 veggies  |eggplant|

          +--------+          

A cell that is not in the first column of a row can be #'cont to indicate that the previous cell’s content continues into the next column. Table formatting ensures that spanned columns via #'cont are together wide enough for the cell content, instead of requiring just the content’s first cell to fit the entire content.

> println(str.tabular([["apple", "banana"],

                       ["cantaloupe", #'cont]],

                      ~pad: [1, 0],

                      ~cell_properties: [[#'border]]))

+-------+--------+

| apple | banana |

+-------+--------+

| cantaloupe     |

+-------+--------+

If pad is not a str.CellPadding, it is converted to one by treating a single as a padding for all sizes, a list of two Nats and horizontal and vertical padding, and a list of four Nat as left, top, right, and bottom padding. The normalized pad becomes the default property for all cells.

Each property (or list of propertie) in colum_properties is then combined with the properties accumulated so far for each cell in the corresponding column, where the last elemen of colum_properties (is any) is duplicated when thete are more column cells than elements of colum_properties. Combining means that properties in colum_properties can complement or supercede the padding specified as pad.

Along similar lines, each property (or list of properties) in row_properties is combined, dupliacting the last element of row_properties as needed to match the number of cell rows, complementing and superceding any properties from colum_properties.

Finally, cell_properties provides cell-specific properties, where the last lists of cell_properties is duplicated as need to match the number of cell rows, and the last element of each list in cell_properties is duplicated as need to match the cell columns. These cell-specific properties complement or supercede properties from row_properties, colum_properties, and pad.

> println(str.tabular(

            [["apple", "banana"],

             ["cherry", "durian"]],

            ~pad: [1, 0],

            ~row_properties: [#'top_border, #'bottom_border],

            ~column_properties: [[#'left_border, #'right_border], #'border]

          ))

+--------+--------+

| apple  | banana |

|        +--------+

| cherry | durian |

+--------+--------+

The default alignment for a cell is #'left and #'top, and a cell has no broders by default.

enumeration

enum str.CellProperty:

  ~is_a str.Align

  ~is_a str.VerticalAlign

  ~is_a str.Border

  ~is_a str.CellPadding

 

annotation

str.CellProperties

A str.CellProperty describes formatting options for an individual cell in a table formatted with str.tabular.

The str.CellProperties annotation is equivalent to str.CellProperty || List.of(str.CellProperty). The individual elements in a list of cell properties provided to str.tabular satisfy str.CellProperties, which means that each can be a single property or a list of properties to combine for a cell.

enumeration

enum str.VerticalAlign:

  top

  vcenter

  bottom

Vertical alignment options for a cell formatted by str.tabular. Horizontal alignment uses str.Align as provided directly by rhombus.

Border options for or a cell formatted by str.tabular. The #'border option is equivalent to specifying all of #'top_border, #'left_border, #'right_border, and #'bottom_border.

Merging for border options is cumulative, combining #'left_border with an existing #'border option is the same as #'border, not a reduction in borders to just #'left_border.

Borders overlaop on the shared edge of adjacent cells. For example, if one cell has #'right_border and the cell just to its right has #'left_border, then only one border is drawn, and it’s the same if either #'right_border or #'left_border (but not both) is removed.

class

class str.CellPadding(left :: Nat,

                      top :: Nat,

                      right :: Nat,

                      bottom :: Nat):

  constructor (around :: Real = 0,

               ~horiz: horiz :: Real = around,

               ~vert: vert :: Real = around,

               ~left: left :: Real = horiz,

               ~top: top :: Real = vert,

               ~right: right :: Real = horiz,

               ~bottom: bottom :: Real = vert)

Describes padding for a cell as formatted by str.tabular. For each direction, a padding of amt adds amt space characters (before a border, if any).