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

herdr in iTerm2: a page is flipped into a file of its own

razorback16ad87528350cf
diff --git a/hooks/lib/typst.ts b/hooks/lib/typst.ts
index 7c6fe8b..bdf2657 100644
--- a/hooks/lib/typst.ts
+++ b/hooks/lib/typst.ts
@@ -564,8 +564,8 @@ export function probeCall(o: { typst: string; root: string; run: string; cache:
}
// A drawn page's file says how it was compiled: `b` and `d` (the split
-// retry) are the safe mode's tags.
-export const isSafePage = (file: string) => /\/[bd]-\d+\.png$/.test(file)
+// retry) are the safe mode's tags (`f` before them: stored for flip mode).
+export const isSafePage = (file: string) => /\/f?[bd]-\d+\.png$/.test(file)
// The word positions of page `page` (0-based) of a key folder: the probe
// of that page only (page=<n>), with the inputs of the PNG compile, built
@@ -687,7 +687,7 @@ export async function compile($: EngineInterface, job: Job, signal?: AbortSignal
}
pages = pages.map(p => ({ ...p, rows: Math.min(MAX_IMAGE_ROWS, p.rows) }))
}
- // Flip mode: each page stored for herdr in iTerm2, in place; a page that can not be stored fails the compile (the text shows).
+ // Flip mode: each page stored for herdr in iTerm2 (fa-1.png from a-1.png); a page that can not be stored fails the compile (the text shows).
if (job.flip) {
const flipped = await flipPages($, job, pages, signal)
if (typeof flipped === 'string') return flipped
@@ -700,17 +700,19 @@ export async function compile($: EngineInterface, job: Job, signal?: AbortSignal
}
// The pages of a compile stored for flip mode (flip.ts: rows × B pixels
-// tall, the bands reversed), each in place, with its new height.
+// tall, the bands reversed), each as a file of its own (`f` before its
+// name: a flip of a flipped page would undo it), with its new height.
async function flipPages($: EngineInterface, job: Job, pages: readonly Page[], signal?: AbortSignal): Promise<Page[] | 'failed' | 'cancelled'> {
- const out: Page[] = []
+ const flipped: Page[] = []
for (const p of pages) {
- const made = await flipFile($, { src: p.file, out: p.file, rows: p.rows, isPage: true, perl: perlFor(job.env.isMac), ...(signal ? { signal } : {}) })
+ const out = p.file.replace(/\/([^/]*)$/, '/f$1')
+ const made = await flipFile($, { src: p.file, out, rows: p.rows, isPage: true, perl: perlFor(job.env.isMac), ...(signal ? { signal } : {}) })
if (signal?.aborted) return 'cancelled'
if (!made) {
await logDebug($, job.env.cache, `compile ${job.key}: could not store ${p.file} for herdr in iTerm2`)
return 'failed'
}
- out.push({ ...p, height: made.height, flip: true })
+ flipped.push({ ...p, file: out, height: made.height, flip: true })
}
- return out
+ return flipped
}
diff --git a/tests/copy.test.ts b/tests/copy.test.ts
index 136931d..cc445cc 100644
--- a/tests/copy.test.ts
+++ b/tests/copy.test.ts
@@ -874,6 +874,9 @@ test('a compile retried in safe mode gives the probe safe=1 (its page files say
expect(isSafePage('/c/k/d-12.png')).toBe(true)
expect(isSafePage('/c/k/a-1.png')).toBe(false)
expect(isSafePage('/c/k/c-3.png')).toBe(false)
+ // A page stored for flip mode (herdr in iTerm2) keeps its tag after the `f`.
+ expect(isSafePage('/c/k/fb-1.png')).toBe(true)
+ expect(isSafePage('/c/k/fa-1.png')).toBe(false)
})
test('a grab over two pages of a long answer probes those two pages only', async ($, on) => {
diff --git a/tests/flip.test.ts b/tests/flip.test.ts
index 7302f08..5555d64 100644
--- a/tests/flip.test.ts
+++ b/tests/flip.test.ts
@@ -176,15 +176,22 @@ const jobOf = (key: string, flip: boolean): Job => ({
env: ENV, epoch: 0, ...(flip ? { flip } : {}),
})
-test('a compile in flip mode stores each page rows × B tall with its bands reversed, in place, and notes it in pages.json', async () => {
+test('a compile in flip mode stores each page rows × B tall with its bands reversed, as a file of its own, and notes it in pages.json', async () => {
forgetTouches()
const img = imageOf(16, 41)
const h = host(encode(img))
const result = await compile(h.$, jobOf('kf', true))
if (typeof result !== 'object') throw new Error(`compile: ${result}`)
const page = result.pages[0]!
- expect([page.file, page.width, page.height, page.rows, page.flip]).toEqual(['/c/kf/a-1.png', 16, 40, 4, true])
- expect([...decodePng(h.written.get('/c/kf/a-1.png')!).data]).toEqual([...flipRows(padRows(img, 4), 4).data])
+ expect([page.file, page.width, page.height, page.rows, page.flip]).toEqual(['/c/kf/fa-1.png', 16, 40, 4, true])
+ expect([...decodePng(h.written.get('/c/kf/fa-1.png')!).data]).toEqual([...flipRows(padRows(img, 4), 4).data])
+ // Typst's page is not written over: another compile of the key (another
+ // session's) flips it again from the page as made, never a flip of a flip.
+ expect(h.written.has('/c/kf/a-1.png')).toBe(false)
+ h.texts.delete('/c/kf/pages.json')
+ const again = await compile(h.$, jobOf('kf', true))
+ if (typeof again !== 'object') throw new Error(`compile: ${again}`)
+ expect([...decodePng(h.written.get('/c/kf/fa-1.png')!).data]).toEqual([...flipRows(padRows(img, 4), 4).data])
expect(JSON.parse(h.texts.get('/c/kf/pages.json')!).pages[0].flip).toBe(true)
// Not in flip mode: the page as Typst made it.
const plain = await compile(host(encode(img)).$, jobOf('kp', false))