Introduction to cli-table3
cli-table3 is a powerful Node.js module that allows developers to generate beautifully formatted tables in the console. It’s an improved version of the original cli-table library with several added features and better performance. This guide will introduce you to the basics of cli-table3 and provide numerous API examples to help you get started.
Installation
npm install cli-table3
Creating a Basic Table
To create a basic table, you can use the following code snippet:
const Table = require('cli-table3');
const table = new Table();
table.push(['Name', 'Age', 'Gender']);
table.push(['John Doe', 28, 'Male']);
table.push(['Jane Doe', 32, 'Female']);
console.log(table.toString());
Customizing Table Border
You can customize the table border using the following code:
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(['Name', 'Age', 'Gender']);
table.push(['John Doe', 28, 'Male']);
table.push(['Jane Doe', 32, 'Female']);
console.log(table.toString());
Setting Column Width
You can define the column widths as demonstrated below:
const table = new Table({
colWidths: [20, 10, 10]
});
table.push(['Name', 'Age', 'Gender']);
table.push(['John Doe', 28, 'Male']);
table.push(['Jane Doe', 32, 'Female']);
console.log(table.toString());
Using Table Headers
Here is how you can add headers to your table:
const table = new Table({
head: ['Name', 'Age', 'Gender']
});
table.push(['John Doe', 28, 'Male']);
table.push(['Jane Doe', 32, 'Female']);
console.log(table.toString());
Aligning Text
You can align text within the cells as shown in this example:
const table = new Table({
head: ['Name', 'Age', 'Gender'],
colAligns: ['left', 'right', 'center']
});
table.push(['John Doe', 28, 'Male']);
table.push(['Jane Doe', 32, 'Female']);
console.log(table.toString());
Practical Application Example
Finally, let’s look at a practical application of cli-table3 in a simple CLI app:
const inquirer = require('inquirer');
const Table = require('cli-table3');
const users = [
{ name: 'John Doe', age: 28, gender: 'Male' },
{ name: 'Jane Doe', age: 32, gender: 'Female' }
];
inquirer.prompt([
{
type: 'input',
name: 'name',
message: 'Enter name:'
},
{
type: 'input',
name: 'age',
message: 'Enter age:'
},
{
type: 'list',
name: 'gender',
message: 'Select gender:',
choices: ['Male', 'Female']
}
]).then(answers => {
users.push({ name: answers.name, age: answers.age, gender: answers.gender });
const table = new Table({
head: ['Name', 'Age', 'Gender']
});
users.forEach(user => {
table.push([user.name, user.age, user.gender]);
});
console.log(table.toString());
});
With the examples provided above, you can create a wide range of tables in your CLI applications using cli-table3. Experiment with different configurations to get the style and format you desire.
Hash: ca37e4577bf930e2e85073d083005556f372aaf2b1a963cad2fc0e38bc743c19