sorensen / ascii-table Goto Github PK
View Code? Open in Web Editor NEWAscii Tables for JS
License: MIT License
Ascii Tables for JS
License: MIT License
There is no need of having the entire concept of heading.
setHeading(heading, [...])
setHeadingAlign(direction)
setHeadingAlignLeft()
setHeadingAlignCenter()
CLI is non-interactive, therefore differentiating between a header and a regular row does not have value, i.e. user can set headings using a regular row at the beginning of the table.
Instead, I propose that a new method addBreak
is added, that would generate that line that separates the main content from the header.
Is it currently possible to align the heading? I personally, would almost always want the headings aligned to the left, not the center. If it's not currently possible, would you consider adding an such an option?
I did try .setAlign(0, AsciiTable.LEFT), but that didn't seem to work. Otherwise, working as expected.
Cool library. Thanks for your work : )
I love this project, but for Typescript I have to write it myself every time. Here is a sample code for those looking for d.ts for Typescript.
declare module 'ascii-table' {
export default class AsciiTable {
constructor(title?: string);
setTitle(title: string): void;
setHeading(...headings: string[]): void;
addRow(...values: string[]): void;
addRow(row: string[]): void;
addRow(row: Record<string, string>): void;
addRow(row: string[], truncate: boolean): void;
addRow(row: Record<string, string>, truncate: boolean): void;
setAlign(column: number, alignment: 'left' | 'center' | 'right'): void;
setAlign(alignments: ('left' | 'center' | 'right')[]): void;
setJustify(column: number, justify: boolean): void;
setJustify(justify: boolean[]): void;
setChars(chars: Record<string, string>): void;
setStyle(style: 'default' | 'fat' | 'thin' | 'double' | 'round' | 'single'): void;
setBorder(border: boolean): void;
setTruncate(truncate: boolean): void;
setMaxColWidth(maxWidth: number): void;
setMinColWidth(minWidth: number): void;
setColWidth(column: number, width: number): void;
setColWidths(widths: number[]): void;
setDefaultColWidth(width: number): void;
setDefaultAlignment(alignment: 'left' | 'center' | 'right'): void;
setDefaultJustify(justify: boolean): void;
setDefaultTruncate(truncate: boolean): void;
setDefaultMaxColWidth(maxWidth: number): void;
setDefaultMinColWidth(minWidth: number): void;
setDefaultStyle(style: 'default' | 'fat' | 'thin' | 'double' | 'round' | 'single'): void;
setDefaultBorder(border: boolean): void;
setDefaultChars(chars: Record<string, string>): void;
toString(): string;
}
}
Would be really great if there were a TypeScript definition for this project.
Using TS as an excuse to revamp code is also good too :)
Thanks!
Whatever I try, I can't get it to work. I always get this error. Is this a problem with my code or the module/script?
It appears I am hitting a max number of columns issues, could it be that there is a max of 11 columns?
Would it be possible to add an option to prepend whitespace before each line?
The use case for this is to create a Markdown code block. In my case, I'm trying to create a Hubot script to insert an ASCII table into a Flowdock chat, using ascii-table.
Flowdock can create a code block (which will use mono-spaced font), however, each line needs to be pre-pended with four space characters. Stackoverflow also uses a similar system (four spaces), as does Github.
Is it possible to add an option to allow you to pad the beginning each line with spaces? E.g.
.----------------.
| A Title |
|----------------|
| | Name | Age |
|---|------|-----|
| 1 | Bob | 52 |
| 2 | John | 34 |
| 3 | Jim | 83 |
'----------------'
Ok, the above will probably render as a code block, lol, but you get what I mean. Four spaces at the beginning of each line?
i should how to solve the problem
The readme example includes a ----
style border below the header for .removeBorder()
, but when I tried it, it seems to be removed as well:
> console.log(new (require('ascii-table'))().setHeading('a', 'b').addRow(1, 2).removeBorder().toString())
a b
1 2
In your last line, you pass 'this' context to a self-calling function. 'this' later gets used to append AsciiTable class to it. Normally, 'this' references the window object. Depending on how you load the script, however, 'this' could be undefined. One example is if you load the script via an eval. My solution was to change .call(this)
to .call(window)
, I suggest a similar fix for the version in this repo, or something like .call(window || global)
if it needs to work in node.
Hi, when I put in a heading that is longer than the width of the column headers and/or row data the character to the right of the heading gets pushed out like below resulting in an uneven right border.
If I make one of the column headers one or more characters wider, it fixes it like ...
Shouldn't it of expanded all the right boundary characters by one in first screenshot above so they all align? If not, is there a way to add a line break in the heading so it does not go so wide or an alternative solution other than unnecessarily widening the column header names or row data?
Line Column Type Message Rule ID
1 1 warning Definition for rule 'react/no-unused-vars' was not found react/no-unused-vars
15 5 error Variables within the same declaration block should be sorted alphabetically sort-vars
17 5 error Variables within the same declaration block should be sorted alphabetically sort-vars
18 5 error Variables within the same declaration block should be sorted alphabetically sort-vars
35 9 error Variables within the same declaration block should be sorted alphabetically sort-vars
35 9 error Variable 'appendPaths' should not be initialized on declaration init-declarations
The code:
table.setHeading([`Line`, `Column`, `Type`, `Message`, `Rule ID`]);
table.setHeadingAlignLeft();
table.addRow([
message.line || 0,
message.column || 0,
messageType,
message.message.replace(/\.$/, ``),
chalk.gray(message.ruleId || ``)
]);
I'd expect headings to align with the beginning of the text in each column.
Is there any way to add row separators? Like separator after heading.
Remove:
alignLeft(val, len, [pad])
alignCenter(val, len, [pad])
alignRight(val, len, [pad])
alignAuto(val, len, [pad])
setAlignLeft(idx)
setAlignCenter(idx)
setAlignRight(idx)
setTitleAlignLeft()
setTitleAlignCenter()
setTitleAlignRight()
setHeadingAlignLeft()
setHeadingAlignCenter()
setHeadingAlignRight()
None of the methods provide value to the package. It only makes the API looks less approachable and use less predictable. Removing these methods in favour of keeping just align
, setAlign
, and setHeadingAlign
would reduce the complexity of the API.
Hi I'm getting the following error when running some example code:
.setTitle(obj.title)
^
TypeError: Cannot call method 'setTitle' of undefined
at AsciiTable.parse.AsciiTable.fromJSON (/Users/jvallelonga/src/sandbox/stick-tools/query-viewer/node_modules/ascii-table/ascii-table.js:450:6)
at AsciiTable.reset.AsciiTable.clear (/Users/jvallelonga/src/sandbox/stick-tools/query-viewer/node_modules/ascii-table/ascii-table.js:204:10)
at new AsciiTable (/Users/jvallelonga/src/sandbox/stick-tools/query-viewer/node_modules/ascii-table/ascii-table.js:27:8)
at Function.AsciiTable.factory (/Users/jvallelonga/src/sandbox/stick-tools/query-viewer/node_modules/ascii-table/ascii-table.js:56:10)
The example code that I'm running is:
var table = AsciiTable.factory({
title: 'Title'
, heading: [ 'id', 'name' ]
, rows: [
[ 1, 'Bob' ]
, [ 2, 'Steve' ]
]
})
I haven't taken the time to understand your codebase fully, but I think it's because the clear method isn't returning this
. Maybe adding return this
after line 205 of ascii-table.js would fix it. I can make a pull request if this is the correct fix for the issue. Just wanted to get your input first.
If the table's title width is bigger than the rest of the content's width, the title goes out of the table.
e.g.:
.-----------.
| == CONDITIONNEL == |
|-----------|
| Présent |
|-----------|
| verrais |
| verrais |
| verrait |
| verrions |
| verriez |
| verraient |
'-----------'
WARN engine [email protected]: wanted: {"node":"0.x"} (current: {"node":"5.4.1","npm":"3.3.12"})
can you update this? thanks :)
Learn the project, seem unicode languages are not supported.
I think this project word-width will be help for you.I code one project word-table base on it, but after I see the project, I think ascii-table
is enough for me, except unicode supported.
Hi,
I'm using phantomas, which uses ascii-table.js. On certain runs of that tool I was getting this error:
usr/local/lib/node_modules/phantomas/node_modules/ascii-table/ascii-table.js:95
return str + Array(len + 1 - str.length).join(pad)
^
RangeError: Invalid array length
at Function.AsciiTable.alignLeft (/usr/local/lib/node_modules/phantomas/node_modules/ascii-table/ascii-table.js:95:16)
at Function.AsciiTable.align (/usr/local/lib/node_modules/phantomas/node_modules/ascii-table/ascii-table.js:70:50)
at AsciiTable._renderTitle (/usr/local/lib/node_modules/phantomas/node_modules/ascii-table/ascii-table.js:552:24)
at AsciiTable.render.AsciiTable.valueOf.AsciiTable.toString (/usr/local/lib/node_modules/phantomas/node_modules/ascii-table/ascii-table.js:503:20)
at formatMultipleRunResults (/usr/local/lib/node_modules/phantomas/reporters/plain.js:142:16)
at Object.render (/usr/local/lib/node_modules/phantomas/reporters/plain.js:152:12)
at Object.render (/usr/local/lib/node_modules/phantomas/core/reporter.js:64:20)
at /usr/local/lib/node_modules/phantomas/bin/phantomas.js:191:19
at /usr/local/lib/node_modules/phantomas/node_modules/async/lib/async.js:254:17
at /usr/local/lib/node_modules/phantomas/node_modules/async/lib/async.js:157:25
After a little debugging, I found that the problem was that a negative array length was being passed to the Array constructor here:
https://github.com/sorensen/ascii-table/blob/master/ascii-table.js#L90
I solved it by checking to make sure the calculated length is greater than 0:
if ((len + 1 - str.length) > 0) {
return str + Array(len + 1 - str.length).join(pad)
}
else {
return str;
}
Makes for an uneven table, but at least it doesn't break. Sorry I didn't make a proper PR.
unused is defined twice.
{
"node": true
, "asi": true
, "nonstandard": true
, "strict": false
, "smarttabs": true
, "maxlen": 1800
, "newcap": false
, "expr": true
, "undef": true
, "unused": false
, "onecase": true
, "laxcomma": true
, "laxbreak": true
, "white": false
, "shadow": true
, "proto": true
, "loopfunc": true
, "boss": true
, "validthis": true
, "unused": "vars"
, "globals": {
"describe": false
, "it": false
}
}
Consider the following use case:
Now consider the case when there are more than one row and some rows are not filled to 3 items. In this case the omitted columns will be rendered as empty rather than being dropped for rows that do have them.
Case 2 and case 1 contradict each other in terms of behavior. Case 2 makes sense to me, case 1 does not. If the heading is specified, I expect the column to render. Could this edge case be fixed? Or, perhaps if this behavior is desired in some cases add a flag to the table that tells it whether to render or omit empty columns.
I have implemented all the previously suggested ideas into a package table
.
https://github.com/gajus/table
This package fixes some of the issues with ascii-table too, e.g. support for ANSI escape codes and fullwidth characters. In addition, new features added such as minWidth
and maxWidth
properties.
Consider deprecating ascii-table
in favour of table
package.
For my application I'd like to be able to pass arrays of headers and rows in.
I thought I could pull this off by using the "apply" method... but it doesn't work for me.
So I want to say:
var heading = ['a', 'b', 'c']
var row = [1, 2, 3...]
table.setHeading(heading)
table.addRow(row)
but it doesn't like arrays as an input. So I tried "apply" like this:
table.setHeading.apply(this, heading)
table.addRow.apply(this, row)
but no dice, get an error with both. Is there a way I can do this?
These methods unnecessarily abstract table data. Table data is just an array of arrays. A single method along the lines of https://github.com/sorensen/ascii-table#instanceaddrowmatrixrows is sufficient to instantiate table data.
Having data in an array enables developer to append rows, remove selected rows, etc. using regular array functions.
Would this project be open to accepting new maintainers to push it forward?
The current API allows providing column values using variable number of arguments, e.g.
addRow('a', 'b', 'c')
There is no good reason to allow that. Requiring an array for column data would make the overall all API smaller and use of the library more consistent.
Hi, I tried to use colors (I used gulp util which is using chalk) and it misaligned the values. I guess it's because of the extra characters for the color or style. There's probably not a fix for this but figure I ask.
Great job by the way, real easy to use.
Thanks.
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.