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
Copy: a click or an up with nothing to copy cancels no other copy
razorback168d78b718f844
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex dc98894..9a43e1b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -6,7 +6,7 @@ All releases of Intelligent UI for Claude Code (`cc-intelligent-ui`). Claude Cod### Fixed-- **The newest copy wins.** A click, a picture selection's up, `/iui grab` and the `ui.copy` change of picture cells each read their text first. An older one that was still reading wrote the clipboard over a newer copy. Now it writes nothing when a newer copy came: a newer click or up, a `$.ui.copy` (another plugin's, or the command button of a link row), Claude Code's own copy of a selection, or a copy in another app. A `ui.copy` change that lost its turn is refused. `/iui grab` that copies nothing does not cancel another copy.+- **The newest copy wins.** A click, a picture selection's up, `/iui grab` and the `ui.copy` change of picture cells each read their text first. An older one that was still reading wrote the clipboard over a newer copy. Now it writes nothing when a newer copy came: a newer click or up, a `$.ui.copy` (another plugin's, or the command button of a link row), Claude Code's own copy of a selection, or a copy in another app. A `ui.copy` change that lost its turn is refused. `/iui grab`, a click on no target and an up with no text copy nothing and cancel no other copy. An older copy waits for a newer click or up that still reads its text, so it never writes first and is then overwritten.- **A password manager's copy is never read.** On macOS the mod reads the clipboard with a short `perl` script (PerlObjCBridge) in place of `pbpaste`, and stops at a copy with a concealed or transient type. On Linux, `xclip`, `wl-paste` and GTK look for the `x-kde-passwordManagerHint` target before and after the read. On Wayland, a copy marked sensitive was skipped already.- **The code in an answer can read no other file.** Each compile of an answer or a plan had `/` as its Typst root, so a Typst block could read any file (`read`, `include`, `image`, `json`, `csv`), and `read("/dev/zero")` used all the memory. Now each compile runs in a root of its own, with only the template, the theme and the answer's images. The skill's `check.sh` does the same, and it stops with an error when its cache folder is a link or not yours. Pictures are the same as before.- `/iui setup`, `/iui setup remove` and the setup question never write a shell start file that they cannot read (no permission, or too large). Before, such a file was replaced with only the Intelligent UI block. Now the mod changes no file and shows the line to add.diff --git a/hooks/register.tsx b/hooks/register.tsxindex b33dfbe..becb17d 100644--- a/hooks/register.tsx+++ b/hooks/register.tsx@@ -18,7 +18,7 @@// page a copy reads) and updates (the daily check for a new version). This// file joins them to the hooks, and gives the tests the names they import.import { atom, read, update } from 'claude-code'-import type { EngineInterface, Register, RenderElement, RenderSurface, RenderViewport } from 'claude-code'+import type { EngineInterface, Register, RenderElement, RenderSurface, RenderViewport, UiCopyArgs } from 'claude-code'import {DEFAULT_THEME, gridText, heightText, IMAGE_MAX_COLUMNS, MARGIN_TOP, MAX_HEIGHT, MAX_WIDTH, MIN_HEIGHT, MIN_WIDTH, RESULTS_KEPT, TYPST_PROMPT,@@ -330,7 +330,7 @@ async function readClipboard($: EngineInterface) {const text = await clipboardRead($)if (text === undefined || text === clip.seen) continueclip.seen = text- if (text === null || !copiedTexts.includes(text)) copies+++ if (text === null || !copiedTexts.includes(text)) copies = ++askedif (text === null || text === clip.wrote || !hasPlaceholders(text)) continuehintGrab($, text)} while (clip.again)@@ -374,19 +374,37 @@ async function updateState($: EngineInterface): Promise<UpdateState> {return { on: S.updates, off: S.updatesOff, own: S.ownVersion, checked: S.updateChecked, now }}-// The newest copy (a grab, a click, an up, any $.ui.copy, or a copy from-// outside that the watcher sees): an older one that still reads its text-// when a newer one starts copies nothing. `copiedTexts`: the texts of the+// Each copy (a grab, a click, an up, any $.ui.copy, or a copy from outside+// that the watcher sees) gets a number when it begins (`asked`), and claims+// the clipboard once it has a text (`copies`: the newest claim). An older+// one that still reads its text when a newer one claimed copies nothing; a+// click or an up with nothing to copy claims nothing. `copiedTexts`: the texts of the// last 4 $.ui.copy calls (their changes, even read late, are no copy from outside).+let asked = 0let copies = 0const copiedTexts: string[] = []function noteCopied(text: string) {+ if (copiedTexts.at(-1) === text) returncopiedTexts.push(text)if (copiedTexts.length > 4) copiedTexts.shift()}-// A copy's turn: its number and the native selection when it began.-type Turn = { n: number; native: Promise<string> }+// The mod's own write, noted here: a $.ui.copy made after an await does not+// pass the mod's own ui.copy hook. While it runs (`writing`), the hook passes it with no new claim.+const writing: string[] = []+async function copyOwn($: EngineInterface, args: UiCopyArgs) {+ noteCopied(args.text)+ writing.push(args.text)+ try {+ return await copy($, args)+ } finally {+ writing.splice(writing.indexOf(args.text), 1)+ }+}++// A copy's turn: its number, the native selection when it began, and its+// end of not knowing yet whether it copies anything (`done`).+type Turn = { n: number; native: Promise<string>; done: () => void }const nativeKey = (s: { text: string; requestId?: string } | undefined) => (s ? `${s.requestId ?? ''}|${s.text}` : '')function nativeNow($: EngineInterface): Promise<string> {try {@@ -396,7 +414,30 @@ function nativeNow($: EngineInterface): Promise<string> {return Promise.resolve(nativeKey(undefined))}}-const takeTurn = ($: EngineInterface, native?: Promise<string>): Turn => ({ n: ++copies, native: native ?? nativeNow($) })+// The turns that do not know yet whether they copy anything.+const undecided = new Map<number, Promise<void>>()+function takeTurn($: EngineInterface, native?: Promise<string>): Turn {+ const n = ++asked+ let end = () => {}+ undecided.set(n, new Promise<void>(r => (end = r)))+ return { n, native: native ?? nativeNow($), done: () => (undecided.delete(n), end()) }+}++// A copy that has its text claims the clipboard: false when a newer one+// claimed. A newer one that does not know yet whether it copies is waited for.+async function claim(turn: Turn): Promise<boolean> {+ for (const [n, decided] of undecided) if (n > turn.n) await decided+ if (copies > turn.n) return false+ copies = turn.n+ return true+}++// A copy that has its text when it begins (a grab, a $.ui.copy): it claims at once.+function claimNow($: EngineInterface, native: Promise<string>): Turn {+ const n = ++asked+ copies = n+ return { n, native, done: () => {} }+}// Whether a copy is still the newest when its text is read: no newer copy// came, and no new native selection (Claude Code copies that itself, past@@ -425,13 +466,13 @@ async function grab($: EngineInterface): Promise<string> {clip.raw = rawclip.rawRow = row// Only a grab that copies takes a turn.- const turn = takeTurn($, Promise.resolve(nativeKey(selected)))+ const turn = claimNow($, Promise.resolve(nativeKey(selected)))const read = await readCopy($, raw, row)if (!(await isNewest($, turn))) return 'Intelligent UI: a newer copy came first; this one copied nothing.'if (!read.resolved) return `Intelligent UI: found ${read.runs} picture run(s) but could not read them (see /iui dump).`clip.wrote = read.textclip.seen = read.text- const copied = await copy($, { text: read.text })+ const copied = await copyOwn($, { text: read.text })return copied.isCopied? `Intelligent UI: copied ${read.text.length} chars (${read.resolved} of ${read.runs} picture runs read${row ? `, row ${row}` : ''}).`: `Intelligent UI: the copy failed: ${copied.reason}.`@@ -559,18 +600,26 @@ function lightHover($: EngineInterface) {// A click (a clear) on a target of the page copies its text (a link's URL,// a file's path, a command, a code block's code). The page's words are probed first when they// are not known; a newer copy while they are (or a new native selection): the newer one copies.+// A click on no target copies nothing and cancels no other copy.async function clickAt($: EngineInterface, d: Drawn & { job?: Job }, post: SelectPost, surface: RenderSurface) {const turn = takeTurn($)- const links = () => clickLinks.get(d.id) ?? NO_LINKS- let targets = knownTargets(d, post.page, links())- if (!targets) {- await wordsOf($, d, post.page)- if (!(await isNewest($, turn))) return- targets = knownTargets(d, post.page, links())+ try {+ const links = () => clickLinks.get(d.id) ?? NO_LINKS+ let targets = knownTargets(d, post.page, links())+ if (!targets) {+ await wordsOf($, d, post.page)+ targets = knownTargets(d, post.page, links())+ }+ const target = targets?.[targetAt(targets, post.anchor.x, post.anchor.y)]?.target+ if (S.debug) traceDebug($, `select: click ${d.id} page ${post.page} at ${post.anchor.x},${post.anchor.y} target ${target ? target.kind : 'none'}`)+ if (!target) return+ const mine = await claim(turn)+ turn.done()+ if (!mine || !(await isNewest($, turn))) return+ await copyTarget($, target, surface)+ } finally {+ turn.done()}- const target = targets?.[targetAt(targets, post.anchor.x, post.anchor.y)]?.target- if (S.debug) traceDebug($, `select: click ${d.id} page ${post.page} at ${post.anchor.x},${post.anchor.y} target ${target ? target.kind : 'none'}`)- if (target) await copyTarget($, target, surface)}// A target's text on the clipboard (clickAt), with its toast.@@ -580,7 +629,7 @@ async function copyTarget($: EngineInterface, target: Target, surface: RenderSur// The clipboard change of this text is not read again.clip.wrote = textclip.seen = text- const copied = await copy($, { text, surface })+ const copied = await copyOwn($, { text, surface })toast($, copied.isCopied ? `Intelligent UI: copied the ${name}` : `Intelligent UI: the copy failed: ${copied.reason}`)}@@ -599,24 +648,34 @@ async function selectionText($: EngineInterface, d: Drawn & { job?: Job }, sel:}// The button came up: the selection's text on the clipboard, with the-// toast of a copy. No text under it: nothing is copied; a newer up while+// toast of a copy. No text under it: nothing is copied (and no other copy+// is cancelled); a newer up while// its words are read (or any newer copy, or a new native selection): the newer one copies.async function copySelection($: EngineInterface, d: Drawn & { job?: Job }, sel: Selection, surface: RenderSurface) {const t = performance.now()const turn = takeTurn($)const probed: number[] = []- const text = await selectionText($, d, sel, probed)- if (!(await isNewest($, turn))) return- const read = performance.now() - t- if (text === null) {- toast($, 'Intelligent UI: could not read the selected picture')- return+ let text: string | null+ try {+ text = await selectionText($, d, sel, probed)+ // A newer copy claimed first: no toast either.+ if (copies > turn.n) return+ if (text === null) {+ toast($, 'Intelligent UI: could not read the selected picture')+ return+ }+ if (!text) return+ const mine = await claim(turn)+ turn.done()+ if (!mine || !(await isNewest($, turn))) return+ } finally {+ turn.done()}- if (!text) return+ const read = performance.now() - t// The clipboard change of this text is not read again.clip.wrote = textclip.seen = text- const copied = await copy($, { text, surface })+ const copied = await copyOwn($, { text, surface })toast($, copied.isCopied ? `Intelligent UI: copied ${text.length} chars of text` : `Intelligent UI: the copy failed: ${copied.reason}`)if (S.debug) void traceCopy($, text, performance.now() - t, read, probed)}@@ -712,14 +771,15 @@ export const register: Register = on => {// hold no placeholders and pass on as they are. Any copy is the newest:// one with placeholders whose words are read when a newer one came is refused.on('ui.copy', async ($, e, next) => {+ if (writing.includes(e.text)) return next(e)noteCopied(e.text)if (!hasPlaceholders(e.text) || !drawnAnswers.size) {- copies+++ copies = ++askedreturn next(e)}if (S.debug) traceDebug($, 'select: native copy seen: a $.ui.copy with picture cells (read as their text)')const selection = $.ui.selection().catch(() => undefined)- const turn = takeTurn($, selection.then(nativeKey))+ const turn = claimNow($, selection.then(nativeKey))const selected = await selectionclip.raw = e.textclip.rawRow = selected?.text === e.text ? selected.requestId : undefineddiff --git a/tests/copy.test.ts b/tests/copy.test.tsindex 03badf2..952e5fe 100644--- a/tests/copy.test.ts+++ b/tests/copy.test.ts@@ -2172,6 +2172,69 @@ test('/iui grab that copies nothing (no selection, no picture cells) does not caexpect(w.clipboard).toBe('https://example.com')})+test('a click on no target copies nothing and cancels no click whose words are still read', async ($, on) => {+ const { w, ui } = await drawTargets($, on)+ await hover(ui, 52, 4)+ await settle(w)+ w.hold()+ const slow = await drawAnswer($, w, 'ans-2', `${TARGETS_MD}\n\nOne more line.`)+ await click(slow, 6, 3)+ await settle(w)+ // Plain text of the ready page: no target.+ await click(ui, 1, 3)+ await settle(w)+ w.release()+ await settle(w)+ expect(w.copies).toEqual(['https://example.com'])+ expect(w.toasts.filter(t => t.startsWith('Intelligent UI: copied the '))).toEqual(['Intelligent UI: copied the link'])+})++test('a click on no target cancels no up whose words are still read', async ($, on) => {+ const { w, ui } = await drawTargets($, on)+ await hover(ui, 52, 4)+ await settle(w)+ w.hold()+ const slow = await drawAnswer($, w, 'ans-2', `${TARGETS_MD}\n\nOne more line.`)+ await slow.pointer({ type: 'down', x: 0, y: 1, button: 'left', in: 'sel0' })+ await slow.pointer({ type: 'move', x: 20, y: 3, button: 'left', in: 'sel0' })+ await slow.pointer({ type: 'up', x: 30, y: 3, button: 'left', in: 'sel0' })+ await settle(w)+ await click(ui, 1, 3)+ await settle(w)+ w.release()+ await settle(w)+ expect(w.copies.length).toBe(1)+ expect(w.copies[0]).toContain('Targets')+})++test('two clicks whose words are still read: only the newer one copies, also when the older one has its words first', async ($, on) => {+ const { w } = await drawTargets($, on)+ w.hold()+ const older = await drawAnswer($, w, 'ans-2', `${TARGETS_MD}\n\nOne more line.`)+ const newer = await drawAnswer($, w, 'ans-3', `${TARGETS_MD}\n\nAnother line.`)+ await click(older, 6, 3)+ await click(newer, 52, 4)+ await settle(w)+ w.release()+ await settle(w)+ // The older one waits for the newer one to know its target: no write of the link at any time.+ expect(w.copies).toEqual(['ls -la'])+ expect(w.clipboard).toBe('ls -la')+})++test('an older click whose words are still read copies when the newer click finds no target', async ($, on) => {+ const { w } = await drawTargets($, on)+ w.hold()+ const older = await drawAnswer($, w, 'ans-2', `${TARGETS_MD}\n\nOne more line.`)+ const newer = await drawAnswer($, w, 'ans-3', `${TARGETS_MD}\n\nAnother line.`)+ await click(older, 6, 3)+ await click(newer, 1, 3)+ await settle(w)+ w.release()+ await settle(w)+ expect(w.copies).toEqual(['https://example.com'])+})+test('a clipboard read that fails (an error, the 2 s timeout) is no copy from outside: a click whose words are still read still copies', async ($, on) => {const { w } = await drawTargets($, on)w.hold()