Skip to content

Add --markdown-tables to lay out Markdown tables - #4008

Closed
Arya-Programmer wants to merge 6 commits into
sharkdp:masterfrom
Arya-Programmer:markdown-tables
Closed

Arya-Programmer wants to merge 6 commits into
sharkdp:masterfrom
Arya-Programmer:markdown-tables

Conversation

@Arya-Programmer

Copy link
Copy Markdown

Closes #4007.

Adds --markdown-tables, which lays out the tables in a Markdown file so the columns line up, and draws a border around them.

                                  ┌───────────┬─────────────┬───────┬───────┐
| Component | Status | Owner |    │ Component │   Status    │ Owner │ Notes │
|---|:---:|---|---:|         ->   ╞═══════════╪═════════════╪═══════╪═══════╡
| parser | done | alice | 12 |    │ parser    │    done     │ alice │    12 │
| renderer | in progress | b |    │ renderer  │ in progress │ bob   │     3 │
                                  └───────────┴─────────────┴───────┴───────┘

Cells keep the alignment the delimiter row asks for, and cell widths are measured with unicode-width, so wide characters line up too. The border shape is the one comfy-table and rich settled on.

How it works

A table cannot be laid out until all of its rows have been seen, so TableRenderer in the new src/markdown_table.rs holds the lines back until the table ends, then rewrites them. The rewrite happens before highlighting, so syntect and the decorations carry on as normal.

The two border lines are the only lines that do not exist in the file. They are printed the way a wrapped line is, as a continuation with an empty gutter, so every real line keeps its own number.

   1 Before the table.
   2
     ┌─────────┬───────┬───────┐
   3 │ Fruit   │ Price │ Stock │
   4 ╞═════════╪═══════╪═══════╡
   5 │ Apples  │  1.20 │  yes  │
     └─────────┴───────┴───────┘

Notes

It is off by default, because it changes the text that gets printed and that would work against the drop-in cat goal. Without the flag the output is byte for byte the input, and the Markdown syntax test fixture is unchanged.

It has no effect under --unbuffered, since holding lines back is the opposite of what that mode is for, and no effect inside fenced or indented code blocks.

PrettyPrinter::markdown_tables exposes the same thing to library users.

Known limits

Tabs inside cells are measured before expand_tabs runs, so a cell containing a tab can still be off. A table wider than the terminal wraps like any other long line. With --line-range, only the visible rows are measured, so the columns can come out narrower than in a full render.

- Add markdown_table module: buffers table lines, pads cells, draws borders
- Honour :--- / :--: / ---: alignment and unicode cell widths
- Skip fenced code blocks; keep one output line per input line
- Add integration test and markdown-table.md example
- Refresh Markdown syntax-test snapshot
- Add CHANGELOG entry
- Surround tables with ┌┬┐ / └┴┘ and rule the header with ╞═╪╡,
  matching comfy-table UTF8_FULL and rich SQUARE
- Print the two added border lines as continuations, so they
  take a blank gutter instead of a line number
- Leave tables alone under --unbuffered, which cannot buffer
- Leave 4-space indented code blocks alone
- Move the test ANSI helper into tests/utils
- Off by default, so bat's output stays byte-for-byte identical
  to the input and keeps the drop-in cat goal
- Wire the flag through clap, app, Config and PrettyPrinter
- Document it in README, the man page, both help texts and the
  bash/fish/zsh/ps1 completions
- Restore the Markdown syntax-test snapshot to its upstream form
- Test the default, the flag, and --unbuffered
@Arya-Programmer
Arya-Programmer deleted the markdown-tables branch September 16, 2026 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Option to lay out Markdown tables with aligned columns

1 participant