What is cli-columns?
The cli-columns npm package is a utility for formatting text into columns for command-line interfaces. It helps in organizing and displaying data in a tabular format, making it easier to read and understand.
What are cli-columns's main functionalities?
Basic Column Formatting
This feature allows you to format an array of strings into columns. The code sample demonstrates how to use the cli-columns package to display a list of items in a columnar format.
const columns = require('cli-columns');
const data = ['Name', 'Age', 'Occupation', 'Location'];
console.log(columns(data));
Custom Column Width
This feature allows you to specify a custom width for the columns. The code sample shows how to set the column width to 20 characters.
const columns = require('cli-columns');
const data = ['Name', 'Age', 'Occupation', 'Location'];
console.log(columns(data, { width: 20 }));
Column Padding
This feature allows you to add padding between columns. The code sample demonstrates how to add 2 spaces of padding between each column.
const columns = require('cli-columns');
const data = ['Name', 'Age', 'Occupation', 'Location'];
console.log(columns(data, { padding: 2 }));
Other packages similar to cli-columns
columnify
The columnify package is another utility for formatting text into columns. It offers more customization options such as column alignment, data transformation, and custom column headers. Compared to cli-columns, columnify provides more advanced features for complex data formatting.
cli-table
The cli-table package is a popular choice for creating tables in the command line. It supports features like text wrapping, custom table styles, and nested tables. While cli-columns focuses on simple column formatting, cli-table offers a more comprehensive solution for creating detailed tables.
easy-table
The easy-table package is designed for creating ASCII tables in the command line. It provides features like column alignment, table sorting, and custom cell formatting. Compared to cli-columns, easy-table is more suited for creating structured tables with various formatting options.
cli-columns
Columnated lists for the CLI. Unicode and ANSI safe.
Install
$ npm install --save cli-columns
Usage
const columns = require('cli-columns');
const chalk = require('chalk');
const values = [
'blue' + chalk.bgBlue('berry'),
'笔菠萝' + chalk.yellow('苹果笔'),
chalk.red('apple'), 'pomegranate',
'durian', chalk.green('star fruit'),
'パイナップル', 'apricot', 'banana',
'pineapple', chalk.bgRed.yellow('orange')
];
console.log(columns(values));
API
columns(values [, options]): String
values
{Array<String>}
Array of strings to display.options
{Object}
character
{String}
(default: ' '
) Padding character.newline
{String}
(default: '\n'
) Newline character.padding
{Number}
(default: 2
) Space between columns.sort
{Boolean}
(default: true
) Whether to sort results.width
{Number}
(default: process.stdout.columns
) Max width of list.
Sorts and formats a list of values into columns suitable to display in a given width.
Contribute
Standards for this project, including tests, code coverage, and semantics are enforced with a build tool. Pull requests must include passing tests with 100% code coverage and no linting errors.
Test
$ npm test
MIT © Shannon Moeller