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
Grid: a heading's half row is its gap below, and the page has the body text's phase
razorback16d184f9bd6f68
diff --git a/docs/limitations.md b/docs/limitations.mdindex 041359b..acd02bf 100644--- a/docs/limitations.md+++ b/docs/limitations.md@@ -14,8 +14,10 @@- **Links in a picture:** a click on the picture does not open a link, because the mod cannot put a link on a picture. Use the link rows under the picture (refer to [Links, file paths and commands](usage.md#links-file-paths-and-commands)). A click on a command row copies the command only where Claude Code gets mouse clicks (fullscreen mode). A `file://` link opens in the app that the terminal chooses. A file row shows only when the file exists when the mod draws the answer.- **The row grid** (`/iui grid on`, refer to [Selection and the row grid](usage.md#selection-and-the-row-grid)) has these limits:- Inline math that is taller than a row adds 1 row or more to its line. The text stays on the rows. Math that is only a little taller than the row does not add a row, and its ink can come near the line above or below.- - At 100% text size, a level-1, level-2 or level-3 heading uses 3 rows. A level-2 or level-3 heading has 2 rows for the text and half a row above and below it. With a large text size (`/iui size`), a line uses 2 rows. A line of code uses 2 rows from approximately 111%. A line of body text uses 2 rows from approximately 117%. Then each block of such lines (a paragraph, a list, a table row, a quote or a footnote) is 1 row taller, because the mod moves its lines down by half a row. These values change with the terminal font and the cell size.- - In a list or a quote with lines of 2 rows, the rules of a table, display math, an image, a Typst drawing and a horizontal rule are half a row down. The text stays in the middle of the rows.+ - At 100% text size, a level-1, level-2 or level-3 heading uses 3 rows. A level-2 or level-3 heading has 2 rows for the text and half a row above and below it. With a large text size (`/iui size`), a line uses 2 rows. A line of code uses 2 rows from approximately 111%. A line of body text uses 2 rows from approximately 117%. Then there is an almost empty row between the lines of a paragraph, and the picture starts half a row down. A heading or a code block is 1 row taller if its lines use an odd number of rows and a line of body text uses an even number of rows (or the opposite), because the mod moves its lines down by half a row. These values change with the terminal font and the cell size.+ - If an answer starts with a heading that has the half rows at 100% (for example, a level-3 heading), the first row of the picture is empty.+ - When a line of body text uses 2 rows, the rules of a table, display math, an image, a Typst drawing and a horizontal rule are half a row down. The text stays in the middle of the rows.+ - A level-4 to level-6 heading at 100% (and a level-3 or level-4 heading at approximately 117%) has the same gap above it and below it (1 row), because the two gaps are nearest to 1 row.- If inline math makes the first line of an answer taller, the reply bullet can be one row above the text.- **Search does not find text in a picture.**- **While Claude writes**, the block is hidden, and Claude Code's spinner shows the progress. When the block is final, its picture shows after the compile (0.15–0.6 s). A long answer shows only when it is done. If the picture is not ready 2 s after the block is final, the normal text shows until the picture is ready. If the compile fails, the normal text stays. An earlier answer that is not in the cache (for example, after a resume) is blank for up to 1 s, then shows as text until its picture is ready.diff --git a/docs/usage.md b/docs/usage.mdindex 4ff6dee..f213dbf 100644--- a/docs/usage.md+++ b/docs/usage.md@@ -90,6 +90,8 @@ The terminal draws a mouse selection as full cells, thus a selection covers full- Each line of body text, a list, a quote or a table uses one row (or more rows if the text is large). The middle of the text is in the middle of a row.- Each heading, each code line and each gap between blocks uses full rows.- The middle of a line that uses 2 rows (or 4 rows) is on the border between two rows. Thus the mod moves such a line down by half a row, and adds half a row above and below the block that holds it. For example, a level-2 heading uses 3 rows, and its text is in the middle row. Thus a selection of one row covers the full height of the text.+- If a line of body text uses 2 rows (with a large text size), the picture starts half a row down, thus body text does not move. Only a line that uses an odd number of rows (for example, some headings) gets the half rows. If the picture starts with such a heading, the picture starts at the heading.+- A heading that has the half rows has no gap below it: the half row below its text is the gap to the text under the heading. The gap above the heading stays. For example, at 100%, a level-3 heading has 2 empty rows above it and 1 empty row below it.- The reply bullet is on the row that holds the middle of the first line. For example, the bullet is on the second row of a level-1 heading.- Some blocks have a height that the mod cannot set, for example display math, an image or a Typst drawing. The mod adds space to such a block, so that its height is a number of full rows.diff --git a/hooks/lib/grid.ts b/hooks/lib/grid.tsindex ce3786f..f729a62 100644--- a/hooks/lib/grid.ts+++ b/hooks/lib/grid.ts@@ -44,8 +44,11 @@ const rowsNear = (pt: number, rowPt: number) => Math.round(pt / rowPt)// The row of the ink centre of a page's first line: its padding in whole// rows, then row floor(n / 2) of its line box of n rows (an even box is half-// a row down, so its centre is in row n / 2). `level`: a heading's (1 to 6);-// `code`: the job's code input. A list is a `para`: body lines.+// a row down, so its centre is in row n / 2). When a line of body text is an+// 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.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)@@ -57,8 +60,11 @@ export function gridFirstRow(kind: FirstLine, level: number, rowPt: number, g: Greturn middle(g.heading[level - 1] ?? g.body)case 'quote':return rowsNear(g.pad.quote, rowPt) + body- case 'code':- return rowsNear(g.pad.code, rowPt) + middle(g.code * BODY_PT * (g.codeSize ?? code))+ case 'code': {+ const n = gridRowsOf(g.code * BODY_PT * (g.codeSize ?? code), rowPt)+ const half = gridRowsOf(g.body, rowPt) % 2 === 0+ return rowsNear(g.pad.code, rowPt) + Math.floor(n / 2) + (half && n % 2 === 1 ? 1 : 0)+ }case 'table':return rowsNear(g.pad.table, rowPt) + bodydefault:diff --git a/render/md.typ b/render/md.typindex b67a681..d7fb050 100644--- a/render/md.typ+++ b/render/md.typ@@ -43,8 +43,10 @@// with its text centred, every block starts and ends on a row boundary, and// blocks of free height (display math, live drawings, images) are made a// whole number of rows. A line box of an even number of rows is half a row-// down (phase-pad), so the centre of every line is on the middle of a row.-// Every page's top margin is 0 (`top` is not used). With row=0, grid=1 does+// down, so the centre of every line is on the middle of a row: when a line+// of body text is an even number of rows, the page's top margin is half a+// row, else 0 (`top` is not used), and a block of lines of the other parity+// gets half a row above and below (phase-pad). With row=0, grid=1 does// nothing.#import "@preview/cmarker:0.1.6"@@ -204,6 +206,48 @@let pad = phase-pad(half)if pad == 0pt { body } else { block(inset: (y: pad), { set text(costs: half-costs(half)); body }) }}+// The phase of a page's flow is the body text's: true when a line of body+// text is an even number of rows (needs context, at the body's size). Then+// the page starts half a row down, and the lines of body text (paragraphs,+// lists, table cells, quotes, footnotes) need no half row of their own;+// only a block of the other parity (a heading, code) gets one.+#let flow-half() = calc.even(grid-edges().rows)+// The half row under a heading of the other phase is its gap to the block+// under it, so there is no gap there (the gap above stays). The heading+// sets its padding here; heading-gaps puts a strong -1 row after it, which+// takes away its gap below (one row; with a larger gap above the next block,+// for example a heading's, that gap less one row).+#let heading-pad = state("grid-heading-pad", 0pt)+// `body` (a flow: the page's, a quote's) with that -1 row after each+// heading that has a block after it. After the last block it would make+// the page one row too short, which cuts the heading.+#let heading-gaps(body, page: false) = {+ if type(body) != content or body.func() != [].func() { return body }+ let kids = body.children+ let blank = c => c.func() in ([ ].func(), parbreak)+ let last = kids.len() - 1+ while last >= 0 and blank(kids.at(last)) { last -= 1 }+ let first = 0+ while first <= last and blank(kids.at(first)) { first += 1 }+ let out = kids.enumerate().map(((i, c)) => if c.func() == heading and i < last {+ (c, context if heading-pad.get() > 0pt { v(-row) })+ } else { (c,) }).flatten()+ // page: true for a page's flow. When it has the half-row phase and starts+ // with a heading of the other phase, the page's half row and the+ // heading's half row above are an empty row: -1 row takes it away (the+ // heading's line box starts the page, as in a flow of its own phase).+ // The heading moves half a row more than the others when its height+ // (whole rows) in the other phase is less.+ if page and first <= last and kids.at(first).func() == heading {+ let c = kids.at(first)+ out.insert(0, context if flow-half() {+ let here = measure(block(width: content-w, c)).height+ let other = measure(block(width: content-w, { set text(costs: half-costs(false)); c })).height+ if here > other { v(-row) }+ })+ }+ out.join()+}// ---------- headings ----------#set heading(numbering: none)@@ -217,22 +261,27 @@// The grid: a line box of whole rows (3 for h1 and 2 for h2 and h3 at// the usual sizes: Typst's own heading rule makes h1 1.4em and h2 1.2em// of the theme's size, so h1 is 30.8pt), whole-row gaps (at least one),- // and the rule on the bottom row boundary (no padding under the text: it- // would add height). An even number of rows is half a row down- // (phase-pad): an h2 is then 3 rows, its text in the middle one.+ // and the rule on the bottom of the line box (no padding under the+ // text: it would add height). A line box of the other parity than the+ // flow is half a row down (phase-pad): at 100%, an h2 is then 3 rows,+ // its text in the middle one. Such a heading has no gap below+ // (heading-gaps): the half row under its line box is the gap, and its+ // rule is on the bottom of the line box, above that half row.return {set text(size: size, weight: H.weight)context {let g = grid-edges()let half = calc.even(g.rows)let pad = phase-pad(half)+ let rule = if lvl <= H.rule-levels { (bottom: px + C.border) }set text(top-edge: g.top, bottom-edge: g.bottom, costs: half-costs(half))show raw: set text(top-edge: g.top, bottom-edge: g.bottom)set par(leading: 0pt)+ heading-pad.update(pad)block(- width: 100%, above: calc.max(row, rows-near(above)), below: calc.max(row, rows-near(below)), sticky: true,- inset: (y: pad), stroke: if lvl <= H.rule-levels { (bottom: px + C.border) },- it.body,+ width: 100%, above: calc.max(row, rows-near(above)), below: if pad > 0pt { row } else { calc.max(row, rows-near(below)) },+ sticky: true, inset: (y: pad), stroke: if pad == 0pt { rule },+ if pad == 0pt { it.body } else { block(width: 100%, stroke: rule, it.body) },)}}@@ -363,7 +412,7 @@width: 100%, fill: C.quote-bg, radius: Q.radius-px * px, above: row, below: row,stroke: (left: Q.bar-px * px + C.quote-border),inset: (left: (Q.pad-left-px + Q.bar-px) * px, right: Q.pad-right-px * px, y: rows-near(Q.pad-y-em * 11pt) + phase-pad(half)),- { set block(spacing: row); set text(costs: half-costs(half)); it.body },+ { set block(spacing: row); set text(costs: half-costs(half)); heading-gaps(it.body) },)}}@@ -934,8 +983,8 @@// Body text: line boxes of exactly g.rows rows (fixed edges, no leading),// one row between paragraphs and blocks. Any smaller text (inline code,// footnotes, sub- and superscripts) gets the same fixed edges, so it never- // makes its line taller.- set text(top-edge: g.top, bottom-edge: g.bottom)+ // makes its line taller. The flow has the body text's phase (flow-half).+ set text(top-edge: g.top, bottom-edge: g.bottom, costs: half-costs(calc.even(g.rows)))show raw: set text(top-edge: g.top, bottom-edge: g.bottom)// Raised and lowered text (footnote numbers): x-height to baseline edges,// which stay inside the line box, so it never makes its line taller.@@ -953,14 +1002,14 @@// the text's (its smaller text has the body's fixed edges, so each of its// lines is g.rows rows too).show footnote.entry: set par(leading: 0pt)- // Each paragraph and table cell in its phase (phase-pad; a cell's rules- // stay on row boundaries), not a live drawing's. A footnote entry always- // starts on a row (under the separator's row), whatever refers to it.+ // Each paragraph and table cell in its phase (phase-pad), not a live+ // drawing's: in a flow of the body text's phase it does not move. A+ // footnote entry has the phase of the page's flow, whatever refers to it+ // (its lines are g.rows rows, and the separator and the entries are whole+ // rows under the flow), so it does not move either.show par: it => context if no-marks(text.costs) { it } else { in-phase(it) }show table.cell: in-phase- show footnote.entry: it => context if calc.even(line-rows()) {- block(inset: (y: row / 2), { set text(costs: half-costs(true)); it })- } else { it }+ show footnote.entry: set text(costs: half-costs(calc.even(g.rows)))set footnote.entry(clearance: 0pt, gap: 0pt,separator: block(height: row, align(horizon, line(length: 30%, stroke: px + C.border))),@@ -1047,12 +1096,14 @@#for (i, seg) in segments.enumerate() {if only > 0 and i + 1 != only { continue }let md = if type(seg) == str { seg } else { str(seg) }- // On the grid, every page starts on a row: no top margin.- let top = if on-grid { 0pt } else if i == 0 { first-top } else { margin-y }let body = render-md(md, i)+ if on-grid { body = heading-gaps(body, page: true) }if notes-before > 0 { body = { counter(footnote).update(notes-before); body } }// Pad the bottom so the page is a whole number of terminal rows.context {+ // On the grid, every page starts on a row (no top margin), or half a+ // row lower when the flow has the half-row phase (grid-rules).+ let top = if on-grid { if flow-half() { row / 2 } else { 0pt } } else if i == 0 { first-top } else { margin-y }// No bottom margin: text is measured to its descenders, so the last line// ends the page and the next row is the engine's own gap.let total = top + measure(block(width: content-w, body)).heightdiff --git a/tests/pure.test.ts b/tests/pure.test.tsindex 053ceed..d3ea5f3 100644--- a/tests/pure.test.ts+++ b/tests/pure.test.ts@@ -289,9 +289,14 @@ test('a line box of n rows has its ink centre on row floor(n / 2), under the padexpect(gridFirstRow('quote', 0, row, g(19, 14), 1)).toBe(1 + 1)expect(gridFirstRow('table', 0, row, g(9, 4), 1)).toBe(0)// Code: the code font's need per pt at the theme's size, or at `code` for "terminal".- const code = { ...DEFAULT_GRID, code: 1.5, pad: { ...DEFAULT_GRID.pad, code: 0 } }+ const code = { ...DEFAULT_GRID, body: 9, code: 1.5, pad: { ...DEFAULT_GRID.pad, code: 0 } }expect(gridFirstRow('code', 0, row, code, 0.5)).toBe(1)expect(gridFirstRow('code', 0, row, { ...code, codeSize: null }, 0.5)).toBe(0)+ // Body text of 2 rows: the page starts half a row down, so code lines of+ // an odd number of rows are half a row down in it (one row lower). A+ // heading's line box starts the page.+ expect(gridFirstRow('code', 0, row, { ...code, body: 19, codeSize: null }, 0.5)).toBe(1)+ expect(gridFirstRow('h1', 1, row, { ...DEFAULT_GRID, body: 19, heading: [29, 20, 20, 9, 9, 9] }, 1)).toBe(1)})test('the grid numbers of the font measure are read, and a bad one is refused', () => {