Comprehensive Guide to cli-table for Building Command Line Interfaces

Comprehensive Guide to cli-table for Building Command Line Interfaces

The cli-table library is a powerful tool for creating clean, formatted tables in your command-line interface (CLI) applications. This guide will introduce you to cli-table and demonstrate multiple useful APIs through examples. Whether you are a beginner or an expert, this guide aims to make you proficient in using cli-table.

Getting Started with cli-table

First, install the library using npm:

  
  npm install cli-table
  

Creating Your First Table

Here is a basic example to create a table with columns and rows:

  
  const Table = require('cli-table');

  const table = new Table({
    head: ['Name', 'Age', 'Gender'],
    colWidths: [20, 10, 10]
  });

  table.push(
    ['John Doe', 28, 'Male'],
    ['Jane Doe', 22, 'Female']
  );

  console.log(table.toString());
  

More Advanced Table Customization

Let’s explore more features of cli-table, such as cell styling and table layout:

Styling Cells

  
  const Table = require('cli-table');

  const table = new Table({
    head: ['Name', 'Age', 'Gender'],
    style: { 'padding-left': 2, 'padding-right': 2 }
  });

  table.push(
    [{ content: 'John Doe', hAlign: 'center' }, 28, 'Male'],
    [{ content: 'Jane Doe', hAlign: 'left', vAlign: 'center' }, 22, 'Female']
  );

  console.log(table.toString());
  

Table Layout: Horizontal Alignment

  
  const Table = require('cli-table');

  const table = new Table({
    head: ['Name', 'Age', 'Gender'],
    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(
    ['John Doe', 28, 'Male'],
    ['Jane Doe', 22, 'Female']
  );

  console.log(table.toString());
  

Table Layout: Vertical Layout

Transform the table into a vertical layout with rows as headers:

  
  const Table = require('cli-table');

  const table = new Table({ 
    colWidths: [10, 20] 
  });

  table.push(
    {'Name': 'John Doe'},
    {'Age': 28},
    {'Gender': 'Male'},
    {'Name': 'Jane Doe'},
    {'Age': 22},
    {'Gender': 'Female'}
  );

  console.log(table.toString());
  

Comprehensive Application Example

Integrating cli-table into an actual CLI application can look like this:

  
  const Table = require('cli-table');
  const fetch = require('node-fetch');

  const displayUsers = async () => {
    const res = await fetch('https://jsonplaceholder.typicode.com/users');
    const users = await res.json();
  
    const table = new Table({
      head: ['ID', 'Name', 'Username', 'Email'],
      colWidths: [5, 20, 15, 30]
    });
  
    users.forEach(user => {
      table.push([user.id, user.name, user.username, user.email]);
    });
  
    console.log(table.toString());
  };

  displayUsers();
  

This example introduces you to the power of cli-table when handling real-world data from an external API.

Hash: fe58c388951121efe3206f41fb8a51a41a4ba397914c9cb96c1c2f8e8f1adaa5

Leave a Reply

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