Skip to content

Commit

Permalink
feat(print): add style options for tables (#736 by @PauloRSF)
Browse files Browse the repository at this point in the history
  • Loading branch information
PauloRSF committed Sep 3, 2021
1 parent 69ce257 commit 58816e3
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 7 deletions.
35 changes: 35 additions & 0 deletions docs/toolbox-print.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,38 @@ Output:
| Steve | Kellock | 43 |
| Gary | Busey | 73 |
```

You can also pass styles for the table (as specified in [cli-table3](https://github.com/cli-table/cli-table3)):

```js
const { table } = toolbox.print
table(
[
['First Name', 'Last Name', 'Age'],
['Jamon', 'Holmgren', 35],
['Gant', 'Laborde', 36],
['Steve', 'Kellock', 43],
['Gary', 'Busey', 73],
],
{
format: 'lean',
style: { 'padding-left': 0 , 'padding-right': 8 }
},
)
```

Output:

```
┌──────────────────┬─────────────────┬───────────┐
│First Name │Last Name │Age │
├──────────────────┼─────────────────┼───────────┤
│Jamon │Holmgren │35 │
├──────────────────┼─────────────────┼───────────┤
│Gant │Laborde │36 │
├──────────────────┼─────────────────┼───────────┤
│Steve │Kellock │43 │
├──────────────────┼─────────────────┼───────────┤
│Gary │Busey │73 │
└──────────────────┴─────────────────┴───────────┘
```
13 changes: 13 additions & 0 deletions src/toolbox/__snapshots__/print-tools.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,18 @@ Array [
| matthew | 2 |",
undefined,
],
Array [
"| liam | 5 |
| ----------- | ----- |
| matthew | 2 |",
undefined,
],
Array [
"┌─────────┬───┐
│ liam │ 5 │
│ matthew │ 2 │
└─────────┴───┘",
undefined,
],
]
`;
14 changes: 14 additions & 0 deletions src/toolbox/print-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ test('info', () => {
],
{ format: 'markdown' },
)
print.table(
[
['liam', '5'],
['matthew', '2'],
],
{ format: 'markdown', style: { 'padding-left': 1, 'padding-right': 3 } },
)
print.table(
[
['liam', '5'],
['matthew', '2'],
],
{ format: 'lean', style: { compact: true } },
)

expect(spyLogger).toMatchSnapshot()
})
Expand Down
34 changes: 28 additions & 6 deletions src/toolbox/print-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as importedColors from 'colors/safe'
import { commandInfo } from './meta-tools'
import { Toolbox } from '../domain/toolbox'
import { times } from './utils'
import { GluegunPrint, GluegunPrintColors, GluegunPrintTableOptions } from './print-types'
import { GluegunPrint, GluegunPrintColors, GluegunPrintTableOptions, TableStyle } from './print-types'

// We're extending `colors` with a few more attributes
const colors = importedColors as GluegunPrintColors
Expand Down Expand Up @@ -73,13 +73,30 @@ function findWidths(cliTable: CLITable): number[] {
}

/**
* Returns an array of column dividers based on column widths.
* Returns an array of column dividers based on column widths, taking possible
* paddings into account.
*
* @param cliTable Data table.
* @returns Array of properly sized column dividers.
*/
function columnHeaderDivider(cliTable: CLITable): string[] {
return findWidths(cliTable).map(w => Array(w).join('-'))
function columnHeaderDivider(cliTable: CLITable, style: TableStyle = {}): string[] {
const padding = (style['padding-left'] || 0) + (style['padding-right'] || 0)

return findWidths(cliTable).map(w => Array(w + padding).join('-'))
}

/**
* Resets the padding of a table.
*
* @param cliTable Data table.
*/
function resetTablePadding(cliTable: CLITable) {
const style = (cliTable as any).options.style

if (style) {
style['padding-left'] = 1
style['padding-right'] = 1
}
}

/**
Expand All @@ -97,17 +114,22 @@ function table(data: string[][], options: GluegunPrintTableOptions = {}): void {
t = new CLITable({
head: header,
chars: CLI_TABLE_MARKDOWN,
style: options.style,
})
t.push(...data)
t.unshift(columnHeaderDivider(t))
t.unshift(columnHeaderDivider(t, options.style))
resetTablePadding(t)
break
case 'lean':
t = new CLITable()
t = new CLITable({
style: options.style,
})
t.push(...data)
break
default:
t = new CLITable({
chars: CLI_TABLE_COMPACT,
style: options.style,
})
t.push(...data)
}
Expand Down
5 changes: 4 additions & 1 deletion src/toolbox/print-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ export type GluegunPrintColors = typeof importedColors & {
muted: (t: string) => string
}

export type TableStyle = Partial<CLITable.TableInstanceOptions['style']>

export interface GluegunPrintTableOptions {
format?: 'markdown' | 'lean' | 'default'
style?: TableStyle
}

export interface GluegunPrint {
Expand Down Expand Up @@ -46,7 +49,7 @@ export interface GluegunPrint {
/* Finds the column widths for a table */
findWidths: (cliTable: CLITable) => number[]
/* Returns column header dividers for a table */
columnHeaderDivider: (cliTable: CLITable) => string[]
columnHeaderDivider: (cliTable: CLITable, style: TableStyle) => string[]
/* Prints a newline. */
newline: () => void
/* Prints a table of data (usually a 2-dimensional array). */
Expand Down

0 comments on commit 58816e3

Please sign in to comment.