Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/js/builtins/ConsoleObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export function createConsoleConstructor(console: typeof globalThis.console) {
return internalGetStringWidth(str, removeControlChars);
};

// https://github.com/nodejs/node/blob/b7e6a5d37e7a14ef0f2cc95214b95d66c4081415/lib/internal/cli_table.js
const tableChars = {
middleMiddle: "─",
rowMiddle: "┼",
Expand All @@ -195,11 +196,8 @@ export function createConsoleConstructor(console: typeof globalThis.console) {
for (let i = 0; i < row.length; i++) {
const cell = row[i];
const len = getStringWidth(cell);
const needed = (columnWidths[i] - len) / 2;
// round(needed) + ceil(needed) will always add up to the amount
// of spaces we need while also left justifying the output.
out +=
(StringPrototypeRepeat as any).$call(" ", needed) + cell + StringPrototypeRepeat.$call(" ", Math.ceil(needed));
const needed = columnWidths[i] - len;
out += cell + StringPrototypeRepeat.$call(" ", needed);
if (i !== row.length - 1) out += tableChars.middle;
}
out += tableChars.right;
Expand Down
11 changes: 6 additions & 5 deletions test/js/node/console/console-table-iterators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@ test("console.Console#table renders Map and Set iterators", async () => {
if (exitCode !== 0) {
expect(stderr).toBe("");
}
// Known gap: Node splits the Map iterator into `Key` and `Values` columns (https://github.com/oven-sh/bun/pull/42359).
expect(stdout).toMatchInlineSnapshot(`
"┌───────────────────┬────────────┐
│ (iteration index) │ Values │
│ (iteration index) │ Values │
├───────────────────┼────────────┤
│ 0 │ [ 'a', 1 ] │
│ 1 │ [ 'b', 2 ] │
│ 0 │ [ 'a', 1 ] │
│ 1 │ [ 'b', 2 ] │
└───────────────────┴────────────┘
┌───────────────────┬────────┐
│ (iteration index) │ Values │
├───────────────────┼────────┤
│ 0 │ 7 │
│ 1 │ 8 │
│ 0 │ 7 │
│ 1 │ 8 │
└───────────────────┴────────┘
"
`);
Expand Down
83 changes: 83 additions & 0 deletions test/js/node/console/console.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,89 @@ describe("console.Console", () => {
expect(await outValue()).toBe("hello world!\n");
expect(await errValue()).toBe("uh oh!\n");
});

// Each expected table is the output of node v26.3.0 for the same call, not a capture of Bun's.
describe("table pads cells on the right", () => {
async function table(data: unknown, properties?: string[]) {
const [stream, value] = writable();
const c = new Console({ stdout: stream, stderr: stream, colorMode: false });
c.table(data, properties);
stream.end();
return await value();
}

test("array of objects with absent cells", async () => {
expect(
await table([
{ aaaaa: 1, b: "x" },
{ aaaaa: 22222, c: true },
]),
).toMatchInlineSnapshot(`
"┌─────────┬───────┬─────┬──────┐
│ (index) │ aaaaa │ b │ c │
├─────────┼───────┼─────┼──────┤
│ 0 │ 1 │ 'x' │ │
│ 1 │ 22222 │ │ true │
└─────────┴───────┴─────┴──────┘
"
`);
});

test("object of primitives", async () => {
expect(await table({ a: 1, bbbb: "two" })).toMatchInlineSnapshot(`
"┌─────────┬────────┐
│ (index) │ Values │
├─────────┼────────┤
│ a │ 1 │
│ bbbb │ 'two' │
└─────────┴────────┘
"
`);
});

test("Map", async () => {
expect(await table(new Map([["k", 1]]))).toMatchInlineSnapshot(`
"┌───────────────────┬─────┬────────┐
│ (iteration index) │ Key │ Values │
├───────────────────┼─────┼────────┤
│ 0 │ 'k' │ 1 │
└───────────────────┴─────┴────────┘
"
`);
});

test("properties filter", async () => {
expect(
await table(
[
{ a: 1, b: 2 },
{ a: 3, c: 4 },
],
["a", "c"],
),
).toMatchInlineSnapshot(`
"┌─────────┬───┬───┐
│ (index) │ a │ c │
├─────────┼───┼───┤
│ 0 │ 1 │ │
│ 1 │ 3 │ 4 │
└─────────┴───┴───┘
"
`);
});

test("cells wider than their string length", async () => {
expect(await table([{ name: "日本語" }, { name: "ab" }])).toMatchInlineSnapshot(`
"┌─────────┬──────────┐
│ (index) │ name │
├─────────┼──────────┤
│ 0 │ '日本語' │
│ 1 │ 'ab' │
└─────────┴──────────┘
"
`);
});
});
});

test("console._stdout", () => {
Expand Down
Loading