Socket
Socket
Sign inDemoInstall

tty-table

Package Overview
Dependencies
15
Maintainers
1
Versions
89
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.0 to 2.0.0

cli-table-adapter.js

8

examples/chinese-characters-output.txt
┌───────────────────┬───────────────────┬───────────────────┐
│ 项目 │ 价格 │ 有机 │
│ 项目 │ 价格 │ 有机 │
├───────────────────┼───────────────────┼───────────────────┤
│ 汉堡包 │ 2.5 │ #ERR │
│ 汉堡包 │ 2.5 │ ? │
├───────────────────┼───────────────────┼───────────────────┤
│ 特制的酱汁 │ 0.1 │ #ERR │
│ 特制的酱汁 │ 0.1 │ ? │
├───────────────────┼───────────────────┼───────────────────┤

@@ -14,3 +14,3 @@ │ 2玉米饼, 大米和 │ 9.8 │ │

├───────────────────┼───────────────────┼───────────────────┤
│ #ERR │ 1.5 │ no │
│ ? │ 1.5 │ no │
├───────────────────┼───────────────────┼───────────────────┤

@@ -17,0 +17,0 @@ │ 意大利粉, 火腿, │ 3.75 │ no │

┌───────────────────┬───────────────────┬───────────────────┐
│ item │ price │ Is organic? │
├───────────────────┼───────────────────┼───────────────────┤
│hamburger │ $2.50 │ no │
├───────────────────┼───────────────────┼───────────────────┤
│el jefe's special │ $0.10 │ yes │
│cream sauce │ │ │
├───────────────────┼───────────────────┼───────────────────┤
│two tacos, rice and│ $9.80 │ no │
│beans topped with │ │ │
│cheddar cheese │ │ │
├───────────────────┼───────────────────┼───────────────────┤
│apple slices │ $1.00 │ yes │
├───────────────────┼───────────────────┼───────────────────┤
│ham sandwich │ $1.50 │ no │
├───────────────────┼───────────────────┼───────────────────┤
│macaroni, ham and │ $3.75 │ no │
│peruvian mozzarella│ │ │
├───────────────────┼───────────────────┼───────────────────┤
│ TOTAL │ 18.65 │ 33.33% │
└───────────────────┴───────────────────┴───────────────────┘
┌─────────────────────────────┬─────────┬──────────────┐
│ item │ price │ Is organic? │
├─────────────────────────────┼─────────┼──────────────┤
│ hamburger │ $2.50 │ no │
├─────────────────────────────┼─────────┼──────────────┤
│ el jefe's special │ $0.10 │ yes │
│ cream sauce │ │ │
├─────────────────────────────┼─────────┼──────────────┤
│ two tacos, rice and │ $9.80 │ no │
│ beans topped with │ │ │
│ cheddar cheese │ │ │
├─────────────────────────────┼─────────┼──────────────┤
│ apple slices │ $1.00 │ yes │
├─────────────────────────────┼─────────┼──────────────┤
│ ham sandwich │ $1.50 │ no │
├─────────────────────────────┼─────────┼──────────────┤
│ macaroni, ham and │ $3.75 │ no │
│ peruvian mozzarella │ │ │
├─────────────────────────────┼─────────┼──────────────┤
│ TOTAL │ 18.65 │ 33.33% │
└─────────────────────────────┴─────────┴──────────────┘
┌───────────────────┬───────────────────┬───────────────────┐
│ item │ price │ Is organic? │
├───────────────────┼───────────────────┼───────────────────┤
│hamburger │ $2.50 │ no │
├───────────────────┼───────────────────┼───────────────────┤
│el jefe's special │ $0.10 │ yes │
│cream sauce │ │ │
├───────────────────┼───────────────────┼───────────────────┤
│two tacos, rice and│ $9.80 │ no │
│beans topped with │ │ │
│cheddar cheese │ │ │
├───────────────────┼───────────────────┼───────────────────┤
│apple slices │ $1.00 │ yes │
├───────────────────┼───────────────────┼───────────────────┤
│ham sandwich │ $1.50 │ no │
├───────────────────┼───────────────────┼───────────────────┤
│macaroni, ham and │ $3.75 │ no │
│peruvian mozzarella│ │ │
└───────────────────┴───────────────────┴───────────────────┘
┌─────────────────────────────┬─────────┬──────────────┐
│ item │ price │ Is organic? │
├─────────────────────────────┼─────────┼──────────────┤
│ hamburger │ $2.50 │ no │
├─────────────────────────────┼─────────┼──────────────┤
│ el jefe's special │ $0.10 │ yes │
│ cream sauce │ │ │
├─────────────────────────────┼─────────┼──────────────┤
│ two tacos, rice and │ $9.80 │ no │
│ beans topped with │ │ │
│ cheddar cheese │ │ │
├─────────────────────────────┼─────────┼──────────────┤
│ apple slices │ $1.00 │ yes │
├─────────────────────────────┼─────────┼──────────────┤
│ ham sandwich │ $1.50 │ no │
├─────────────────────────────┼─────────┼──────────────┤
│ macaroni, ham and │ $3.75 │ no │
│ peruvian mozzarella │ │ │
└─────────────────────────────┴─────────┴──────────────┘

