automattic / cli-table Goto Github PK
View Code? Open in Web Editor NEWPretty unicode tables for the CLI with Node.JS
License: MIT License
Pretty unicode tables for the CLI with Node.JS
License: MIT License
This does not treat wide chars in CLI correctly. It assume each wide-char as one-char width, but actually it takes two char's span.
The tests pass locally for me, but looks like they're broken on Travis.
Colors are nice but don't render well when used with automated systems and passed through non-terminal interface.
For example, during a build the generated table could end up in a text email or on a HTML field that does not parse colors.
Now Chrome, has added
console.table( data );
and soon Firefox will add this too. Maybe we could change the syntax to support this for consistency across client and server side JS.
Move from:
var data = [ ['First value', 'Second value']
, ['First value', 'Second value'] ];
table.push( data );
console.log( table.toString() );
To something like:
var data = [ ['First value', 'Second value']
, ['First value', 'Second value'] ];
console.table( data );
e.g Simplified Chinese (as 简体中文)
And for simplified chinese, test with unicode \u4E00-\u9FA5
, should double the length, then it will display well.
strlen('你好world') => 2*2 + 5 = 9
BTW, I'm using the Hack Font for display.
In the readme it says:
Color/background styling in the header through colors.js
fantastic. but nowhere does it explain how to change the header color? Right now I have a cli table where the header has red text, looking to change it to a different color.
A cell containing null
as the value throws an error (TypeError: Cannot read property 'toString' of null
). I'm not sure whether a more sensible default would be to stringify it to an empty string or to the string null
but it seems like either would be preferable to an error.
Please update the library so that npm install does an install of the latest version. The current version that npm installs throws errors on examples given in the docs (e.g. it throws an error on the cross table example).
I was about to file a bug report because of this error but then noticed that the bug is no longer present in the version on github (while it is till present on the version that installs when using npm install).
Thanks!
How can I add some bottom padding between each vertical table element?
Would be sweet.
The rows seem to be stuck at a fixed height of one line atm? Any chance to add the ability to specify row height?
Perhaps add a wordwrap option for cells so if the content exceeds the width it will create a new line and increase the cell height?
Hi! Thank you very much, very useful tool.
But I was confused about how to align the data in cells and rows, for example by center (I've read in README that it is possible, but I did not find how).
I'm trying to figure out how to best approach this.
I've been thinking about adding an autoColSpan
that auto-spans columns to fit the total number of columns in the table:
var table = new Table({ autoColSpan:true });
table.push(
[ ["column"] ], // stretches to fit 2 columns
[ ["column"], ["column"] ]
);
While the above allows for overall simple list-based tables, it does not allow for individual control or rowspan.
Another approach is to do something similar to #42 with per-column config:
var table = new Table();
table.push(
[ {content:"column", colSpan:3} ],
[ ["column"], {content:"column", colSpan:2} ],
[ ["column"], ["column"], ["column"] ]
);
which can also be applied to a rowspan:
var table = new Table();
table.push(
[ ["column"], ["column"], {content:"column", rowSpan:2} ],
[ ["column"], ["column"] ],
[ ["column"], ["column"], ["column"] ]
);
Other per-column settings can be added as well, such as align
(to override options.colAligns
), etc.
Yes, it's a sugar.
Depending on width/height of char map it will calculate automatically all properties for borders.
For 3+ chars the first and last one are outer borders.
For 4+ chars the third char is inner border (cell delimiter).
etc.
Something like that (4x4):
{ "chars": [
"╔═╤╗",
"║ │║",
"╟─┼╢",
"╚═╧╝"
] }
//╔══════╤═════╤══════╗
//║ foo │ bar │ baz ║
//╟──────┼─────┼──────╢
//║ frob │ bar │ quuz ║
//╚══════╧═════╧══════╝
Or (4x3):
{ "chars": [
"┌─┬┐",
"│ ││",
"└─┴┘"
] }
//┌────────────┬─────┬──────┐
//│ foo │ bar │ baz │
//│ frobnicate │ bar │ quuz │
//└────────────┴─────┴──────┘
Or (3x3):
{ "chars": [
"┌─┐",
"│ │",
"└─┘"
] }
//┌───────────────────────┐
//│ foo bar baz │
//│ frobnicate bar quuz │
//└───────────────────────┘
Or (3x1):
{ "chars": [
"│ │",
] }
// │foo bar baz │
// │frob bar quuz │
Or (2x2):
{ "chars": [
" │",
"─┼",
] }
// foo │ bar │ baz
//──────┼─────┼──────
// frob │ bar │ quuz
Or (1x2):
{ "chars": [
" ",
"─",
] }
// foo bar baz
//─────────────────
// frob bar quuz
Or (nothing):
{ "chars": "", style: { 'padding-left': 0, 'padding-right': 0 } }
// foo bar baz
// frobnicate bar quuz
I can delete my private table.js thing I never finished now :D dopeee!
https://github.com/Automattic/cli-table/blob/master/lib/index.js#L78
This line does nothing. ASI adds a semicolon at the end, and that makes the line an expression statement, but since the expression has no side effects, it does nothing!
Perchance, are you missing an =
sign there?
When pushing using the array-per-row method, if one of the elements in the array is undefined
, the table allows you to push it, but then will throw an error when table.toString()
is called. The expected behavior that I would prefer is to just fill in the cell with space, but I guess a case could be made for throwing an error on the push.
I've written a test to demonstrate it; I'll write a fix when I get some time.
'test pushing undefined': function (){
var table = new Table()
table.push(['v0.1', 'Testing something cool']);
table.push(['v0.2', undefined]);
var expected = [
'┌────┬──────────────────────┐'
, '│v0.1│Testing something cool│'
, '├────┼──────────────────────┤'
, '│v0.2│ │'
, '└────┴──────────────────────┘'
];
table.toString().should.eql(expected.join("\n"));
}
which throws:
index.test.js test pushing undefined: TypeError: Cannot call method 'toString' of undefined
at /Users/user/Desktop/cli-table/lib/cli-table/index.js:183:27
at Array.forEach (native)
at generateRow (/Users/user/Desktop/cli-table/lib/cli-table/index.js:182:11)
at /Users/croberts1/Desktop/cli-table/lib/cli-table/index.js:276:16
at Table.forEach (native)
at Table.toString (/Users/user/Desktop/cli-table/lib/cli-table/index.js:259:10)
at Test.module.exports.test pushing undefined [as fn] (/Users/user/Desktop/cli-table/test/index.test.js:263:11)
at Test.runParallel (/Users/user/Desktop/cli-table/node_modules/expresso/bin/expresso:959:10)
at Test.run (/Users/user/Desktop/cli-table/node_modules/expresso/bin/expresso:924:18)
at next (/Users/user/Desktop/cli-table/node_modules/expresso/bin/expresso:867:22)
Failures: 1
It would be a nice addon for the scenario where you would like to email the table that node generates :).
The html pre
tag doesn't always work for mobile email.
Even though strlen
tries to account for terminal coloring (https://github.com/LearnBoost/cli-table/blob/master/lib/cli-table/utils.js#L79), it seems whenever I use chalk.cyan('string')
cli-table treats the non-displaying color chars as real chars for the purposes of colWidths
. Whatever is particular about chalk.cyan
needs to be ignored for width calculations.
Error message:
npm ERR! Linux 3.10.0-229.el7.x86_64
npm ERR! argv "node" "/usr/local/bin/npm" "install" "cli-table" "--save"
npm ERR! node v0.12.1
npm ERR! npm v2.11.1
npm ERR! shasum check failed for /tmp/npm-5519-25fd97d9/registry.npmjs.org/colors/-/colors-1.0.3.tgz
npm ERR! Expected: 0433f44d809680fdeb60ed260f1b0c262e82a40b
npm ERR! Actual: 5996ece2af528700f64e9794429fe8a7a594fd14
npm ERR! From: https://registry.npmjs.org/colors/-/colors-1.0.3.tgz
One could want to have a line between the header and the data rows but opt out from lines between data rows (to be able to display more data in the same console).
Cheers!
I'd love an option to turn off colors. I'd like to use this for the hubot of a Slack channel, but the colorization seems extremely resistant to removal. I've tried all of these:
/(\x03\d{0,2}(,\d{0,2})?|\u200B)/g
/[\x0F\x02\x16\x1F]/g
/\x1b\[.\{1,5\}m/g
But I still see [90m
, [39m�
, [31m
, etc throughout the table. 😕
I want to overide the table with new data.
Maybe should wcwidth
, not string length
to calculate padding
:-(
Every-time I try to add colors to any of the contents of a cell, it messes up the padding. It's probably because the padding offset is calculated by the length of the cell's string content. If an ANSI color code has been applied to this content, the length of the string is no longer a "valid" way to calculate the padding offset since the ANSI escape codes don't output to the terminal...
Not sure how to fix this one.
┌────┬─────┬────────────┬────────────────────┐
│ id │ iid │ project_id │ title │
├────┼─────┼────────────┼────────────────────┤
│ 2 │ 1 │ 10 │ Test Issue │
├────┼─────┼────────────┼────────────────────┤
│ 3 │ 2 │ 10 │ Test issue2 │
├────┼─────┼────────────┼────────────────────┤
│ 5 │ 4 │ 10 │ 把node-gitlab移植到项目中 │
└────┴─────┴────────────┴────────────────────┘
┌────┬─────┬────────────┬────────────────────┬────────────────┬────────┬──────────────────────────┬──────────────────────────┬────────┬──────────┬─────────┐
│ id │ iid │ project_id │ title │ description │ state │ created_at │ updated_at │ labels │ assignee │ author │
├────┼─────┼────────────┼────────────────────┼────────────────┼────────┼──────────────────────────┼──────────────────────────┼────────┼──────────┼─────────┤
│ 2 │ 1 │ 10 │ Test Issue │ 测试一下 │ closed │ 2014-08-14T06:28:20.398Z │ 2014-08-15T10:25:31.588Z │ │ Cao Jun │ Cao Jun │
├────┼─────┼────────────┼────────────────────┼────────────────┼────────┼──────────────────────────┼──────────────────────────┼────────┼──────────┼─────────┤
│ 3 │ 2 │ 10 │ Test issue2 │ # testMarkdown │ closed │ 2014-08-15T06:38:54.650Z │ 2014-08-15T10:25:30.353Z │ │ Cao Jun │ Cao Jun │
├────┼─────┼────────────┼────────────────────┼────────────────┼────────┼──────────────────────────┼──────────────────────────┼────────┼──────────┼─────────┤
│ 5 │ 4 │ 10 │ 把node-gitlab移植到项目中 │ │ opened │ 2014-08-15T10:26:48.557Z │ 2014-08-15T10:26:48.557Z │ │ Cao Jun │ Cao Jun │
└────┴─────┴────────────┴────────────────────┴────────────────┴────────┴──────────────────────────┴──────────────────────────┴────────┴──────────┴─────────┘
I have written a library for displaying data in a table.
https://github.com/gajus/table
This package implements all features of cli-table
and addresses some of the open issues:
Consider deprecating cli-table
in favour of table
package.
The reason I choose to write a library from the ground up is because I am building something that will heavily rely on text tables and neither of the implementations (ascii-table, cli-table or text-table) met my requirements. I have looked at all the source codes. ascii-table package had the best API, but I did not feel comfortable working with a codebase using prototype inheritance, no separation of logic and minimal test coverage.
I have put a lot of thinking into how to make table package modular, easy to test, easy to contribute to and easy to extend for different use cases.
It would be nice to be able to specify percentages as column width. cli-table could find out the term width and chose an absolute width at run time.
Looks like this dir hasn't been touched in 5 years:
https://github.com/sorensen/ascii-table/tree/master/example
might be nice to delete it or update it, thx
Is there a way support for this?
I am using cli-table in my new splunkrepl cli (https://www.npmjs.com/package/splunkrepl). One of the things popular in the Splunk world is streaming results, and some of those results display in tables. It would be nice to be able to output the results as they come in, ie progressively rendering the table as currently I wait until all results are in before I render.
If this is doable today, I'd be happy to just use that approach.
I'm trying to use extended characters like String.fromCharCode(0x2714)
... which is the ✔
.
When doing so, this seems to goof up the length calculation in the table and my row is shifted over one space.
/Users/dasilvacontin/temp/node_modules/cli-table/lib/index.js:181
items.forEach(function (item, i) {
^
TypeError: items.forEach is not a function
at generateRow (/Users/dasilvacontin/temp/node_modules/cli-table/lib/index.js:181:11)
at /Users/dasilvacontin/temp/node_modules/cli-table/lib/index.js:275:16
at Table.forEach (native)
at Table.toString (/Users/dasilvacontin/temp/node_modules/cli-table/lib/index.js:258:10)
at Object.<anonymous> (/Users/dasilvacontin/temp/node-course.js:15:19)
at Module._compile (module.js:573:32)
at Object.Module._extensions..js (module.js:582:10)
at Module.load (module.js:490:32)
at tryModuleLoad (module.js:449:12)
at Function.Module._load (module.js:441:3)
When doing table.toString()
it'd be nice to get a "You should only push Arrays into the table" or smth, instead of getting this exception.
Sorry for mistaking the repo. Please close this issue!
From cli:
$ pbpaste | cli-table --csv | pbcopy
is it possible to have a row / cell with a specific color ?
very useful for printing out numbers with decmials
var Table = require('cli-table')
var table = new Table({
head: ['a', 'b']
})
table.push(
['foo', 'bar'],
['xyz', undefined]
)
console.log(table.toString())
┌─────┬───────────┐
│ a │ b │
├─────┼───────────┤
│ foo │ bar │
├─────┼───────────┤
│ xyz │ │
└─────┴───────────┘
I wonder what the rationale here was,
A straightforward ASCII character set (for windows support, which doesn't support Unicode chars...)
chars: {
top: '-',
'top-mid':'+',
'top-left':'+',
'top-right':'+',
bottom: '-',
'bottom-mid':'+',
'bottom-left':'+',
'bottom-right':'+',
left: '|',
'left-mid': '|',
mid: '-',
'mid-mid': '+',
right: '|',
'right-mid': '+'
}
maybe put it in README
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.