From 14af1672092e10f60c7f22e41d5f2a185a94583d Mon Sep 17 00:00:00 2001 From: Jithil P Ponnan Date: Sat, 28 Oct 2023 12:14:22 +1100 Subject: [PATCH] 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 Reviewed-By: Trivikram Kamat --- lib/internal/cli_table.js | 5 +- test/parallel/test-console-table.js | 130 ++++++++++++++-------------- 2 files changed, 67 insertions(+), 68 deletions(-) diff --git a/lib/internal/cli_table.js b/lib/internal/cli_table.js index 79b15a38f49bb5..be0bd44bbc36fe 100644 --- a/lib/internal/cli_table.js +++ b/lib/internal/cli_table.js @@ -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; } diff --git a/test/parallel/test-console-table.js b/test/parallel/test-console-table.js index fb1de08323e325..3dac53a5663bde 100644 --- a/test/parallel/test-console-table.js +++ b/test/parallel/test-console-table.js @@ -38,19 +38,19 @@ test([1, 2, 3], ` ┌─────────┬────────┐ │ (index) │ Values │ ├─────────┼────────┤ -│ 0 │ 1 │ -│ 1 │ 2 │ -│ 2 │ 3 │ +│ 0 │ 1 │ +│ 1 │ 2 │ +│ 2 │ 3 │ └─────────┴────────┘ `); test([Symbol(), 5, [10]], ` ┌─────────┬────┬──────────┐ -│ (index) │ 0 │ Values │ +│ (index) │ 0 │ Values │ ├─────────┼────┼──────────┤ -│ 0 │ │ Symbol() │ -│ 1 │ │ 5 │ -│ 2 │ 10 │ │ +│ 0 │ │ Symbol() │ +│ 1 │ │ 5 │ +│ 2 │ 10 │ │ └─────────┴────┴──────────┘ `); @@ -58,46 +58,46 @@ test([null, 5], ` ┌─────────┬────────┐ │ (index) │ Values │ ├─────────┼────────┤ -│ 0 │ null │ -│ 1 │ 5 │ +│ 0 │ null │ +│ 1 │ 5 │ └─────────┴────────┘ `); test([undefined, 5], ` ┌─────────┬───────────┐ -│ (index) │ Values │ +│ (index) │ Values │ ├─────────┼───────────┤ -│ 0 │ undefined │ -│ 1 │ 5 │ +│ 0 │ undefined │ +│ 1 │ 5 │ └─────────┴───────────┘ `); test({ a: 1, b: Symbol(), c: [10] }, ` ┌─────────┬────┬──────────┐ -│ (index) │ 0 │ Values │ +│ (index) │ 0 │ Values │ ├─────────┼────┼──────────┤ -│ a │ │ 1 │ -│ b │ │ Symbol() │ -│ c │ 10 │ │ +│ a │ │ 1 │ +│ b │ │ Symbol() │ +│ c │ 10 │ │ └─────────┴────┴──────────┘ `); test(new Map([ ['a', 1], [Symbol(), [2]] ]), ` ┌───────────────────┬──────────┬────────┐ -│ (iteration index) │ Key │ Values │ +│ (iteration index) │ Key │ Values │ ├───────────────────┼──────────┼────────┤ -│ 0 │ 'a' │ 1 │ -│ 1 │ Symbol() │ [ 2 ] │ +│ 0 │ 'a' │ 1 │ +│ 1 │ Symbol() │ [ 2 ] │ └───────────────────┴──────────┴────────┘ `); test(new Set([1, 2, Symbol()]), ` ┌───────────────────┬──────────┐ -│ (iteration index) │ Values │ +│ (iteration index) │ Values │ ├───────────────────┼──────────┤ -│ 0 │ 1 │ -│ 1 │ 2 │ -│ 2 │ Symbol() │ +│ 0 │ 1 │ +│ 1 │ 2 │ +│ 2 │ Symbol() │ └───────────────────┴──────────┘ `); @@ -105,8 +105,8 @@ test({ a: 1, b: 2 }, ['a'], ` ┌─────────┬───┐ │ (index) │ a │ ├─────────┼───┤ -│ a │ │ -│ b │ │ +│ a │ │ +│ b │ │ └─────────┴───┘ `); @@ -114,8 +114,8 @@ test([{ a: 1, b: 2 }, { a: 3, c: 4 }], ['a'], ` ┌─────────┬───┐ │ (index) │ a │ ├─────────┼───┤ -│ 0 │ 1 │ -│ 1 │ 3 │ +│ 0 │ 1 │ +│ 1 │ 3 │ └─────────┴───┘ `); @@ -123,9 +123,9 @@ test(new Map([[1, 1], [2, 2], [3, 3]]).entries(), ` ┌───────────────────┬─────┬────────┐ │ (iteration index) │ Key │ Values │ ├───────────────────┼─────┼────────┤ -│ 0 │ 1 │ 1 │ -│ 1 │ 2 │ 2 │ -│ 2 │ 3 │ 3 │ +│ 0 │ 1 │ 1 │ +│ 1 │ 2 │ 2 │ +│ 2 │ 3 │ 3 │ └───────────────────┴─────┴────────┘ `); @@ -133,9 +133,9 @@ test(new Map([[1, 1], [2, 2], [3, 3]]).values(), ` ┌───────────────────┬────────┐ │ (iteration index) │ Values │ ├───────────────────┼────────┤ -│ 0 │ 1 │ -│ 1 │ 2 │ -│ 2 │ 3 │ +│ 0 │ 1 │ +│ 1 │ 2 │ +│ 2 │ 3 │ └───────────────────┴────────┘ `); @@ -143,9 +143,9 @@ test(new Map([[1, 1], [2, 2], [3, 3]]).keys(), ` ┌───────────────────┬────────┐ │ (iteration index) │ Values │ ├───────────────────┼────────┤ -│ 0 │ 1 │ -│ 1 │ 2 │ -│ 2 │ 3 │ +│ 0 │ 1 │ +│ 1 │ 2 │ +│ 2 │ 3 │ └───────────────────┴────────┘ `); @@ -153,9 +153,9 @@ test(new Set([1, 2, 3]).values(), ` ┌───────────────────┬────────┐ │ (iteration index) │ Values │ ├───────────────────┼────────┤ -│ 0 │ 1 │ -│ 1 │ 2 │ -│ 2 │ 3 │ +│ 0 │ 1 │ +│ 1 │ 2 │ +│ 2 │ 3 │ └───────────────────┴────────┘ `); @@ -164,15 +164,15 @@ test({ a: { a: 1, b: 2, c: 3 } }, ` ┌─────────┬───┬───┬───┐ │ (index) │ a │ b │ c │ ├─────────┼───┼───┼───┤ -│ a │ 1 │ 2 │ 3 │ +│ a │ 1 │ 2 │ 3 │ └─────────┴───┴───┴───┘ `); test({ a: { a: { a: 1, b: 2, c: 3 } } }, ` ┌─────────┬──────────┐ -│ (index) │ a │ +│ (index) │ a │ ├─────────┼──────────┤ -│ a │ [Object] │ +│ a │ [Object] │ └─────────┴──────────┘ `); @@ -180,7 +180,7 @@ test({ a: [1, 2] }, ` ┌─────────┬───┬───┐ │ (index) │ 0 │ 1 │ ├─────────┼───┼───┤ -│ a │ 1 │ 2 │ +│ a │ 1 │ 2 │ └─────────┴───┴───┘ `); @@ -188,9 +188,9 @@ test({ a: [1, 2, 3, 4, 5], b: 5, c: { e: 5 } }, ` ┌─────────┬───┬───┬───┬───┬───┬───┬────────┐ │ (index) │ 0 │ 1 │ 2 │ 3 │ 4 │ e │ Values │ ├─────────┼───┼───┼───┼───┼───┼───┼────────┤ -│ a │ 1 │ 2 │ 3 │ 4 │ 5 │ │ │ -│ b │ │ │ │ │ │ │ 5 │ -│ c │ │ │ │ │ │ 5 │ │ +│ a │ 1 │ 2 │ 3 │ 4 │ 5 │ │ │ +│ b │ │ │ │ │ │ │ 5 │ +│ c │ │ │ │ │ │ 5 │ │ └─────────┴───┴───┴───┴───┴───┴───┴────────┘ `); @@ -198,9 +198,9 @@ test(new Uint8Array([1, 2, 3]), ` ┌─────────┬────────┐ │ (index) │ Values │ ├─────────┼────────┤ -│ 0 │ 1 │ -│ 1 │ 2 │ -│ 2 │ 3 │ +│ 0 │ 1 │ +│ 1 │ 2 │ +│ 2 │ 3 │ └─────────┴────────┘ `); @@ -208,9 +208,9 @@ test(Buffer.from([1, 2, 3]), ` ┌─────────┬────────┐ │ (index) │ Values │ ├─────────┼────────┤ -│ 0 │ 1 │ -│ 1 │ 2 │ -│ 2 │ 3 │ +│ 0 │ 1 │ +│ 1 │ 2 │ +│ 2 │ 3 │ └─────────┴────────┘ `); @@ -218,7 +218,7 @@ test({ a: undefined }, ['x'], ` ┌─────────┬───┐ │ (index) │ x │ ├─────────┼───┤ -│ a │ │ +│ a │ │ └─────────┴───┘ `); @@ -238,23 +238,23 @@ test(new Map(), ` test([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], ` ┌─────────┬─────┬─────┐ -│ (index) │ a │ b │ +│ (index) │ a │ b │ ├─────────┼─────┼─────┤ -│ 0 │ 1 │ 'Y' │ -│ 1 │ 'Z' │ 2 │ +│ 0 │ 1 │ 'Y' │ +│ 1 │ 'Z' │ 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 }], ` ┌─────────┬──${line}──┐ -│ (index) │ ${header}│ +│ (index) │ ${header} │ ├─────────┼──${line}──┤ -│ 0 │ '${name}' │ +│ 0 │ '${name}' │ └─────────┴──${line}──┘ `); } @@ -263,8 +263,8 @@ test({ foo: '¥', bar: '¥' }, ` ┌─────────┬────────┐ │ (index) │ Values │ ├─────────┼────────┤ -│ foo │ '¥' │ -│ bar │ '¥' │ +│ foo │ '¥' │ +│ bar │ '¥' │ └─────────┴────────┘ `); @@ -272,8 +272,8 @@ test({ foo: '你好', bar: 'hello' }, ` ┌─────────┬─────────┐ │ (index) │ Values │ ├─────────┼─────────┤ -│ foo │ '你好' │ -│ bar │ 'hello' │ +│ foo │ '你好' │ +│ bar │ 'hello' │ └─────────┴─────────┘ `); @@ -285,8 +285,8 @@ test([{ foo: 10 }, { foo: 20 }], ['__proto__'], ` ┌─────────┬───────────┐ │ (index) │ __proto__ │ ├─────────┼───────────┤ -│ 0 │ │ -│ 1 │ │ +│ 0 │ │ +│ 1 │ │ └─────────┴───────────┘ `); assert.strictEqual('0' in Object.prototype, false);