CLI Column Formatter

4.0.0 · active · verified Wed Apr 22

cli-columns is a lightweight Node.js utility designed to format arrays of strings into neatly aligned, columnated lists suitable for command-line interfaces. It is particularly noted for its robust handling of both Unicode characters and ANSI escape codes (e.g., for colored text) by accurately calculating visible widths, preventing visual misalignment common in other simpler columnizers. The current stable version is 4.0.0, which notably addressed a ReDoS vulnerability in its upstream dependencies and updated its minimum Node.js requirement to version 10 or higher. While its release cadence may appear measured, the project prioritizes stability and precise rendering for CLI output. Key differentiators include customizable padding characters, column spacing, newline characters, optional sorting, and the ability to set a maximum output width, often defaulting to `process.stdout.columns`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `cli-columns` to format a list of strings, including those with Unicode and ANSI colors, into columns. It also shows how to customize width, sorting, and padding.

import columns from 'cli-columns';
import chalk from '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'),
    'long-long-item-that-will-wrap-if-width-is-too-small'
];

// Basic usage, uses process.stdout.columns for width
console.log('--- Default Columnation ---');
console.log(columns(values));

// Custom width and no sorting
console.log('\n--- Custom Width (40) & Unsorted ---');
console.log(columns(values, { width: 40, sort: false }));

// Custom padding character
console.log('\n--- Custom Padding Character (".") ---');
console.log(columns(values, { character: '.' }));

view raw JSON →