Skip to content

feat: support console.table#14338

Merged
zerosnacks merged 11 commits intofoundry-rs:masterfrom
ndavd:feat/console-table
Apr 22, 2026
Merged

feat: support console.table#14338
zerosnacks merged 11 commits intofoundry-rs:masterfrom
ndavd:feat/console-table

Conversation

@ndavd
Copy link
Copy Markdown
Contributor

@ndavd ndavd commented Apr 15, 2026

Implements console.table formatting.

Changes:

  • Regenerates Console.json to include the 12 new table variants
  • Adds console_table_format
  • Extends ConsoleFmt derive macro to handle table call structs

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@zerosnacks zerosnacks self-assigned this Apr 16, 2026
Copy link
Copy Markdown
Collaborator

@mablr mablr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, sgtm!

Comment thread crates/macros/src/console_fmt.rs Outdated
mablr
mablr previously approved these changes Apr 17, 2026
mablr
mablr previously approved these changes Apr 22, 2026
Copy link
Copy Markdown
Collaborator

@mablr mablr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better with comfy-table!

@mablr mablr self-requested a review April 22, 2026 10:14
Replace fragile name-based detection (`starts_with("table")`) with a
structural check: table call structs have all fields of type `Vec<T>`
(Solidity arrays), while regular `log` calls never use array parameters.
mablr added 2 commits April 22, 2026 12:26
…elds

Combine the name-based check (`starts_with("table")`) with the
structural Vec<T> field check so both must hold. This prevents
accidental table rendering for unrelated structs that happen to
have all-Vec fields.
Copy link
Copy Markdown
Collaborator

@mablr mablr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made table detection more robust.
s/o @zerosnacks for guidance 🫡

Copy link
Copy Markdown
Member

@zerosnacks zerosnacks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@zerosnacks zerosnacks enabled auto-merge (squash) April 22, 2026 10:47
@zerosnacks zerosnacks requested a review from mablr April 22, 2026 10:47
@ndavd
Copy link
Copy Markdown
Contributor Author

ndavd commented Apr 22, 2026

Thank you both for the nice improvements !

@zerosnacks zerosnacks merged commit 3ec88b3 into foundry-rs:master Apr 22, 2026
16 checks passed
@github-project-automation github-project-automation Bot moved this to Done in Foundry Apr 22, 2026
zerosnacks added a commit to foundry-rs/forge-std that referenced this pull request Apr 22, 2026
Resolves #91

- Depends on foundry-rs/foundry#14338

Two of the most common use-cases for tables are easily logging arrays
and comparing multiple labelled values, which often leads the user to
manually line up the values like so:

```solidity
console.log("alice  ", 0x328809Bc894f92807417D2dAD6b7C998c1aFdac6);
console.log("bob    ", 0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e);
console.log("charlie", 0xea475d60c118d7058beF4bDd9c32bA51139a74e0);
```

Both of those use-cases can be solved with a table with a single column
of values. For this reason and because it would double the variants,
I've opted to not support tables with multiple value columns, though it
can be added in the future.

This adds 12 variants for `console.table`:
- 6 single-array (auto-indexed, array of values)
- 6 keyed (`string[]` indexes, array of values)

Value types supported: `uint256`, `int256`, `address`, `bytes32`,
`string`, `bool`.

Some notes:
- I've added this to `console` and not `console2` since it's just adding
new functionality and not introducing any breaking changes. For
`console2` to inherit from `console` and have its own new methods, I
would need to import each method from `console` and define new ones in
`console2` since libraries don't support inheritance, which would be a
lot of maintenance, and also would involve the question of whether to
use the same `CONSOLE_ADDRESS`
- I've chosen the box characters `┌`, `─`, `┼`, `│`, etc. to more
closely match Node.js's `console.table` output and because it's more
aesthetically pleasing, but if preferred, a simpler option is to use the
ASCII version with just `-`, `+`, `|`

Usage example:

```solidity
string[] memory idxs = new string[](3);
idxs[0] = "alice";
idxs[1] = "bob";
idxs[2] = "charlie";
address[] memory addrs = new address[](3);
bytes32[] memory hashes = new bytes32[](3);
for (uint256 i = 0; i < idxs.length; i++) {
    addrs[i] = makeAddr(idxs[i]);
    hashes[i] = keccak256(abi.encodePacked(idxs[i]));
}
console.table(addrs);
console.table(idxs, addrs);
console.table(idxs, hashes);
```

Output:

```txt
Logs:
  ┌─────────┬────────────────────────────────────────────┐
  │ (index) │ Values                                     │
  ├─────────┼────────────────────────────────────────────┤
  │ 0       │ 0x328809Bc894f92807417D2dAD6b7C998c1aFdac6 │
  │ 1       │ 0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e │
  │ 2       │ 0xea475d60c118d7058beF4bDd9c32bA51139a74e0 │
  └─────────┴────────────────────────────────────────────┘
  ┌─────────┬────────────────────────────────────────────┐
  │ (index) │ Values                                     │
  ├─────────┼────────────────────────────────────────────┤
  │ alice   │ 0x328809Bc894f92807417D2dAD6b7C998c1aFdac6 │
  │ bob     │ 0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e │
  │ charlie │ 0xea475d60c118d7058beF4bDd9c32bA51139a74e0 │
  └─────────┴────────────────────────────────────────────┘
  ┌─────────┬────────────────────────────────────────────────────────────────────┐
  │ (index) │ Values                                                             │
  ├─────────┼────────────────────────────────────────────────────────────────────┤
  │ alice   │ 0x9c0257114eb9399a2985f8e75dad7600c5d89fe3824ffa99ec1c3eb8bf3b0501 │
  │ bob     │ 0x38e47a7b719dce63662aeaf43440326f551b8a7ee198cee35cb5d517f2d296a2 │
  │ charlie │ 0x87a213ce1ee769e28decedefb98f6fe48890a74ba84957ebf877fb591e37e0de │
  └─────────┴────────────────────────────────────────────────────────────────────┘
```

Co-authored-by: zerosnacks <95942363+zerosnacks@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants