Skip to content
Node CLI table. Also works in browser console. Word wrap, padding, alignment, colors, Asian character support, per-column callbacks, and you can pass rows as objects or arrays. Backwards compatible with Automattic/cli-table.
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Added issue template for Github Mar 12, 2018
adapters
dist Updated browser dist Nov 27, 2019
docker Updated node version in Centos test container Jul 6, 2019
docs Added docs for piping data inline. Jun 7, 2016
examples
src Fix doc spacing typo Dec 4, 2019
test Added columnIndex, rowIndex, rowData, inputData accessors for formatt… Nov 27, 2019
.babelrc Updated deps Oct 4, 2018
.editorconfig To fix indentation on github Apr 20, 2017
.eslintrc.js Refactoring, styleguide update Nov 3, 2019
.gitignore
.travis.yml Updated travis xvfb syntax Oct 17, 2019
Gruntfile.js Build now dependent on local modules only Oct 17, 2019
LICENSE.txt Changed license to MIT such that businesses may use in closed source Jun 22, 2018
README.md Fix doc spacing typo Dec 4, 2019
conf.json Updated docs. Nov 5, 2017
package-lock.json Added table of contents to README Nov 27, 2019
package.json Added table of contents to README Nov 27, 2019
packaging.md Added note. Jun 21, 2016

README.md

tty-table 电传打字台

Build Status NPM version


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


Table of Contents

Examples

See here for complete example list

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

Working Example in Browser

Note that neither ASCI colors nor default borders are rendered in the browser. An alternative border style, as shown below, should be used by setting the following option:

borderStyle : "dashed"


API Reference

Table(header array, rows array, options object)

Param Type Description
header array Per-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
rows array Your data. An array of arrays or objects. See examples
options object Global table configuration. See available properties

header array of objects

Param Type Description
alias string Text to display in column header cell
align string default: "center"
color string default: terminal default color
footerAlign string default: "center"
footerColor string default: terminal default color
formatter function(cellValue, columnIndex, rowIndex, rowData, inputData) Runs a callback on each cell value in the parent column
headerAlign string default: "center"
headerColor string default: terminal's default color
marginLeft integer default: 0
marginTop integer default: 0
width string || integer default: "auto"
paddingBottom integer default: 0
paddingLeft integer default: 1
paddingRight integer default: 1
paddingTop integer default: 0
value string Name of the property to display in each cell when data passed as an array of objects

Example

let header = [
  {
    alias: "my items",
    value: "item",
    headerColor: "cyan",
    color: "white",
    align: "left",
    paddingLeft: 5,
    width: 30
  },
  {
    value: "price", // if not set, alias will default to "price"
    color: "red",
    width: 10,
    formatter: function(cellValue) {
      var str = `$${cellValue.toFixed(2)}`
      if(value > 5) {
        str = chalk.underline.green(str)
      }
      return str
    }
  }
]


rows array

Example

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


options object

Param Type Description
borderStyle string default: "solid". "solid", "dashed", "none"
borderColor string default: terminal default color
color string default: terminal default color
compact boolean default: false Removes horizontal lines when true.
defaultErrorValue mixed default: 'ERROR!'
defaultValue mixed default: '?'
errorOnNull boolean default: false
truncate mixed default: 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 "..."

Example

const options = {
  borderStyle: 1,
  borderColor: "blue",
  headerAlign: "center",
  align: "left",
  color: "white",
  truncate: "..."
}

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
<script src="tty-table.bundle.min.js"></script>
<script>
 const Table = require('tty-table');
 ...
</script>

Running tests

$ npx grunt test

Saving the output of new unit tests

$ npx grunt st
  • Because:

node script.js --color=always

Dev Tips

  • To generate vim tags (make sure jsctags is installed globally)
$ npx grunt tags
  • To generate vim tags on file save
$ npx grunt watch

Packaging as a distributable

License

MIT License

Copyright 2015-2019, Tecfu.

You can’t perform that action at this time.