Socket
Socket
Sign inDemoInstall

tty-table

Package Overview
Dependencies
6
Maintainers
1
Versions
89
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

tty-table


Version published
Maintainers
1
Created

Package description

What is tty-table?

The tty-table npm package is used to create and display tables in the terminal. It provides a simple and flexible way to format and present tabular data in a visually appealing manner.

What are tty-table's main functionalities?

Basic Table Creation

This feature allows you to create a basic table with specified headers and rows. The table is then rendered and displayed in the terminal.

const ttyTable = require('tty-table');
const header = [
  { value: 'Name', width: 30, headerColor: 'cyan' },
  { value: 'Age', width: 10, headerColor: 'cyan' }
];
const rows = [
  ['Alice', 30],
  ['Bob', 25]
];
const table = ttyTable(header, rows);
console.log(table.render());

Customizing Table Appearance

This feature allows you to customize the appearance of the table, including border style, border color, padding, alignment, and text color.

const ttyTable = require('tty-table');
const header = [
  { value: 'Name', width: 30, headerColor: 'cyan', color: 'white', align: 'left' },
  { value: 'Age', width: 10, headerColor: 'cyan', color: 'white', align: 'right' }
];
const rows = [
  ['Alice', 30],
  ['Bob', 25]
];
const options = {
  borderStyle: 'solid',
  borderColor: 'blue',
  paddingBottom: 0,
  headerAlign: 'center',
  align: 'center',
  color: 'white'
};
const table = ttyTable(header, rows, options);
console.log(table.render());

Adding Footers

This feature allows you to add footers to the table, which can be used to display summary information or totals.

const ttyTable = require('tty-table');
const header = [
  { value: 'Name', width: 30, headerColor: 'cyan' },
  { value: 'Age', width: 10, headerColor: 'cyan' }
];
const rows = [
  ['Alice', 30],
  ['Bob', 25]
];
const footer = [
  { value: 'Total', colspan: 1, align: 'right' },
  { value: '2', align: 'right' }
];
const table = ttyTable(header, rows, { footer });
console.log(table.render());

Other packages similar to tty-table

Readme

Source

tty-table 端子台

Build Status NPM version Coverage Status

Display your data in a table using a terminal, browser, or browser console.


Examples

See here for complete example list

To view all example output:

$ git clone https://github.com/tecfu/tty-table && cd tty-table && npm i
$ npm run view-examples

Terminal (Static)

examples/styles-and-formatting.js

Static

Terminal (Streaming)

$ node examples/data/fake-stream.js | tty-table --format json --header examples/config/header.js

Streaming

  • See the built-in help for the terminal version of tty-table with:
$ tty-table -h

Browser & Browser Console

Browser Console Example



API Reference

Table(header array, rows array, options object)

ParamTypeDescription
headerarrayPer-column configuration. An array of objects, one object for each column. Each object contains properties you can use to configure that particular column. See available properties
rowsarrayYour data. An array of arrays or objects. See examples
optionsobjectGlobal table configuration. See available properties

header array of objects
ParamTypeDescription
aliasstringText to display in column header cell
alignstringdefault: "center"
colorstringdefault: terminal default color
footerAlignstringdefault: "center"
footerColorstringdefault: terminal default color
formatterfunction(cellValue, columnIndex, rowIndex, rowData, inputData)Runs a callback on each cell value in the parent column.
Use this.style within function body to style text, i.e. this.style("mytext", "bold", "green", "underline").
Please note that fat arrow functions () => {} don't support scope overrides, and this feature won't work within them. For a full list of options, see: chalk.
headerAlignstringdefault: "center"
headerColorstringdefault: terminal's default color
marginLeftintegerdefault: 0
marginTopintegerdefault: 0
paddingBottomintegerdefault: 0
paddingLeftintegerdefault: 1
paddingRightintegerdefault: 1
paddingTopintegerdefault: 0
valuestringName of the property to display in each cell when data passed as an array of objects
widthstring || integerdefault: "auto"
Can be a percentage of table width i.e. "20%" or a fixed number of columns i.e. "20".
When set to the default ("auto"), the column widths are made proportionate by the longest value in each column.
Note: Percentage columns and fixed value colums not intended to be mixed in the same table.

