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

Copy: a fullscreen session no longer writes over another session's copy

razorback162a62f16c6103
diff --git a/docs/how-it-works.md b/docs/how-it-works.md
index 1b688ff..3f48d3e 100644
--- a/docs/how-it-works.md
+++ b/docs/how-it-works.md
@@ -12,7 +12,7 @@
6. Pictures are kept in `${TMPDIR}/cc-intelligent-ui-<uid>/` (`/tmp` when `TMPDIR` is not set). Folders older than 2 days are deleted at start. The drawing space of each answer at its first compile is kept in `~/.cache/cc-intelligent-ui/designs`, so that its Typst drawings keep the same share of the screen in later sessions.
7. Links: the mod finds the web links, file paths and commands in the Markdown (not in code blocks or math). It draws one dim row for each under the pages, with a `Link` (an OSC 8 link) for a web link or a file, and a `Button` that copies a command (`$.ui.copy`). It keeps a file row only when the file exists (`$.fs.exists`, kept in memory for 512 paths). Each row is one terminal row, cut at its end. The copy (step 9) counts these rows under the pages, so it still finds the page that you select.
8. The plan: a `ui.render` hook on the ExitPlanMode `ToolUse` row draws the plan through the same steps. Claude Code shows that row above the approval question. The approved result row then shows only its header, so the plan does not show two times. A `tool.call` hook on ExitPlanMode asks the mod's question (`$.ui.ask`) before Claude Code asks for permission. Then a `classic.PermissionRequest` hook gives the answer to Claude Code at once, so the Claude Code dialog does not show. For an approval, the hook sends the plan (`updatedInput`) and the new permission mode (`setMode`). For the person's text, the `tool.call` hook refuses the call with that text.
-9. Copy: one long-lived `perl` process watches the terminal size (at each SIGWINCH) and the clipboard. On macOS, it uses `/usr/bin/perl` with Apple's PerlObjCBridge and reads the change counter of the clipboard (`NSPasteboard changeCount`) every 0.3 s. This read is in the process and starts no other process (approximately 0.05% of one CPU core). On Linux, the process starts a child in a process group of its own. The child runs a tool that prints a line at each clipboard change (`wl-paste --watch`, `clipnotify` or GTK 3 from Python, refer to [Linux](install.md#linux)) and gives the mod a counter for each line. Nothing runs between two changes. When the `perl` process stops, it stops that process group. The clipboard part starts only while the mod is on and after the mod drew its first picture in this session; then the mod starts the process again with the clipboard part (and again without it after `/iui off`). When the counter changes, the mod reads the clipboard one time (`pbpaste` on macOS; `wl-paste`, `xclip`, `xsel` or GTK on Linux). The first counter after a start is not a change, so an old clipboard is not copied. A picture cell holds its row and its column (kitty placeholder U+10EEEE plus two combining marks). The mod finds the drawn page that has these rows, at the size that it is drawn. Then it finds the position of each word on that page: it runs the probe (`typst query` on the same template, with the same inputs as the PNG compile, plus `probe=1` and `page=<n>`) for the selected pages only. The mod keeps the positions of the last 64 pages in memory. It writes nothing to disk for this, and the PNGs do not change. The mod takes the words in the selected cells. Immediately before it writes the text with `$.ui.copy`, it reads the clipboard again. If the clipboard has a newer copy, the mod does not write over it, and it reads that copy next. If the process stops, the mod starts it again after 1 s (3 times at most). At `/clear` or a resume, the process continues without the clipboard part. At other session ends (for example `/exit`), the mod stops the process and does not start a new one. A `ui.copy` hook also changes a copy with picture cells to the text.
+9. Copy: one long-lived `perl` process watches the terminal size (at each SIGWINCH) and the clipboard. On macOS, it uses `/usr/bin/perl` with Apple's PerlObjCBridge and reads the change counter of the clipboard (`NSPasteboard changeCount`) every 0.3 s. This read is in the process and starts no other process (approximately 0.05% of one CPU core). On Linux, the process starts a child in a process group of its own. The child runs a tool that prints a line at each clipboard change (`wl-paste --watch`, `clipnotify` or GTK 3 from Python, refer to [Linux](install.md#linux)) and gives the mod a counter for each line. Nothing runs between two changes. When the `perl` process stops, it stops that process group. The clipboard part starts only while the mod is on and after the mod drew its first picture in this session; then the mod starts the process again with the clipboard part (and again without it after `/iui off`). When the counter changes, the mod reads the clipboard one time (`pbpaste` on macOS; `wl-paste`, `xclip`, `xsel` or GTK on Linux). In fullscreen mode, it reads the clipboard only when the selection of its session has picture cells, because a copy from this session is always a copy of that selection. The first counter after a start is not a change, so an old clipboard is not copied. A picture cell holds its row and its column (kitty placeholder U+10EEEE plus two combining marks). The mod finds the drawn page that has these rows, at the size that it is drawn. Then it finds the position of each word on that page: it runs the probe (`typst query` on the same template, with the same inputs as the PNG compile, plus `probe=1` and `page=<n>`) for the selected pages only. The mod keeps the positions of the last 64 pages in memory. It writes nothing to disk for this, and the PNGs do not change. The mod takes the words in the selected cells. Immediately before it writes the text with `$.ui.copy`, it reads the clipboard again. If the clipboard has a newer copy, the mod does not write over it, and it reads that copy next. If the process stops, the mod starts it again after 1 s (3 times at most). At `/clear` or a resume, the process continues without the clipboard part. At other session ends (for example `/exit`), the mod stops the process and does not start a new one. A `ui.copy` hook also changes a copy with picture cells to the text.
## The lean font set
diff --git a/docs/limitations.md b/docs/limitations.md
index 1beffa4..1e8653b 100644
--- a/docs/limitations.md
+++ b/docs/limitations.md
@@ -11,7 +11,8 @@
- The mod does not copy math, the text in an image, or the text in a Typst drawing.
- The cells do not identify the answer. The mod uses the answer of the selected row if it knows it, else the newest answer whose pages fit the selection. A selection in one page of a long answer fits each page that is tall enough, and the mod uses the first one. Thus the text can come from a different page or an older answer of the same width. `/iui dump` shows the page that the mod used.
- The mod knows only the last 64 answers, plans or files that it drew in this session.
- - With two Claude Code sessions on one computer, a session that has no selection can change a copy from the other session, with the text of its own picture.
+ - With two Claude Code sessions on one computer, a session on the main screen (not fullscreen mode) can change a copy from the other session, with the text of its own picture. A session in fullscreen mode changes only a copy of its own selection.
+ - In fullscreen mode, the mod does not change a copy of a selection that the terminal makes itself (for example, a drag with Option in iTerm2), because Claude Code does not see that selection. Select without the modifier key.
- To stop the clipboard write on select, set `copyOnSelect` to `false` in `/config`. Then the mod does not copy a selection either, because it acts only when the clipboard changes. It changes only a clipboard that has picture cells (for example, after a copy with a key).
- **Links in a picture:** a click on the picture does not open a link, because the mod cannot put a link on a picture. Use the link rows under the picture (refer to [Links, file paths and commands](usage.md#links-file-paths-and-commands)). A click on a command row copies the command only where Claude Code gets mouse clicks (fullscreen mode). A `file://` link opens in the app that the terminal chooses. A file row shows only when the file exists when the mod draws the answer.
- **PDF files** (refer to [What it draws](usage.md#what-it-draws)): a click on `‹`, `›` or the page number works only where Claude Code gets mouse clicks (fullscreen mode). The page field gets the keyboard only when the prompt is empty. If the prompt has text, the field opens without the keyboard: click the field to type. Then Esc goes to the prompt, not to the field. To close the field, click it and push Esc, or click its close mark. The mod draws one page at a time, and it does not show the number of pages. You cannot copy text from a PDF page.
diff --git a/docs/usage.md b/docs/usage.md
index 627dac1..af7be1e 100644
--- a/docs/usage.md
+++ b/docs/usage.md
@@ -81,7 +81,7 @@ The mod does not take web links from code blocks, inline code or math. It takes
## Copy text from a picture
-Select the text in a picture (an answer, a plan or a Markdown file that Claude sent) with the mouse, as you do with normal text. The selection puts picture cells on the clipboard. While the mod is on and has drawn a picture in this session, the mod sees each change of the clipboard (on macOS in 0.3 s or less; on Linux at once, with the tools in [Linux](install.md#linux)). When the clipboard has picture cells, the mod puts the text under your selection on the clipboard, and a toast shows the number of characters. Wait for this toast, then paste. In fullscreen mode, if this session has a selection, the mod changes the clipboard only when the clipboard holds that selection. Thus a copy from a different Claude Code session usually stays as it is. The mod does not change a clipboard that has no picture cells.
+Select the text in a picture (an answer, a plan or a Markdown file that Claude sent) with the mouse, as you do with normal text. The selection puts picture cells on the clipboard. While the mod is on and has drawn a picture in this session, the mod sees each change of the clipboard (on macOS in 0.3 s or less; on Linux at once, with the tools in [Linux](install.md#linux)). When the clipboard has picture cells, the mod puts the text under your selection on the clipboard, and a toast shows the number of characters. Wait for this toast, then paste. In fullscreen mode, the mod changes the clipboard only when the clipboard holds the selection of its own session. Thus a copy from a different Claude Code session stays as it is. The mod does not change a clipboard that has no picture cells.
The mod finds the position of each word on a page when you copy from that page for the first time. This takes approximately 0.05 s for plain text (only ASCII characters, no math, no Typst block and no image), 0.15–0.5 s for other text, and more (2–3 s) for a page with a large Typst drawing. No toast shows while it does this. Only the toast of the copy shows. The mod keeps the positions in memory, so the next copy from the same page is fast.
diff --git a/hooks/lib/state.ts b/hooks/lib/state.ts
index 864bbbf..781abe3 100644
--- a/hooks/lib/state.ts
+++ b/hooks/lib/state.ts
@@ -127,6 +127,9 @@ export const S = {
tty: null as Frame | null,
viewColumns: null as number | null,
viewRows: null as number | null,
+ // The terminal's layout (a viewport's isFullscreen; fixed per session),
+ // null before a render hook says.
+ isFullscreen: null as boolean | null,
// The key folders in the cache at setUp: a key found there is read back
// at once (a reopened session), with no queue and no blank.
onDisk: new Set<string>(),
diff --git a/hooks/register.tsx b/hooks/register.tsx
index 853e3ae..d2791dd 100644
--- a/hooks/register.tsx
+++ b/hooks/register.tsx
@@ -110,6 +110,7 @@ const done = atom({ plugin: 'cc-intelligent-ui', key: 'done' } as const, 0)
async function pictureEnv($: EngineInterface, e: { viewport?: RenderViewport }): Promise<{ env: Env; done: number } | null> {
if (!(await read($, isOn))) return null
if (e.viewport) noteView($, e.viewport)
+ if (typeof e.viewport?.isFullscreen === 'boolean') S.isFullscreen = e.viewport.isFullscreen
// Read so the end of setUp, a compile and a commit draw this row again.
const now = await read($, done)
if (!S.env) {
@@ -296,10 +297,12 @@ function clipChanged($: EngineInterface, count: number | null, first: boolean) {
// The clipboard changed: one read (clipboardNow). Placeholders that are not our
// own write are read and written back as text. A clipboard with no
-// placeholders is never touched. When this session has a selection
-// (fullscreen), the clipboard is rewritten only when it is that selection:
-// another session's copy is that session's to read. A change while a read
-// runs (a probe can take a second) reads once more after it.
+// placeholders is never touched. When this session has a selection, the
+// clipboard is rewritten only when it is that selection: another session's
+// copy is that session's to read. In fullscreen a copy of this session's
+// pictures is always of its selection, so with no selection that has
+// placeholders the clipboard is not even read. A change while a read runs
+// (a probe can take a second) reads once more after it.
async function readClipboard($: EngineInterface) {
if (clip.busy) {
clip.again = true
@@ -309,11 +312,12 @@ async function readClipboard($: EngineInterface) {
try {
do {
clip.again = false
+ const selected = await uiSelection($).catch(() => undefined)
+ if (S.isFullscreen && !(selected && hasPlaceholders(selected.text))) continue
const text = await clipboardNow($)
if (text === null || text === clip.seen) continue
clip.seen = text
if (text === clip.wrote || !hasPlaceholders(text)) continue
- const selected = await uiSelection($).catch(() => undefined)
if (selected && !sameCopy(selected.text, text)) continue
await copyRead($, text, selected?.requestId)
} while (clip.again)
diff --git a/tests/copy.test.ts b/tests/copy.test.ts
index 1bc7a44..8abb65a 100644
--- a/tests/copy.test.ts
+++ b/tests/copy.test.ts
@@ -770,6 +770,34 @@ test('a clipboard with picture cells that is not this session\'s selection is no
expect(w.copies).toEqual([COPIED])
})
+test('in fullscreen the clipboard is read only with a selection of picture cells; with none it is another session\'s copy', async ($, on) => {
+ const w = world(on)
+ await $.command.run({ command: 'iui', args: '' } as any)
+ const ui = await $.ui.mount({ ...message('ans-1', SMALL_MD), viewport: { columns: 100, rows: 40, isFullscreen: true } })
+ await w.clock.settle()
+ await ui.redraw(props(SMALL_MD))
+ await w.clock.settle()
+ await w.clock.advance(300)
+ await w.clock.settle()
+ // No selection here: another fullscreen session's copy, not even read.
+ w.selection = undefined
+ w.copy(COPY)
+ await w.clock.settle()
+ expect(w.pastes).toBe(0)
+ expect(w.copies).toEqual([])
+ // A selection of text only: a copy of it has no picture cells.
+ w.selection = { text: 'my own text', requestId: 'u1' }
+ w.copy('my own text')
+ await w.clock.settle()
+ expect(w.pastes).toBe(0)
+ expect(w.copies).toEqual([])
+ // This session's own selection: rewritten.
+ w.selection = { text: COPY, requestId: 'ans-1' }
+ w.copy(`${COPY}\n`)
+ await w.clock.settle()
+ expect(w.copies).toEqual([COPIED])
+})
+
test('a watcher that starts again does not copy the clipboard that was there before (its first count is only noted)', async ($, on) => {
const w = world(on)
// Picture cells on the clipboard before this load (a hot reload).