Comprehensive Guide to Using cli-table for Command Line Interface Tables

Comprehensive Guide to Using cli-table for Command Line Interface Tables

The cli-table package is a popular library used in Node.js to generate visually appealing tables for command-line applications. It allows you to create tables with various formatting options and is ideal for displaying data in a tabular format right in your terminal.

Introduction to cli-table

The cli-table library is easy to use and supports multiple customization options, ensuring that the tables fit perfectly with the needs of your command-line application. Whether you need to display simple data or complex tables with various headers, cli-table has got you covered.

Getting Started

First, you need to install the cli-table package:

 npm install cli-table 

Basic Usage

Creating a simple table with cli-table is straightforward. Here’s a basic example:

 const Table = require('cli-table');
// instantiate const table = new Table({
    head: ['TH 1 label', 'TH 2 label']
  , colWidths: [100, 200]
});
// table is an Array, so you can `push`, `unshift`, `splice` and friends table.push(
    ['First value', 'Second value']
  , ['First value', 'Second value']
);
console.log(table.toString()); 

Advanced Features

Column Alignments

You can align the text within the columns:

 const table = new Table({
  head: ['R'], 
  colWidths: [10],
  colAligns: ['right'] 
});
table.push(['value 1'], ['value 2']);
console.log(table.toString()); 

Column Colors

The cli-table library allows setting colors for headers and cells using the colors package:

 const colors = require('colors'); const Table = require('cli-table');
let table = new Table({
  head: [colors.red('Heading 1'), colors.green('Heading 2')]
});
table.push(
  [colors.yellow('Value 1'), colors.blue('Value 2')],
  ['Value 3', 'Value 4']
);
console.log(table.toString()); 

Custom Borders

You can customize the borders of the table as follows:

 const table = new Table({
  chars: { 
    'top': '=' , 'top-mid': '||' , 'top-left': '*' , 'top-right' : '*'
  , 'bottom': '=' , 'bottom-mid': '||' , 'bottom-left': '#' , 'bottom-right': '#'
  , 'left': '>' , 'left-mid': '>' , 'mid': '-' , 'mid-mid': '+', 'right': '<' , 'right-mid': '<'
  , 'middle': '|' 
  }
});
table.push(
    ['value 1', 'value 2']
);
console.log(table.toString()); 

App Example Using cli-table

Let's build a small application that reads data from an array and displays it using cli-table:

 const Table = require('cli-table');
const data = [
  {name: 'John', age: 28, city: 'New York'},
  {name: 'Jane', age: 32, city: 'San Francisco'},
  {name: 'Mike', age: 45, city: 'Chicago'}
];
const table = new Table({
  head: ['Name', 'Age', 'City']
});
data.forEach(row => {
  table.push([row.name, row.age, row.city]);
});
console.log(table.toString()); 

This small example initializes a table with header values and pushes rows of data to it, displaying the information neatly formatted in the command-line interface.

By leveraging the cli-table package in your Node.js application, you can greatly enhance the readability and organization of tabular data in your CLI tools.

Hash: fe58c388951121efe3206f41fb8a51a41a4ba397914c9cb96c1c2f8e8f1adaa5

Leave a Reply

Your email address will not be published. Required fields are marked *