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

feat: add title to tables and improve output structure #364

Merged
merged 4 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/styled/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Table<T extends object> {
})

// assign options
const {columns: cols, filter, csv, output, extended, sort, printLine} = options
const {columns: cols, filter, csv, output, extended, sort, title, printLine} = options
this.options = {
columns: cols,
output: csv ? 'csv' : output,
Expand All @@ -42,7 +42,9 @@ class Table<T extends object> {
'no-header': options['no-header'] || false,
'no-truncate': options['no-truncate'] || false,
printLine: printLine || ((s: any) => process.stdout.write(s + '\n')),
rowStart: ' ',
sort,
title,
}
}

Expand Down Expand Up @@ -236,14 +238,31 @@ class Table<T extends object> {
}
shouldShorten()

// print table title
if (options.title) {
options.printLine(options.title)
// print title divider
options.printLine(''.padEnd(columns.reduce((sum, col) => sum + col.width!, 1), '='))

options.rowStart = '| '
}

// print headers
if (!options['no-header']) {
let headers = ''
let headers = options.rowStart
for (const col of columns) {
const header = col.header!
headers += header.padEnd(col.width!)
}
options.printLine(chalk.bold(headers))

// print header dividers
let dividers = options.rowStart
for (const col of columns) {
const divider = ''.padEnd(col.maxWidth! - 1, '─') + ' '
dividers += divider.padEnd(col.width!)
}
options.printLine(chalk.bold(dividers))
}

// print rows
Expand All @@ -262,7 +281,7 @@ class Table<T extends object> {
// print row
// including multi-lines
linesIndexess.forEach((i: number) => {
let l = ''
let l = options.rowStart
for (const col of columns) {
const width = col.width!
let d = (row as any)[col.key]
Expand Down
74 changes: 47 additions & 27 deletions test/styled/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,28 @@ describe('styled/table', () => {
.stdout()
.end('displays table', output => {
cli.table(apps, columns)
expect(output.stdout).to.equal(`ID Name${ws.padEnd(14)}
123 supertable-test-1${ws}
321 supertable-test-2${ws}\n`)
expect(output.stdout).to.equal(` ID Name${ws.padEnd(14)}
─── ─────────────────${ws}
123 supertable-test-1${ws}
321 supertable-test-2${ws}\n`)
})

describe('columns', () => {
fancy
.stdout()
.end('use header value for id', output => {
cli.table(apps, columns)
expect(output.stdout.slice(0, 2)).to.equal('ID')
expect(output.stdout.slice(1, 3)).to.equal('ID')
})

fancy
.stdout()
.end('shows extended columns/uses get() for value', output => {
cli.table(apps, columns, {extended: true})
expect(output.stdout).to.equal(`${extendedHeader}
123 supertable-test-1 https://supertable-test-1.herokuapp.com/ heroku-16${ws}
321 supertable-test-2 https://supertable-test-2.herokuapp.com/ heroku-16${ws}\n`)
expect(output.stdout).to.equal(`${ws}${extendedHeader}
─── ───────────────── ──────────────────────────────────────── ─────────${ws}
123 supertable-test-1 https://supertable-test-1.herokuapp.com/ heroku-16${ws}
321 supertable-test-2 https://supertable-test-2.herokuapp.com/ heroku-16${ws}\n`)
})
})

Expand All @@ -121,21 +123,34 @@ describe('styled/table', () => {
expect(output.stdout).to.contain(extendedHeader)
})

fancy
.stdout()
.end('shows title with divider', output => {
cli.table(apps, columns, {title: 'testing'})
expect(output.stdout).to.equal(`testing
=======================
| ID Name${ws.padEnd(14)}
| ─── ─────────────────${ws}
| 123 supertable-test-1${ws}
| 321 supertable-test-2${ws}\n`)
})

fancy
.stdout()
.end('skips header', output => {
cli.table(apps, columns, {'no-header': true})
expect(output.stdout).to.equal(`123 supertable-test-1${ws}
321 supertable-test-2${ws}\n`)
expect(output.stdout).to.equal(` 123 supertable-test-1${ws}
321 supertable-test-2${ws}\n`)
})

fancy
.stdout()
.end('only displays given columns', output => {
cli.table(apps, columns, {columns: 'id'})
expect(output.stdout).to.equal(`ID${ws}${ws}
123${ws}
321${ws}\n`)
expect(output.stdout).to.equal(` ID${ws}${ws}
───${ws}
123${ws}
321${ws}\n`)
})

fancy
Expand Down Expand Up @@ -225,26 +240,29 @@ describe('styled/table', () => {
.stdout()
.end('sorts by property', output => {
cli.table(apps, columns, {sort: '-name'})
expect(output.stdout).to.equal(`ID Name${ws.padEnd(14)}
321 supertable-test-2${ws}
123 supertable-test-1${ws}\n`)
expect(output.stdout).to.equal(` ID Name${ws.padEnd(14)}
─── ─────────────────${ws}
321 supertable-test-2${ws}
123 supertable-test-1${ws}\n`)
})

fancy
.stdout()
.end('filters by property & value (partial string match)', output => {
cli.table(apps, columns, {filter: 'id=123'})
expect(output.stdout).to.equal(`ID Name${ws.padEnd(14)}
123 supertable-test-1${ws}\n`)
expect(output.stdout).to.equal(` ID Name${ws.padEnd(14)}
─── ─────────────────${ws}
123 supertable-test-1${ws}\n`)
})

fancy
.stdout()
.end('does not truncate', output => {
const three = {...apps[0], id: '0'.repeat(80), name: 'supertable-test-3'}
cli.table(apps.concat(three), columns, {filter: 'id=0', 'no-truncate': true})
expect(output.stdout).to.equal(`ID${ws.padEnd(78)} Name${ws.padEnd(14)}
${three.id} supertable-test-3${ws}\n`)
expect(output.stdout).to.equal(` ID${ws.padEnd(78)} Name${ws.padEnd(14)}
${''.padEnd(three.id.length, '─')} ─────────────────${ws}
${three.id} supertable-test-3${ws}\n`)
})
})

Expand All @@ -269,9 +287,10 @@ ${three.id} supertable-test-3${ws}\n`)
.stdout()
.end('ignores header case', output => {
cli.table(apps, columns, {columns: 'iD,Name', filter: 'nAMe=supertable-test', sort: '-ID'})
expect(output.stdout).to.equal(`ID Name${ws.padEnd(14)}
321 supertable-test-2${ws}
123 supertable-test-1${ws}\n`)
expect(output.stdout).to.equal(` ID Name${ws.padEnd(14)}
─── ─────────────────${ws}
321 supertable-test-2${ws}
123 supertable-test-1${ws}\n`)
})

fancy
Expand All @@ -287,11 +306,12 @@ ${three.id} supertable-test-3${ws}\n`)
}

cli.table(apps.concat(app3 as any), columns, {sort: '-ID'})
expect(output.stdout).to.equal(`ID Name${ws.padEnd(14)}
456 supertable-test${ws.padEnd(3)}
3${ws.padEnd(17)}
321 supertable-test-2${ws}
123 supertable-test-1${ws}\n`)
expect(output.stdout).to.equal(` ID Name${ws.padEnd(14)}
─── ─────────────────${ws}
456 supertable-test${ws.padEnd(3)}
3${ws.padEnd(17)}
321 supertable-test-2${ws}
123 supertable-test-1${ws}\n`)
})
})
})