Skip to content

Claude's answers, rendered: Markdown, math, charts and diagrams drawn in the Claude Code terminal. Independent project, not affiliated with Anthropic.

TypeScript claude-code-mod

Tables: the grid only for claude-code (table.style)

razorback16e696c021a705
diff --git a/hooks/lib/grid.ts b/hooks/lib/grid.ts
index f729a62..80549a0 100644
--- a/hooks/lib/grid.ts
+++ b/hooks/lib/grid.ts
@@ -5,13 +5,15 @@ import { BODY_PT } from './settings'
// render/measure.typ `grid` (pt): the height a line box of the body and of
// h1 to h6 needs, the same per pt of the code size, the code size in body
-// text (null: the `code` input) and the padding over a first line.
+// text (null: the `code` input), the padding over a first line and the
+// theme's table.style.
export type GridMetrics = {
body: number
heading: number[]
code: number
codeSize: number | null
pad: { quote: number; code: number; table: number }
+ tableStyle: 'rules' | 'grid'
}
// Measured with Typst on macOS: SF Pro Text and Menlo, the vscode values.
export const DEFAULT_GRID: GridMetrics = {
@@ -20,6 +22,7 @@ export const DEFAULT_GRID: GridMetrics = {
code: 1.0775,
codeSize: 1,
pad: { quote: 6.05, code: 12.5714, table: 3.9286 },
+ tableStyle: 'rules',
}
// measure.typ's `grid` as GridMetrics; null when it is not one.
@@ -28,13 +31,15 @@ export function gridOf(v: unknown): GridMetrics | null {
const pad = g?.pad as Record<string, unknown> | undefined
const pt = (x: unknown) => typeof x === 'number' && Number.isFinite(x) && x >= 0
const size = g?.['code-size']
+ const style = g?.['table-style']
if (
!g || !pt(g.body) || !pt(g.code) || !Array.isArray(g.heading) || g.heading.length !== 6 || !g.heading.every(pt)
|| !(size === null || (pt(size) && (size as number) > 0)) || !pad || !pt(pad.quote) || !pt(pad.code) || !pt(pad.table)
+ || (style !== 'rules' && style !== 'grid')
) return null
return {
body: g.body as number, heading: g.heading as number[], code: g.code as number, codeSize: size as number | null,
- pad: { quote: pad.quote as number, code: pad.code as number, table: pad.table as number },
+ pad: { quote: pad.quote as number, code: pad.code as number, table: pad.table as number }, tableStyle: style,
}
}
@@ -48,7 +53,8 @@ const rowsNear = (pt: number, rowPt: number) => Math.round(pt / rowPt)
// even number of rows, the page starts half a row down; code lines of an odd
// number of rows are then half a row down in it, one row more (a heading's
// line box starts the page). `level`: a heading's (1 to 6); `code`: the
-// job's code input. A list is a `para`: body lines.
+// job's code input. A list is a `para`: body lines. A "grid" table: its
+// padding in whole rows and a half, and half a row above its box.
export function gridFirstRow(kind: FirstLine, level: number, rowPt: number, g: GridMetrics, code: number): number {
const middle = (needPt: number) => Math.floor(gridRowsOf(needPt, rowPt) / 2)
const body = middle(g.body)
@@ -66,6 +72,7 @@ export function gridFirstRow(kind: FirstLine, level: number, rowPt: number, g: G
return rowsNear(g.pad.code, rowPt) + Math.floor(n / 2) + (half && n % 2 === 1 ? 1 : 0)
}
case 'table':
+ if (g.tableStyle === 'grid') return rowsNear(Math.max(0, g.pad.table - rowPt / 2), rowPt) + 1 + body
return rowsNear(g.pad.table, rowPt) + body
default:
return body
diff --git a/render/md.typ b/render/md.typ
index 8defb53..30bab3a 100644
--- a/render/md.typ
+++ b/render/md.typ
@@ -426,32 +426,34 @@
}
// ---------- tables ----------
+// table.style: "rules" (VS Code's preview: a rule under the header and
+// between the rows) or "grid" (Claude Code's own tables). Another value
+// draws as "rules". Each column, its header too, has the alignment of the
+// Markdown's delimiter row.
// The grid: each table row is whole rows (padding of whole rows: 5px is
// none), so the rules lie on row boundaries.
-// Claude Code's own table look: a full grid of thin rules (border-px,
-// th-border), the header centred (a column's own alignment wins) and half
-// a row more padding above and below the text of a cell.
+#let table-pad = E.table.pad-y-px * px
+#let rules-inset = (x: E.table.pad-x-px * px, y: if on-grid { rows-near(table-pad) } else { table-pad })
+#let rules-stroke = (x, y) => if y == 1 { (top: E.table.border-px * px + C.th-border) } else if y > 1 { (top: E.table.border-px * px + C.border) }
+// "grid": a full grid of thin rules (border-px, th-border). On the row grid
+// the padding is whole rows and a half, so each rule is in the middle of a
+// row.
#let table-line = E.table.border-px * px + C.th-border
-#let table-rules = (x, y) => (left: if x > 0 { table-line }, top: if y > 0 { table-line })
-#set table(
- inset: (x: E.table.pad-x-px * px, y: if on-grid { rows-near(E.table.pad-y-px * px) + row / 2 } else { 2 * E.table.pad-y-px * px }),
- stroke: table-rules,
-)
+#let grid-stroke = (x, y) => (left: if x > 0 { table-line }, top: if y > 0 { table-line })
+#let grid-inset = (x: E.table.pad-x-px * px, y: if on-grid { rows-near(calc.max(0pt, table-pad - row / 2)) + row / 2 } else { table-pad })
+#let table-grid = E.table.style == "grid"
+#set table(inset: if table-grid { grid-inset } else { rules-inset }, stroke: if table-grid { grid-stroke } else { rules-stroke })
// The outer border is the block's, inside the table's edges (a negative
// outset changes no layout), so a table at a page's edge (no left margin)
// keeps all of it. The clip trims the rule ends that Typst draws past the
-// edges. On the grid, half a row around the box keeps the text on its rows
-// and puts each rule in the middle of a row.
-#show table: it => if it.stroke == table-rules {
- show table.cell.where(y: 0): set align(center)
+// edges. On the row grid, half a row around the box keeps the text on its
+// rows and puts each rule in the middle of a row.
+#show table: it => if it.stroke == grid-stroke {
let framed = block(stroke: table-line, outset: -table-line.thickness / 2, clip: true, it)
if on-grid { block(inset: (y: row / 2), framed) } else { framed }
} else { it }
-// Live ```typst drawings (the only scale here) keep the earlier rules.
-#show scale: set table(
- inset: (x: E.table.pad-x-px * px, y: if on-grid { rows-near(E.table.pad-y-px * px) } else { E.table.pad-y-px * px }),
- stroke: (x, y) => if y == 1 { (top: E.table.border-px * px + C.th-border) } else if y > 1 { (top: E.table.border-px * px + C.border) },
-)
+// Live ```typst drawings (the only scale here) keep the "rules" look.
+#show scale: set table(inset: rules-inset, stroke: rules-stroke)
#show table.cell.where(y: 0): set text(weight: E.table.header-weight)
#show table: set par(leading: 0.38em)
diff --git a/render/measure.typ b/render/measure.typ
index bf9bd15..151ea42 100644
--- a/render/measure.typ
+++ b/render/measure.typ
@@ -11,7 +11,8 @@
// a line in pt (line-ink); `code` the code font's per pt of its size, and
// `code-size` that size in body text (none: the `code` input); `pad` the
// padding in pt over the first line of a quote, a code block and a table
-// cell (md.typ rounds it to whole rows).
+// cell (md.typ rounds it to whole rows); `table-style` the theme's
+// table.style ("grid" or "rules").
//
// `fonts`: the theme's `body` and `code` font lists (merged over vscode),
// and `plain`: its list markers and numbering are ASCII only (fonts.ts uses
@@ -57,6 +58,7 @@
code: query(<mc>).first().value,
code-size: if type(T.code-block.size) in (int, float) { float(T.code-block.size) } else { none },
pad: (quote: T.quote.pad-y-em * 11, code: T.code-block.pad-px * px, table: T.table.pad-y-px * px),
+ table-style: if T.table.style == "grid" { "grid" } else { "rules" },
),
fonts: (body: sans, code: mono-base, plain: (T.list.markers + (T.list.numbering,)).all(is-ascii)),
)) <m>]
diff --git a/render/themes/README.md b/render/themes/README.md
index a892120..95a5ec4 100644
--- a/render/themes/README.md
+++ b/render/themes/README.md
@@ -58,6 +58,7 @@ Example: pink inline code, rounder boxes.
| `code-block.size`, `line-height` | `1.0`, `1.357` | |
| `code-block.pad-px`, `radius-px`, `border-px` | `16`, `3`, `1` | |
| `quote.bar-px`, `radius-px`, `pad-left-px`, `pad-right-px`, `pad-y-em` | `5`, `2`, `10`, `16`, `0.55` | |
+| `table.style` | `"rules"` | `"rules"`: VS Code's preview (a rule under the header and between the rows). `"grid"`: Claude Code's own tables (a box and a rule between all rows and columns, all in `th-border`). Another value draws as `"rules"`. Each column, its header too, has the alignment of the Markdown (`:---` left, `:---:` centre, `---:` right, `---` left). |
| `table.pad-x-px`, `pad-y-px`, `border-px`, `header-weight` | `10`, `5`, `1`, `700` | |
| `rule.border-px`, `gap-em` | `1`, `1.5` | Horizontal rule |
@@ -65,6 +66,7 @@ With `/iui grid on`, the lines of text lie on the terminal's rows. Then some key
- `text.leading-em`, `text.paragraph-em`, `heading.leading-em`, `heading.rule-pad-em`, `list.item-spacing-em` and `code-block.line-height` are not used. Each line is one row (a large heading is two or more rows; a line of an even number of rows is half a row down, with half a row more above and below its block). The gap between paragraphs and blocks is one row. The items of a tight list have no gap. The items of a loose list (a list with a blank line between its items or in an item) have a gap of one row.
- `heading.above-em` and `below-em`, `rule.gap-em`, `code-block.pad-px`, `quote.pad-y-em` and `table.pad-y-px` are rounded to the nearest whole number of rows. Gaps above and below headings and rules are one row or more.
+- With `table.style` `"grid"`, `table.pad-y-px` is rounded to the nearest whole number of rows and a half (half a row or more), and the table has half a row more above and below its box. Each rule is then in the middle of a row.
## Licence
diff --git a/render/themes/claude-code/theme.json b/render/themes/claude-code/theme.json
index 8795383..aae9a5b 100644
--- a/render/themes/claude-code/theme.json
+++ b/render/themes/claude-code/theme.json
@@ -64,5 +64,9 @@
"quote": {
"bar-px": 3,
"radius-px": 0
+ },
+ "table": {
+ "style": "grid",
+ "pad-y-px": 10
}
}
diff --git a/render/themes/vscode/theme.json b/render/themes/vscode/theme.json
index b4537cb..bc7faaa 100644
--- a/render/themes/vscode/theme.json
+++ b/render/themes/vscode/theme.json
@@ -102,6 +102,7 @@
"pad-y-em": 0.55
},
"table": {
+ "style": "rules",
"pad-x-px": 10,
"pad-y-px": 5,
"border-px": 1,
diff --git a/tests/draw.test.ts b/tests/draw.test.ts
index c37640a..8387c74 100644
--- a/tests/draw.test.ts
+++ b/tests/draw.test.ts
@@ -373,7 +373,7 @@ test('with the grid on, the bullet row comes from the grid numbers that the font
// Body text that needs 2 rows (a large size): its line is half a row down, its text in row 1.
const measured = (body: number) => JSON.stringify({
term: null, menlo: { adv: 0.602, xh: 0.547, avg: 0.602 }, body: { adv: 0.63, xh: 0.526, avg: 0.483 },
- grid: { body, heading: [31.8, 20.4, 14.2, 11.4, 9.9, 9.7], code: 1.0775, 'code-size': 1, pad: { quote: 6.05, code: 12.57, table: 3.93 } },
+ grid: { body, heading: [31.8, 20.4, 14.2, 11.4, 9.9, 9.7], code: 1.0775, 'code-size': 1, pad: { quote: 6.05, code: 12.57, table: 3.93 }, 'table-style': 'rules' },
})
const { clock, compiles } = world(on, { TERM: 'xterm-kitty' }, HEAD, { grid: true }, undefined, { metrics: () => measured(20) })
await $.command.run({ command: 'iui', args: '' } as any)
@@ -384,6 +384,25 @@ test('with the grid on, the bullet row comes from the grid numbers that the font
expect(bulletRowOf(await ui.drawn())).toBe(1)
})
+test('with the grid on, a page that starts with a "grid" table has its bullet one row lower, on its first line', async ($, on) => {
+ // A measure of table.style "grid" with padding 5 px, at rows of 11.63 pt:
+ // the box's half row and the cell's half row put the first line in row 1
+ // ("rules": row 0).
+ const measured = (style: string, table: number) => JSON.stringify({
+ term: null, menlo: { adv: 0.602, xh: 0.547, avg: 0.602 }, body: { adv: 0.63, xh: 0.526, avg: 0.483 },
+ grid: { body: 11.2836, heading: [31.8, 20.4, 14.2, 11.4, 9.9, 9.7], code: 1.0775, 'code-size': 1, pad: { quote: 6.05, code: 12.57, table }, 'table-style': style },
+ })
+ const table = '| a | b |\n|---|---|\n| 1 | 2 |'
+ const { clock, compiles } = world(on, { TERM: 'xterm-kitty' }, HEAD, { grid: true }, undefined, { metrics: () => measured('grid', 3.93) })
+ await $.command.run({ command: 'iui', args: '' } as any)
+ const ui = await $.ui.mount(message('g-5', table))
+ await clock.settle()
+ await ui.redraw(props(table))
+ expect(compiles[0]).toMatchObject({ grid: '1', top: '0' })
+ expect(compiles[0]!.row).toBe('11.6331')
+ expect(bulletRowOf(await ui.drawn())).toBe(1)
+})
+
test('a heading with the grid off keeps its bullet row and top margin', async ($, on) => {
const { clock, compiles } = world(on, { TERM: 'xterm-kitty' }, HEAD, { grid: false })
await $.command.run({ command: 'iui', args: '' } as any)
diff --git a/tests/pure.test.ts b/tests/pure.test.ts
index a71cce5..502b528 100644
--- a/tests/pure.test.ts
+++ b/tests/pure.test.ts
@@ -273,6 +273,10 @@ test('with the row grid, no top margin and the bullet on the row of the first li
for (const [md, rows] of Object.entries(drawn)) {
rowsPt.forEach((rowPt, i) => expect([md, placeFirstLine(md, rowPt, grid)]).toEqual([md, { topPt: 0, bulletRow: rows[i] }]))
}
+ // A "grid" table (claude-code): the half row above its box and its
+ // padding of whole rows and a half put its first line one row lower.
+ const boxed = { metrics: { ...DEFAULT_GRID, pad: { ...DEFAULT_GRID.pad, table: 7.8571 }, tableStyle: 'grid' as const }, code: 0.8785 }
+ rowsPt.forEach((rowPt, i) => expect(placeFirstLine('| a | b |\n|---|---|', rowPt, boxed)).toEqual({ topPt: 0, bulletRow: [1, 2, 2, 2, 3][i] }))
// Grid off: the margin and row of before.
for (const md of Object.keys(drawn)) expect(placeFirstLine(md, 11)).toEqual(alignFirstLine(firstLineOf(md), 11))
})
@@ -302,17 +306,20 @@ test('a line box of n rows has its ink centre on row floor(n / 2), under the pad
test('the grid numbers of the font measure are read, and a bad one is refused', () => {
const measured = {
body: 11.2836, heading: [31.8, 20.4, 14.2, 11.4, 9.9, 9.7], code: 1.0775, 'code-size': null,
- pad: { quote: 6.05, code: 12.57, table: 3.93 },
+ pad: { quote: 6.05, code: 12.57, table: 3.93 }, 'table-style': 'rules',
}
expect(gridOf(measured)).toEqual({
- body: 11.2836, heading: [31.8, 20.4, 14.2, 11.4, 9.9, 9.7], code: 1.0775, codeSize: null, pad: { quote: 6.05, code: 12.57, table: 3.93 },
+ body: 11.2836, heading: [31.8, 20.4, 14.2, 11.4, 9.9, 9.7], code: 1.0775, codeSize: null, pad: { quote: 6.05, code: 12.57, table: 3.93 }, tableStyle: 'rules',
})
+ expect(gridOf({ ...measured, 'table-style': 'grid' })?.tableStyle).toBe('grid')
expect(gridOf({ ...measured, 'code-size': 1 })?.codeSize).toBe(1)
expect(gridOf(undefined)).toBe(null)
expect(gridOf({ ...measured, heading: [1, 2] })).toBe(null)
expect(gridOf({ ...measured, body: '11' })).toBe(null)
expect(gridOf({ ...measured, pad: { quote: 1 } })).toBe(null)
expect(gridOf({ ...measured, 'code-size': 'terminal' })).toBe(null)
+ expect(gridOf({ ...measured, 'table-style': 'box' })).toBe(null)
+ expect(gridOf({ ...measured, 'table-style': undefined })).toBe(null)
})
test('pages are padded to whole rows, so rows round; a page keeps the box it was compiled for', () => {
diff --git a/tests/table-fixture.ts b/tests/table-fixture.ts
new file mode 100644
index 0000000..d371a4e
--- /dev/null
+++ b/tests/table-fixture.ts
@@ -0,0 +1,77 @@
+// A real table: render/md.typ's words and rules for TABLE_MD, with the row
+// grid, at 98 columns of a 16 x 32 px cell (width 570.02 pt, ppi 198.0562,
+// row 11.6331 pt, code 0.8784, no term-font), dark, in the vscode theme
+// ("rules") and the claude-code theme ("grid"). `rows`: the PNG's height in
+// rows. To make it again (after a template change), with doc.json
+// {"base": "/tmp", "segments": [TABLE_MD], "images": []} and
+// IN="--input theme=dark --input width=570.02 --input top=0
+// --input row=11.6331 --input grid=1 --input code=0.8784
+// --input theme-dir=$PWD/render/themes/<theme> --input doc=$PWD/doc.json":
+// typst compile --root / --package-path render/packages --format png \
+// --ppi 198.0562 $IN render/md.typ 'a-{0p}.png' # rows: height / 32
+// typst eval --root / --package-path render/packages $IN --input probe=1 \
+// '(words: query(<mdpos>).map(m => m.value).filter(m => m.k == "w")
+// .map(m => (t: m.t, x: calc.round(m.x, digits: 2), y: calc.round(m.y, digits: 2),
+// w: calc.round(m.w, digits: 2), h: calc.round(m.h, digits: 2))),
+// rules: range(3).map(y => range(4).map(x => (query(table).first().stroke)(x, y))))' \
+// --in render/md.typ # words, rules
+// `rules[y][x]`: the table's stroke of cell (x, y).
+export const TABLE_MD = '| L | C | R | P |\n|:---|:---:|---:|---|\n| a long left cell | a long centre cell | a long right cell | a long plain cell |\n| 1 | 2 | 3 | 4 |\n'
+export const TABLE_ROW = 11.6331
+
+// The font measure's `grid` of each theme (rounded), the hooks module's
+// numbers for the bullet row:
+// typst eval 'query(<m>).first().value.grid' --in render/measure.typ \
+// --root / --input term-font= --input theme-dir=$PWD/render/themes/<theme>
+const measured = (table: number, style: string) => ({
+ body: 11.2836, heading: [31.8046, 20.4458, 14.1985, 11.3588, 9.9389, 9.655], code: 1.0775, 'code-size': 1,
+ pad: { quote: 6.05, code: 12.5714, table }, 'table-style': style,
+})
+export const TABLE_GRID: Record<'vscode' | 'vscode-modern' | 'claude-code', unknown> = {
+ vscode: measured(3.9286, 'rules'),
+ 'vscode-modern': measured(3.9286, 'rules'),
+ 'claude-code': measured(7.8571, 'grid'),
+}
+
+type Word = { t: string; x: number; y: number; w: number; h: number }
+type Rule = { left?: string | null; top: string | null } | null
+export type TableDrawn = { rows: number; words: Word[]; rules: Rule[][] }
+
+const vscodeTh = '0.79pt + rgb("#ffffffb0")'
+const vscodeBorder = '0.79pt + rgb("#ffffff2e")'
+const claudeLine = '0.79pt + rgb("#888888")'
+
+export const TABLE_DRAWN: Record<'vscode' | 'claude-code', TableDrawn> = {
+ vscode: {
+ rows: 3,
+ words: [
+ {"t":"L","x":7.86,"y":0.0,"w":6.54,"h":11.63},{"t":"C","x":136.76,"y":0.0,"w":8.15,"h":11.63},{"t":"R","x":273.21,"y":0.0,"w":7.64,"h":11.63},{"t":"P","x":296.56,"y":0.0,"w":7.41,"h":11.63},
+ {"t":"a","x":7.86,"y":11.63,"w":6.07,"h":11.63},{"t":"long","x":17.02,"y":11.63,"w":22.4,"h":11.63},{"t":"left","x":42.52,"y":11.63,"w":17.04,"h":11.63},{"t":"cell","x":62.65,"y":11.63,"w":18.0,"h":11.63},
+ {"t":"a","x":96.37,"y":11.63,"w":6.07,"h":11.63},{"t":"long","x":105.53,"y":11.63,"w":22.4,"h":11.63},{"t":"centre","x":131.03,"y":11.63,"w":33.17,"h":11.63},{"t":"cell","x":167.29,"y":11.63,"w":18.0,"h":11.63},
+ {"t":"a","x":201.01,"y":11.63,"w":6.07,"h":11.63},{"t":"long","x":210.17,"y":11.63,"w":22.4,"h":11.63},{"t":"right","x":235.67,"y":11.63,"w":24.08,"h":11.63},{"t":"cell","x":262.84,"y":11.63,"w":18.0,"h":11.63},
+ {"t":"a","x":296.56,"y":11.63,"w":6.07,"h":11.63},{"t":"long","x":305.72,"y":11.63,"w":22.4,"h":11.63},{"t":"plain","x":331.22,"y":11.63,"w":24.7,"h":11.63},{"t":"cell","x":359.01,"y":11.63,"w":18.0,"h":11.63},
+ {"t":"1","x":7.86,"y":23.27,"w":5.1,"h":11.63},{"t":"2","x":137.51,"y":23.27,"w":6.64,"h":11.63},{"t":"3","x":273.95,"y":23.27,"w":6.9,"h":11.63},{"t":"4","x":296.56,"y":23.27,"w":7.08,"h":11.63},
+ ],
+ rules: [
+ [null, null, null, null],
+ [{ top: vscodeTh }, { top: vscodeTh }, { top: vscodeTh }, { top: vscodeTh }],
+ [{ top: vscodeBorder }, { top: vscodeBorder }, { top: vscodeBorder }, { top: vscodeBorder }],
+ ],
+ },
+ 'claude-code': {
+ rows: 7,
+ words: [
+ {"t":"L","x":7.86,"y":11.63,"w":6.54,"h":11.63},{"t":"C","x":136.76,"y":11.63,"w":8.15,"h":11.63},{"t":"R","x":273.21,"y":11.63,"w":7.64,"h":11.63},{"t":"P","x":296.56,"y":11.63,"w":7.41,"h":11.63},
+ {"t":"a","x":7.86,"y":34.9,"w":6.07,"h":11.63},{"t":"long","x":17.02,"y":34.9,"w":22.4,"h":11.63},{"t":"left","x":42.52,"y":34.9,"w":17.04,"h":11.63},{"t":"cell","x":62.65,"y":34.9,"w":18.0,"h":11.63},
+ {"t":"a","x":96.37,"y":34.9,"w":6.07,"h":11.63},{"t":"long","x":105.53,"y":34.9,"w":22.4,"h":11.63},{"t":"centre","x":131.03,"y":34.9,"w":33.17,"h":11.63},{"t":"cell","x":167.29,"y":34.9,"w":18.0,"h":11.63},
+ {"t":"a","x":201.01,"y":34.9,"w":6.07,"h":11.63},{"t":"long","x":210.17,"y":34.9,"w":22.4,"h":11.63},{"t":"right","x":235.67,"y":34.9,"w":24.08,"h":11.63},{"t":"cell","x":262.84,"y":34.9,"w":18.0,"h":11.63},
+ {"t":"a","x":296.56,"y":34.9,"w":6.07,"h":11.63},{"t":"long","x":305.72,"y":34.9,"w":22.4,"h":11.63},{"t":"plain","x":331.22,"y":34.9,"w":24.7,"h":11.63},{"t":"cell","x":359.01,"y":34.9,"w":18.0,"h":11.63},
+ {"t":"1","x":7.86,"y":58.17,"w":5.1,"h":11.63},{"t":"2","x":137.51,"y":58.17,"w":6.64,"h":11.63},{"t":"3","x":273.95,"y":58.17,"w":6.9,"h":11.63},{"t":"4","x":296.56,"y":58.17,"w":7.08,"h":11.63},
+ ],
+ rules: [
+ [{ left: null, top: null }, { left: claudeLine, top: null }, { left: claudeLine, top: null }, { left: claudeLine, top: null }],
+ [{ left: null, top: claudeLine }, { left: claudeLine, top: claudeLine }, { left: claudeLine, top: claudeLine }, { left: claudeLine, top: claudeLine }],
+ [{ left: null, top: claudeLine }, { left: claudeLine, top: claudeLine }, { left: claudeLine, top: claudeLine }, { left: claudeLine, top: claudeLine }],
+ ],
+ },
+}
diff --git a/tests/tables.test.ts b/tests/tables.test.ts
new file mode 100644
index 0000000..545bfda
--- /dev/null
+++ b/tests/tables.test.ts
@@ -0,0 +1,73 @@
+import { expect, test } from 'claude-code/testing'
+
+import { placeFirstLine } from '../hooks/lib/draw'
+import { DEFAULT_GRID, gridFirstRow, gridOf } from '../hooks/lib/grid'
+import { TABLE_DRAWN, TABLE_GRID, TABLE_MD, TABLE_ROW, type TableDrawn } from './table-fixture'
+
+// The row of a word's ink centre (its line box is centred on its ink).
+const rowOf = (w: { y: number; h: number }) => Math.floor((w.y + w.h / 2) / TABLE_ROW)
+// The text rows of a table, top to bottom.
+const textRows = (d: TableDrawn) => [...new Set(d.words.map(rowOf))]
+// The header cells (one word each) and the cells of the first body row
+// (four words each), as [left, right] in pt.
+const headers = (d: TableDrawn) => d.words.slice(0, 4).map(w => [w.x, w.x + w.w])
+const cells = (d: TableDrawn) => [0, 1, 2, 3].map(i => {
+ const ws = d.words.slice(4 + 4 * i, 8 + 4 * i)
+ return [ws[0]!.x, ws[3]!.x + ws[3]!.w]
+})
+// The grid numbers of the fixture's themes.
+const RULES_GRID = gridOf(TABLE_GRID.vscode)!
+const GRID_GRID = gridOf(TABLE_GRID['claude-code'])!
+
+test('table.style: vscode and vscode-modern draw "rules"; claude-code draws "grid", with twice the padding', () => {
+ expect(gridOf(TABLE_GRID.vscode)).toEqual(DEFAULT_GRID)
+ expect(gridOf(TABLE_GRID['vscode-modern'])).toEqual(DEFAULT_GRID)
+ const grid = gridOf(TABLE_GRID['claude-code'])!
+ expect(grid.tableStyle).toBe('grid')
+ expect(Math.abs(grid.pad.table - 2 * DEFAULT_GRID.pad.table)).toBeLessThan(0.001)
+ expect({ ...grid, pad: DEFAULT_GRID.pad, tableStyle: 'rules' }).toEqual(DEFAULT_GRID)
+})
+
+test('a "rules" table (vscode): a rule under the header and between the rows, no column rules and no box', () => {
+ const d = TABLE_DRAWN.vscode
+ expect(d.rules[0]).toEqual([null, null, null, null])
+ for (const [y, colour] of [[1, '#ffffffb0'], [2, '#ffffff2e']] as const) {
+ for (const rule of d.rules[y]!) expect(rule).toEqual({ top: `0.79pt + rgb("${colour}")` })
+ }
+ // No box: the lines are on rows 0, 1 and 2, and the page is those 3 rows.
+ expect(textRows(d)).toEqual([0, 1, 2])
+ expect(d.rows).toBe(3)
+})
+
+test('a "grid" table (claude-code): thin rules between all rows and columns, in a box', () => {
+ const d = TABLE_DRAWN['claude-code']
+ const line = '0.79pt + rgb("#888888")'
+ d.rules.forEach((row, y) => row.forEach((rule, x) => expect(rule).toEqual({ left: x > 0 ? line : null, top: y > 0 ? line : null })))
+ // Each rule in the middle of a row: one row between the lines, and half a
+ // row of padding and half a row around the box above the first line and
+ // under the last.
+ expect(textRows(d)).toEqual([1, 3, 5])
+ expect(d.rows).toBe(7)
+})
+
+test('each column, its header too, has the alignment of the delimiter row: :--- left, :---: centre, ---: right, --- left', () => {
+ for (const d of Object.values(TABLE_DRAWN)) {
+ const [h, c] = [headers(d), cells(d)]
+ const near = (a: number, b: number) => expect(Math.abs(a - b)).toBeLessThan(0.05)
+ near(h[0]![0]!, c[0]![0]!)
+ near((h[1]![0]! + h[1]![1]!) / 2, (c[1]![0]! + c[1]![1]!) / 2)
+ near(h[2]![1]!, c[2]![1]!)
+ near(h[3]![0]!, c[3]![0]!)
+ }
+})
+
+test('with the row grid, the bullet of a page that starts with a table is on its first line\'s row, in both styles', () => {
+ for (const [d, metrics, row] of [[TABLE_DRAWN.vscode, RULES_GRID, 0], [TABLE_DRAWN['claude-code'], GRID_GRID, 1]] as const) {
+ expect(rowOf(d.words[0]!)).toBe(row)
+ expect(placeFirstLine(TABLE_MD, TABLE_ROW, { metrics, code: 0.8784 })).toEqual({ topPt: 0, bulletRow: row })
+ }
+ // "grid": the padding rounds to whole rows and a half (at least half a row).
+ expect(gridFirstRow('table', 0, 10, { ...GRID_GRID, body: 9, pad: { ...GRID_GRID.pad, table: 0 } }, 1)).toBe(1)
+ expect(gridFirstRow('table', 0, 10, { ...GRID_GRID, body: 9, pad: { ...GRID_GRID.pad, table: 9 } }, 1)).toBe(1)
+ expect(gridFirstRow('table', 0, 10, { ...GRID_GRID, body: 9, pad: { ...GRID_GRID.pad, table: 11 } }, 1)).toBe(2)
+})