What is @oclif/table?
@oclif/table is a utility for formatting and displaying tables in command-line interfaces (CLIs) built with the oclif framework. It provides a simple way to present tabular data in a readable format.
What are @oclif/table's main functionalities?
Basic Table Rendering
This feature allows you to render a basic table with specified columns and data. The code sample demonstrates how to create a table with 'Name' and 'Age' columns using the @oclif/table package.
const { table } = require('@oclif/table');
const data = [
{ name: 'John Doe', age: 30 },
{ name: 'Jane Smith', age: 25 }
];
const columns = {
name: {
header: 'Name'
},
age: {
header: 'Age'
}
};
table(data, { columns });
Custom Column Formatting
This feature allows for custom formatting of table columns. The code sample shows how to format the 'Name' column to be uppercase and the 'Age' column to append 'years' to the age value.
const { table } = require('@oclif/table');
const data = [
{ name: 'John Doe', age: 30 },
{ name: 'Jane Smith', age: 25 }
];
const columns = {
name: {
header: 'Name',
get: row => row.name.toUpperCase()
},
age: {
header: 'Age',
get: row => `${row.age} years`
}
};
table(data, { columns });
Sorting Table Data
This feature allows you to sort table data by a specified column. The code sample demonstrates sorting the table by the 'Age' column.
const { table } = require('@oclif/table');
const data = [
{ name: 'John Doe', age: 30 },
{ name: 'Jane Smith', age: 25 }
];
const columns = {
name: {
header: 'Name'
},
age: {
header: 'Age'
}
};
table(data, { columns, sort: 'age' });
Other packages similar to @oclif/table
cli-table3
cli-table3 is a popular package for creating tables in the command line. It offers similar functionality to @oclif/table, such as custom column formatting and alignment. However, cli-table3 is more widely used outside of the oclif framework and provides more customization options for table styling.
table
The 'table' package is another alternative for rendering tables in the command line. It provides a flexible API for table creation and supports features like text wrapping and alignment. Compared to @oclif/table, it is more focused on general CLI applications rather than being tied to a specific framework like oclif.
Description
Print tables to the terminal using ink
Examples
You can see examples of how to use it in the examples directory.
You can run any of these with the following:
tsx examples/basic.ts
Contributing
See the contributing guide.