lib: refactor cli table

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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Ruben Bridgewater 2018-05-25 12:11:37 +02:00 committed by Michaël Zasso
parent 7c2925e609
commit 8b0c6d8322
No known key found for this signature in database
GPG Key ID: 770F7A9A5AE15600

View File

@ -51,11 +51,11 @@ const table = (head, columns) => {
for (var i = 0; i < head.length; i++) { for (var i = 0; i < head.length; i++) {
const column = columns[i]; const column = columns[i];
for (var j = 0; j < longestColumn; j++) { for (var j = 0; j < longestColumn; j++) {
if (!rows[j]) if (rows[j] === undefined)
rows[j] = []; 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 width = columnWidths[i] || 0;
const counted = countSymbols(v); const counted = countSymbols(value);
columnWidths[i] = Math.max(width, counted); columnWidths[i] = Math.max(width, counted);
} }
} }
@ -63,19 +63,16 @@ const table = (head, columns) => {
const divider = columnWidths.map((i) => const divider = columnWidths.map((i) =>
tableChars.middleMiddle.repeat(i + 2)); tableChars.middleMiddle.repeat(i + 2));
const tl = tableChars.topLeft; let result = `${tableChars.topLeft}${divider.join(tableChars.topMiddle)}` +
const tr = tableChars.topRight; `${tableChars.topRight}\n${renderRow(head, columnWidths)}\n` +
const lm = tableChars.leftMiddle; `${tableChars.leftMiddle}${divider.join(tableChars.rowMiddle)}` +
let result = `${tl}${divider.join(tableChars.topMiddle)}${tr} `${tableChars.rightMiddle}\n`;
${renderRow(head, columnWidths)}
${lm}${divider.join(tableChars.rowMiddle)}${tableChars.rightMiddle}
`;
for (const row of rows) for (const row of rows)
result += `${renderRow(row, columnWidths)}\n`; result += `${renderRow(row, columnWidths)}\n`;
result += `${tableChars.bottomLeft}${ result += `${tableChars.bottomLeft}${divider.join(tableChars.bottomMiddle)}` +
divider.join(tableChars.bottomMiddle)}${tableChars.bottomRight}`; tableChars.bottomRight;
return result; return result;
}; };