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

First line: off the row grid, the theme's padding

razorback1658c3137eb5d9
diff --git a/hooks/lib/draw.tsx b/hooks/lib/draw.tsx
index 40d6060..5e81c42 100644
--- a/hooks/lib/draw.tsx
+++ b/hooks/lib/draw.tsx
@@ -7,7 +7,7 @@ import type { EngineInterface, RenderComponent, RenderSurface, RenderViewport, R
import type { IuiTheme } from '../../types'
import { BODY_PT, bulletOf, GRID_VERSION, GUTTER, LINK_FILES_KEPT, MARGIN_TOP, TEMPLATE_VERSION } from './settings'
import { INLINE_KEPT, inlined, keep, linkFiles, S, type Cell, type Look, type Metrics, type Result, type Room } from './state'
-import { gridFirstRow, type GridMetrics } from './grid'
+import { DEFAULT_GRID, gridFirstRow, type GridMetrics } from './grid'
import { FENCE, imagePathOf } from './markdown'
import { linksOf, type LinkItem } from './links'
import { fitBox, fitsInline, roomColumns } from './terminal'
@@ -89,8 +89,8 @@ export function renderKey(o: {
}
// Where the first line of a page's text lies with no top margin, in the
-// template's pt (measured from its PNGs at 11 pt body text): the centre of
-// its ink, and the top of its ink. By what the answer starts with.
+// template's pt (measured from its PNGs at 11 pt body text, vscode): the
+// centre of its ink, and the top of its ink. By what the answer starts with.
export type FirstLine = 'para' | 'h1' | 'h2' | 'h3' | 'h4' | 'quote' | 'code' | 'table'
const FIRST_LINE: Record<FirstLine, { centre: number; inkTop: number }> = {
para: { centre: 7.45, inkTop: 2.55 },
@@ -115,22 +115,36 @@ export function firstLineOf(md: string): FirstLine {
return 'para'
}
+// FIRST_LINE in a theme (its grid numbers): a quote's, code block's or
+// table's first line moves by the theme's padding less vscode's. A "grid"
+// table's ink starts at its box's top rule, so the box is not cut.
+function firstLineIn(kind: FirstLine, g: GridMetrics): { centre: number; inkTop: number } {
+ const at = FIRST_LINE[kind]
+ if (kind !== 'quote' && kind !== 'code' && kind !== 'table') return at
+ const more = g.pad[kind] - DEFAULT_GRID.pad[kind]
+ return { centre: at.centre + more, inkTop: kind === 'table' && g.tableStyle === 'grid' ? 0 : at.inkTop + more }
+}
+
// The first page's top margin (pt, may be negative) that puts the centre of
// the first line of text on the centre of a terminal row, and which row that
// is: row 0 when the line fits there, else the first row its ink can reach
// without being cut at the picture's top. The bullet is drawn on that row.
-export function alignFirstLine(kind: FirstLine, rowPt: number): { topPt: number; bulletRow: number } {
- const { centre, inkTop } = FIRST_LINE[kind]
+// `g`: the theme's grid numbers (its padding).
+export function alignFirstLine(kind: FirstLine, rowPt: number, g: GridMetrics = DEFAULT_GRID): { topPt: number; bulletRow: number } {
+ const { centre, inkTop } = firstLineIn(kind, g)
const bulletRow = Math.max(0, Math.ceil((centre - inkTop + 0.5) / rowPt - 0.5 - 1e-9))
return { topPt: Math.round(((bulletRow + 0.5) * rowPt - centre) * 100) / 100, bulletRow }
}
// The first page's top margin and bullet row. With the row grid (the
// fonts' grid numbers and the job's code size): no margin, and the row of
-// the first line's ink centre (gridFirstRow).
-export function placeFirstLine(md: string, rowPt: number, grid?: { metrics: GridMetrics; code: number }): { topPt: number; bulletRow: number } {
+// the first line's ink centre (gridFirstRow). Off the grid, `metrics` gives
+// the theme's padding.
+export function placeFirstLine(
+ md: string, rowPt: number, grid?: { metrics: GridMetrics; code: number }, metrics: GridMetrics = grid?.metrics ?? DEFAULT_GRID,
+): { topPt: number; bulletRow: number } {
const kind = firstLineOf(md)
- if (!grid) return alignFirstLine(kind, rowPt)
+ if (!grid) return alignFirstLine(kind, rowPt, metrics)
const level = HEADING.exec(firstTextLine(md))?.[1]!.length ?? 0
return { topPt: 0, bulletRow: gridFirstRow(kind, level, rowPt, grid.metrics, grid.code) }
}
diff --git a/hooks/lib/typst.ts b/hooks/lib/typst.ts
index 27e06dc..e6d3b46 100644
--- a/hooks/lib/typst.ts
+++ b/hooks/lib/typst.ts
@@ -303,8 +303,9 @@ export async function runKillable(
function firstLinePlace(job: Job, segments: readonly string[]) {
const fit = fontFit(job.env.metrics, job.size)
const { rowPt } = sizing(job.columns, job.cell, fit.columnPt)
- const grid = job.grid ? { metrics: job.env.metrics.grid ?? DEFAULT_GRID, code: fit.code } : undefined
- return placeFirstLine(segments[0] ?? '', rowPt, grid)
+ const metrics = job.env.metrics.grid ?? DEFAULT_GRID
+ const grid = job.grid ? { metrics, code: fit.code } : undefined
+ return placeFirstLine(segments[0] ?? '', rowPt, grid, metrics)
}
// The template's inputs for a job and its ppi (the PNG compile and the
diff --git a/tests/draw.test.ts b/tests/draw.test.ts
index 8387c74..2591cb9 100644
--- a/tests/draw.test.ts
+++ b/tests/draw.test.ts
@@ -4,6 +4,7 @@ import {
alignFirstLine, scaledBulletRow, base64Bytes, canDrawIn, COMMIT_MIN_MS, DEFAULT_CELL, fileHref, fitsInline, INLINE_MAX_BYTES, INLINE_MAX_SIDE, inlineBudget, renderKey,
RESIZE_QUIET_MS, resolveLinks, SEGMENT_ROWS, WATCH_PL,
} from '../hooks/register'
+import { TABLE_GRID } from './table-fixture'
// ---- Where pictures draw ----------------------------------------------------
@@ -417,6 +418,25 @@ test('a heading with the grid off keeps its bullet row and top margin', async ($
expect(bulletRowOf(await ui.drawn())).toBe(bulletRow)
})
+test('with the grid off, a page that starts with a "grid" table (claude-code) has the top margin of its padding', async ($, on) => {
+ // claude-code's measure (10 px, its box), at rows of 11.63 pt: its line
+ // is lower than vscode's (margin -5.45, row 0), so a margin that puts it
+ // on row 1 and keeps the box's top rule.
+ const measured = 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: TABLE_GRID['claude-code'],
+ })
+ const table = '| a | b |\n|---|---|\n| 1 | 2 |'
+ const { clock, compiles } = world(on, { TERM: 'xterm-kitty' }, HEAD, { grid: false }, undefined, { metrics: () => measured })
+ await $.command.run({ command: 'iui', args: '' } as any)
+ const ui = await $.ui.mount(message('g-6', table))
+ await clock.settle()
+ await ui.redraw(props(table))
+ expect(compiles[0]!.grid).toBe(undefined)
+ expect(compiles[0]!.row).toBe('11.6331')
+ expect(Number(compiles[0]!.top)).toBe(2.25)
+ expect(bulletRowOf(await ui.drawn())).toBe(1)
+})
+
test('a bad kept grid is on (the default); a bad value is refused and not kept', async ($, on) => {
const { kept } = world(on, { TERM: 'xterm-kitty' }, HEAD, { grid: 'yes' })
const shown = await $.command.run({ command: 'iui', args: 'grid' } as any)
diff --git a/tests/pure.test.ts b/tests/pure.test.ts
index 502b528..d038386 100644
--- a/tests/pure.test.ts
+++ b/tests/pure.test.ts
@@ -250,6 +250,14 @@ test('the first line of text is centred on the bullet row', () => {
expect(inkTop[kind] + topPt).toBeGreaterThan(0)
}
}
+ // A theme's padding over a quote or code block moves its first line by what it pads more.
+ for (const kind of ['quote', 'code'] as const) {
+ const padded = { ...DEFAULT_GRID, pad: { ...DEFAULT_GRID.pad, [kind]: DEFAULT_GRID.pad[kind] + 2 } }
+ const { topPt, bulletRow } = alignFirstLine(kind, 11)
+ const moved = alignFirstLine(kind, 11, padded)
+ expect(moved.bulletRow).toBe(bulletRow)
+ near(moved.topPt, topPt - 2, 0.001)
+ }
})
test('with the row grid, no top margin and the bullet on the row of the first line\'s ink centre', () => {
diff --git a/tests/table-fixture.ts b/tests/table-fixture.ts
index d371a4e..1e1fff8 100644
--- a/tests/table-fixture.ts
+++ b/tests/table-fixture.ts
@@ -33,6 +33,16 @@ export const TABLE_GRID: Record<'vscode' | 'vscode-modern' | 'claude-code', unkn
'claude-code': measured(7.8571, 'grid'),
}
+// The row grid off (IN without --input grid=1): the top-left word's y and
+// h, as in TABLE_DRAWN. claude-code's line is lower by its padding (10 px,
+// not 5 px); its box changes no layout.
+// typst eval --root / --package-path render/packages $IN --input probe=1 \
+// 'query(<mdpos>).map(m => m.value).filter(m => m.k == "w").first()' --in render/md.typ
+export const TABLE_OFF_GRID: Record<'vscode' | 'claude-code', { y: number; h: number }> = {
+ vscode: { y: 3.93, h: 13.13 },
+ 'claude-code': { y: 7.86, h: 13.13 },
+}
+
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[][] }
diff --git a/tests/tables.test.ts b/tests/tables.test.ts
index 545bfda..2b9a13b 100644
--- a/tests/tables.test.ts
+++ b/tests/tables.test.ts
@@ -2,7 +2,7 @@ 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'
+import { TABLE_DRAWN, TABLE_GRID, TABLE_MD, TABLE_OFF_GRID, 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)
@@ -71,3 +71,21 @@ test('with the row grid, the bullet of a page that starts with a table is on its
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)
})
+
+test('with the row grid off, the first line of a page that starts with a table is centred on the bullet row, in both styles', () => {
+ const rowsPt = [16, 13.091, 11.6331, 8.7273]
+ const rules = rowsPt.map(rowPt => placeFirstLine(TABLE_MD, rowPt, undefined, RULES_GRID))
+ const boxed = rowsPt.map(rowPt => placeFirstLine(TABLE_MD, rowPt, undefined, GRID_GRID))
+ // vscode: the margin and row of before.
+ expect(rules).toEqual(rowsPt.map(rowPt => placeFirstLine(TABLE_MD, rowPt)))
+ expect(rules).toEqual([{ topPt: -3.27, bulletRow: 0 }, { topPt: -4.72, bulletRow: 0 }, { topPt: -5.45, bulletRow: 0 }, { topPt: 1.82, bulletRow: 1 }])
+ // claude-code: a margin over 0 keeps the box's top rule in the picture.
+ expect(boxed).toEqual([{ topPt: 8.8, bulletRow: 1 }, { topPt: 4.44, bulletRow: 1 }, { topPt: 2.25, bulletRow: 1 }, { topPt: 6.62, bulletRow: 2 }])
+ // Its first line is as far from its bullet row's centre as vscode's (on it).
+ const fromCentre = (w: { y: number; h: number }, p: { topPt: number; bulletRow: number }, rowPt: number) =>
+ w.y + w.h / 2 + p.topPt - (p.bulletRow + 0.5) * rowPt
+ rowsPt.forEach((rowPt, i) => {
+ const off = fromCentre(TABLE_OFF_GRID['claude-code'], boxed[i]!, rowPt) - fromCentre(TABLE_OFF_GRID.vscode, rules[i]!, rowPt)
+ expect(Math.abs(off)).toBeLessThan(0.02)
+ })
+})