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

Paste preview: every drawing that waits for a filled copy draws again; the part file in the run folder

razorback16c5ca6feb6f0e
diff --git a/hooks/lib/composer.tsx b/hooks/lib/composer.tsx
index eff234a..e11ff8d 100644
--- a/hooks/lib/composer.tsx
+++ b/hooks/lib/composer.tsx
@@ -129,23 +129,28 @@ export async function previewsOf(
}
// The filled copies made in this load by key (null: it failed), and the
-// ones being made.
+// ones being made, each with the redraws of the drawings that wait for it.
const filled = new Map<string, string | null>()
-const filling = new Set<string>()
+const filling = new Map<string, Set<() => void>>()
// A picture's filled copy for `box` pixels: its path; undefined while it is
-// made (`redraw` once it is: the plain image shows meanwhile); null when it
-// failed (not tried again) or with no Typst.
+// made (`redraw` once it is, with every other caller's: the plain image
+// shows meanwhile); null when it failed (not tried again) or with no Typst.
function filledOf($: EngineInterface, ready: Env, p: Picture, box: PixelBox, redraw: () => void): string | null | undefined {
if (!ready.typst) return null
const key = `${p.file}@${p.stamp}@${box.width}x${box.height}`
if (filled.has(key)) return filled.get(key) ?? null
- if (filling.has(key)) return undefined
- filling.add(key)
+ const waits = filling.get(key)
+ if (waits) {
+ waits.add(redraw)
+ return undefined
+ }
+ const redraws = new Set([redraw])
+ filling.set(key, redraws)
void makeFilled($, ready, p, box).catch(() => null).then(png => {
filling.delete(key)
keep(filled, key, png, PICTURES_KEPT)
- if (png) redraw()
+ if (png) for (const again of redraws) again()
})
return undefined
}
@@ -161,8 +166,9 @@ async function makeFilled($: EngineInterface, ready: Env, p: Picture, box: Pixel
const dir = folderOf(out)
// The write makes the key folder; Typst does not.
await write($, `${dir}/source.txt`, `${p.file}\n`).catch(() => undefined)
- const part = `${dir}/image.part.png`
const run = `${dir}/r-${Math.random().toString(36).slice(2, 10)}`
+ // The part file is in the run folder: another load's fill of the key has its own.
+ const part = `${run}/image.part.png`
const staged = await exec($, ['sh', '-c', FILL_SH, 'sh', run, ready.root, p.file], { timeoutMs: 10000 }).catch(() => null)
if (staged?.exitCode !== 0) {
await logDebug($, ready.cache, `fill ${p.file}: the root ${run} could not be made: ${staged?.stderr ?? 'sh did not run'}`)
@@ -175,11 +181,11 @@ async function makeFilled($: EngineInterface, ready: Env, p: Picture, box: Pixel
await logDebug($, ready.cache, `fill ${p.file} failed:\n${ran.stderr}`)
return null
}
+ const moved = await exec($, ['mv', '-f', part, out])
+ return moved.exitCode === 0 ? out : null
} finally {
void Promise.resolve(exec($, ['rm', '-rf', run], { timeoutMs: 10000 })).catch(() => undefined)
}
- const moved = await exec($, ['mv', '-f', part, out])
- return moved.exitCode === 0 ? out : null
}
// A draft with an image not found: `redraw` (the band alone) a little later
diff --git a/tests/images.test.ts b/tests/images.test.ts
index a81e8b1..de7d5e5 100644
--- a/tests/images.test.ts
+++ b/tests/images.test.ts
@@ -1296,7 +1296,7 @@ test('under the prompt row: the plain image while Typst makes the filled copy, t
const before = { filled: sets.filter(k => k === 'filled').length, done: sets.filter(k => k === 'done').length }
release()
await clock.settle()
- const out = fills[0]!.at(-1)!.replace(/image\.part\.png$/, 'image.png')
+ const out = fills[0]!.at(-1)!.replace(/\/r-[^/]*\/image\.part\.png$/, '/image.png')
expect(images(await ui.drawn())[0].props).toMatchObject({ source: { file: out }, columns: 20, rows: 5 })
expect(sets.filter(k => k === 'filled').length).toBe(before.filled + 1)
expect(sets.filter(k => k === 'done').length).toBe(before.done)
@@ -1492,7 +1492,7 @@ test('the filled copy: the plain image while Typst makes it, then the copy (the
const redraws = sets.filter(k => k === 'done').length
release()
await clock.settle()
- const out = argv.at(-1)!.replace(/image\.part\.png$/, 'image.png')
+ const out = argv.at(-1)!.replace(/\/r-[^/]*\/image\.part\.png$/, '/image.png')
const [image] = images(await ui.drawn())
expect(image.props.source.file).toBe(out)
expect(image.props).toMatchObject({ columns: 20, rows: 5, alt: '[Image #5]' })
@@ -1517,7 +1517,9 @@ test('a fill runs in a root of its own: only fill.typ and a link to the pasted f
const argv = fills[0]!
const run = argv[argv.indexOf('--root') + 1]!
expect(run).not.toBe('/')
- const key = argv.at(-1)!.replace(/\/image\.part\.png$/, '')
+ // The part file is in the run folder (another load's fill has its own), then moved to the key folder.
+ expect(argv.at(-1)).toBe(`${run}/image.part.png`)
+ const key = run.replace(/\/r-[^/]*$/, '')
expect(run.startsWith(`${key}/r-`)).toBe(true)
expect(argv.at(-2)).toBe(`${run}/fill.typ`)
expect(argv).toContain('f=/image.png')
@@ -1576,6 +1578,33 @@ test('in herdr in iTerm2, a sent prompt draws its pasted images from their flipp
expect(pngs.has(image.props.source.file)).toBe(true)
})
+for (const flip of [false, true]) {
+ test(`a copy made while the band and a sent row both wait for it draws both again${flip ? ' (herdr in iTerm2)' : ''}`, async ($, on) => {
+ let release = () => {}
+ const holdFill = new Promise<void>(r => (release = r))
+ const { clock, fills } = world(on, {
+ typst: true, files: { [`${PASTED}/5.png`]: HEAD }, dirs: [PASTED], holdFill,
+ ...(flip ? { herdr: '/Applications/iTerm.app/Contents/MacOS/iTerm2', bytes: ROWS_PNG } : {}),
+ })
+ editor(on)
+ await ready($)
+ const band1 = await $.ui.mount(band())
+ await ($ as any).prompt.edit(typed('', '[Image #5]'))
+ await band1.drawn()
+ await clock.settle()
+ // Sent before the copy is made: the row's image has the same box, so it waits for the same copy.
+ await ($ as any).prompt.change({ text: '', cursor: 0, origin: { kind: 'clear' } })
+ const row = await $.ui.mount(prompt('[Image #5] what'))
+ await row.drawn()
+ expect(fills.length).toBe(1)
+ release()
+ await clock.settle()
+ // No redraw by the test: the row draws again by itself, with the copy.
+ const [image] = images(await row.drawn())
+ expect(image.props.source.file).toMatch(flip ? /^\/c\/[0-9a-f]{16}\/flip\.png$/ : /^\/c\/[0-9a-f]{16}\/image\.png$/)
+ })
+}
+
test('a fill compile that fails leaves the plain image, and is not tried again', async ($, on) => {
const { clock, fills } = world(on, { typst: true, files: { [`${PASTED}/5.png`]: HEAD }, dirs: [PASTED], failFill: true })
editor(on)