@@ -8,6 +8,6 @@ var Table = require('../');

headerColor : "cyan",
color: "yellow",
color: "white",
align : "left",
// paddingRight : 5,
// width : 30
paddingLeft : 5,
width : 30
},

@@ -17,2 +17,3 @@ {

color : "red",
width : 10,
formatter : function(value){

@@ -29,6 +30,10 @@ var str = "$" + value.toFixed(2);

value : "organic",
width : 15,
formatter : function(value){
//will convert an empty string to 0
//value = value * 1;
if(value === 'yes'){
value = chalk.stripColor(value);
value = chalk.green(value);
value = chalk.black.bgGreen(value);
}

@@ -69,2 +74,3 @@ else{

borderStyle : 1,
borderColor : "blue",
paddingBottom : 0,

@@ -71,0 +77,0 @@ headerAlign : "center",

┌───────────────────┬───────────────────┬───────────────────┐
│ item │ price │ organic │
├───────────────────┼───────────────────┼───────────────────┤
│ hamburger │ 2.5 │ #ERR │
├───────────────────┼───────────────────┼───────────────────┤
│ el jefe's special │ 0.1 │ #ERR │
│ cream sauce │ │ │
├───────────────────┼───────────────────┼───────────────────┤
│two tacos, rice and│ 9.8 │ │
│ beans topped with │ │ │
│ cheddar cheese │ │ │
├───────────────────┼───────────────────┼───────────────────┤
│ apple slices │ 1 │ yes │
├───────────────────┼───────────────────┼───────────────────┤
│ #ERR │ 1.5 │ no │
├───────────────────┼───────────────────┼───────────────────┤
│ macaroni, ham and │ 3.75 │ no │
│peruvian mozzarella│ │ │
├───────────────────┼───────────────────┼───────────────────┤
│ TOTAL│ 18.65│ N/A│
└───────────────────┴───────────────────┴───────────────────┘
┌───────────────────┬─────────┬─────────┐
│ item │ price │ organic │
├───────────────────┼─────────┼─────────┤
│ ? │ ? │ ? │
├───────────────────┼─────────┼─────────┤
│ hamburger │ 2.5 │ ? │
├───────────────────┼─────────┼─────────┤
│ el jefe's special │ 0.1 │ ? │
│ cream sauce │ │ │
├───────────────────┼─────────┼─────────┤
│two tacos, rice and│ 9.8 │ │
│ beans topped with │ │ │
│ cheddar cheese │ │ │
├───────────────────┼─────────┼─────────┤
│ apple slices │ 1 │ yes │
├───────────────────┼─────────┼─────────┤
│ ? │ 1.5 │ no │
├───────────────────┼─────────┼─────────┤
│ macaroni, ham and │ 3.75 │ no │
│peruvian mozzarella│ │ │
├───────────────────┼─────────┼─────────┤
│ chocolate cake │ 4.65 │ no │
├───────────────────┼─────────┼─────────┤
│ TOTAL│ 18.65│ N/A│
└───────────────────┴─────────┴─────────┘
┌───────────────────┬───────────────────┬───────────────────┐
│ item │ price │ organic │
├───────────────────┼───────────────────┼───────────────────┤
│ hamburger │ 2.5 │ #ERR │
├───────────────────┼───────────────────┼───────────────────┤
│ el jefe's special │ 0.1 │ #ERR │
│ cream sauce │ │ │
├───────────────────┼───────────────────┼───────────────────┤
│two tacos, rice and│ 9.8 │ no │
│ beans topped with │ │ │
│ cheddar cheese │ │ │
├───────────────────┼───────────────────┼───────────────────┤
│ apple slices │ 1 │ yes │
├───────────────────┼───────────────────┼───────────────────┤
│ #ERR │ 1.5 │ no │
├───────────────────┼───────────────────┼───────────────────┤
│ macaroni, ham and │ 3.75 │ no │
│peruvian mozzarella│ │ │
└───────────────────┴───────────────────┴───────────────────┘
┌───────────────────┬─────────┬─────────┐
│ item │ price │ organic │
├───────────────────┼─────────┼─────────┤
│ ? │ ? │ ? │
├───────────────────┼─────────┼─────────┤
│ hamburger │ 2.5 │ ? │
├───────────────────┼─────────┼─────────┤
│ el jefe's special │ 0.1 │ ? │
│ cream sauce │ │ │
├───────────────────┼─────────┼─────────┤
│two tacos, rice and│ 9.8 │ no │
│ beans topped with │ │ │
│ cheddar cheese │ │ │
├───────────────────┼─────────┼─────────┤
│ apple slices │ 1 │ yes │
├───────────────────┼─────────┼─────────┤
│ ? │ 1.5 │ no │
├───────────────────┼─────────┼─────────┤
│ macaroni, ham and │ 3.75 │ no │
│peruvian mozzarella│ │ │
└───────────────────┴─────────┴─────────┘

@@ -13,5 +13,7 @@ var Chalk = require('chalk');

value : "price",
width : 10
},
{
value : "organic",
width : 10
}

@@ -22,2 +24,3 @@ ];

var rows = [
[],
["hamburger",2.50,null],

@@ -35,3 +38,3 @@ ["el jefe's special cream sauce",0.10],

return rows.reduce(function(prev,curr){
return prev+curr[1]
return (typeof curr[1] === 'number') ? prev+curr[1] : prev
},0)

@@ -51,3 +54,7 @@ }()),

str1 = t1.render();
t1.push(
["chocolate cake",4.65,"no"]
);
var str1 = t1.render();
console.log(str1);

@@ -57,2 +64,3 @@

var rows = [
{},
{

@@ -59,0 +67,0 @@ item : "hamburger",

{
"name": "tty-table",
"version": "1.1.0",
"version": "2.0.0",
"description": "Command line table generator.",

@@ -30,3 +30,5 @@ "main": "src/main.js",

"chalk": "^1.1.0",
"csv": "^0.4.6",
"merge": "^1.2.0",
"minimist": "^1.2.0",
"strip-ansi": "^3.0.0",

@@ -33,0 +35,0 @@ "word-wrap": "^1.1.0"

@@ -35,4 +35,4 @@ # tty-table

- [Supports Asian characters](https://github.com/tecfu/tty-table/pull/5)
- [Responsive](https://github.com/tecfu/tty-table/issues/4)
- [Footer Support](https://github.com/tecfu/tty-table/issues/6)
- [Automatically resizes to terminal width](https://github.com/tecfu/tty-table/issues/4)
- [Footer support](https://github.com/tecfu/tty-table/issues/6)

@@ -61,3 +61,3 @@ ## Example Output

```
var Table = require('../');
var Table = require('tty-table');
var chalk = require('chalk');

@@ -69,6 +69,6 @@

headerColor : "cyan",
color: "yellow",
color: "white",
align : "left",
// paddingRight : 5,
// width : 30
paddingLeft : 5,
width : 30
},

@@ -78,2 +78,3 @@ {

color : "red",
width : 10,
formatter : function(value){

@@ -90,6 +91,10 @@ var str = "$" + value.toFixed(2);

value : "organic",
width : 15,
formatter : function(value){
//will convert an empty string to 0
//value = value * 1;
if(value === 'yes'){
value = chalk.stripColor(value);
value = chalk.green(value);
value = chalk.black.bgGreen(value);
}

@@ -130,2 +135,3 @@ else{

borderStyle : 1,
borderColor : "blue",
paddingBottom : 0,

@@ -140,3 +146,4 @@ headerAlign : "center",

//You can also pass an array of objects as rows
//Example with objects as rows
var rows = [

@@ -175,4 +182,13 @@ {

...
var t2 = Table(header,rows,{
borderStyle : 1,
paddingBottom : 0,
headerAlign : "center",
align : "center",
color : "white"
});
var str2 = t2.render();
console.log(str2);
```

@@ -212,3 +228,3 @@ <!--END-EXAMPLE-USAGE-->

* [Table(header, rows, options)](#new_Table_new)
* [.render()](#Table.render) ⇒ <code>String</code>
* [.Cls.render()](#Table.Cls.render) ⇒ <code>String</code>

@@ -223,3 +239,10 @@ <a name="new_Table_new"></a>

| header.column | <code>object</code> | Column options |
| header.column.alias | <code>string</code> | Alernate header column name |
| header.column.align | <code>string</code> | default: "center" |
| header.column.color | <code>string</code> | default: terminal default color |
| header.column.footerAlign | <code>string</code> | default: "center" |
| header.column.footerColor | <code>string</code> | default: terminal default color |
| header.column.formatter | <code>function</code> | Runs a callback on each cell value in the parent column |
| header.column.headerAlign | <code>string</code> | default: "center" |
| header.column.headerColor | <code>string</code> | default: terminal's default color |
| header.column.marginLeft | <code>number</code> | default: 0 |

@@ -232,9 +255,2 @@ | header.column.marginTop | <code>number</code> | default: 0 |

| header.column.paddingTop | <code>number</code> | default: 0 |
| header.column.alias | <code>string</code> | Alernate header column name |
| header.column.align | <code>string</code> | default: "center" |
| header.column.color | <code>string</code> | default: terminal default color |
| header.column.headerAlign | <code>string</code> | default: "center" |
| header.column.headerColor | <code>string</code> | default: terminal default color |
| header.column.footerAlign | <code>string</code> | default: "center" |
| header.column.footerColor | <code>string</code> | default: terminal default color |
| rows | <code>array</code> | [See example](#example-usage) |

@@ -244,2 +260,3 @@ | options | <code>object</code> | Table options |

| options.borderCharacters | <code>array</code> | [See @note](#note) |
| options.borderColor | <code>array</code> | default: terminal's default color |

@@ -249,7 +266,8 @@ **Example**

var Table = require('tty-table');
Table(header,rows,options);
var t1 = Table(header,rows,options);
console.log(t1.render());
```
<a name="Table.render"></a>
<a name="Table.Cls.render"></a>
### Table.render() ⇒ <code>String</code>
### Table.Cls.render() ⇒ <code>String</code>
Renders a table to a string

@@ -256,0 +274,0 @@

@@ -1,581 +0,8 @@

var Merge = require("merge"),
Chalk = require("chalk"),
StripAnsi = require("strip-ansi"),
Wrap = require("word-wrap");
var cls = function(){
var _public = this._public = {},
_private = this._private = {};
/**
* Private Variables
*
*/
_private.defaults = {
defaultValue : (function(){
return (typeof Chalk !== 'undefined') ? Chalk.red("#ERR") : "#ERR";
}()),
marginTop : 1,
marginLeft : 2,
maxWidth : 20,
formatter : null,
headerAlign : "center",
footerAlign : "center",
align : "center",
paddingRight : 0,
paddingLeft : 0,
paddingBottom : 0,
paddingTop : 0,
color : false,
headerColor : false,
footerColor : false,
borderStyle : 1,
borderCharacters : [
[
{v: " ", l: " ", j: " ", h: " ", r: " "},
{v: " ", l: " ", j: " ", h: " ", r: " "},
{v: " ", l: " ", j: " ", h: " ", r: " "}
],
[
{v: "│", l: "┌", j: "┬", h: "─", r: "┐"},
{v: "│", l: "├", j: "┼", h: "─", r: "┤"},
{v: "│", l: "└", j: "┴", h: "─", r: "┘"}
],
[
{v: "|", l: "+", j: "+", h: "-", r: "+"},
{v: "|", l: "+", j: "+", h: "-", r: "+"},
{v: "|", l: "+", j: "+", h: "-", r: "+"}
]
]
};
_private.GUTTER = 1;
_private.header = []; //saved so cell options can be merged into
//column options
_private.table = {
columns : [],
columnWidths : [],
columnInnerWidths : [],
header : [],
body : []
};
/**
* Private Methods
*
*/
_private.buildRow = function(row,rowType){
var minRowHeight = 0;
//support both rows passed as an array
//and rows passed as an object
if(typeof row === 'object' && !(row instanceof Array)){
row = _private.table.columns.map(function(object){
return row[object.value] || null;
});
}
else{
//equalize array lengths
var difL = _private.table.columnWidths.length - row.length;
if(difL > 0){
//add empty element to array
row = row.concat(Array.apply(null, new Array(difL))
.map(function(){return null}));
}
else if(difL < 0){
//truncate array
row.length = _private.table.columnWidths.length;
}
}
//get row as array of cell arrays
var cArrs = row.map(function(cell,index){
var c = _private.buildCell(cell,index,rowType);
var cellArr = c.cellArr;
if(rowType === 'header'){
_private.table.columnInnerWidths.push(c.width);
}
minRowHeight = (minRowHeight < cellArr.length) ?
cellArr.length : minRowHeight;
return cellArr;
});
//Adjust minRowHeight to reflect vertical row padding
minRowHeight = (rowType === 'header') ? minRowHeight :
minRowHeight +
(_public.options.paddingBottom +
_public.options.paddingTop);
//convert array of cell arrays to array of lines
var lines = Array.apply(null,{length:minRowHeight})
.map(Function.call,function(){return []});
cArrs.forEach(function(cellArr,a){
var whiteline = Array(_private.table.columnWidths[a]).join('\ ');
if(rowType ==='body'){
//Add whitespace for top padding
for(var i=0; i<_public.options.paddingTop; i++){
cellArr.unshift(whiteline);
}
//Add whitespace for bottom padding
for(i=0; i<_public.options.paddingBottom; i++){
cellArr.push(whiteline);
}
}
for(var b=0; b<minRowHeight; b++){
lines[b].push((typeof cellArr[b] !== 'undefined') ?
cellArr[b] : whiteline);
}
});
return lines;
};
_private.buildCell = function(cell,columnIndex,rowType){
var cellValue,
cellOptions = Merge(true,
_public.options,
(rowType === 'body') ?
_private.header[columnIndex] : {}, //ignore columnOptions for footer
cell);
if(rowType === 'header'){
cell = Merge(true,_public.options,cell);
_private.table.columns.push(cell);
cellValue = cell.alias || cell.value;
}
else{
if(typeof cell === 'object' && cell !== null){
cellValue = cell.value;
}
else{
cellValue = cell;
}
//Replace undefined/null cell values with placeholder
cellValue = (typeof cellValue === 'undefined' || cellValue === null) ?
_public.options.defaultValue : cellValue;
//Run formatter
if(typeof cellOptions.formatter === 'function'){
cellValue = cellOptions.formatter(cellValue);
}
}
//colorize cellValue
cellValue = _private.colorizeCell(cellValue,cellOptions,rowType);
//textwrap cellValue
var WrapObj = _private.wrapCellContent(cellValue,
columnIndex,
cellOptions,
rowType);
cellValue = WrapObj.output;
//return as array of lines
return {
cellArr : cellValue.split('\n'),
width : WrapObj.width
};
};
/*
_private.colorizeAllWords = function(color,str){
//Color each word in the cell so that line breaks don't break color
var arr = str.replace(/(\S+)/gi,function(match){
return Chalk[color](match)+'\ ';
});
return arr;
};
*/
_private.colorizeCell = function(str,cellOptions,rowType){
var color = false; //false will keep terminal default
switch(true){
case(rowType === 'body'):
color = cellOptions.color || color;
break;
case(rowType === 'header'):
color = cellOptions.headerColor || color;
break;
default:
color = cellOptions.footerColor || color;
}
if (color){
str = Chalk[color](str);
}
return str;
};
_private.calculateLength = function (line) {
return StripAnsi(line.replace(/[^\x00-\xff]/g,'XX')).length;
};
_private.wrapCellContent = function(cellValue,
columnIndex,
cellOptions,
rowType){
//remove ANSI color codes from the beginning and end of string
var string = cellValue.toString(),
startAnsiRegexp = /^(\033\[[0-9;]*m)+/,
endAnsiRegexp = /(\033\[[0-9;]*m)+$/,
startMatches = string.match(startAnsiRegexp),
endMatches = string.match(endAnsiRegexp),
startFound = false,
endFound = false;
if(startMatches instanceof Array && startMatches.length > 0){
startFound = true;
string = string.replace(startAnsiRegexp,'');
}
if(endMatches instanceof Array && endMatches.length > 0){
endFound = true;
string = string.replace(endAnsiRegexp,'');
}
var alignTgt;
switch(rowType){
case('header'):
alignTgt = "headerAlign"
break;
case('body'):
alignTgt = "align"
break;
default:
alignTgt = "footerAlign"
break;
}
//Equalize padding for centered lines
if(cellOptions[alignTgt] === 'center'){
cellOptions.paddingLeft = cellOptions.paddingRight =
Math.max(cellOptions.paddingRight,cellOptions.paddingLeft,0);
}
var width = _private.table.columnWidths[columnIndex],
innerWidth = width - cellOptions.paddingLeft -
cellOptions.paddingRight -
_private.GUTTER; //border/gutter
if (string.length < _private.calculateLength(string)) {
//Wrap Asian characters
var count = 0;
var start = 0;
var characters = string.split('');
string = characters.reduce(function (prev, cellValue, i) {
count += _private.calculateLength(cellValue);
if (count > innerWidth) {
prev.push(string.slice(start, i));
start = i;
count = 0;
} else if (characters.length === i + 1) {
prev.push(string.slice(start));
}
return prev;
}, []).join('\n');
} else {
string = Wrap(string,{
width : innerWidth -
cellOptions.paddingLeft -
cellOptions.paddingRight,
trim : true,
indent : ''
});
}
//Break string into array of lines
var strArr = string.split('\n');
//Format each line
strArr = strArr.map(function(line){
line = line.trim();
var lineLength = _private.calculateLength(line);
//alignment
if(lineLength < width){
var emptySpace = width - lineLength;
switch(true){
case(cellOptions[alignTgt] === 'center'):
emptySpace --;
var padBoth = Math.floor(emptySpace / 2),
padRemainder = emptySpace % 2;
line = Array(padBoth + 1).join(' ') +
line +
Array(padBoth + 1 + padRemainder).join(' ');
break;
case(cellOptions[alignTgt] === 'right'):
line = Array(emptySpace - cellOptions.paddingRight).join(' ') +
line +
Array(cellOptions.paddingRight + 1).join(' ');
break;
default:
line = Array(cellOptions.paddingLeft + 1).join(' ') +
line + Array(emptySpace - cellOptions.paddingLeft).join(' ');
}
}
//put ANSI color codes BACK on the beginning and end of string
if(startFound){
line = startMatches[0] + line;
}
if(endFound){
line = line + endMatches[0];
}
return line;
});
string = strArr.join('\n');
return {
output : string,
width : innerWidth
};
};
_private.getColumnWidths = function(row){
//Widths as prescribed
var widths = row.map(function(cell){
if(typeof cell === 'object' && typeof cell.width !=='undefined'){
return cell.width;
}
else{
return _public.options.maxWidth;
}
});
//Check to make sure widths will fit the current display, or resize.
var totalWidth = widths.reduce(function(prev,curr){
return prev+curr;
});
//Add marginLeft to totalWidth
totalWidth += _public.options.marginLeft;
//Check process exists in case we are in browser
if(process && process.stdout && totalWidth > process.stdout.columns){
//recalculate proportionately to fit size
var prop = process.stdout.columns / totalWidth;
prop = prop.toFixed(2)-0.01;
widths = widths.map(function(value){
return Math.floor(prop*value);
});
}
return widths;
};
/**
* Public Variables
*
*/
_public.options = {};
/**
* Public Methods
*
*/
_private.setup = function(header,body,footer,options){
_public.options = Merge(true,_private.defaults,options);
//backfixes for shortened option names
_public.options.align = _public.options.alignment || _public.options.align;
_public.options.headerAlign = _public.options.headerAlignment || _public.options.headerAlign;
_private.table.columnWidths = _private.getColumnWidths(header);
//Build header
_private.header = header; //save for merging columnOptions into cell options
header = [header];
_private.table.header = header.map(function(row){
return _private.buildRow(row,'header');
});
//Build body
_private.table.body = body.map(function(row){
return _private.buildRow(row,'body');
});
//Build footer
footer = (footer.length > 0) ? [footer] : [];
_private.table.footer = footer.map(function(row){
return _private.buildRow(row,'footer');
});
return _public;
};
/**
* Renders a table to a string
* @returns {String}
* @memberof Table
* @example
* ```
* var str = t1.render();
* console.log(str); //outputs table
* ```
*/
_public.render = function(){
var str = '',
part = ['header','body','footer'],
marginLeft = Array(_public.options.marginLeft + 1).join('\ '),
bS = _public.options.borderCharacters[_public.options.borderStyle],
borders = [];
//Borders
for(var a=0;a<3;a++){
borders.push('');
_private.table.columnWidths.forEach(function(w,i,arr){
borders[a] += Array(w).join(bS[a].h) +
((i+1 !== arr.length) ? bS[a].j : bS[a].r);
});
borders[a] = bS[a].l + borders[a];
borders[a] = borders[a].split('');
borders[a][borders[a].length1] = bS[a].r;
borders[a] = borders[a].join('');
borders[a] = marginLeft + borders[a] + '\n';
}
//Top horizontal border
str += borders[0];
//Rows
var row;
part.forEach(function(p,i){
while(_private.table[p].length){
row = _private.table[p].shift();
if(row.length === 0) break;
row.forEach(function(line){
str = str
+ marginLeft
+ bS[1].v
+ line.join(bS[1].v)
+ bS[1].v
+ '\n';
});
//Adds bottom horizontal row border
switch(true){
//If end of body and no footer, skip
case(_private.table[p].length === 0
&& i === 1
&& _private.table.footer.length === 0):
break;
//if end of footer, skip
case(_private.table[p].length === 0
&& i === 2):
break;
default:
str += borders[1];
}
}
});
//Bottom horizontal border
str += borders[2];
return Array(_public.options.marginTop + 1).join('\n') + str;
}
};
/**
* @class Table
* @param {array} header - [See example](#example-usage)
* @param {object} header.column - Column options
* @param {function} header.column.formatter - Runs a callback on each cell value in the parent column
* @param {number} header.column.marginLeft - default: 0
* @param {number} header.column.marginTop - default: 0
* @param {number} header.column.maxWidth - default: 20
* @param {number} header.column.paddingBottom - default: 0
* @param {number} header.column.paddingLeft - default: 0
* @param {number} header.column.paddingRight - default: 0
* @param {number} header.column.paddingTop - default: 0
* @param {string} header.column.alias - Alernate header column name
* @param {string} header.column.align - default: "center"
* @param {string} header.column.color - default: terminal default color
* @param {string} header.column.headerAlign - default: "center"
* @param {string} header.column.headerColor - default: terminal default color
* @param {string} header.column.footerAlign - default: "center"
* @param {string} header.column.footerColor - default: terminal default color
*
* @param {array} rows - [See example](#example-usage)
*
* @param {object} options - Table options
* @param {number} options.borderStyle - default: 1 (0 = no border)
* Refers to the index of the desired character set.
* @param {array} options.borderCharacters - [See @note](#note)
* @returns {Table}
* @note
* <a name="note"/>
* Default border character sets:
* ```
* [
* [
* {v: " ", l: " ", j: " ", h: " ", r: " "},
* {v: " ", l: " ", j: " ", h: " ", r: " "},
* {v: " ", l: " ", j: " ", h: " ", r: " "}
* ],
* [
* {v: "│", l: "┌", j: "┬", h: "─", r: "┐"},
* {v: "│", l: "├", j: "┼", h: "─", r: "┤"},
* {v: "│", l: "└", j: "┴", h: "─", r: "┘"}
* ],
* [
* {v: "|", l: "+", j: "+", h: "-", r: "+"},
* {v: "|", l: "+", j: "+", h: "-", r: "+"},
* {v: "|", l: "+", j: "+", h: "-", r: "+"}
* ]
* ]
* ```
* @example
* ```
* var Table = require('tty-table');
* Table(header,rows,options);
* ```
*
*/
module.exports = function(){
var o = new cls(),
header = arguments[0],
body = arguments[1],
footer = (arguments[2] instanceof Array) ? arguments[2] : [],
options = (typeof arguments[3] === 'object') ? arguments[3] :
(typeof arguments[2] === 'object') ? arguments[2] : {};
return o._private.setup(header,body,footer,options);
};
if(require.main === module){
//called directly in terminal
require('./terminal-adapter.js');
}
else{
//called as a module
module.exports = require('./default-adapter.js');
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc