Create text-based columns suitable for console output from objects or
arrays of objects.
Columns are automatically resized to fit the content of the largest
cell. Each cell will be padded with spaces to fill the available space
and ensure column contents are left-aligned.
As with all options, you can define the maxWidth and minWidth globally, or for specified columns. By default, wrapping will happen at word boundaries. Empty cells or those which do not fill the minWidth will be padded with spaces.
var columns = columnify([{ name: 'mod1', description: 'some description which happens to be far larger than the max', version: '0.0.1',}, { name: 'module-two', description: 'another description larger than the max', version: '0.2.0',}], { minWidth: 20, config: { description: {maxWidth: 30} }})console.log(columns)
Output:
NAME DESCRIPTION VERSION mod1 some description which happens 0.0.1 to be far larger than the max module-two another description larger 0.2.0 than the max
Maximum Line Width
You can set a hard maximum line width using the maxLineWidth option.
Beyond this value data is unceremoniously truncated with no truncation
marker.
This can either be a number or 'auto' to set the value to the width of
stdout.
Setting this value to 'auto' prevent TTY-imposed line-wrapping when
lines exceed the screen width.
Truncating Column Cells Instead of Wrapping
You can disable wrapping and instead truncate content at the maximum
column width by using the truncate option. Truncation respects word boundaries. A truncation marker, …, will appear next to the last word in any truncated line.
By default, columnify sanitises text by replacing any occurance of 1 or more whitespace characters with a single space.
columnify can be configured to respect existing new line characters using the preserveNewLines option. Note this will still collapse all other whitespace.
NAME DESCRIPTION VERSIONmod1 some description> 0.0.1 module-two another description> 0.2.0
Custom Column Splitter
If your columns need some bling, you can split columns with custom
characters by using the columnSplitter option.
var columns = columnify(data, { columnSplitter: ' | '})console.log(columns)
Output:
NAME | DESCRIPTION | VERSIONmod1 | some description which happens to be far larger than the max | 0.0.1module-two | another description larger than the max | 0.2.0
Control Header Display
Control whether column headers are displayed by using the showHeaders option.
var columns = columnify(data, { showHeaders: false})
This also works well for hiding a single column header, like an id column:
If you need to modify the presentation of column content or heading content there are two useful options for doing that: dataTransform and headingTransform. Both of these take a function and need to return a valid string.