Introduction to ASCII Table API
The ascii-table is an incredibly useful tool for manipulating and displaying data in tabular form, particularly if you are working on a terminal or console-based application. This comprehensive guide will explore various APIs provided by ascii-table and demonstrate how to integrate them effectively into your applications.
Creating a Simple ASCII Table
To create a simple ASCII table, you can use the following code:
const AsciiTable = require('ascii-table');
let table = new AsciiTable();
table
.setHeading('Name', 'Age', 'Email')
.addRow('John Doe', 25, 'john@example.com')
.addRow('Jane Smith', 30, 'jane@example.com')
.addRow('Emily Davis', 22, 'emily@example.com');
console.log(table.toString());
Customizing Table Appearance
You can easily customize the appearance of your ASCII table by adjusting its properties:
table
.setBorder('|', '=', '°', '°')
.setAlign(0, AsciiTable.CENTER)
.setAlign(1, AsciiTable.LEFT)
.setAlign(2, AsciiTable.RIGHT);
console.log(table.toString());
Adding a Title to Your Table
Adding a title to your table gives it a more professional appearance:
table.setTitle('User Information');
console.log(table.toString());
Handling Long Text
If your data contains long text, the table can automatically adjust column widths to fit:
table
.addRow('A very very long name', 29, 'example@longemail.com')
.setAlign(0, AsciiTable.LEFT);
console.log(table.toString());
Ascii Table Application Example
Let’s create a simple node.js application that makes use of the ASCII table:
const AsciiTable = require('ascii-table');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let table = new AsciiTable();
table.setHeading('Task', 'Status', 'Deadline');
function addTask(task, status, deadline) {
table.addRow(task, status, deadline);
console.log(table.toString());
}
rl.question('Enter Task: ', (task) => {
rl.question('Enter Status: ', (status) => {
rl.question('Enter Deadline: ', (deadline) => {
addTask(task, status, deadline);
rl.close();
});
});
});
In this application, users enter task information which is then displayed in an ASCII table format.
With this guide, you should now be well-equipped to leverage the ASCII table for your data representation needs in terminal applications.
Hash: 1789875865deb07c3678b62add517ead6598210d194fb43a97771853c4f317de