Skip to content

Commit 336393a

Browse files
MrJithilalexfernandez
authored andcommitted
lib: align console.table row to the left
PR-URL: nodejs#50135 Fixes: nodejs#50117 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 3b74897 commit 336393a

File tree

2 files changed

+67
-68
lines changed

2 files changed

+67
-68
lines changed

lib/internal/cli_table.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ const renderRow = (row, columnWidths) => {
4040
for (let i = 0; i < row.length; i++) {
4141
const cell = row[i];
4242
const len = getStringWidth(cell);
43-
const needed = (columnWidths[i] - len) / 2;
43+
const needed = (columnWidths[i] - len);
4444
// round(needed) + ceil(needed) will always add up to the amount
4545
// of spaces we need while also left justifying the output.
46-
out += StringPrototypeRepeat(' ', needed) + cell +
47-
StringPrototypeRepeat(' ', MathCeil(needed));
46+
out += cell + StringPrototypeRepeat(' ', MathCeil(needed));
4847
if (i !== row.length - 1)
4948
out += tableChars.middle;
5049
}

test/parallel/test-console-table.js

+65-65
Original file line numberDiff line numberDiff line change
@@ -38,124 +38,124 @@ test([1, 2, 3], `
3838
┌─────────┬────────┐
3939
│ (index) │ Values │
4040
├─────────┼────────┤
41-
0 1
42-
1 2
43-
2 3
41+
01
42+
12
43+
23
4444
└─────────┴────────┘
4545
`);
4646

4747
test([Symbol(), 5, [10]], `
4848
┌─────────┬────┬──────────┐
49-
│ (index) │ 0 │ Values │
49+
│ (index) │ 0 │ Values
5050
├─────────┼────┼──────────┤
51-
0 │ │ Symbol() │
52-
1 │ │ 5
53-
2 │ 10 │ │
51+
0 │ │ Symbol() │
52+
1 │ │ 5
53+
2 │ 10 │ │
5454
└─────────┴────┴──────────┘
5555
`);
5656

5757
test([null, 5], `
5858
┌─────────┬────────┐
5959
│ (index) │ Values │
6060
├─────────┼────────┤
61-
0 null │
62-
1 5
61+
0 │ null
62+
15
6363
└─────────┴────────┘
6464
`);
6565

6666
test([undefined, 5], `
6767
┌─────────┬───────────┐
68-
│ (index) │ Values │
68+
│ (index) │ Values
6969
├─────────┼───────────┤
70-
0 │ undefined │
71-
15
70+
0 │ undefined │
71+
15
7272
└─────────┴───────────┘
7373
`);
7474

7575
test({ a: 1, b: Symbol(), c: [10] }, `
7676
┌─────────┬────┬──────────┐
77-
│ (index) │ 0 │ Values │
77+
│ (index) │ 0 │ Values
7878
├─────────┼────┼──────────┤
79-
a │ │ 1
80-
b │ │ Symbol() │
81-
c │ 10 │ │
79+
a │ │ 1
80+
b │ │ Symbol() │
81+
c │ 10 │ │
8282
└─────────┴────┴──────────┘
8383
`);
8484

8585
test(new Map([ ['a', 1], [Symbol(), [2]] ]), `
8686
┌───────────────────┬──────────┬────────┐
87-
│ (iteration index) │ Key │ Values │
87+
│ (iteration index) │ Key │ Values │
8888
├───────────────────┼──────────┼────────┤
89-
0 'a' │ 1
90-
1 │ Symbol() │ [ 2 ] │
89+
0 │ 'a' │ 1
90+
1 │ Symbol() │ [ 2 ] │
9191
└───────────────────┴──────────┴────────┘
9292
`);
9393

9494
test(new Set([1, 2, Symbol()]), `
9595
┌───────────────────┬──────────┐
96-
│ (iteration index) │ Values │
96+
│ (iteration index) │ Values
9797
├───────────────────┼──────────┤
98-
01
99-
12
100-
2 │ Symbol() │
98+
01
99+
12
100+
2 │ Symbol() │
101101
└───────────────────┴──────────┘
102102
`);
103103

104104
test({ a: 1, b: 2 }, ['a'], `
105105
┌─────────┬───┐
106106
│ (index) │ a │
107107
├─────────┼───┤
108-
a │ │
109-
b │ │
108+
a │ │
109+
b │ │
110110
└─────────┴───┘
111111
`);
112112

113113
test([{ a: 1, b: 2 }, { a: 3, c: 4 }], ['a'], `
114114
┌─────────┬───┐
115115
│ (index) │ a │
116116
├─────────┼───┤
117-
0 │ 1 │
118-
1 │ 3 │
117+
0 │ 1 │
118+
1 │ 3 │
119119
└─────────┴───┘
120120
`);
121121

122122
test(new Map([[1, 1], [2, 2], [3, 3]]).entries(), `
123123
┌───────────────────┬─────┬────────┐
124124
│ (iteration index) │ Key │ Values │
125125
├───────────────────┼─────┼────────┤
126-
0 1 │ 1
127-
1 2 │ 2
128-
2 3 │ 3
126+
0 │ 1 │ 1
127+
1 │ 2 │ 2
128+
2 │ 3 │ 3
129129
└───────────────────┴─────┴────────┘
130130
`);
131131

132132
test(new Map([[1, 1], [2, 2], [3, 3]]).values(), `
133133
┌───────────────────┬────────┐
134134
│ (iteration index) │ Values │
135135
├───────────────────┼────────┤
136-
0 1
137-
1 2
138-
2 3
136+
01
137+
12
138+
23
139139
└───────────────────┴────────┘
140140
`);
141141

142142
test(new Map([[1, 1], [2, 2], [3, 3]]).keys(), `
143143
┌───────────────────┬────────┐
144144
│ (iteration index) │ Values │
145145
├───────────────────┼────────┤
146-
0 1
147-
1 2
148-
2 3
146+
01
147+
12
148+
23
149149
└───────────────────┴────────┘
150150
`);
151151

152152
test(new Set([1, 2, 3]).values(), `
153153
┌───────────────────┬────────┐
154154
│ (iteration index) │ Values │
155155
├───────────────────┼────────┤
156-
0 1
157-
1 2
158-
2 3
156+
01
157+
12
158+
23
159159
└───────────────────┴────────┘
160160
`);
161161

@@ -164,61 +164,61 @@ test({ a: { a: 1, b: 2, c: 3 } }, `
164164
┌─────────┬───┬───┬───┐
165165
│ (index) │ a │ b │ c │
166166
├─────────┼───┼───┼───┤
167-
a │ 1 │ 2 │ 3 │
167+
a │ 1 │ 2 │ 3 │
168168
└─────────┴───┴───┴───┘
169169
`);
170170

171171
test({ a: { a: { a: 1, b: 2, c: 3 } } }, `
172172
┌─────────┬──────────┐
173-
│ (index) │ a
173+
│ (index) │ a
174174
├─────────┼──────────┤
175-
a │ [Object] │
175+
a │ [Object] │
176176
└─────────┴──────────┘
177177
`);
178178

179179
test({ a: [1, 2] }, `
180180
┌─────────┬───┬───┐
181181
│ (index) │ 0 │ 1 │
182182
├─────────┼───┼───┤
183-
a │ 1 │ 2 │
183+
a │ 1 │ 2 │
184184
└─────────┴───┴───┘
185185
`);
186186

187187
test({ a: [1, 2, 3, 4, 5], b: 5, c: { e: 5 } }, `
188188
┌─────────┬───┬───┬───┬───┬───┬───┬────────┐
189189
│ (index) │ 0 │ 1 │ 2 │ 3 │ 4 │ e │ Values │
190190
├─────────┼───┼───┼───┼───┼───┼───┼────────┤
191-
a │ 1 │ 2 │ 3 │ 4 │ 5 │ │ │
192-
b │ │ │ │ │ │ │ 5
193-
c │ │ │ │ │ │ 5 │ │
191+
a │ 1 │ 2 │ 3 │ 4 │ 5 │ │ │
192+
b │ │ │ │ │ │ │ 5
193+
c │ │ │ │ │ │ 5 │ │
194194
└─────────┴───┴───┴───┴───┴───┴───┴────────┘
195195
`);
196196

197197
test(new Uint8Array([1, 2, 3]), `
198198
┌─────────┬────────┐
199199
│ (index) │ Values │
200200
├─────────┼────────┤
201-
0 1
202-
1 2
203-
2 3
201+
01
202+
12
203+
23
204204
└─────────┴────────┘
205205
`);
206206

207207
test(Buffer.from([1, 2, 3]), `
208208
┌─────────┬────────┐
209209
│ (index) │ Values │
210210
├─────────┼────────┤
211-
0 1
212-
1 2
213-
2 3
211+
01
212+
12
213+
23
214214
└─────────┴────────┘
215215
`);
216216

217217
test({ a: undefined }, ['x'], `
218218
┌─────────┬───┐
219219
│ (index) │ x │
220220
├─────────┼───┤
221-
a │ │
221+
a │ │
222222
└─────────┴───┘
223223
`);
224224

@@ -238,23 +238,23 @@ test(new Map(), `
238238

239239
test([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], `
240240
┌─────────┬─────┬─────┐
241-
│ (index) │ a │ b
241+
│ (index) │ a │ b
242242
├─────────┼─────┼─────┤
243-
0 1 │ 'Y' │
244-
1 │ 'Z' │ 2
243+
01 │ 'Y' │
244+
1 │ 'Z' │ 2
245245
└─────────┴─────┴─────┘
246246
`);
247247

248248
{
249249
const line = '─'.repeat(79);
250-
const header = `${' '.repeat(37)}name${' '.repeat(40)}`;
250+
const header = `name${' '.repeat(77)}`;
251251
const name = 'very long long long long long long long long long long long ' +
252252
'long long long long';
253253
test([{ name }], `
254254
┌─────────┬──${line}──┐
255-
│ (index) │ ${header}
255+
│ (index) │ ${header}
256256
├─────────┼──${line}──┤
257-
0 │ '${name}' │
257+
0 │ '${name}' │
258258
└─────────┴──${line}──┘
259259
`);
260260
}
@@ -263,17 +263,17 @@ test({ foo: '¥', bar: '¥' }, `
263263
┌─────────┬────────┐
264264
│ (index) │ Values │
265265
├─────────┼────────┤
266-
foo '¥' │
267-
bar '¥' │
266+
foo │ '¥'
267+
bar │ '¥'
268268
└─────────┴────────┘
269269
`);
270270

271271
test({ foo: '你好', bar: 'hello' }, `
272272
┌─────────┬─────────┐
273273
│ (index) │ Values │
274274
├─────────┼─────────┤
275-
foo │ '你好' │
276-
bar │ 'hello' │
275+
foo │ '你好' │
276+
bar │ 'hello' │
277277
└─────────┴─────────┘
278278
`);
279279

@@ -285,8 +285,8 @@ test([{ foo: 10 }, { foo: 20 }], ['__proto__'], `
285285
┌─────────┬───────────┐
286286
│ (index) │ __proto__ │
287287
├─────────┼───────────┤
288-
0 │ │
289-
1 │ │
288+
0 │ │
289+
1 │ │
290290
└─────────┴───────────┘
291291
`);
292292
assert.strictEqual('0' in Object.prototype, false);

0 commit comments

Comments
 (0)