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
Changes from 1 commit
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
22 changes: 19 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 @@ -43,6 +43,7 @@ class Table<T extends object> {
'no-truncate': options['no-truncate'] || false,
printLine: printLine || ((s: any) => process.stdout.write(s + '\n')),
sort,
title,
}
}

Expand Down Expand Up @@ -236,14 +237,29 @@ 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), '='))
}

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

// print header dividers
let dividers = '| '
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the | devider also be between all columns? SHould both of these be configurable?

Copy link
Contributor Author

@RodEsp RodEsp May 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amphro Geordie brought that up during one of the previous design sessions and we basically all said in the name of saving space they weren't needed between columns. That's why they don't show up in the env command spec.

I could make them configurable but I don't think it's necessary since it's a straight up improvement over the old format that was harder to read and it's a human readable format so we're not breaking (or shouldn't be) things for people.

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 +278,7 @@ class Table<T extends object> {
// print row
// including multi-lines
linesIndexess.forEach((i: number) => {
let l = ''
let l = '| '
for (const col of columns) {
const width = col.width!
let d = (row as any)[col.key]
Expand Down