diff --git a/src/styled/table.ts b/src/styled/table.ts index 6576baa8..1a7387cb 100644 --- a/src/styled/table.ts +++ b/src/styled/table.ts @@ -46,6 +46,17 @@ export interface TableOptions { */ export default function table(data: any[], inputOptions: Partial = {}) { 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() : ''), @@ -68,17 +79,6 @@ export default function table(data: any[], inputOptions: Partial = }, ...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) { diff --git a/test/styled/table.test.ts b/test/styled/table.test.ts index 6bc7471e..4b68dfe3 100644 --- a/test/styled/table.test.ts +++ b/test/styled/table.test.ts @@ -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 `) }) })