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: flip slices, tall pages, two writers at once
razorback1636a43fd684f1
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex eb79b03..f750858 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -31,7 +31,8 @@ All releases of Intelligent UI for Claude Code (`cc-intelligent-ui`). Claude Cod- With the `auto` theme, the background comes from herdr: herdr gives the background of the terminal in front (OSC 11). This also works with `herdr --remote`. If an answer does not come, the mod keeps the last background for up to 2 more checks.- The font comes from the herdr client that you use. You can attach to one herdr server from more than one terminal, for example from Ghostty and from kitty. On macOS, the mod uses the client in the terminal app in front. When the app in front is not a terminal (for example a browser), it keeps the last client. On Linux, and between two clients in the same app, it uses the client where you typed last. When you change to a different terminal, the mod finds it at the next check (every 1 s), reads its font and draws the pictures again. If the mod cannot find the client, it uses the variables of the terminal that started the herdr server.- In herdr, a tall page of a long answer did not show (herdr does not send an image that is too large to the terminal). Refer to [Limitations](docs/limitations.md).-- At a page break between two blocks (for example a paragraph and a list, or a table and a heading), the next page starts with the same gap as in one page. Before, the gap was lost. With the row grid off, the gap can be up to 1 row larger.+- herdr in iTerm2: the last rows of a page taller than 255 rows no longer go missing, and a picture that two sessions stored at the same time no longer stays blank.+- At a page break between two blocks (for example a paragraph and a list, or a table and a heading), the next page starts with the same gap as in one page. Before, the gap was lost. With the row grid off, the gap can be up to 1 row larger. At a break between two items of a list, the gap is the gap between the items (it was 1 or 2 rows too large).### Changeddiff --git a/docs/development.md b/docs/development.mdindex 8e39f56..3df9601 100644--- a/docs/development.md+++ b/docs/development.md@@ -19,6 +19,7 @@ sh tests/fill-sh-test.sh # the root of a pasted image's preview (FILL_SH):sh tests/check-sh-test.sh # the skill's check.sh: a private cache folder, and a block reads no other file (skips with no Typst 0.15)sh tests/footprint-sh-test.sh # release gate: the validator's footprint is whole (no [+N chars] mark, calls line of 850 characters at most)sh tests/gap-sh-test.sh # the gap at a page break of md.typ (seams), on and off the row grid, with the real Typst (skips with no typst)+sh tests/flip-sh-test.sh # the write of a flipped PNG or a frame (WRITE_PL) with the real perl, also two writers of one file at oncesh tests/herdr-sh-test.sh # the herdr client finder (HERDR_CLIENT_SH) with fake ps, stat, lsof and lsappinfo, in sh, dash and bashsh tests/install-sh-test.sh # the installer site/install.sh in a sandbox HOME with a fake claude, curl and uname (shellcheck when installed)claude plugin validate .diff --git a/docs/how-it-works.md b/docs/how-it-works.mdindex 21667c0..a2ed3af 100644--- a/docs/how-it-works.md+++ b/docs/how-it-works.md@@ -94,7 +94,7 @@ In **herdr**, `TERM_PROGRAM` is `herdr`, so it does not name the terminal that h- **Background:** herdr answers OSC 11 with the background of the terminal in front. In herdr, this is source 0, before the sources above. It also works with `herdr --remote`, where the mod runs on the remote computer. Claude Code reads some answers first. If no answer comes, Intelligent UI keeps the last answer until 3 checks in a row get no answer. Then it uses the sources above.- **Terminal and font:** herdr runs a server, and one or more clients (`herdr`, `herdr --session <name>`, `herdr session attach <name>`) attach to it from terminals. Intelligent UI finds the herdr server above its own process. Then it lists the herdr clients of this user that have a tty (one `ps`). With more than one herdr server, it keeps only the clients that connect to its own server (`lsof`). On macOS, it uses a client in the terminal app in front (`lsappinfo`, by the app's bundle id). If there are two or more such clients, it uses the one whose tty was read last (the access time of the tty changes when you type in that terminal). If the app in front has no client (for example, a browser), it keeps the client that it used before. On Linux, and when it cannot read the app in front, it uses the client whose tty was read last. The tty time alone is not sufficient: iTerm2 answers each picture that herdr sends (herdr puts `q=2`, no answer, only in the first part of the upload), so the tty of the iTerm2 client is read each time that the mod draws pictures, also when you do not type there. An answer of this type from a terminal on Linux is not known. It reads the variables of that client (Linux: `/proc/<pid>/environ`; macOS: `ps eww`) and the terminal app above it (the nearest process with no tty). The terminal app comes first, then `TERM`, `TERM_PROGRAM` and the variables of each terminal. Intelligent UI then uses that terminal's sources above (with that client's `ITERM_PROFILE`, `KITTY_CONFIG_DIRECTORY`, `GHOSTTY_BIN_DIR`, `GHOSTTY_RESOURCES_DIR`, `RIO_CONFIG_HOME` and `COLORFGBG`) and its font for the text size. It checks the client every 1 s. This check reads only the app in front and the tty times of the known clients (one small `sh` with `lsappinfo` and `stat`, approximately 0.01 s of CPU time). The full check with the process list (approximately 0.1 s of CPU time) runs only when the 1 s check finds a change (a different client, a terminal in front with no known client, or a tty that is gone), and every 10 s to find new and gone clients. When the terminal changes, it reads the font and the background again and draws the pictures again.-- **iTerm2:** herdr runs a terminal of its own for each pane. It draws a picture of Claude Code (kitty placeholder cells) in the outer terminal as one kitty placement for each row, with a source rectangle: row k of a picture that is H pixels tall and R rows tall shows the source rows from floor(k × H / R), floor(H / R) rows tall. iTerm2 3.6 and 3.7 count the top of a source rectangle from the bottom of the image. When the client is iTerm2 (not with `herdr --remote`), Intelligent UI stores each picture with the band of each row at the rows that iTerm2 reads for that row (`hooks/lib/flip.ts`). Before this, a page is made exactly R × B pixels tall (B is a whole number), so the change is the reverse order of the bands. Pictures under rows are first made by Typst at their size in cells. The render key includes this mode, so a change of client draws new files.+- **iTerm2:** herdr runs a terminal of its own for each pane. It draws a picture of Claude Code (kitty placeholder cells) in the outer terminal as one kitty placement for each row, with a source rectangle: row k of a picture that is H pixels tall and R rows tall shows the source rows from floor(k × H / R), floor(H / R) rows tall. iTerm2 3.6 and 3.7 count the top of a source rectangle from the bottom of the image. When the client is iTerm2 (not with `herdr --remote`), Intelligent UI stores each picture with the band of each row at the rows that iTerm2 reads for that row (`hooks/lib/flip.ts`). Before this, a page is made exactly R × B pixels tall (B = H / R, rounded): the last row is repeated, or, when the page is taller, each row gets the band that herdr shows for it (so no rows are cut at the bottom). Thus the change is the reverse order of the bands. Pictures under rows are first made by Typst at their size in cells. The render key includes this mode, so a change of client draws new files.- **Fallback:** if it finds no client, it uses the variables that herdr keeps of the terminal that started the herdr server: `CMUX_*` for cmux, `GHOSTTY_RESOURCES_DIR` or `GHOSTTY_BIN_DIR` for Ghostty, `KITTY_PID` or `KITTY_INSTALLATION_DIR` for kitty, `ITERM_PROFILE` for iTerm2. If it finds none, it uses `COLORFGBG`, then the OS appearance.Intelligent UI checks the theme every 3 s (in herdr, also at once when the terminal changes). After a change, each mode's pictures compile once. `/iui probe` asks the terminal for its background with OSC 11 (5 times) and shows the raw answers. This is a test of whether the answer can reach the mod. The 3 s check uses OSC 11 only in herdr.diff --git a/hooks/lib/flip.ts b/hooks/lib/flip.tsindex 04980df..8126ee9 100644--- a/hooks/lib/flip.ts+++ b/hooks/lib/flip.ts@@ -12,7 +12,7 @@import type { EngineInterface } from 'claude-code'import { chunk, concat, decodeSteps, deflatePiece, fromBase64, headOf, tailOf, toBase64, type Piece, type Rgba } from './png'-import { delay, cat, exec } from './api'+import { delay, cat, exec, nowMs } from './api'// The source rows herdr places for row k of a picture `height` pixels tall drawn `rows` rows tall.export function herdrBand(k: number, height: number, rows: number): { y: number; h: number } {@@ -37,15 +37,24 @@ export function flipRows(img: Rgba, rows: number): Rgba {}// The picture `rows` × B pixels tall, B = round(height / rows) (1 at least):-// rows past it cut, or the last row repeated.+// the last row repeated when it is shorter; when it is taller, row k is the+// band herdr shows for it (herdrBand), so the rows it drops are spread, as+// on a right terminal, and the bottom is never cut.export function padRows(img: Rgba, rows: number): Rgba {- const height = rows * Math.max(1, Math.round(img.height / rows))+ const band = Math.max(1, Math.round(img.height / rows))+ const height = rows * bandif (height === img.height) return imgconst stride = img.width * 4const data = new Uint8Array(height * stride)- const kept = Math.min(height, img.height)- data.set(img.data.subarray(0, kept * stride))- for (let y = kept; y < height; y++) data.copyWithin(y * stride, (kept - 1) * stride, kept * stride)+ if (height > img.height) {+ data.set(img.data)+ for (let y = img.height; y < height; y++) data.copyWithin(y * stride, (img.height - 1) * stride, img.height * stride)+ } else {+ for (let k = 0; k < rows; k++) {+ const y = Math.floor((k * img.height) / rows)+ data.set(img.data.subarray(y * stride, (y + band) * stride), k * band * stride)+ }+ }return { width: img.width, height, data }}@@ -87,17 +96,19 @@ export function* encodeSteps(img: Rgba): Generator<undefined, Uint8Array, void>// A PNG stored for flip mode, in steps: decoded, made `rows` × B tall when// `isPage` (padRows), its bands moved (flipRows), encoded; with its size.-export function* flipSteps(png: Uint8Array, rows: number, isPage: boolean): Generator<undefined, Rgba & { png: Uint8Array }, void> {- let img = yield* decodeSteps(png)- if (isPage) img = padRows(img, rows)+// `tall`: the height its rows map to (copy.ts), the PNG's own when padRows+// dropped rows (they were spread over the page, as herdr does).+export function* flipSteps(png: Uint8Array, rows: number, isPage: boolean): Generator<undefined, Rgba & { png: Uint8Array; tall: number }, void> {+ const made = yield* decodeSteps(png)+ const img = isPage ? padRows(made, rows) : madeyield- img = flipRows(img, rows)+ const flipped = flipRows(img, rows)yield- return { ...img, png: yield* encodeSteps(img) }+ return { ...flipped, png: yield* encodeSteps(flipped), tall: Math.max(made.height, img.height) }}// The same at once (tests).-export function flipPng(png: Uint8Array, rows: number, isPage: boolean): Rgba & { png: Uint8Array } {+export function flipPng(png: Uint8Array, rows: number, isPage: boolean): Rgba & { png: Uint8Array; tall: number } {const steps = flipSteps(png, rows, isPage)for (;;) {const got = steps.next()@@ -107,23 +118,41 @@ export function flipPng(png: Uint8Array, rows: number, isPage: boolean): Rgba &// ---- Files --------------------------------------------------------------------------// Writes stdin's base64 to the file (its folder made, a temporary name,-// then renamed), and deletes the older frames named after it (blit.ts).-export const WRITE_PL = 'use MIME::Base64; local $/; my $b = <STDIN>; my ($f, @old) = @ARGV; my ($d) = $f =~ m{^(.*)/}; mkdir($d, 0700) unless -d $d; open(my $o, ">:raw", "$f.tmp") or die "$!\\n"; print $o decode_base64($b); close($o) or die "$!\\n"; rename("$f.tmp", $f) or die "$!\\n"; unlink(@old)'+// Writes stdin's base64 to the file (its folder made, a temporary name of+// this process, then renamed), and deletes the older frames named after it (blit.ts).+export const WRITE_PL = 'use MIME::Base64; local $/; my $b = <STDIN>; my ($f, @old) = @ARGV; my ($d) = $f =~ m{^(.*)/}; mkdir($d, 0700) unless -d $d; my $t = "$f.$$.tmp"; open(my $o, ">:raw", $t) or die "$!\\n"; print $o decode_base64($b); close($o) or die "$!\\n"; rename($t, $f) or die "$!\\n"; unlink(@old)'// A slice of the steps: the hooks run between slices (clock.after(0)).const SLICE_MS = 8-// Runs `steps` in slices; null when `signal` stopped them.+// The time a slice reads (a test can set it).+export const sliceClock = { now: () => performance.now() }++// The engine's time; NaN when it can not be read.+async function engineNow($: EngineInterface): Promise<number> {+ try {+ return await nowMs($)+ } catch {+ return NaN+ }+}++// Runs `steps` in slices; null when `signal` stopped them. No yield while+// the engine's clock stands still (a test's: it fires no clock.after(0)+// while a draw is awaited).async function runSliced<T>($: EngineInterface, steps: Generator<undefined, T, void>, signal?: AbortSignal): Promise<T | null> {+ let at = await engineNow($)for (;;) {- const t0 = performance.now()+ const t0 = sliceClock.now()for (;;) {const got = steps.next()if (got.done) return got.value- if (performance.now() - t0 >= SLICE_MS) break+ if (sliceClock.now() - t0 >= SLICE_MS) break}if (signal?.aborted) return null+ const now = await engineNow($)+ if (now === at) continue+ at = nowawait new Promise<void>(done => delay($, 0, done))if (signal?.aborted) return null}@@ -140,7 +169,7 @@ export async function flipFile(const made = await runSliced($, flipSteps(fromBase64(base64), o.rows, o.isPage), o.signal)if (!made) return nullconst ran = await exec($, [o.perl, '-e', WRITE_PL, o.out], { stdin: toBase64(made.png) })- return ran.exitCode === 0 ? { width: made.width, height: made.height } : null+ return ran.exitCode === 0 ? { width: made.width, height: made.tall } : null} catch {return null}diff --git a/hooks/lib/pictures.tsx b/hooks/lib/pictures.tsxindex 890f3de..19b145d 100644--- a/hooks/lib/pictures.tsx+++ b/hooks/lib/pictures.tsx@@ -141,19 +141,21 @@ export async function sourceOf($: EngineInterface, ready: Env, p: Picture, rows?// Flip mode: a PNG stored for herdr in iTerm2 drawn `rows` rows tall// (flip.ts), in a key folder of the cache by the file, its stat stamp and// the rows; made once (one run per key), kept from an earlier load. Null-// when it fails.+// when it fails (not kept: a later draw tries again).const flipped = new Map<string, Promise<string | null>>()function flippedOf($: EngineInterface, ready: Env, p: Picture, rows: number): Promise<string | null> {const key = `${p.file}@${p.stamp}@${rows}`let got = flipped.get(key)if (!got) {const out = `${ready.cache}/${hashOf(['flip', p.file, p.stamp, rows])}/flip.png`- got = (async () => {+ got = (async (): Promise<string | null> => {const made = await stat($, out).catch(() => null)if (made?.kind === 'file' && made.size) return outconst ok = await flipFile($, { src: p.file, out, rows, isPage: false, perl: perlFor(ready.isMac) })- if (!ok) await logDebug($, ready.cache, `could not store ${p.file} for herdr in iTerm2`)- return ok ? out : null+ if (ok) return out+ if (flipped.get(key) === got) flipped.delete(key)+ await logDebug($, ready.cache, `could not store ${p.file} for herdr in iTerm2`)+ return null})()keep(flipped, key, got, PICTURES_KEPT)}diff --git a/hooks/lib/typst.ts b/hooks/lib/typst.tsindex bdf2657..6dcad84 100644--- a/hooks/lib/typst.ts+++ b/hooks/lib/typst.ts@@ -701,7 +701,7 @@ 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 as a file of its own (`f` before its-// name: a flip of a flipped page would undo it), with its new height.+// name: a flip of a flipped page would undo it), with the height its rows map to (flipSteps).async function flipPages($: EngineInterface, job: Job, pages: readonly Page[], signal?: AbortSignal): Promise<Page[] | 'failed' | 'cancelled'> {const flipped: Page[] = []for (const p of pages) {diff --git a/tests/flip-sh-test.sh b/tests/flip-sh-test.shnew file mode 100644index 0000000..2bb1200--- /dev/null+++ b/tests/flip-sh-test.sh@@ -0,0 +1,57 @@+#!/bin/sh+# Runs WRITE_PL (hooks/lib/flip.ts: a flipped PNG or a picture select frame+# written from base64) with the real perl: the file and its folder, the+# older frames deleted, a folder it can not write, and two writers of one+# file at the same time (each its own temporary file: both end 0, the file+# whole, no temporary file left).+# Usage: sh tests/flip-sh-test.sh (exit 0 when all pass)+set -u+here=$(cd "$(dirname "$0")/.." && pwd)+tmp=$(mktemp -d "${TMPDIR:-/tmp}/iui-flip-test.XXXXXX")+trap 'chmod -R u+rwx "$tmp" 2>/dev/null; rm -rf "$tmp"' EXIT+pass=0+fail=0+ok() { pass=$((pass + 1)); }+bad() { fail=$((fail + 1)); echo "FAIL: $*"; }++# The TypeScript string as text, its escapes undone.+pl=$(perl -ne 'if (/^export const WRITE_PL = \x27(.*)\x27$/) { (my $s = $1) =~ s/\\\\/\\/g; print $s }' "$here/hooks/lib/flip.ts")+[ -n "$pl" ] && ok || bad "WRITE_PL: not found in flip.ts"++# 1. The file in a new folder; the older frames named after it deleted.+printf 'hello' | base64 > "$tmp/small.b64"+mkdir -p "$tmp/old" && : > "$tmp/old/f-1.png" && : > "$tmp/old/f-2.png"+perl -e "$pl" "$tmp/new/f.png" "$tmp/old/f-1.png" "$tmp/old/f-2.png" < "$tmp/small.b64" && ok || bad "a write exited $?"+[ "$(cat "$tmp/new/f.png" 2>/dev/null)" = hello ] && ok || bad "the file: $(cat "$tmp/new/f.png" 2>&1)"+[ ! -e "$tmp/old/f-1.png" ] && [ ! -e "$tmp/old/f-2.png" ] && ok || bad "the older frames are still there"+[ -z "$(find "$tmp/new" -name '*.tmp')" ] && ok || bad "a temporary file is left: $(find "$tmp/new" -name '*.tmp')"++# 2. A folder it can not write: exit 1, nothing there.+mkdir "$tmp/ro" && chmod 500 "$tmp/ro"+if [ -w "$tmp/ro" ]; then+ echo "flip-sh-test: the read-only case skipped (the folder stays writable: root?)"+else+ perl -e "$pl" "$tmp/ro/f.png" < "$tmp/small.b64" 2> "$tmp/err" && bad "a write to a read-only folder exited 0" || ok+ [ -z "$(ls "$tmp/ro")" ] && ok || bad "the read-only folder has: $(ls "$tmp/ro")"+fi++# 3. Two writers of one file at once, five times: a large picture (12 MB),+# so the two overlap.+head -c 12000000 /dev/zero | tr '\0' 'x' | base64 > "$tmp/big.b64"+size=$(perl -MMIME::Base64 -e 'local $/; print length(decode_base64(<STDIN>))' < "$tmp/big.b64")+i=0+while [ "$i" -lt 5 ]; do+ i=$((i + 1))+ rm -rf "$tmp/k"+ perl -e "$pl" "$tmp/k/flip.png" < "$tmp/big.b64" 2> "$tmp/e1" & a=$!+ perl -e "$pl" "$tmp/k/flip.png" < "$tmp/big.b64" 2> "$tmp/e2" & b=$!+ wait "$a"; ea=$?+ wait "$b"; eb=$?+ [ "$ea" = 0 ] && [ "$eb" = 0 ] && ok || bad "run $i: two writers at once exited $ea and $eb: $(cat "$tmp/e1" "$tmp/e2")"+ got=$(wc -c < "$tmp/k/flip.png" 2>/dev/null | tr -d ' ')+ [ "$got" = "$size" ] && ok || bad "run $i: the file has ${got:-no} bytes, not $size"+ [ -z "$(find "$tmp/k" -name '*.tmp')" ] && ok || bad "run $i: a temporary file is left"+done++echo "flip-sh-test: $pass passed, $fail failed"+[ "$fail" = 0 ]diff --git a/tests/flip.test.ts b/tests/flip.test.tsindex 5555d64..b991f60 100644--- a/tests/flip.test.ts+++ b/tests/flip.test.ts@@ -1,10 +1,11 @@import { expect, test } from 'claude-code/testing'-import { encodeSteps, flipPng, flipRows, herdrBand, itermRowOf, padRows, reverseBands, WRITE_PL } from '../hooks/lib/flip'+import { encodeSteps, flipFile, flipPng, flipRows, herdrBand, itermRowOf, padRows, reverseBands, sliceClock, WRITE_PL } from '../hooks/lib/flip'import { frameOf, framesOf, framesSteps, rgbOf } from '../hooks/lib/frames'import { decodePng, fromBase64, toBase64, type Rgba } from '../hooks/lib/png'import { compile, forgetTouches } from '../hooks/lib/typst'import { DEFAULT_METRICS, type Env, type Job } from '../hooks/lib/state'+import { MAX_IMAGE_ROWS } from '../hooks/lib/settings'import { flipOf, herdrText, renderKey, type HerdrClient } from '../hooks/register'// A picture whose pixel at (x, y) tells its row: R = y, G = y >> 8, B = x, A = 255 - (y & 15).@@ -53,10 +54,12 @@ test('a picture stored with flipRows shows each row right in herdr in iTerm2, fo}})-test('padRows makes a picture rows × B tall (B = round(H / R)): rows cut, or the last one repeated', () => {- const tall = padRows(imageOf(2, 37), 4)+test('padRows makes a picture rows × B tall (B = round(H / R)): the last row repeated, or each row the band herdr shows for it', () => {+ const img = imageOf(2, 37)+ const tall = padRows(img, 4)expect(tall.height).toBe(36)- expect(rowOf(tall, 35)).toEqual(rowOf(imageOf(2, 37), 35))+ // Band k is source rows floor(k·37/4) on (0, 9, 18, 27): herdr's bands, the rows it drops spread, the bottom kept.+ for (let k = 0; k < 4; k++) for (let i = 0; i < 9; i++) expect(rowOf(tall, k * 9 + i)).toEqual(rowOf(img, herdrBand(k, 37, 4).y + i))const short = padRows(imageOf(2, 35), 4)expect(short.height).toBe(36)expect(rowOf(short, 35)).toEqual(rowOf(short, 34))@@ -66,6 +69,14 @@ test('padRows makes a picture rows × B tall (B = round(H / R)): rows cut, or thexpect(padRows(imageOf(2, 1), 3).height).toBe(3)})+test('padRows never cuts the bottom of a page taller than rows × B: each row shows what herdr shows on a right terminal', () => {+ for (const [height, rows] of [[258 * 10, MAX_IMAGE_ROWS], [34 * 12 + 1, 12], [41, 4], [37 * 3 + 1, 3]] as const) {+ const img = imageOf(3, height)+ const page = padRows(img, rows)+ for (let k = 0; k < rows; k++) expect(rightRows(page, k, rows)).toEqual(rightRows(img, k, rows))+ }+})+test('for a page rows × B tall, flipRows reverses the bands, and reverseBands is that move and its own inverse', () => {const img = imageOf(4, 5 * 7)const flipped = flipRows(img, 5)@@ -81,7 +92,8 @@ test('for a page rows × B tall, flipRows reverses the bands, and reverseBands itest('flipPng: a page is padded then flipped; a picture keeps its height; both decode to the moved rows', () => {const img = imageOf(5, 41)const page = flipPng(encode(img), 4, true)- expect([page.width, page.height]).toEqual([5, 40])+ // 40 rows stored; rows map to the 41 of the page (herdr's bands of it).+ expect([page.width, page.height, page.tall]).toEqual([5, 40, 41])expect([...decodePng(page.png).data]).toEqual([...flipRows(padRows(img, 4), 4).data])const picture = flipPng(encode(img), 6, false)expect(picture.height).toBe(41)@@ -183,7 +195,7 @@ test('a compile in flip mode stores each page rows × B tall with its bands reveconst 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/fa-1.png', 16, 40, 4, true])+ expect([page.file, page.width, page.height, page.rows, page.flip]).toEqual(['/c/kf/fa-1.png', 16, 41, 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.@@ -211,3 +223,50 @@ test('a compile in flip mode whose page can not be stored fails, so the text shoexpect(g.texts.has('/c/kb/pages.json')).toBe(false)forgetTouches()})++test('a page taller than MAX_IMAGE_ROWS in flip mode keeps its last rows: its last row shows what a right terminal shows', async () => {+ forgetTouches()+ // 258 rows of 10 px: one block that can not be cut, so it is drawn at 255 rows.+ const img = imageOf(16, 258 * 10)+ const h = host(encode(img))+ const result = await compile(h.$, jobOf('kt', true))+ if (typeof result !== 'object') throw new Error(`compile: ${result}`)+ const page = result.pages[0]!+ expect([page.rows, page.height]).toEqual([MAX_IMAGE_ROWS, 2580])+ const stored = decodePng(h.written.get(page.file)!)+ const source = (r: number[]) => r[0]! + 256 * r[1]!+ for (const k of [0, 127, MAX_IMAGE_ROWS - 1]) expect(shownRows(stored, k, MAX_IMAGE_ROWS).map(source)).toEqual(rightRows(img, k, MAX_IMAGE_ROWS).map(source))+ forgetTouches()+})++test('two writers of one file at once each write a temporary file of their own', () => {+ // One name for all ("$f.tmp"): the second one's rename finds no file, and its flip fails.+ expect(WRITE_PL).toContain('my $t = "$f.$$.tmp"')+ expect(WRITE_PL).not.toContain('"$f.tmp"')+})++test('a flip runs in slices with a yield between them, and stops when its signal does', async () => {+ const was = sliceClock.now+ let t = 0+ // Each step ends its slice.+ sliceClock.now = () => (t += 10)+ try {+ const img = imageOf(4, 40)+ const h = host(encode(img))+ let yields = 0+ h.$.clock.after = (ms: number, fn: () => void) => (yields++, void Promise.resolve().then(fn), { cancel() {} })+ const out = await flipFile(h.$, { src: '/c/a.png', out: '/c/fa.png', rows: 4, isPage: true, perl: '/usr/bin/perl' })+ expect(out).toEqual({ width: 4, height: 40 })+ expect([...decodePng(h.written.get('/c/fa.png')!).data]).toEqual([...flipRows(img, 4).data])+ expect(yields).toBeGreaterThan(3)+ const stop = new AbortController()+ h.$.clock.after = (ms: number, fn: () => void) => (stop.abort(), void Promise.resolve().then(fn), { cancel() {} })+ expect(await flipFile(h.$, { src: '/c/a.png', out: '/c/fb.png', rows: 4, isPage: true, perl: '/usr/bin/perl', signal: stop.signal })).toBe(null)+ expect(h.written.has('/c/fb.png')).toBe(false)+ // A clock that stands still (a test's) fires no clock.after(0): no yield then, and the flip ends.+ h.$.clock = { now: async () => 1000, after: () => ({ cancel() {} }) }+ expect(await flipFile(h.$, { src: '/c/a.png', out: '/c/fc.png', rows: 4, isPage: true, perl: '/usr/bin/perl' })).toEqual({ width: 4, height: 40 })+ } finally {+ sliceClock.now = was+ }+})diff --git a/tests/images.test.ts b/tests/images.test.tsindex de7d5e5..ff3623b 100644--- a/tests/images.test.ts+++ b/tests/images.test.ts@@ -377,6 +377,8 @@ function world(on: any, o: {failFill?: boolean// The root of a fill (FILL_SH) can not be made.failStage?: boolean+ // The first flip writes (WRITE_PL) that fail.+ failWrites?: number} = {}) {// Intelligent UI's store in memory (as mock.store), and its writes by key;// `redrawsAt`: by key, the `done` writes (redraws) made when its write ended.@@ -415,8 +417,9 @@ function world(on: any, o: {const fillRoots: string[][] = []const removed: string[] = []const moves: string[][] = []- // The PNGs perl writes (flip mode), by path.+ // The PNGs perl writes (flip mode), by path, and the writes asked.const pngs = new Map<string, Uint8Array>()+ let writes = 0const docs: any[] = []const logs: string[] = []const stats = new Map<string, number>()@@ -447,7 +450,7 @@ function world(on: any, o: {const cmd = argv.join(' ')if (argv[0] === 'kill') return ok('')if (o.herdr && argv[0] === 'sh' && argv[2] === HERDR_CLIENT_SH) return ok(`pid 77\natime 5\ntty /dev/ttys003\ncmd herdr\napp ${o.herdr}\nenv\nherdr TERM=xterm-256color\n`)- if (argv[0] === '/usr/bin/perl' && argv[2] === WRITE_PL) return (pngs.set(argv[3]!, fromBase64(e.init.stdin)), ok(''))+ if (argv[0] === '/usr/bin/perl' && argv[2] === WRITE_PL) return writes++ < (o.failWrites ?? 0) ? ok('', 1) : (pngs.set(argv[3]!, fromBase64(e.init.stdin)), ok(''))if (/cc-intelligent-ui-/.test(cmd)) return ok('/c\nDarwin\n')if (argv[0] === 'id' && argv[1] === '-u') return ok('501\n')if (cmd.includes('command -v typst')) return o.typst ? ok('/usr/bin/typst\n') : ok('', 1)@@ -524,7 +527,7 @@ function world(on: any, o: {})on('ui.toast', async () => ({ value: undefined }))on('ui.render', () => ENGINE as any)- return { clock, heads, reads, converts, moves, pngs, docs, logs, files, texts, mtimes, written, kept, redrawsAt, redraws: () => redraws, sets, stats, fills, fillRoots, removed }+ return { clock, heads, reads, converts, moves, pngs, writes: () => writes, docs, logs, files, texts, mtimes, written, kept, redrawsAt, redraws: () => redraws, sets, stats, fills, fillRoots, removed }}const sent = (output: unknown, isErrored = false, id = 'tu-1') =>@@ -586,6 +589,33 @@ test('in herdr in iTerm2, a PNG Claude sends is made by Typst at its box, then sexpect((await $.command.run({ command: 'iui', args: 'details' } as any)).text).toContain("pages and pictures stored upside down by rows, for iTerm2's placement bug")})+test('in herdr in iTerm2, a PNG whose flip takes longer than a slice of real time draws, with the clock not moved', async ($, on) => {+ // 1200 x 1200: its decode alone takes more than a slice (8 ms); the mocked clock fires no clock.after(0) in ui.drawn().+ const big: Rgba = { width: 1200, height: 1200, data: new Uint8Array(1200 * 1200 * 4).map((_, i) => (i * 7) & 255) }+ const steps = encodeSteps(big)+ let got = steps.next()+ while (!got.done) got = steps.next()+ const { pngs } = world(on, { typst: true, herdr: '/Applications/iTerm.app/Contents/MacOS/iTerm2', files: { '/t/icon.png': HEAD }, bytes: toBase64(got.value) })+ await ready($)+ const ui = await $.ui.mount(sent(attach('/t/icon.png')))+ const [image] = images(await ui.drawn())+ expect(image.props.source.file).toMatch(/^\/c\/[0-9a-f]{16}\/flip\.png$/)+ expect(pngs.has(image.props.source.file)).toBe(true)+})++test('in herdr in iTerm2, a picture whose flipped copy could not be stored is tried again at the next draw', async ($, on) => {+ // The first write fails (as when another session wrote the same file at that time).+ const { pngs, writes } = world(on, { typst: true, herdr: '/Applications/iTerm.app/Contents/MacOS/iTerm2', files: { '/t/icon.png': HEAD }, bytes: ROWS_PNG, failWrites: 1 })+ await ready($)+ const ui = await $.ui.mount(sent(attach('/t/icon.png')))+ expect(images(await ui.drawn())).toEqual([])+ await ui.redraw(sent(attach('/t/icon.png')).props)+ const [image] = images(await ui.drawn())+ expect(writes()).toBe(2)+ expect(image.props.source.file).toMatch(/^\/c\/[0-9a-f]{16}\/flip\.png$/)+ expect([...decodePng(pngs.get(image.props.source.file)!).data]).toEqual([...flipRows(ROWS_IMG, 10).data])+})+test('in herdr in Ghostty, a PNG Claude sends draws as it is', async ($, on) => {const { converts, pngs } = world(on, { typst: true, herdr: '/Applications/Ghostty.app/Contents/MacOS/ghostty', files: { '/t/icon.png': HEAD }, bytes: ROWS_PNG })await ready($)