Example

let header = [{
  value: "item",
  headerColor: "cyan",
  color: "white",
  align: "left",
  width: 20
},
{
  value: "price",
  color: "red",
  width: 10,
  formatter: function (value) {
    let str = `$${value.toFixed(2)}`
    return (value > 5) ? this.style(str, "green", "bold") : 
      this.style(str, "red", "underline")
  }
}]


rows array

Example

  • each row an array
const rows = [
  ["hamburger",2.50],
]
  • each row an object
const rows = [
  {
    item: "hamburger",
    price: 2.50
  }
]


  • Footer is optional

Example

const footer = [
  "TOTAL",
  function (cellValue, columnIndex, rowIndex, rowData) {
    let total = rowData.reduce((prev, curr) => {
      return prev + curr[1]
    }, 0)
    .toFixed(2)

    return this.style(`$${total}`, "italic")
  }
]


options object
ParamTypeDescription
borderStylestringdefault: "solid".
options: "solid", "dashed", "none"
borderColorstringdefault: terminal default color
colorstringdefault: terminal default color
compactbooleandefault: false
Removes horizontal borders when true.
defaultErrorValuemixeddefault: '�'
defaultValuemixeddefault: '?'
errorOnNullbooleandefault: false
truncatemixeddefault: false
When this property is set to a string, cell contents will be truncated by that string instead of wrapped when they extend beyond of the width of the cell.
For example if:
"truncate":"..."
the cell will be truncated with "..."
Note: tty-table wraps overflowing cell text into multiple lines by default, so you would likely only utilize truncate for extremely long values.
widthstringdefault: "100%"
Width of the table. Can be a percentage of i.e. "50%" or a fixed number of columns in the terminal viewport i.e. "100".
Note: When you use a percentage, your table will be "responsive".

Example

const options = {
  borderStyle: "solid",
  borderColor: "blue",
  headerAlign: "center",
  align: "left",
  color: "white",
  truncate: "...",
  width: "90%"
}

Table.render() ⇒ String

Add method to render table to a string

Example

const out = Table(header,rows,options).render()
console.log(out); //prints output


Installation

$ npm install tty-table -g
  • Node Module
$ npm install tty-table
  • Browser
import Table from 'https://cdn.jsdelivr.net/gh/tecfu/tty-table/dist/tty-table.esm.js'
let Table = require('tty-table')   // https://cdn.jsdelivr.net/gh/tecfu/tty-table/dist/tty-table.cjs.js
let Table = TTY_Table;             // https://cdn.jsdelivr.net/gh/tecfu/tty-table/dist/tty-table.umd.js

Version Compatibility

Node Versiontty-table Version
11<= 3.0
8>= 2.0
0.11>= 0.0

Running tests

$ npm test
$ npm run coverage

Saving the output of new unit tests

$ npm run save-tests

Dev Tips

  • To generate vim tags (make sure jsctags is installed globally)
$ npm run tags
  • To generate vim tags on file save
$ npm run watch-tags

Pull Requests

Pull requests are encouraged!

  • Please remember to add a unit test when necessary
  • Please format your commit messages according to the "Conventional Commits" specification

If you aren't familiar with Conventional Commits, here's a good article on the topic

TL/DR:

  • feat: a feature that is visible for end users.
  • fix: a bugfix that is visible for end users.
  • chore: a change that doesn't impact end users (e.g. chances to CI pipeline)
  • docs: a change in the README or documentation
  • refactor: a change in production code focused on readability, style and/or performance.

Packaging as a distributable

License

MIT License

Copyright 2015-2020, Tecfu.

Keywords

FAQs

Last updated on 06 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc