Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Pretty unicode tables for the command line

License

NotificationsYou must be signed in to change notification settings

cli-table/cli-table3

Repository files navigation

npm versionBuild Status

This utility allows you to render unicode-aided tables on the command line fromyour node.js scripts.

cli-table3 is based on (and api compatible with) the originalcli-table,andcli-table2, which are bothunmaintained.cli-table3 includes all the additional features fromcli-table2.

Screenshot

Features not in the original cli-table

  • Ability to make cells span columns and/or rows.
  • Ability to set custom styles per cell (border characters/colors, padding, etc).
  • Vertical alignment (top, bottom, center).
  • Word wrapping options.
  • More robust truncation of cell text that contains ansi color characters.
  • Better handling of text color that spans multiple lines.
  • API compatible with the original cli-table.
  • Exhaustive test suite including the entire original cli-table test suite.
  • Lots of examples auto-generated from the tests (basic,advanced).

Features

  • Customizable characters that constitute the table.
  • Color/background styling in the header throughcolors.js@colors/colors
  • Column width customization
  • Text truncation based on predefined widths
  • Text alignment (left, right, center)
  • Padding (left, right)
  • Easy-to-use API

Installation

npm install cli-table3

How to use

A portion of the unit test suite is used to generate examples:

This package is api compatible with the originalcli-table.So all the original documentation still applies (copied below).

Horizontal Tables

varTable=require('cli-table3');// instantiatevartable=newTable({head:['TH 1 label','TH 2 label'],colWidths:[100,200]});// table is an Array, so you can `push`, `unshift`, `splice` and friendstable.push(['First value','Second value'],['First value','Second value']);console.log(table.toString());

Vertical Tables

varTable=require('cli-table3');vartable=newTable();table.push({'Some key':'Some value'},{'Another key':'Another value'});console.log(table.toString());

Cross Tables

Cross tables are very similar to vertical tables, with two key differences:

  1. They require ahead setting when instantiated that has an empty string as the first header
  2. The individual rows take the general form of { "Header": ["Row", "Values"] }
varTable=require('cli-table3');vartable=newTable({head:["","Top Header 1","Top Header 2"]});table.push({'Left Header 1':['Value Row 1 Col 1','Value Row 1 Col 2']},{'Left Header 2':['Value Row 2 Col 1','Value Row 2 Col 2']});console.log(table.toString());

Custom styles

Thechars property controls how the table is drawn:

vartable=newTable({chars:{'top':'═','top-mid':'╤','top-left':'╔','top-right':'╗','bottom':'═','bottom-mid':'╧','bottom-left':'╚','bottom-right':'╝','left':'║','left-mid':'╟','mid':'─','mid-mid':'┼','right':'║','right-mid':'╢','middle':'│'}});table.push(['foo','bar','baz'],['frob','bar','quuz']);console.log(table.toString());// Outputs:////╔══════╤═════╤══════╗//║ foo  │ bar │ baz  ║//╟──────┼─────┼──────╢//║ frob │ bar │ quuz ║//╚══════╧═════╧══════╝

Empty decoration lines will be skipped, to avoid vertical separator rows justset the 'mid', 'left-mid', 'mid-mid', 'right-mid' to the empty string:

vartable=newTable({chars:{'mid':'','left-mid':'','mid-mid':'','right-mid':''}});table.push(['foo','bar','baz'],['frobnicate','bar','quuz']);console.log(table.toString());// Outputs: (note the lack of the horizontal line between rows)//┌────────────┬─────┬──────┐//│ foo        │ bar │ baz  │//│ frobnicate │ bar │ quuz │//└────────────┴─────┴──────┘

By setting all chars to empty with the exception of 'middle' being set to asingle space and by setting padding to zero, it's possible to get the mostcompact layout with no decorations:

vartable=newTable({chars:{'top':'','top-mid':'','top-left':'','top-right':'','bottom':'','bottom-mid':'','bottom-left':'','bottom-right':'','left':'','left-mid':'','mid':'','mid-mid':'','right':'','right-mid':'','middle':' '},style:{'padding-left':0,'padding-right':0}});table.push(['foo','bar','baz'],['frobnicate','bar','quuz']);console.log(table.toString());// Outputs://foo        bar baz//frobnicate bar quuz

Debugging

Later versions of cli-table3 supporting debugging your table data.

Enable and use debugging:

var table = new Table({ debug: 1 });table.push([{}, {},}); // etc.console.log(table.toString());table.messages.forEach((message) => console.log(message));

If you are rendering multiple tables with debugging on runTable.reset() afterrendering each table.

Build Targets

Clone the repository and runyarn install to install all its submodules, then run one of the following commands:

Run the tests with coverage reports.
$ yarn test:coverage
Run the tests every time a file changes.
$ yarn test:watch
Update the documentation.
$ yarn docs

Credits

License

(The MIT License)

Copyright (c) 2014 James Talmage <james.talmage@jrtechnical.com>

Original cli-table code/documentation: Copyright (c) 2010 LearnBoost <dev@learnboost.com>

Permission is hereby granted, free of charge, to any person obtaininga copy of this software and associated documentation files (the'Software'), to deal in the Software without restriction, includingwithout limitation the rights to use, copy, modify, merge, publish,distribute, sublicense, and/or sell copies of the Software, and topermit persons to whom the Software is furnished to do so, subject tothe following conditions:

The above copyright notice and this permission notice shall beincluded in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANYCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THESOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


[8]ページ先頭

©2009-2025 Movatter.jp