Skip to content

Commit

Permalink
lib: refactor cli table
Browse files Browse the repository at this point in the history
The cli table used multi line template strings which are normally
not used in our code base and it also upper cased a regular function
name. This is changed by this patch.

PR-URL: #20960
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
BridgeAR authored and targos committed Jul 14, 2018
1 parent f1b18ba commit 4433ecb
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions lib/internal/cli_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,28 @@ const table = (head, columns) => {
for (var i = 0; i < head.length; i++) {
const column = columns[i];
for (var j = 0; j < longestColumn; j++) {
if (!rows[j])
if (rows[j] === undefined)
rows[j] = [];
const v = rows[j][i] = HasOwnProperty(column, j) ? column[j] : '';
const value = rows[j][i] = HasOwnProperty(column, j) ? column[j] : '';
const width = columnWidths[i] || 0;
const counted = countSymbols(v);
const counted = countSymbols(value);
columnWidths[i] = Math.max(width, counted);
}
}

const divider = columnWidths.map((i) =>
tableChars.middleMiddle.repeat(i + 2));

const tl = tableChars.topLeft;
const tr = tableChars.topRight;
const lm = tableChars.leftMiddle;
let result = `${tl}${divider.join(tableChars.topMiddle)}${tr}
${renderRow(head, columnWidths)}
${lm}${divider.join(tableChars.rowMiddle)}${tableChars.rightMiddle}
`;
let result = `${tableChars.topLeft}${divider.join(tableChars.topMiddle)}` +
`${tableChars.topRight}\n${renderRow(head, columnWidths)}\n` +
`${tableChars.leftMiddle}${divider.join(tableChars.rowMiddle)}` +
`${tableChars.rightMiddle}\n`;

for (const row of rows)
result += `${renderRow(row, columnWidths)}\n`;

result += `${tableChars.bottomLeft}${
divider.join(tableChars.bottomMiddle)}${tableChars.bottomRight}`;
result += `${tableChars.bottomLeft}${divider.join(tableChars.bottomMiddle)}` +
tableChars.bottomRight;

return result;
};
Expand Down

0 comments on commit 4433ecb

Please sign in to comment.