Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
fix(table): Allow user to override tableoptions (#40)
Browse files Browse the repository at this point in the history
[fixes #37]
  • Loading branch information
paulelliott authored and jdx committed Jun 19, 2018
1 parent a584138 commit 14b972e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/styled/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ export interface TableOptions {
*/
export default function table(data: any[], inputOptions: Partial<TableOptions> = {}) {
const options: TableOptions = {
colSep: ' ',
after: () => {},
headerAnsi: _.identity,
printLine: (s: any) => console.log(s),
printRow(cells: any[]) {
this.printLine((cells.join(this.colSep) as any).trimRight())
},
printHeader(cells: any[]) {
this.printRow(cells.map(_.ary(this.headerAnsi, 1)))
this.printRow(cells.map(hdr => hdr.replace(/./g, '─')))
},
...inputOptions,
columns: (inputOptions.columns || []).map(c => ({
format: (value: any) => (value != null ? value.toString() : ''),
Expand All @@ -68,17 +79,6 @@ export default function table(data: any[], inputOptions: Partial<TableOptions> =
},
...c,
})),
colSep: ' ',
after: () => {},
headerAnsi: _.identity,
printLine: (s: any) => console.log(s),
printRow(cells: any[]) {
this.printLine((cells.join(this.colSep) as any).trimRight())
},
printHeader(cells: any[]) {
this.printRow(cells.map(_.ary(this.headerAnsi, 1)))
this.printRow(cells.map(hdr => hdr.replace(/./g, '─')))
},
}

function calcWidth(cell: any) {
Expand Down
21 changes: 21 additions & 0 deletions test/styled/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ describe('styled/table', () => {
1 1
2 2
3 3
`)
})

fancy
.stdout()
.end('hides the header', output => {
cli.table([
{foo: 1, bar: 1},
{foo: 2, bar: 2},
{foo: 3, bar: 3},
], {
printHeader: undefined,
columns: [
{key: 'bar'},
{key: 'foo'},
]
})

expect(output.stdout).to.equal(`1 1
2 2
3 3
`)
})
})

0 comments on commit 14b972e

Please sign in to comment.