table
Table
Produces a string that represents array data in a text table.

Features
Works with strings containing fullwidth characters.
Works with strings containing ANSI escape codes.
Configurable border characters.
Configurable content alignment per column.
Configurable content padding per column.
Configurable column width.
Text wrapping.
Install
Usage
Table data is described using an array (rows) of array (cells).
Cell Content Alignment
{string} config.columns[{number}].alignment property controls content horizontal alignment within a cell.
Valid values are: "left", "right" and "center".
Column Width
{number} config.columns[{number}].width property restricts column width to a fixed width.
Custom Border
{object} config.border property describes characters used to draw the table border.
Draw Horizontal Line
{function} config.drawHorizontalLine property is a function that is called for every non-content row in the table. The result of the function {boolean} determines whether a row is drawn.
Single Line Mode
Horizontal lines inside the table are not drawn.
Padding Cell Content
{number} config.columns[{number}].paddingLeft and {number} config.columns[{number}].paddingRight properties control content padding within a cell. Property value represents a number of whitespaces used to pad the content.
Predefined Border Templates
You can load one of the predefined border templates using getBorderCharacters function.
Raise an issue if you'd like to contribute a new border template.
Borderless Table
Simply using "void" border character template creates a table with a lot of unnecessary spacing.
To create a more plesant to the eye table, reset the padding and remove the joining rows, e.g.
Streaming
table package exports createStream function used to draw a table and append rows.
createStream requires {number} columnDefault.width and {number} columnCount configuration properties.

table package uses ANSI escape codes to overwrite the output of the last line when a new row is printed.
The underlying implementation is explained in this Stack Overflow answer.
Streaming supports all of the configuration properties and functionality of a static table (such as auto text wrapping, alignment and padding), e.g.

Text Truncation
To handle a content that overflows the container width, table package implements text wrapping. However, sometimes you may want to truncate content that is too long to be displayed in the table.
{number} config.columns[{number}].truncate property (default: Infinity) truncates the text at the specified length.
Text Wrapping
table package implements auto text wrapping, i.e. text that has width greater than the container width will be separated into multiple lines, e.g.
When wrapWord is true the text is broken at the nearest space or one of the special characters ("-", "_", "", "/", ".", ",", ";"), e.g.
Last updated
Was this helpful?
