What is ink-table?
The ink-table npm package is used to render tables in Ink applications. Ink is a React-like library for building command-line interfaces (CLIs). ink-table allows you to easily display tabular data in a visually appealing way within your CLI applications.
What are ink-table's main functionalities?
Basic Table Rendering
This feature allows you to render a basic table with rows and columns. The data is passed as an array of objects, where each object represents a row.
const { render } = require('ink');
const Table = require('ink-table').default;
const React = require('react');
const data = [
{ name: 'John Doe', age: 28 },
{ name: 'Jane Doe', age: 26 }
];
render(<Table data={data} />);
Custom Column Headers
This feature allows you to customize the column headers of the table. You can specify the header text and the key that corresponds to the data field.
const { render } = require('ink');
const Table = require('ink-table').default;
const React = require('react');
const data = [
{ name: 'John Doe', age: 28 },
{ name: 'Jane Doe', age: 26 }
];
const columns = [
{ header: 'Full Name', key: 'name' },
{ header: 'Age', key: 'age' }
];
render(<Table data={data} columns={columns} />);
Table with Custom Styles
This feature allows you to apply custom styles to the table. In this example, the table is wrapped in a Box component with a green border.
const { render } = require('ink');
const Table = require('ink-table').default;
const React = require('react');
const { Box } = require('ink');
const data = [
{ name: 'John Doe', age: 28 },
{ name: 'Jane Doe', age: 26 }
];
const CustomTable = () => (
<Box borderStyle="round" borderColor="green">
<Table data={data} />
</Box>
);
render(<CustomTable />);
Other packages similar to ink-table
cli-table
cli-table is a widely-used package for rendering tables in the command line. It offers a lot of customization options for table appearance, including column alignment, padding, and border styles. Compared to ink-table, cli-table is more focused on traditional CLI applications and does not integrate with React-like components.
table
The table package is another popular option for creating tables in the command line. It provides a simple API for defining table data and supports various formatting options. Like cli-table, it is not designed to work with Ink or React-like components, making it less suitable for Ink-based applications.
blessed-contrib
blessed-contrib is a library for building dashboards and other terminal-based UIs. It includes a table component among many other widgets like charts and graphs. While it offers more features than ink-table, it is also more complex and not specifically designed for use with Ink.
ink-table
A table component for Ink.
Install
npm install ink-table
Usage
import Table from 'ink-table'
const data = [
{
name: "Sosa Saunders",
gender: "male",
age: 17,
email: "sosa.saunders@mail.com",
phone: "+1 (809) 435-2786"
},
{
name: "Angelina Kirk",
gender: "female",
age: 3,
email: "angelina@kirk.io",
phone: "+1 (870) 567-3516"
},
{
name: "Bradford Rosales",
gender: "male",
age: 20,
email: "bradfordrosales@fast.com",
phone: "+1 (918) 573-3240"
},
{
name: "Gwen Schroeder",
gender: "female",
age: 17,
email: "gwen@corp.xyz",
phone: "+1 (987) 417-2062"
},
{
name: "Ellison Mann",
gender: "male",
age: 5,
email: "ellisonmann@katakana.com",
phone: "+1 (889) 411-2186"
}
];
const Basic = () => (
<Table data={data} />
);
render(<Basic />);
Props
data array<object>
List of all the values (rows).
padding number
Offset inside each cell. This is considered one side value (set to 2 will have 2 spaces on the left and on the right - 4 combined).
A component used as header cell. Value is passed as children
prop.
(Recommend using <Color/>
with chalk
props.)
cell ({children}) => h
A component used as regular cell. Value is passed as children
prop.
(Recommend using <Color/>
with chalk
props.)
skeleton ({children}) => h
A component used as skeleton (lines and crosses ...). Value is passed as children
prop.
(Recommend using <Color/>
with chalk
props.)
License
MIT © Matic Zavadlal