lib: align console.table row to the left

PR-URL: https://github.com/nodejs/node/pull/50135
Fixes: https://github.com/nodejs/node/issues/50117
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Jithil P Ponnan 2023-10-28 12:14:22 +11:00 committed by GitHub
parent e2cf2f0fda
commit 14af167209
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 68 deletions

View File

@ -40,11 +40,10 @@ const renderRow = (row, columnWidths) => {
for (let i = 0; i < row.length; i++) {
const cell = row[i];
const len = getStringWidth(cell);
const needed = (columnWidths[i] - len) / 2;
const needed = (columnWidths[i] - len);
// round(needed) + ceil(needed) will always add up to the amount
// of spaces we need while also left justifying the output.
out += StringPrototypeRepeat(' ', needed) + cell +
StringPrototypeRepeat(' ', MathCeil(needed));
out += cell + StringPrototypeRepeat(' ', MathCeil(needed));
if (i !== row.length - 1)
out += tableChars.middle;
}

View File

@ -247,7 +247,7 @@ test([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], `
{
const line = '─'.repeat(79);
const header = `${' '.repeat(37)}name${' '.repeat(40)}`;
const header = `name${' '.repeat(77)}`;
const name = 'very long long long long long long long long long long long ' +
'long long long long';
test([{ name }], `