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

Picture select: a newer copy cancels an older one that still reads its text

razorback162b81e8ea29d9
diff --git a/hooks/register.tsx b/hooks/register.tsx
index 5db9794..919f18a 100644
--- a/hooks/register.tsx
+++ b/hooks/register.tsx
@@ -367,10 +367,15 @@ 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 or an up): an older one that still
+// reads its text when a newer one starts copies nothing.
+let copies = 0
+
// /iui grab: the last mouse selection, read and copied. With no selection
// (the main screen), the picture cells on the clipboard: the command says
// that the copy is this session's.
async function grab($: EngineInterface): Promise<string> {
+ const mine = ++copies
const selected = await uiSelection($).catch(() => undefined)
let raw = selected?.text
if (raw === undefined) {
@@ -385,6 +390,7 @@ async function grab($: EngineInterface): Promise<string> {
clip.raw = raw
clip.rawRow = row
const read = await readCopy($, raw, row)
+ if (mine !== copies) 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.text
clip.seen = read.text
@@ -515,12 +521,14 @@ 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.
+// are not known; a newer copy while they are: the newer one copies.
async function clickAt($: EngineInterface, d: Drawn & { job?: Job }, post: SelectPost, surface: RenderSurface) {
+ const mine = ++copies
const links = () => clickLinks.get(d.id) ?? NO_LINKS
let targets = knownTargets(d, post.page, links())
if (!targets) {
await wordsOf($, d, post.page)
+ if (mine !== copies) return
targets = knownTargets(d, post.page, links())
}
const target = targets?.[targetAt(targets, post.anchor.x, post.anchor.y)]?.target
@@ -555,8 +563,7 @@ 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
-// its words are read: the newer one copies.
-let copies = 0
+// its words are read (or a newer click or grab): the newer one copies.
async function copySelection($: EngineInterface, d: Drawn & { job?: Job }, sel: Selection, surface: RenderSurface) {
const t = performance.now()
const mine = ++copies
diff --git a/tests/copy.test.ts b/tests/copy.test.ts
index 052fad3..7dae4ad 100644
--- a/tests/copy.test.ts
+++ b/tests/copy.test.ts
@@ -917,15 +917,16 @@ test('positions are kept in memory: grabs of the same page share one probe run,
expect(w.copies).toEqual([])
w.release()
await w.clock.settle()
- expect(await first).toBe('Intelligent UI: copied 35 chars (1 of 1 picture runs read, row ans-1).')
+ // The newer grab copies; the older one copies nothing.
+ expect(await first).toBe('Intelligent UI: a newer copy came first; this one copied nothing.')
expect(await second).toBe('Intelligent UI: copied 35 chars (1 of 1 picture runs read, row ans-1).')
- expect(w.copies).toEqual([COPIED, COPIED])
+ expect(w.copies).toEqual([COPIED])
// Known now: no probe for the next grab.
w.gate = null
w.selection = { text: [cells(1, 0, 97), cells(2, 0, 97), cells(3, 0, 21)].join('\n') }
expect(await grab($)).toBe(GRABBED)
expect(w.queries.length).toBe(1)
- expect(w.copies).toEqual([COPIED, COPIED, COPIED])
+ expect(w.copies).toEqual([COPIED, COPIED])
})
test('the watcher: its first count is only noted, each change is read once, the clipboard is never written; off stops its clipboard part', async ($, on) => {
@@ -1878,6 +1879,43 @@ test('picture select: a click copies a link, a file\'s path or a command; a clic
expect(w.copies.length).toBe(4)
})
+test('picture select: a click whose page still reads its words copies nothing after a newer click on a ready page', async ($, on) => {
+ const { w, ui } = await drawTargets($, on)
+ await hover(ui, 52, 4)
+ await settle(w)
+ // A second drawing (one more line: its own folder), its words held.
+ w.hold()
+ const slow = await drawAnswer($, w, 'ans-2', `${TARGETS_MD}\n\nOne more line.`)
+ await click(slow, 6, 3)
+ await settle(w)
+ await click(ui, 52, 4)
+ await settle(w)
+ expect(w.copies).toEqual(['ls -la'])
+ w.release()
+ await settle(w)
+ expect(w.copies).toEqual(['ls -la'])
+ expect(w.clipboard).toBe('ls -la')
+ expect(w.toasts.filter(t => t.startsWith('Intelligent UI: copied the '))).toEqual(['Intelligent UI: copied the command'])
+})
+
+test('picture select: an up whose page still reads its words copies nothing after a newer click', 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, 52, 4)
+ await settle(w)
+ w.release()
+ await settle(w)
+ expect(w.copies).toEqual(['ls -la'])
+ expect(w.clipboard).toBe('ls -la')
+})
+
test("picture select: a click on a code block's copy button copies its whole code; a hover tints the button", async ($, on) => {
const { w, ui } = await drawTargets($, on, { env: { HOME: '/h', CLAUDE_CODE_FORCE_TERMINAL_IMAGES: '1', IUI_DEBUG: '1' } })
// The button: row 8, columns 8 to 15.