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 with row strips; copy shim asks the mod (option B)
razorback164fe0a06e4cd7
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d7ae786..9e11714 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -2,6 +2,18 @@All releases of Intelligent UI for Claude Code (`cc-intelligent-ui`). Claude Code finds a new version by the `version` in `.claude-plugin/plugin.json`. To get it, refer to [Update](docs/install.md#update).+## Unreleased++### New++- **Picture select.** Since Claude Code 2.1.305, the mouse selection of Claude Code skips pictures: no highlight and no text. In fullscreen mode, the mod now selects the text of a picture itself. Drag over the picture: the selected rows show on a blue background, and when you release the button, the mod copies their text and shows the toast "copied N chars of text". A click with no drag removes the selection. This needs Claude Code 2.1.305 or newer. `/iui select off` turns it off. Refer to [Copy text from a picture](docs/usage.md#copy-text-from-a-picture).++### Changed++- **The automatic copy asks its own session.** The mod puts small shims of Claude Code's clipboard tools (`pbcopy`; `wl-copy`, `xclip`, `xsel`) first on the `PATH` of the Claude Code process, in a folder of that process only. When Claude Code copies picture cells, the shim asks the mod of its session for their text through a private FIFO, and the real tool gets that text. The shim waits 1.5 s at most; then it uses the cells, and a toast names `/iui grab`. The mod never writes the clipboard for a copy that it did not answer. Thus a copy of a different session is never changed, also when it has the same cells. Before, the mod read the clipboard after the copy and used the mouse selection: a session with an old selection of the same cells changed the copy of a different session. Refer to [Clipboard shims](README.md#clipboard-shims).+- A copy of picture cells that is not from this session gets the `/iui grab` toast, also in fullscreen mode.+- `/iui details` says why the automatic copy is off when the shims cannot work (ssh, a background session, a terminal with no pictures, no `perl`, or Linux with no `wl-copy`, `xclip` or `xsel`).+## 0.3.0 (2026-10-20)### Newdiff --git a/README.md b/README.mdindex a9c5407..a3443ed 100644--- a/README.md+++ b/README.md@@ -38,7 +38,8 @@ You need Claude Code 2.1.298 or later, [Typst](https://typst.app) 0.15, and macO| ssh | Yes, from kitty or Ghostty | Refer to [Install](docs/install.md#terminals) || tmux, screen | No | — || Warp | Not yet | Support comes when [warpdotdev/warp#16312](https://github.com/warpdotdev/warp/pull/16312) is merged |-| WezTerm, Konsole, Terminal.app, Alacritty, VS Code, Windows Terminal, GNOME Terminal, others | No | — |+| WezTerm | Not yet | Support comes when [wezterm/wezterm#7924](https://github.com/wezterm/wezterm/pull/7924) is merged |+| Konsole, Terminal.app, Alacritty, VS Code, Windows Terminal, GNOME Terminal, others | No | — |In other terminals, the mod shows the normal text. Refer to [Install](docs/install.md#terminals) for all the details, and run `/iui details` to see why pictures do or do not show.@@ -87,6 +88,7 @@ More in the [gallery](docs/gallery.md): plots, fractals and a tour of all the Ma| `/iui theme <name>` | Changes the theme: `claude-code`, `vscode-modern`, `vscode` or your own || `/iui size <80–200>` | Text size in percent of your terminal text || `/iui grid on` / `off` | Puts each line of text on the terminal rows, so that a mouse selection covers the text (on by default) |+| `/iui select on` / `off` | Claude Code 2.1.305+, fullscreen: the mod selects and copies picture text with the mouse (on by default) || `/iui updates on` / `off` | Turns the daily check for a new version on or off (on by default) || `/iui details` | Shows the terminal, font, mode and Typst that the mod found || `/iui setup` | iTerm2 (macOS): adds the shell line that turns pictures on |@@ -95,6 +97,10 @@ More in the [gallery](docs/gallery.md): plots, fractals and a tour of all the MaAll commands are in [Usage](docs/usage.md).+## Clipboard shims++For the automatic copy, the clipboard must get the text of a copy in place of its picture cells. At the start (not over ssh, not in a background session, and only in a terminal that shows the pictures), the mod writes small `sh` scripts (shims) for the clipboard tools that Claude Code uses and that are installed: `pbcopy` on macOS, and `wl-copy`, `xclip` and `xsel` on Linux. It puts them in a folder of this Claude Code process, `shims-<pid>/`, in its cache folder (`${TMPDIR}/cc-intelligent-ui-<uid>/`), and puts this folder first on the `PATH` of the process. It also makes the folder `ask/` and a FIFO (a named pipe) `ask-<pid>` in the cache folder, and sets `IUI_COPY_ASK` to the FIFO. Before that, a setup script (`hooks/clip-shim-setup.sh`) makes sure that these folders are yours, are not links, and only you can write to them. If not, the mod makes no shims and does not change `PATH`. A shim runs the real tool with the same arguments and the same exit status. A shim asks only when Claude Code itself starts it (the shim is a direct child of the process of the FIFO) and Claude Code writes text (not an image) to the clipboard. Then `perl` keeps the text in memory. If the text has picture cells and is 4 MiB or less, the shim writes it to a request file in `ask/` (mode 600) and writes the name of the file to the FIFO. The mod of this session reads the name, finds the text under the cells, and writes it to an answer file. The shim gives this text to the real tool. If no answer comes in 1.5 s, or the mod does not run, the shim gives the cells as they are. Then it deletes the files. The shim writes no other text to disk. Nothing leaves the computer. Refer to [How it works](docs/how-it-works.md).+## Docs- [Install](docs/install.md): requirements, terminal support and updatesdiff --git a/docs/development.md b/docs/development.mdindex 1d71c2f..b087bf6 100644--- a/docs/development.md+++ b/docs/development.md@@ -10,6 +10,7 @@ cd cc-intelligent-uiclaude --plugin-dir . # once: Claude Code writes the API types to .claude-plugin/types/npx -p typescript tsc -p . # type checkclaude plugin test . # tests+sh tests/shim-test.sh # the clipboard shims and their asks, with fake tools and a fake mod (also dash, bash --posix)claude plugin validate .```@@ -19,9 +20,11 @@ claude plugin validate .Set `IUI_DEBUG=1` before you start Claude Code. The mod then writes trace lines to `debug.log` in the cache folder (`${TMPDIR}/cc-intelligent-ui-<uid>/`, `/tmp` when `TMPDIR` is not set).+Picture select writes one line for each post of a `Client` (lines that start with `select:`): the phase and the order, the drawing's id, the page, the anchor and the head (the `Client`'s cells), the size of the `Client`'s region, the page's size in cells, the rows of the message on screen (`onScreen`), the page's top row in the message, its rows above the screen (`above`), and the selected rows of each page.+## Module layout-The hooks module is `hooks/register.tsx`. It connects the hooks to the modules in `hooks/lib/`. Each module has one topic, for example `markdown.ts` (blocks and segments), `mode.ts` (light or dark), `typst.ts` (compile), `fonts.ts` (the lean font set: the fonts that a compile of plain text loads), `draw.tsx` (the pages of an answer and the link rows under them), `links.ts` (the links, file paths and commands of a text), `answer.ts` (one text through the compile and cache steps), `plan.tsx` (the plan approval question and the plan rows), `frame.ts` (the terminal size and the watcher process, which also reports the clipboard changes), `clipboard.ts` (how the clipboard is read and watched: macOS, or the Linux tools), `copy.ts` (the text under a mouse selection of pictures), `probe.ts` (the drawn pages, and the words of a page that a copy reads, kept in memory) and `updates.ts` (the daily check for a new version). The values that change are in one object, `S`, in `state.ts`. Each `$` method has one call site, a wrapper in `api.ts`; the other modules call the wrappers. This keeps the `calls:` note of `claude plugin validate` short (the validator cuts a note at about 1,000 characters, and the catalogue scan then cannot read the footprint). The tests import from `hooks/register.tsx`, which exports the names that they use.+The hooks module is `hooks/register.tsx`. It connects the hooks to the modules in `hooks/lib/`. Each module has one topic, for example `markdown.ts` (blocks and segments), `mode.ts` (light or dark), `typst.ts` (compile), `fonts.ts` (the lean font set: the fonts that a compile of plain text loads), `draw.tsx` (the pages of an answer and the link rows under them), `links.ts` (the links, file paths and commands of a text), `answer.ts` (one text through the compile and cache steps), `plan.tsx` (the plan approval question and the plan rows), `frame.ts` (the terminal size and the watcher process, which also reports the clipboard changes), `clipboard.ts` (how the clipboard is read and watched: macOS, or the Linux tools; the clipboard shims, from the template `hooks/clip-shim.sh`, set up by `hooks/clip-shim-setup.sh`; and the reader of their asks), `copy.ts` (the text under a mouse selection of pictures), `probe.ts` (the drawn pages, and the words of a page that a copy reads, kept in memory) and `updates.ts` (the daily check for a new version). The values that change are in one object, `S`, in `state.ts`. Each `$` method has one call site, a wrapper in `api.ts`; the other modules call the wrappers. This keeps the `calls:` note of `claude plugin validate` short (the validator cuts a note at about 1,000 characters, and the catalogue scan then cannot read the footprint). The tests import from `hooks/register.tsx`, which exports the names that they use.The skill for Claude is in `skills/intelligent-ui/`: `SKILL.md` (the contract, the workflow and an index), `references/` (one file for each topic: Typst, fletcher, CeTZ, plots and charts) and `scripts/check.sh` (the compile check, with the real template). `TYPST_PROMPT` in `hooks/lib/settings.ts` tells Claude when to load it.diff --git a/docs/how-it-works.md b/docs/how-it-works.mdindex 3b622be..242592a 100644--- a/docs/how-it-works.md+++ b/docs/how-it-works.md@@ -12,7 +12,28 @@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 automatic copy works in fullscreen mode only: the mod reads the clipboard only when the selection of its session has picture cells, and it writes only when the clipboard is that selection. On the main screen, Claude Code gives no selection, so the mod cannot know which session a copy is from. There it does not change the clipboard. When the clipboard has picture cells that fit a page drawn in this session, it shows one toast that names `/iui grab`. `/iui grab` then reads the picture cells on the clipboard and writes their text. 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: Claude Code writes a copy (copy on select in fullscreen mode, `/copy`, `$.ui.copy`) with a tool that it starts by name from the `PATH`, as its direct child: `pbcopy` on macOS, and `wl-copy`, `xclip` or `xsel` on Linux. Before it starts the tool, it also sends the text to the terminal (OSC 52). At the start, the mod writes a small `sh` script (a shim) for each of these tools that is installed, into `<cache>/shims-<pid>/`, a folder of this Claude Code process only. It also makes the folder `<cache>/ask/` (mode 700) and the FIFO `<cache>/ask-<pid>` (mode 600) for this process. It does this only when the cache folder, the shims folder and `ask/` are folders of your user, not links, and not writable by other users (`hooks/clip-shim-setup.sh` checks this). It never uses the shared fallback folder `/tmp/cc-intelligent-ui` for this. It does not do this over ssh, in a background session, or in a terminal that shows no pictures. It puts the shims folder first on the `PATH` of the Claude Code process, one time, and sets `IUI_COPY_ASK` to the FIFO (`$.env.set`). With no `perl`, it makes no shims. Each shim has the full path of `perl`. When the mod draws its first picture, it starts a reader: a small `perl` process that keeps the FIFO open and gives the mod each name that a shim writes to it. The reader stops when Claude Code stops. If the reader stops for a different reason, the mod starts it again after 1 s (3 times at most).++ A shim asks only for a clipboard write of text (with no type, or a text type: `text/plain…`, `UTF8_STRING`, `STRING` or `TEXT`), and only when its parent is the process of the FIFO (the number in `ask-<pid>`). Other calls (a read, the primary selection, an image type) go to the real tool with no change and with no `perl`. For a write, the shim starts `perl`, which keeps the text in memory. If the text has picture cells (the bytes of U+10EEEE) and is 4 MiB or less, `perl` writes it to a request file `ask/<pid>-<shim pid>-<time>` (mode 600) and writes the name of the file to the FIFO. Before that, it makes sure that the FIFO and `ask/` are of your user and are not links. If no reader has the FIFO open (for example, the mod stopped), the open fails immediately, and the shim uses the cells. `perl` waits for the answer file for 1.5 s at most from its start, because Claude Code stops the tool after 2 s. Then it gives the answer (or the cells, when the answer is empty or did not come) to the real tool through a pipe, with the same arguments, and gives the exit status of the tool (also a signal). The shim deletes the request and the answer. It writes no other file. The mod takes only names of letters, digits, `.`, `_` and `-` from the FIFO (never a path), and reads the request file only in `ask/`, at most 4 MiB. It finds the text under the cells (see below). When the mouse selection of its session is that copy, the mod uses the row of the selection. It always answers: with the text, or with an empty answer when it cannot read the cells, so the shim does not wait. It writes the answer (mode 600) to a temporary file, then makes a hard link with the answer name. If the shim stopped waiting, the shim made that name first, so the link fails. Then the mod writes nothing to the clipboard: a different session can copy the same cells at that time. It deletes the request and the mark, and a toast tells you to use `/iui grab`. The mod writes the clipboard only in its answer to its own shim, and in `/iui grab` and the `ui.copy` hook. At `/clear` or a resume, the reader continues. At other session ends (for example `/exit`), the mod stops the reader and deletes the FIFO, the request files and the shims folder of this process. At the start, the mod deletes the FIFOs, the request files and the shims folders of processes that stopped, and request files that are older than 2 days. It does nothing in a cache folder that is a link or that is not yours.++ One long-lived `perl` process (the watcher) 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 mod never changes a copy of picture cells that no shim of this session asked for (a copy of a different session, or of the terminal itself on the main screen). When these cells fit a page drawn in this session, it shows one toast that names `/iui grab`. In fullscreen mode, it first waits 2 s, because the OSC 52 copy can come before the shim asks. `/iui grab` then reads the picture cells on the clipboard and writes their text. The first counter after a start is not a change, so an old clipboard gets no toast. If the watcher stops, the mod starts it again after 1 s (3 times at most). At `/clear` or a resume, the watcher continues without the clipboard part. At other session ends, the mod stops it and does not start a new one.++ 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. A `ui.copy` hook also changes a copy with picture cells to the text.++## Picture select++From Claude Code 2.1.305, the mod's `Image` has `noSelect`: the selection painter and the copy on select of Claude Code skip picture cells. Thus the mod selects pictures itself (`hooks/lib/select.ts`):++1. Each page is in a `Box` of its size. The last child is a `Client` (`hooks/sel.tsx`) in a `Box` with a negative top margin (the page's rows), so it is over the page. The `Client` draws only an empty `Box` (anything that it draws hides the picture cells). It takes the pointer and posts the selection whole: the anchor, the head, the phase (`down`, `move`, `up` or `clear`) and an order. Posts of one frame merge, so a bare event could be lost.+2. The `ui.message` hook keeps the newest selection and draws again. The selected rows are in stream order, in the rows of the whole drawing, so a drag can go on into the next page.+3. The selected rows are in a `Box` over the page (also with a negative top margin). Each selected row is a `Box` of the selected columns, with `overflow: hidden` and the selection colour, that holds that row of the page as an `Image` of one row, moved left by the first column ("clip, do not crop"). The page is transparent, so the colour shows under the text. The engine paints an overlay again only when it changes, so the colour changes by one step at each redraw.+4. The rows come from Typst (`render/strips.typ`, `--ppi 72`: one pixel for one pt): the first selection on a page cuts it into one PNG for each row, in a folder beside the page in its key folder (so the 2-day cleanup takes them). A PNG's rows cannot be cut from its bytes (its filters go across rows).+5. The terminal fits an image into its cells with the aspect kept. A row PNG shows the same pixels as its row of the page only when both are at one scale with no space above or below. Thus a page has picture select only when its height divided by its rows is a whole number of pixels (`rowPx`), and its width is at most `rowPx × columns × cell width / cell height` (`stripPlan`). The template pads each page to whole rows at the cell height, so with whole-pixel cells (Ghostty, kitty) `rowPx` is the cell height.+6. At `up`, the mod reads the words under the selected cells the same way as for a copy of these cells (`textOfRun` on the words of the probe), and copies them with `$.ui.copy`.++The `Client` and the rows are in the flow, not in absolute `Box`es. Claude Code (2.1.306) moves an absolute `Box` whose top is above the screen down to row 0, with its children. Then the pointer `y` of a `Client` in such a `Box` counts from row 0 of the screen, not from the top of the page, so the selection was too high by the rows that were scrolled away. Also, the selected rows above the screen showed on row 0, and the `Client` took the pointer under the page. A `Box` in the flow has its true position, also when its top is above the screen.++A `Client` over a picture also takes the selection from Claude Code before 2.1.305 (which selects and copies pictures itself), so the mod draws it only from 2.1.305 (`$.session.version()`), in fullscreen mode. A `Client` that fails (`ui.fault`) turns picture select off until a reload.## The lean font setdiff --git a/docs/install.md b/docs/install.mdindex 1047615..b901397 100644--- a/docs/install.md+++ b/docs/install.md@@ -16,7 +16,8 @@- **Rio 0.5.27 or later (not tested):** set `CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1` before you start Claude Code. Rio works on macOS and on Linux.- Do not put the variable in the `env` of `~/.claude/settings.json`. It then applies in every terminal, also where pictures cannot show.- **Warp (not yet):** Warp does not draw kitty pictures with Unicode placeholders yet. The pull request [warpdotdev/warp#16312](https://github.com/warpdotdev/warp/pull/16312) adds them. The mod will support Warp when that pull request is merged.-- **Not supported:** WezTerm and Konsole do not draw kitty pictures with Unicode placeholders, so they show no pictures, also with the variable set. Alacritty, Terminal.app, Windows Terminal, GNOME Terminal (and other VTE terminals), foot, VS Code, Tabby, Hyper and JetBrains terminals have no kitty pictures.+- **WezTerm (not yet):** WezTerm does not draw kitty pictures with Unicode placeholders yet, so it shows no pictures, also with the variable set. The pull request [wezterm/wezterm#7924](https://github.com/wezterm/wezterm/pull/7924) adds them. The mod will support WezTerm when that pull request is merged.+- **Not supported:** Konsole does not draw kitty pictures with Unicode placeholders, so it shows no pictures, also with the variable set. Alacritty, Terminal.app, Windows Terminal, GNOME Terminal (and other VTE terminals), foot, VS Code, Tabby, Hyper and JetBrains terminals have no kitty pictures.- **tmux and screen:** no pictures. Claude Code does not send pictures through tmux or screen, also with the variable set. Set `IUI_FORCE=1` to try anyway.- **ssh:** pictures work when the terminal on your computer is kitty or Ghostty (`TERM` must come through ssh), or when you set `CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1` on the remote computer. The mod sends each picture as data, because the terminal cannot read files on the remote computer. A page larger than 4096 pixels on a side or 2 MiB shows as text. An image of that size does not show.- Background sessions (agent view, forks, `claude attach`) cannot draw pictures.@@ -54,15 +55,16 @@ The mod finds Typst on the `PATH` of your login shell. It also looks in `/usr/loThe mod works on Linux with X11 and with Wayland. It uses these tools. Most Linux systems have the first three.-- **perl** (perl 5): reads the terminal size and its cell size, and watches for a change of the size.+- **perl** (perl 5): reads the terminal size and its cell size, and watches for a change of the size. The clipboard shims and their reader also use it (for the automatic copy).- **fontconfig** (`fc-match`, `fc-list`): finds the terminal font when the terminal's config does not name one. kitty and most terminals use fontconfig's `monospace` font. Ghostty uses its own JetBrains Mono when its config names no font. If fontconfig does not have JetBrains Mono, the mod copies the font from the Ghostty program (refer to [How it works](how-it-works.md)).- **The light or dark mode of the desktop** (for the `auto` theme): the freedesktop portal (`org.freedesktop.appearance color-scheme`, through `gdbus` or `busctl`). GNOME, KDE Plasma and most desktops have the portal. If the portal does not answer, the mod reads GNOME's `color-scheme` (`gsettings`).-- **The clipboard** (for the automatic copy in fullscreen mode and for `/iui grab` on the main screen, refer to [Copy text from a picture](usage.md#copy-text-from-a-picture)). The mod uses the first of these that it finds:+- **The clipboard** (for the `/iui grab` toast and for `/iui grab` on the main screen, refer to [Copy text from a picture](usage.md#copy-text-from-a-picture)). The mod uses the first of these that it finds:1. **Wayland, not GNOME:** `wl-paste` from `wl-clipboard`. `wl-paste --watch` tells the mod about each clipboard change. It needs the data-control protocol of the compositor (KDE Plasma, Sway, Hyprland and other wlroots compositors have it).2. **X11, and GNOME on Wayland (through Xwayland):** `clipnotify` tells the mod about each clipboard change, and `xclip` or `xsel` reads the clipboard. If `clipnotify` is not installed, the mod uses GTK 3 from Python (`python3-gi`, which most GNOME desktops have) for both.- 3. **GNOME on Wayland with no X11 tool:** `wl-paste`. GNOME does not have the data-control protocol, so `wl-paste --watch` stops, and the automatic copy is off.+ 3. **GNOME on Wayland with no X11 tool:** `wl-paste`. GNOME does not have the data-control protocol, so `wl-paste --watch` stops, and there is no `/iui grab` toast.- If none of these work, the automatic copy is off. `/iui grab` (with a selection, in fullscreen mode), Claude Code's `/copy` and the `ui.copy` hook still work. `/iui details` shows the tool in use, or why the automatic copy is off, on the line `Auto copy:`.+ If none of these work, there is no `/iui grab` toast. The automatic copy, `/iui grab` (with a selection, in fullscreen mode), Claude Code's `/copy` and the `ui.copy` hook still work. `/iui details` shows the tool in use, or why there is no toast, on the line `Auto copy:`.+- **The clipboard write tools** (for the automatic copy): `wl-copy` (from `wl-clipboard`), `xclip` or `xsel`. Claude Code copies with one of them. The mod puts a shim for each one that is installed first on the `PATH` (refer to [How it works](how-it-works.md)). The shims need `perl`. If none of these tools is installed, Claude Code copies with its own built-in code, and the automatic copy is off.- **"Edit the plan"** (refer to [Plan approval](usage.md#plan-approval)): VS Code (`code --wait`), else the first of VSCodium, Zed, Sublime Text, Kate, gedit and gvim that is on the `PATH`. These editors can wait until you close the file. If none is installed, the option does not show.- **The cache folder** is in `$TMPDIR`, else in `/tmp`.diff --git a/docs/limitations.md b/docs/limitations.mdindex c5c367b..849f5d2 100644--- a/docs/limitations.md+++ b/docs/limitations.md@@ -3,19 +3,33 @@← Back to the [README](../README.md)- **Copying text from a picture** (refer to [Copy text from a picture](usage.md#copy-text-from-a-picture)) has these limits:- - The automatic copy works on macOS and on Linux, but not over ssh (the mod reads the clipboard of the computer that runs Claude Code). On macOS, it needs `/usr/bin/perl` with Apple's PerlObjCBridge (macOS has it), which reads the change counter of the clipboard. On Linux, it needs a tool that tells the mod about each clipboard change: `wl-paste --watch` on Wayland, else `clipnotify` or GTK 3 from Python on X11 and Xwayland (refer to [Linux](install.md#linux)). If no such tool works, the automatic copy is off: the mod does not read the clipboard at intervals in its place. `/iui details` shows the state on the line `Auto copy:`.+ - The automatic copy works on macOS and on Linux, but not over ssh (the clipboard tools of Claude Code run on the computer that runs Claude Code), and not in a background session. It needs the clipboard shims and `perl` (refer to [How it works](how-it-works.md)). With no `perl`, the mod makes no shims, and a copy keeps the picture cells. On Linux with no `wl-copy`, `xclip` or `xsel`, Claude Code copies with its own built-in code, which no shim sees. Then there is no automatic copy. `/iui details` shows the state on the line `Auto copy:`.+ - A shim asks only when Claude Code starts it directly. A clipboard tool that a different program starts (for example a script that runs `pbcopy`) gets its text with no change. The shim asks only for 4 MiB of text or less.+ - The `/iui grab` toast for picture cells that are not from this session needs a tool that tells the mod about each clipboard change. On macOS, this is `/usr/bin/perl` with Apple's PerlObjCBridge (macOS has it), which reads the change counter of the clipboard. On Linux, it is `wl-paste --watch` on Wayland, else `clipnotify` or GTK 3 from Python on X11 and Xwayland (refer to [Linux](install.md#linux)). If no such tool works, there is no toast: the mod does not read the clipboard at intervals in its place.- On GNOME with Wayland, `wl-paste --watch` does not work (GNOME does not have the data-control protocol). The mod then uses the X11 tools through Xwayland. This works on Ubuntu 24.04 with GNOME and Python GTK 3.- On Linux with the old `clipnotify` (1.0.2 or earlier), a mouse selection (the PRIMARY selection) also makes the mod read the clipboard once. The mod changes nothing in that case. When the automatic copy is off, the clipboard keeps the picture cells (U+10EEEE plus combining marks), and a paste gives strange characters. Use `/iui grab`, Claude Code's `/copy` (the last answer as Markdown, `/copy 2` for the one before it), or turn the mod off (`/iui off`) to select text.- The first copy from a page takes approximately 0.05 s for plain text (refer to [The lean font set](how-it-works.md#the-lean-font-set)), 0.15–0.5 s for other text, and 2–3 s for a page with a large Typst drawing. No toast shows during this time. If you paste before the toast "copied N chars of text" shows, the paste gives the picture cells.+ - The shim waits 1.5 s at most for the text, because Claude Code stops the clipboard tool after 2 s. When the mod needs more time (for example, the first copy from a large Typst drawing), the shim writes the picture cells. The mod does not write the text later, because a different session can copy the same cells in that time. A toast tells you to use `/iui grab`.+ - While the shim waits, the clipboard can have the picture cells from the OSC 52 copy of the terminal. If you copy something different in a different app in this time, the shim then writes the text over it.+ - If the terminal applies the OSC 52 copy after the clipboard tool wrote, the clipboard has the picture cells again. The mod does not write over them. Use `/iui grab`.- The mod copies whole words. It copies a word when the selection covers the middle of the word. In a code block, in a long word (30 characters or more, for example a path or a URL) and in CJK text, it copies each character whose middle is in the selection.- 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.- - On the main screen (not fullscreen mode), there is no automatic copy. Claude Code gives the mod no selection on the main screen, so the mod cannot know which session a copy is from. Two sessions can have the same picture cells. Thus the mod does not change the clipboard, and a toast tells you to use `/iui grab`. Run `/iui grab` in the session that has the picture.+ - On the main screen (not fullscreen mode), there is no automatic copy. There the terminal copies the selection itself, not Claude Code, so no shim asks the mod. Two sessions can have the same picture cells. Thus the mod does not change the clipboard, and a toast tells you to use `/iui grab`. On the main screen, you must run `/iui grab` in the session that has the picture.- On the main screen, `/iui grab` does not know the row of the selection. Thus it can give the wrong text for a picture from before `/clear`, from a session that ended, or older than the last 64 pictures, and for a page after the first page of a long answer.- - In fullscreen mode, a different fullscreen session can have an old selection of exactly the same cells. Then both sessions read the copy, and the first write stays.+ - When a different session copies exactly the same cells less than 5 s after a copy of this session, the mod shows no `/iui grab` toast for it.+ - The mod makes the shims only in its own cache folder, and only when that folder, its `shims-<pid>` folder and its `ask` folder are folders of your user, not links, with no write access for other users. Else `/iui details` shows `Auto copy: off (the cache folder is not private)`.- 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).+ - To stop the clipboard write on select, set `copyOnSelect` to `false` in `/config`. Then the mod does not copy a selection either, because Claude Code writes no copy that a shim can change. It changes only a clipboard that has picture cells (for example, after a copy with a key).+- **Picture select** (Claude Code 2.1.305 or newer, refer to [Picture select](usage.md#picture-select-claude-code-21305-or-newer)) has these limits:+ - It works only in fullscreen mode, and not over ssh (pages sent as bytes).+ - A selection is in one picture (an answer, a plan or a Markdown file, all its pages). A drag that starts on normal text and goes into a picture is the selection of Claude Code, which skips the picture. A drag that starts in a picture stays in that picture. Thus a selection of text and a picture together is not possible.+ - A click on normal text does not remove a picture selection. A click in a picture, a new picture selection or your next prompt removes it.+ - It has no double click (word) or triple click (line) selection, and no Shift+click to make a selection longer.+ - A page must fill whole cells: its height must be a whole number of rows, each row a whole number of pixels, at the cell shape of the terminal. The mod makes pages so in Ghostty and kitty. A page that is not so (a page drawn smaller after a resize until its new picture is ready, a page cut at 255 rows, or a cell size of a fraction of a pixel) has no picture select. There the selection of Claude Code skips it.+ - The blue background is VS Code's selection colour, not the one of your terminal or Claude Code theme.+ - Each selected row is one more small image in the terminal (approximately 0.5 MB in Ghostty's image memory, whose default limit is 320 MB).- **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.- **The row grid** (`/iui grid on`, refer to [Selection and the row grid](usage.md#selection-and-the-row-grid)) has these limits:diff --git a/docs/usage.md b/docs/usage.mdindex 0150034..67fa8bd 100644--- a/docs/usage.md+++ b/docs/usage.md@@ -61,6 +61,9 @@ You can also start the skill yourself: type `/cc-intelligent-ui:intelligent-ui`| `/iui grid` | Shows the row grid setting: on or off || `/iui grid on` | Puts each line of text in a picture on the terminal rows (kept for later sessions). This is the default. Refer to [Selection and the row grid](#selection-and-the-row-grid). || `/iui grid off` | Lines of text in a picture have the usual spacing (kept for later sessions). |+| `/iui select` | Shows the picture select setting: on or off |+| `/iui select on` | With Claude Code 2.1.305 or newer, in fullscreen mode, a drag over a picture selects its text and copies it (kept for later sessions). This is the default. Refer to [Copy text from a picture](#copy-text-from-a-picture). |+| `/iui select off` | Stops picture select (kept for later sessions). Then the selection of Claude Code skips pictures (2.1.305 or newer). || `/iui updates` | Shows the update check: on or off, and the newest version that it found || `/iui updates on` | Looks for a new version one time each day at most, and shows a toast when there is one (kept for later sessions). This is the default. Refer to [Update](install.md#update). || `/iui updates off` | Stops the check for a new version and its toast (kept for later sessions) |@@ -89,14 +92,24 @@ 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)). The automatic copy works in fullscreen mode only. We recommend fullscreen mode: run `/tui fullscreen` (`/tui default` goes back to the main screen). 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. 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.+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 gives picture cells. The automatic copy works in fullscreen mode only. We recommend fullscreen mode: run `/tui fullscreen` (`/tui default` goes back to the main screen). Claude Code writes the copy with a clipboard tool (`pbcopy` on macOS). A small shim of this tool asks the mod of this session for the text under the picture cells (refer to [How it works](how-it-works.md)). The clipboard then gets this text in place of the cells, and a toast shows the number of characters. Wait for this toast, then paste. If the text is not ready in 1.5 s (for example, the first copy from a large Typst drawing), the clipboard keeps the cells, and a toast tells you to use `/iui grab`. A copy from a different Claude Code session goes to the mod of that session, so this session does not change it. The mod does not change a clipboard that has no picture cells.-On the main screen (not fullscreen mode), Claude Code gives the mod no selection. Thus the mod cannot know which session a copy is from, and it does not change the clipboard. When the clipboard has picture cells of a picture in this session, a toast tells you to use `/iui grab`. Select and copy, then run `/iui grab` in the same session. `/iui grab` puts the text of these picture cells on the clipboard. When the mod draws its first picture on the main screen, a toast recommends fullscreen mode (one time in each session).+On the main screen (not fullscreen mode), the terminal copies the selection itself, not Claude Code. Thus the mod cannot know which session a copy is from, and it does not change the clipboard. While the mod is on and has drawn a picture in this session, it 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 of a picture in this session, a toast tells you to use `/iui grab`. Select and copy, then run `/iui grab` in the same session. `/iui grab` puts the text of these picture cells on the clipboard. When the mod draws its first picture on the main screen, a toast recommends fullscreen mode (one time in each session).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.If the clipboard did not change, use `/iui grab`. In fullscreen mode, it reads the last mouse selection of Claude Code and copies its text. With no selection (the main screen), it reads the picture cells on the clipboard and copies their text. If the clipboard has the text already, it tells you. If the copied text is not correct, `/iui dump` shows the cells that the mod found and the page that it used. It also writes all of this to `copy-debug.json` in the cache folder.+### Picture select (Claude Code 2.1.305 or newer)++From Claude Code 2.1.305, the mouse selection of Claude Code skips pictures. It shows no highlight on a picture and copies no picture cells. Thus in fullscreen mode the mod selects the text of a picture itself:++1. Drag over a picture with the left button. The selected rows show on a blue background, in the order of the text: the first row from where you started, the rows between, and the last row to the pointer.+2. Release the button. The mod copies the text of the selected cells (the same words that a copy of these cells gives) and shows the toast "copied N chars of text".+3. A click with no drag, a new selection in a different picture, or your next prompt removes the selection.++A selection can go on into the next page of the same answer. The first selection on a page takes approximately 0.2 s more: the mod cuts the page into rows (one PNG for each row, kept with the page). `/iui select off` turns picture select off. Before Claude Code 2.1.305, and on the main screen, the selection is the one of Claude Code or of the terminal, as before.+For the limits, refer to [Limitations](limitations.md).## Selection and the row griddiff --git a/hooks/clip-shim-setup.sh b/hooks/clip-shim-setup.shnew file mode 100644index 0000000..9d0a95a--- /dev/null+++ b/hooks/clip-shim-setup.sh@@ -0,0 +1,59 @@+#!/bin/sh+# Intelligent UI (cc-intelligent-ui): sets up the clipboard shims (clip-shim.sh).+# find <cache> <tool>...: prints `pid <Claude Code's pid>`, makes the shims+# folder <cache>/shims-<pid> of this process, then `<tool> <path>` for each+# tool found on PATH without a shims folder, `perl <path>`, and `ask <fifo>`+# when it made the private folder <cache>/ask and the FIFO <cache>/ask-<pid>+# (mode 600) for the shims' asks.+# install <cache> <suffix> <file>...: puts each new shim `<file><suffix>` in+# place, executable first; `-<file>` removes the shim of a tool that is gone.+# Only in the shims folder of this process: never the one of another.+# Exit 3: <cache> or the shims folder is not a private folder of this user.++# A folder of this user (not a link), made 700, with no group or other write.+private() {+ [ ! -L "$1" ] && [ -d "$1" ] && [ -O "$1" ] && chmod 700 "$1" 2>/dev/null || return 1+ case $(ls -ld "$1") in d????-??-*) return 0 ;; esac+ return 1+}+# The shims folder: private, and each entry a file of this user (not a link).+shims_ok() {+ private "$1" || return 1+ [ -z "$(find "$1" -mindepth 1 \( ! -type f -o ! -user "$(id -u)" \) 2>&1)" ]+}+# A FIFO of this user (not a link).+fifo_ok() { [ -p "$1" ] && [ ! -L "$1" ] && [ -O "$1" ]; }++mode=$1; c=$2; d=$2/shims-$PPID+shift 2+case $mode in+ find)+ echo "pid $PPID"+ private "$c" || { echo "unsafe $c"; exit 3; }+ mkdir "$d" 2>/dev/null+ shims_ok "$d" || { echo "unsafe $d"; exit 3; }+ p=; IFS=:; set -f+ for e in $PATH; do case $e in "$c"/shims|"$c"/shims-*) ;; *) p=${p:+$p:}$e ;; esac; done+ unset IFS; set +f; PATH=$p+ for t in "$@" perl; do x=$(command -v "$t" 2>/dev/null) && case $x in /*) echo "$t $x" ;; esac; done+ q=$c/ask; f=$c/ask-$PPID+ mkdir "$q" 2>/dev/null+ private "$q" || exit 0+ fifo_ok "$f" || { rm -f "$f"; mkfifo -m 600 "$f" 2>/dev/null; }+ fifo_ok "$f" && chmod 600 "$f" && echo "ask $f"+ exit 0 ;;+ install)+ private "$c" && shims_ok "$d" || exit 3+ s=$1; shift+ for f in "$@"; do+ # Only files in the shims folder.+ case ${f#-} in "$d"/*) ;; *) exit 1 ;; esac+ case $f in+ -*) rm -f -- "${f#-}" ;;+ *)+ { [ -f "$f$s" ] && [ ! -L "$f$s" ] && [ -O "$f$s" ] && chmod 755 "$f$s" && mv -f "$f$s" "$f"; } || { rm -f "$f$s"; exit 1; }+ [ -f "$f" ] && [ ! -L "$f" ] && [ -O "$f" ] || exit 1 ;;+ esac+ done ;;+ *) exit 2 ;;+esacdiff --git a/hooks/clip-shim.sh b/hooks/clip-shim.shnew file mode 100644index 0000000..a798fde--- /dev/null+++ b/hooks/clip-shim.sh@@ -0,0 +1,157 @@+#!/bin/sh+# Intelligent UI (cc-intelligent-ui): a pass-through shim for one clipboard tool.+# It runs the real tool with the same arguments and exit status. A clipboard+# write of picture cells by the Claude Code process of the FIFO $IUI_COPY_ASK+# (its direct child) asks that process's mod for their text, and the real+# tool gets that text. Only the request file has text on disk. See the README.+tool=@TOOL@+real=@REAL@+dir=@DIR@+perl=@PERL@+if [ ! -x "$real" ]; then+ # The real tool moved: find it on PATH without this folder (never this shim).+ [ -z "${IUI_SHIM_IN:-}" ] || { echo "$tool: not found" >&2; exit 127; }+ IUI_SHIM_IN=1; export IUI_SHIM_IN+ p=; IFS=:; set -f+ for e in $PATH; do [ "$e" = "$dir" ] || p=${p:+$p:}$e; done+ unset IFS; set +f; PATH=$p; real=$tool+fi++# w=1: a write of text to the clipboard (not primary) with the text on stdin.+# Any other call (also a type that is not text), or an argument not known+# here, passes through untouched.+w=1; v=; s=; i=; b=+text_type() { case $1 in text/plain*|UTF8_STRING|STRING|TEXT) ;; *) w=0 ;; esac; }+case $tool in+ pbcopy)+ for a in "$@"; do+ if [ -n "$v" ]; then [ "$a" = general ] || w=0; v=; continue; fi+ case $a in -pboard) v=1 ;; *) w=0 ;; esac+ done ;;+ wl-copy)+ for a in "$@"; do+ if [ -n "$v" ]; then [ "$v" = type ] && text_type "$a"; v=; continue; fi+ case $a in+ -n|--trim-newline|-f|--foreground|-o|--paste-once|--regular|--seat=*) ;;+ --type=*) text_type "${a#--type=}" ;;+ -t|--type) v=type ;;+ -s|--seat) v=1 ;;+ *) w=0 ;;+ esac+ done ;;+ xclip)+ # Its default selection is primary: only an explicit clipboard counts.+ for a in "$@"; do+ if [ -n "$v" ]; then+ if [ "$v" = sel ]; then case $a in c*) s=1 ;; *) s= ;; esac; fi+ [ "$v" = type ] && text_type "$a"+ v=; continue+ fi+ case $a in+ -i|-in|-input|-f|-filter|-quiet|-silent|-verbose|-noutf8|-rmlastnl) ;;+ -sel|-selection) v=sel ;;+ -t|-target) v=type ;;+ -d|-display|-l|-loops) v=1 ;;+ *) w=0 ;;+ esac+ done+ [ "$s" = 1 ] || w=0 ;;+ xsel)+ for a in "$@"; do+ if [ -n "$v" ]; then v=; continue; fi+ case $a in+ --input|--append) i=1 ;;+ --clipboard) b=1 ;;+ --nodetach|--verbose|--zeroflush) ;;+ --selectionTimeout|--logfile|--display) v=1 ;;+ --*|-) w=0 ;;+ -*)+ r=${a#-}+ while [ -n "$r" ]; do+ c=${r%"${r#?}"}; r=${r#?}+ case $c in+ i|a) i=1 ;;+ b) b=1 ;;+ n|v|z) ;;+ t|l) if [ -z "$r" ]; then v=1; else w=0; fi ;;+ *) w=0 ;;+ esac+ done ;;+ *) w=0 ;;+ esac+ done+ [ "$i" = 1 ] && [ "$b" = 1 ] || w=0 ;;+ *) w=0 ;;+esac+[ -z "$v" ] || w=0++# The ask: only from the Claude Code process of the FIFO <cache>/ask-<pid>.+f=${IUI_COPY_ASK:-}; n=${f##*/ask-}+case $n in ''|*[!0-9]*) w=0 ;; esac+[ "$n" = "$PPID" ] || w=0+[ "$w" = 1 ] && [ -x "$perl" ] || exec "$real" "$@"+# Perl keeps the text in memory. Picture cells (U+10EEEE, F4 8E BB AE), 4 MiB+# at most: a request file in <cache>/ask, its id to the FIFO, then the answer+# (1.5 s from the start at most: Claude Code stops the tool at 2 s), else the+# cells as they are. Then the real tool through a pipe, and its exit status.+exec "$perl" -e '+use strict; use Fcntl; use POSIX (); use Config; use Time::HiRes ();+my $END = Time::HiRes::time() + 1.5;+my $MAX = 4 << 20;+my ($fifo, $real, @args) = @ARGV;+binmode STDIN;+# A text over $MAX: no end of file here, so no ask.+my ($text, $eof) = ("", 0);+while (length($text) <= $MAX) {+ my $n = sysread(STDIN, $text, 65536, length $text);+ if (!$n) { $eof = 1; last; }+}+my $out = $text;+my $q;+my ($root, $own) = $fifo =~ m{^(.*)/ask-(\d+)$};+if ($eof && defined $own && $own == getppid() && index($text, "\xF4\x8E\xBB\xAE") >= 0) {+ $q = "$root/ask/$own-$$-" . int(Time::HiRes::time() * 1000);+ eval {+ my @d = lstat("$root/ask"); die unless @d && -d _ && $d[4] == $>;+ my @f = lstat($fifo); die unless @f && -p _ && $f[4] == $>;+ sysopen(my $w, "$q.tmp", O_WRONLY | O_CREAT | O_EXCL, 0600) or die;+ (print $w $text) && close($w) or die;+ rename("$q.tmp", $q) or die;+ # No reader (ENXIO): the mod does not run, so no wait.+ sysopen(my $p, $fifo, O_WRONLY | O_NONBLOCK) or die;+ my @s = stat($p); die unless -p _ && $s[0] == $f[0] && $s[1] == $f[1];+ my $id = substr($q, length("$root/ask/"));+ syswrite($p, "$id\n") == length($id) + 1 or die; close $p;+ my $a;+ until (open($a, "<:raw", "$q.answer")) {+ if (Time::HiRes::time() >= $END) {+ # Too late: the mark stops a later answer (the mod links it with no overwrite).+ die if sysopen(my $m, "$q.answer", O_WRONLY | O_CREAT | O_EXCL, 0600);+ open($a, "<:raw", "$q.answer") or die; last;+ }+ Time::HiRes::sleep(0.01);+ }+ my $got = do { local $/; <$a> }; close $a;+ unlink "$q.answer";+ # An empty answer: the cells as they are.+ $out = $got if defined $got && length $got;+ };+ unlink $q, "$q.tmp";+}+$SIG{PIPE} = "IGNORE";+my $to;+unless (open($to, "|-", $real, @args)) { print STDERR "$real: $!\n"; exit 127; }+binmode $to;+print $to $out;+unless ($eof) { my $b; while (sysread(STDIN, $b, 65536)) { print $to $b or last; } }+my $ok = close($to); my ($st, $err) = ($?, $! + 0);+exit 1 if $st == -1;+if (my $sig = $st & 127) {+ my $name = (split " ", $Config{sig_name})[$sig];+ $SIG{$name} = "DEFAULT" if $name;+ kill $sig, $$;+ exit 128 + $sig;+}+exit($st >> 8) if $st >> 8;+exit(!$ok && $err && $err != POSIX::EPIPE() ? 1 : 0);+' -- "$f" "$real" "$@"diff --git a/hooks/lib/api.ts b/hooks/lib/api.tsindex 49e545f..e712f1a 100644--- a/hooks/lib/api.ts+++ b/hooks/lib/api.ts@@ -14,9 +14,11 @@ export type EnvName =| 'CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC' | 'CLAUDE_CODE_FORCE_TERMINAL_IMAGES' | 'CLAUDE_CODE_SESSION_KIND' | 'CLAUDE_CODE_TMPDIR'| 'CMUX_SOCKET_PATH' | 'CMUX_SURFACE_ID' | 'CMUX_WORKSPACE_ID' | 'COLORFGBG' | 'DISABLE_AUTOUPDATER' | 'DISABLE_UPDATES' | 'DISPLAY'| 'GHOSTTY_RESOURCES_DIR' | 'HOME'- | 'ITERM_PROFILE' | 'ITERM_SESSION_ID' | 'IUI_DEBUG' | 'IUI_FORCE' | 'KITTY_CONFIG_DIRECTORY' | 'KITTY_PID'- | 'KITTY_WINDOW_ID' | 'LC_TERMINAL' | 'RIO_CONFIG_HOME' | 'SHELL' | 'SSH_CONNECTION' | 'SSH_TTY' | 'STY'+ | 'ITERM_PROFILE' | 'ITERM_SESSION_ID' | 'IUI_COPY_ASK' | 'IUI_DEBUG' | 'IUI_FORCE' | 'KITTY_CONFIG_DIRECTORY' | 'KITTY_PID'+ | 'KITTY_WINDOW_ID' | 'LC_TERMINAL' | 'PATH' | 'RIO_CONFIG_HOME' | 'SHELL' | 'SSH_CONNECTION' | 'SSH_TTY' | 'STY'| 'TERM' | 'TERM_PROGRAM' | 'TMUX' | 'WAYLAND_DISPLAY' | 'XDG_CONFIG_HOME' | 'XDG_CURRENT_DESKTOP' | 'ZDOTDIR'+// The variables the mod sets (the clipboard shims).+export type EnvSetName = 'IUI_COPY_ASK' | 'PATH'export function clockAfter($: EngineInterface, ms: number, fn: () => void) {return $.clock.after(ms, fn)@@ -30,7 +32,7 @@ export function clockNow($: EngineInterface) {return $.clock.now()}-export function commandRegister($: EngineInterface, command: CommandSpec) {+export function commandAdd($: EngineInterface, command: CommandSpec) {return $.command.register(command)}@@ -38,7 +40,7 @@ export function commandRun($: EngineInterface, input: Parameters<EngineInterfacereturn $.command.run(input)}-export function configList($: EngineInterface) {+export function configs($: EngineInterface) {return $.config.list()}@@ -59,12 +61,14 @@ export function envGet($: EngineInterface, name: EnvName): Promise<string | undecase 'HOME': return $.env.get('HOME')case 'ITERM_PROFILE': return $.env.get('ITERM_PROFILE')case 'ITERM_SESSION_ID': return $.env.get('ITERM_SESSION_ID')+ case 'IUI_COPY_ASK': return $.env.get('IUI_COPY_ASK')case 'IUI_DEBUG': return $.env.get('IUI_DEBUG')case 'IUI_FORCE': return $.env.get('IUI_FORCE')case 'KITTY_CONFIG_DIRECTORY': return $.env.get('KITTY_CONFIG_DIRECTORY')case 'KITTY_PID': return $.env.get('KITTY_PID')case 'KITTY_WINDOW_ID': return $.env.get('KITTY_WINDOW_ID')case 'LC_TERMINAL': return $.env.get('LC_TERMINAL')+ case 'PATH': return $.env.get('PATH')case 'RIO_CONFIG_HOME': return $.env.get('RIO_CONFIG_HOME')case 'SHELL': return $.env.get('SHELL')case 'SSH_CONNECTION': return $.env.get('SSH_CONNECTION')@@ -80,6 +84,13 @@ export function envGet($: EngineInterface, name: EnvName): Promise<string | unde}}+export function envSet($: EngineInterface, name: EnvSetName, value: string): ReturnType<EngineInterface['env']['set']> {+ switch (name) {+ case 'IUI_COPY_ASK': return $.env.set('IUI_COPY_ASK', value)+ case 'PATH': return $.env.set('PATH', value)+ }+}+export function fsExists($: EngineInterface, path: string) {return $.fs.exists(path)}@@ -116,11 +127,11 @@ export function processRun($: EngineInterface, argv: readonly string[], init?: Preturn $.process.run(argv, init)}-export function processSpawn($: EngineInterface, request: ProcessSpawnRequest) {+export function spawn($: EngineInterface, request: ProcessSpawnRequest) {return $.process.spawn(request)}-export function promptSubmit($: EngineInterface, input: Parameters<EngineInterface['prompt']['submit']>[0]) {+export function submit($: EngineInterface, input: Parameters<EngineInterface['prompt']['submit']>[0]) {return $.prompt.submit(input)}@@ -136,6 +147,10 @@ export function sessionRoot($: EngineInterface) {return $.session.root()}+export function sessionVersion($: EngineInterface) {+ return $.session.version()+}+export function sessionSurfaces($: EngineInterface) {return $.session.surfaces()}@@ -164,7 +179,7 @@ export function uiCopy($: EngineInterface, args: UiCopyArgs) {return $.ui.copy(args)}-export function uiInvalidate($: EngineInterface, event: InvalidatableEventName) {+export function invalidate($: EngineInterface, event: InvalidatableEventName) {return $.ui.invalidate(event)}diff --git a/hooks/lib/clipboard.ts b/hooks/lib/clipboard.tsindex 17bd5cf..47a9c8f 100644--- a/hooks/lib/clipboard.ts+++ b/hooks/lib/clipboard.ts@@ -3,12 +3,15 @@// PerlObjCBridge (host.ts). Linux: Wayland's wl-paste (its --watch sees each// change), else X11 (also Xwayland): xclip or xsel to read, clipnotify for// an event at each change, else GTK 3 from Python (python3-gi, on most GNOME-// desktops) for both. Where no tool sees the changes, the automatic copy is-// off: the clipboard is never read at intervals in its place.+// desktops) for both. The changes give the /iui grab hint (where no tool sees+// them there is none: the clipboard is never read at intervals). The shims+// (below) ask this session's mod for the text of a copy of picture cells.import type { EngineInterface } from 'claude-code'import { S } from './state'-import { envGet, processRun } from './api'+import { lineSplitter } from './frame'+import { perlFor } from './host'+import { clockAfter, envGet, envSet, fsExists, fsRead, fsStat, fsWrite, pluginRoot, processRun, spawn } from './api'// The tools the choice looks for, in the order it prefers them. `gtk` is// python3 when it can load GTK 3 (gi).@@ -153,25 +156,296 @@ export function clipWatchArgs(isMac: boolean): string[] | null {return way && 'watch' in way ? way.watch : null}-// The automatic copy now, for /iui details and dump. `none`: the-// notifier stopped (the watcher printed `clip none`). `isFullscreen`: null-// before a render hook says; only fullscreen copies by itself.+// ---- The shims: Claude Code's own clipboard writes ---------------------------+// Claude Code copies with pbcopy, wl-copy, xclip or xsel, found by name on+// PATH. A shim folder of this process (<cache>/shims-<pid>) first on PATH+// sends each write of picture cells to the mod of its session, which gives+// their text (hooks/clip-shim.sh).+export const SHIM_TOOLS = { mac: ['pbcopy'], linux: ['wl-copy', 'xclip', 'xsel'] } as const+export type ShimState = { tools: string[] } | { off: string }++// The setup script (in the plugin): `find` and `install`, each after it+// checks that the cache folder and the shims folder are private (exit 3).+export const SHIM_SETUP = 'hooks/clip-shim-setup.sh'++export const shQuote = (s: string) => `'${s.replace(/'/g, `'\\''`)}'`++// The shim of one tool: the template with its name, the real tool, the folder and perl.+export const shimText = (template: string, tool: string, real: string, dir: string, perl: string) =>+ template.split('@TOOL@').join(shQuote(tool)).split('@REAL@').join(shQuote(real)).split('@DIR@').join(shQuote(dir))+ .split('@PERL@').join(shQuote(perl))++// The `find` output: the pid, the tools found (absolute, not in a shims+// folder), perl, and the FIFO for the asks ('' when it was not made).+export function shimFindOf(stdout: string, tools: readonly string[], cache: string): { pid: string; found: Record<string, string>; perl: string; ask: string } {+ let pid = ''+ let perl = ''+ let ask = ''+ const found: Record<string, string> = {}+ for (const line of stdout.split('\n')) {+ const m = /^(\S+) (.+)$/.exec(line.trim())+ if (!m) continue+ const [, key, value] = m as unknown as [string, string, string]+ if (key === 'pid' && /^\d+$/.test(value)) pid = value+ else if (key === 'perl' && value.startsWith('/')) perl = value+ else if (key === 'ask' && value.startsWith(`${cache}/ask-`) && /^\d+$/.test(value.slice(cache.length + 5))) ask = value+ else if (tools.includes(key) && value.startsWith('/') && !value.startsWith(`${cache}/shims`)) found[key] = value+ }+ return { pid, found, perl, ask }+}++// PATH with the folder first, once; null when it is so already.+export function pathWith(path: string, dir: string): string | null {+ const parts = path ? path.split(':') : []+ if (parts[0] === dir && !parts.slice(1).includes(dir)) return null+ return [dir, ...parts.filter(p => p !== dir)].join(':')+}++// setUp, on this computer (macOS or Linux): the shims of the tools found,+// IUI_COPY_ASK and PATH (once per process: a hot reload finds them set).+export async function setUpShims($: EngineInterface, cache: string, isMac: boolean): Promise<ShimState> {+ S.askFifo = ''+ S.shimDir = ''+ S.shims = await shimsFor($, cache, isMac).catch(() => ({ off: 'the clipboard shims could not be set up' }))+ return S.shims+}+export const NOT_PRIVATE = 'the cache folder is not private'+// No shims, PATH as it is: no private cache folder (CACHE_SH failed), or no+// pictures here (a terminal with no pictures, a background session).+export function noShims(off = NOT_PRIVATE): ShimState {+ S.askFifo = ''+ S.shimDir = ''+ S.shims = { off }+ return S.shims+}+async function shimsFor($: EngineInterface, cache: string, isMac: boolean): Promise<ShimState> {+ const setup = `${pluginRoot($)}/${SHIM_SETUP}`+ const tools = isMac ? SHIM_TOOLS.mac : SHIM_TOOLS.linux+ const path = await envGet($, 'PATH')+ if (!path) return { off: 'no PATH' }+ const got = await processRun($, ['sh', setup, 'find', cache, ...tools], { timeoutMs: 5000 })+ if (got.exitCode === 3) return { off: NOT_PRIVATE }+ if (got.exitCode !== 0) return { off: 'the shim folder could not be made' }+ const { pid, found, perl, ask } = shimFindOf(got.stdout, tools, cache)+ if (!pid) return { off: 'the shim folder could not be made' }+ const dir = `${cache}/shims-${pid}`+ const names = tools.filter(t => found[t])+ if (!names.length) return { off: isMac ? 'no pbcopy' : 'no wl-copy, xclip or xsel: Claude Code copies with its own addon' }+ if (!perl) return { off: 'no perl' }+ if (!ask) return { off: 'the ask FIFO could not be made' }+ const template = await fsRead($, `${pluginRoot($)}/hooks/clip-shim.sh`)+ const suffix = `.new-${pid || 'x'}`+ const args: string[] = []+ for (const tool of tools) {+ const file = `${dir}/${tool}`+ const real = found[tool]+ if (!real) {+ if (await fsExists($, file).catch(() => false)) args.push(`-${file}`)+ continue+ }+ const text = shimText(template, tool, real, dir, perl)+ if ((await fsRead($, file).catch(() => null)) === text) continue+ await fsWrite($, `${file}${suffix}`, text)+ args.push(file)+ }+ if (args.length) {+ const put = await processRun($, ['sh', setup, 'install', cache, suffix, ...args], { timeoutMs: 5000 })+ if (put.exitCode === 3) return { off: NOT_PRIVATE }+ if (put.exitCode !== 0) return { off: 'the clipboard shims could not be installed' }+ }+ // One FIFO per Claude Code process (a nested one has its own).+ if ((await envGet($, 'IUI_COPY_ASK')) !== ask) {+ const set = await envSet($, 'IUI_COPY_ASK', ask)+ if (set.deny) return { off: `IUI_COPY_ASK not set: ${set.deny}` }+ }+ const next = pathWith(path, dir)+ if (next !== null) {+ const set = await envSet($, 'PATH', next)+ if (set.deny) return { off: `PATH not set: ${set.deny}` }+ }+ S.askFifo = ask+ S.shimDir = dir+ return { tools: names }+}++// ---- The asks: a shim wants the text of the picture cells it copies ----------+// The shim writes the cells to <cache>/ask/<id> and the id to the FIFO+// <cache>/ask-<pid>; the reader (ASK_PL) prints `ask <id>`, the mod answers.++// The reader: prints `pid <n>`, then `ask <id>` for each id line of the FIFO+// (open to read and write, so a writer that closes is no end of file). Exit 3+// when the FIFO is not a FIFO of this user; exit 0 when the parent dies.+export const ASK_PL = [+ 'my $fifo = shift; my $parent = getppid(); $| = 1;',+ '$SIG{PIPE} = sub { exit 0 };',+ 'print "pid $$\\n" or exit 0;',+ 'my @l = lstat($fifo); exit 3 unless @l && -p _ && $l[4] == $>;',+ 'open(my $f, "+<", $fifo) or exit 3;',+ 'my @s = stat($f); exit 3 unless -p _ && $s[0] == $l[0] && $s[1] == $l[1];',+ 'my $buf = "";',+ 'while (1) {',+ ' my $r = ""; vec($r, fileno($f), 1) = 1;',+ ' my $n = select($r, undef, undef, 2);',+ ' exit 0 if getppid() != $parent;',+ ' next unless $n && $n > 0;',+ ' sysread($f, $buf, 4096, length $buf) or exit 0;',+ ' while ($buf =~ s/^([^\\n]*)\\n//) { my $id = $1; if ($id =~ /^[A-Za-z0-9_-][A-Za-z0-9._-]{0,127}$/) { print "ask $id\\n" or exit 0; } }',+ ' $buf = "" if length $buf > 4096;',+ '}',+].join(' ')+// A request id: no path, no dot first.+export const ASK_ID = /^[A-Za-z0-9_-][A-Za-z0-9._-]{0,127}$/+// The answer to <request> (stdin): mode 600, a temp file, then a link, which+// never replaces the mark of a shim that gave up (then exit 1).+export const ANSWER_SH = 'umask 077; cat >"$1.tmp" || { rm -f "$1.tmp"; exit 1; }; ln "$1.tmp" "$1.answer" 2>/dev/null; r=$?; rm -f "$1.tmp"; exit "$r"'+// After a give-up: the request and the shim's mark.+export const ASK_CLEAN_SH = 'rm -f -- "$1" "$1.answer"'+// The end of the session: the FIFO, this process's request files and its shims folder.+export const ASK_END_SH = 'rm -f -- "$1" "$2/$3"-*; rm -rf -- "$4"'+// A request larger than this is not read (the shim does not ask for one).+export const ASK_MAX = 4 << 20+export const ASK_RESTARTS = 3+export const ASK_RESTART_MS = 1000++const askDir = () => `${S.askFifo.slice(0, S.askFifo.lastIndexOf('/'))}/ask`++// The reader's child now, its restarts (at most ASK_RESTARTS per load) and+// where its ids go (register.tsx).+let asker: { ended: boolean; final: boolean; stop: () => void } | null = null+let askRestarts = 0+let askTo: ((id: string) => void) | null = null+export const asking = () => !!asker && !asker.ended++// Starts the reader of the FIFO (at the first picture: before it, a shim+// finds no reader and copies at once), or again when it ended on its own+// (after ASK_RESTART_MS, ASK_RESTARTS times at most). True when one started.+export function listenAsks($: EngineInterface, to?: (id: string) => void): boolean {+ if (to) askTo = to+ if (!S.askFifo || asker?.final) return false+ if (asker) {+ if (!asker.ended || askRestarts >= ASK_RESTARTS) return false+ askRestarts+++ }+ let pid = ''+ let stopped = false+ const kill = () => {+ if (stopped && pid) void Promise.resolve(processRun($, ['kill', pid], { timeoutMs: 5000 })).catch(() => undefined)+ }+ const me = {+ ended: false,+ final: false,+ stop: () => {+ if (stopped) return+ stopped = true+ kill()+ },+ }+ asker = me+ try {+ const child = spawn($, { argv: [perlFor(S.hostOs === 'Darwin'), '-e', ASK_PL, S.askFifo] })+ void followAsks(child, line => {+ const m = /^(pid|ask) (\S+)$/.exec(line)+ if (m?.[1] === 'pid') {+ pid = m[2]!+ kill()+ } else if (m && !stopped) askTo?.(m[2]!)+ }).then(code => {+ me.ended = true+ if (code === 3) me.final = true+ if (stopped || me.final) return+ try {+ clockAfter($, ASK_RESTART_MS, () => {+ if (asker === me) listenAsks($)+ })+ } catch {}+ }).catch(() => undefined)+ } catch {+ me.ended = me.final = true+ }+ return true+}++// The reader's lines, each to `note`, until it ends: its exit code (null+// when it did not start or a signal ended it).+async function followAsks(+ child: AsyncIterator<{ stream: 'stdout' | 'stderr'; text: string }, { code: number | null } | undefined>,+ note: (line: string) => void,+): Promise<number | null> {+ const lines = lineSplitter()+ try {+ for (;;) {+ const got = await child.next()+ if (got.done) return got.value?.code ?? null+ if (got.value.stream === 'stdout') for (const line of lines(got.value.text)) note(line)+ }+ } catch {+ return null+ }+}++// The cells of request `id`: a file in <cache>/ask (never a link), ASK_MAX+// bytes at most; null for a bad id, no such file or a larger one.+export async function askRequest($: EngineInterface, id: string): Promise<string | null> {+ if (!S.askFifo || !ASK_ID.test(id)) return null+ const path = `${askDir()}/${id}`+ const stat = await fsStat($, path).catch(() => null)+ if (stat?.kind !== 'file' || stat.isLink || stat.size > ASK_MAX) return null+ return fsRead($, path).catch(() => null)+}++// Answers request `id` with `text`: false when the shim gave up (or the write failed).+export async function answerAsk($: EngineInterface, id: string, text: string): Promise<boolean> {+ if (!S.askFifo || !ASK_ID.test(id)) return false+ const got = await processRun($, ['sh', '-c', ANSWER_SH, 'sh', `${askDir()}/${id}`], { stdin: text, timeoutMs: 5000 }).catch(() => null)+ return got?.exitCode === 0+}++// After a give-up of the shim of request `id` (its mark): removes its files.+export async function askGaveUp($: EngineInterface, id: string) {+ if (!S.askFifo || !ASK_ID.test(id)) return+ await processRun($, ['sh', '-c', ASK_CLEAN_SH, 'sh', `${askDir()}/${id}`], { timeoutMs: 5000 }).catch(() => undefined)+}++// The session ends (not a /clear or a resume): the reader stops, and the+// FIFO, this process's request files and its shims folder go.+export async function endAsks($: EngineInterface) {+ asker?.stop()+ asker = { ended: true, final: true, stop: () => {} }+ const fifo = S.askFifo+ if (!fifo) return+ const dir = S.shimDir+ S.askFifo = ''+ S.shimDir = ''+ await processRun($, ['sh', '-c', ASK_END_SH, 'sh', fifo, `${fifo.slice(0, fifo.lastIndexOf('/'))}/ask`, fifo.slice(fifo.lastIndexOf('-') + 1), dir], { timeoutMs: 5000 })+ .catch(() => undefined)+}++// The automatic copy now, for /iui details and dump: the shims and the+// reader of their asks (`asking`). `isFullscreen`: null before a render hook+// says; on the main screen the terminal copies, not Claude Code. The watcher+// (`none`: it printed `clip none`; `way` on Linux) only gives the /iui grab+// hint for the other copies.export function autoCopyText(o: {- isMac: boolean; hostOs: string; isRemote: boolean; isOn: boolean; isFullscreen: boolean | null; none: boolean; drawn: boolean; running: boolean; way: ClipWay | null+ isMac: boolean; hostOs: string; isRemote: boolean; isOn: boolean; isFullscreen: boolean | null; none: boolean; drawn: boolean; asking: boolean; way: ClipWay | null+ shims: ShimState | null}): string {if (!o.isMac && o.hostOs !== 'Linux') return 'off (not macOS or Linux)'if (o.isRemote) return 'off (ssh)'- if (!o.isMac && o.way && 'off' in o.way) return `off (${o.way.off})`- if (!o.isMac && !o.way) return 'off (the clipboard tools are not looked up yet)'+ if (!o.shims) return 'off (the clipboard shims are not set up yet)'+ if ('off' in o.shims) return `off (${o.shims.off})`if (!o.isOn) return 'off (Intelligent UI is off)'if (o.isFullscreen === false) return 'off on the main screen (use /iui grab)'- if (o.none) {- if (o.isMac) return 'off (no PerlObjCBridge in /usr/bin/perl)'- const way = o.way as Extract<ClipWay, { watch: string[] }>- return `off (${way.name} stopped${way.wayland ? ': the compositor may not have the data-control protocol' : ''})`- }if (!o.drawn) return 'off (no picture yet)'- if (o.isFullscreen === null) return 'off (fullscreen or main screen is not known yet)'- if (!o.running) return 'off (the watcher does not run)'- return o.isMac ? 'on' : `on, with ${(o.way as Extract<ClipWay, { name: string }>).name}`+ if (!o.asking) return 'off (the ask reader does not run)'+ const hint = hintOffOf(o)+ if (hint) return `on (no /iui grab hint: ${hint})`+ return o.isMac || !o.way || !('name' in o.way) ? 'on' : `on, with ${o.way.name}`+}++// Why the watcher gives no /iui grab hint; '' when it does.+function hintOffOf(o: { isMac: boolean; none: boolean; way: ClipWay | null }): string {+ if (!o.isMac && o.way && 'off' in o.way) return o.way.off+ if (!o.none) return ''+ if (o.isMac || !o.way || 'off' in o.way) return 'no PerlObjCBridge in /usr/bin/perl'+ return `${o.way.name} stopped${o.way.wayland ? ': the compositor may not have the data-control protocol' : ''}`}diff --git a/hooks/lib/command.ts b/hooks/lib/command.tsindex 396879d..98639ec 100644--- a/hooks/lib/command.ts+++ b/hooks/lib/command.ts@@ -7,14 +7,14 @@ import {type Height, type Width,} from './settings'import { isThemeName, type ThemeInfo } from './themes'-import { commandRegister } from './api'+import { commandAdd } from './api'// `/iui` arguments: on, off, size <80-200>, width [40-400|full],-// height [6-200|half|full], grid [on|off], updates [on|off], details (all-// Intelligent UI found), grab (copy the text of the last mouse selection of-// pictures), dump (how the last clipboard with picture cells was read), or-// nothing (show state). A `width`, `height`, `grid` or `updates` with no-// value shows it.+// height [6-200|half|full], grid [on|off], select [on|off], updates+// [on|off], details (all Intelligent UI found), grab (copy the text of the+// last mouse selection of pictures), dump (how the last clipboard with+// picture cells was read), or nothing (show state). A `width`, `height`,+// `grid`, `select` or `updates` with no value shows it.export type Command =| { kind: 'on' | 'off' | 'show' | 'details' | 'probe' | 'grab' | 'dump' }| { kind: 'size'; percent: number }@@ -22,6 +22,7 @@ export type Command =| { kind: 'height'; height?: Height }| { kind: 'grid'; grid?: boolean }| { kind: 'updates'; on?: boolean }+ | { kind: 'select'; on?: boolean }| { kind: 'theme'; name?: string }| { kind: 'setup'; remove: boolean }| { kind: 'bad' }@@ -57,6 +58,8 @@ export function parseCommand(args: string): Command {}if (words[0] === 'grid' && words.length === 1) return { kind: 'grid' }if (words[0] === 'grid' && words.length === 2 && (words[1] === 'on' || words[1] === 'off')) return { kind: 'grid', grid: words[1] === 'on' }+ if (words[0] === 'select' && words.length === 1) return { kind: 'select' }+ if (words[0] === 'select' && words.length === 2 && (words[1] === 'on' || words[1] === 'off')) return { kind: 'select', on: words[1] === 'on' }if (words[0] === 'updates' && words.length === 1) return { kind: 'updates' }if (words[0] === 'updates' && words.length === 2 && (words[1] === 'on' || words[1] === 'off')) return { kind: 'updates', on: words[1] === 'on' }return { kind: 'bad' }@@ -71,6 +74,7 @@ const SUBCOMMANDS = [{ name: 'width', description: `the most columns a picture takes, ${MIN_WIDTH}-${MAX_WIDTH} or full` },{ name: 'height', description: `the most rows an image or a Typst drawing takes, ${MIN_HEIGHT}-${MAX_HEIGHT}, half or full` },{ name: 'grid', description: 'each line of text on whole terminal rows, on or off' },+ { name: 'select', description: 'select and copy picture text with the mouse, on or off' },{ name: 'updates', description: 'look for a new version once a day, on or off' },{ name: 'theme', description: 'list the themes, or switch to one' },{ name: 'setup', description: 'iTerm2: add the shell line that turns pictures on' },@@ -83,6 +87,7 @@ const WIDTHS: readonly Width[] = [80, 100, 120, 160, 200, 'full']const HEIGHTS: readonly Height[] = ['half', 'full', 12, 24, 36]const GRIDS = ['on', 'off'] as constconst UPDATES = ['on', 'off'] as const+const SELECTS = ['on', 'off'] as const// Typeahead rows for `/iui` arguments: the word at the cursor (`token`,// starting at `start` in `text`) completed as a subcommand, a theme name, a@@ -96,7 +101,7 @@ const UPDATES = ['on', 'off'] as constexport type Suggestion = { text: string; label?: string; description?: string }export function commandSuggestions(text: string, token: string, start: number,- o: { themes: readonly ThemeInfo[]; active: string; size: number; width?: Width; height?: Height; grid?: boolean; updates?: boolean },+ o: { themes: readonly ThemeInfo[]; active: string; size: number; width?: Width; height?: Height; grid?: boolean; updates?: boolean; select?: boolean },): Suggestion[] {if (token === '/iui' && /^\s*$/.test(text.slice(0, start))) {return SUBCOMMANDS.map(c => ({ text: `/iui ${c.name}`, label: c.name, description: c.description }))@@ -141,6 +146,10 @@ export function commandSuggestions(const updateRows = (prefix: string, lead: string) => UPDATES.filter(u => u.startsWith(prefix)).map(u => ({ text: `${lead}${u}`, label: u, description: u === updates ? 'in use' : u === 'on' ? 'the default' : undefined }))+ const select = o.select === false ? 'off' : 'on'+ const selectRows = (prefix: string, lead: string) => SELECTS+ .filter(v => v.startsWith(prefix))+ .map(v => ({ text: `${lead}${v}`, label: v, description: v === select ? 'in use' : v === 'on' ? 'the default' : undefined }))if (before.length === 0) {if (word === 'theme') return themeRows('', 'theme ')if (word === 'height') return heightRows('', 'height ')@@ -148,6 +157,7 @@ export function commandSuggestions(if (word === 'width') return widthRows('', 'width ')if (word === 'grid') return gridRows('', 'grid ')if (word === 'updates') return updateRows('', 'updates ')+ if (word === 'select') return selectRows('', 'select ')return SUBCOMMANDS.filter(c => c.name.startsWith(word)).map(c => ({ text: c.name, description: c.description }))}if (before.length === 1 && before[0] === 'theme') return themeRows(word, '')@@ -156,15 +166,16 @@ export function commandSuggestions(if (before.length === 1 && before[0] === 'height') return heightRows(word, '')if (before.length === 1 && before[0] === 'grid') return gridRows(word, '')if (before.length === 1 && before[0] === 'updates') return updateRows(word, '')+ if (before.length === 1 && before[0] === 'select') return selectRows(word, '')return []}// /iui; registered at session start and again in setUp, so it is// there after a hot reload too.export async function registerCommand($: EngineInterface) {- await commandRegister($, {+ await commandAdd($, {name: 'iui',- description: "Draw Claude's answers as rendered Markdown pictures: on, off, size <80-200> (text size in % of the terminal's), width [40-400|full] (the most columns a picture takes), height [6-200|half|full] (the most rows an image or a Typst drawing takes), grid [on|off] (each line of text on whole terminal rows), updates [on|off] (look for a new version once a day), theme [name] (list or switch themes), grab (copy the text of the selected pictures, or of the picture cells on the clipboard), dump (copy diagnostics)",- argumentHint: '[on|off|size <percent>|width [columns|full]|height [rows|half|full]|grid [on|off]|updates [on|off]|theme [name]|grab|dump]',+ description: "Draw Claude's answers as rendered Markdown pictures: on, off, size <80-200> (text size in % of the terminal's), width [40-400|full] (the most columns a picture takes), height [6-200|half|full] (the most rows an image or a Typst drawing takes), grid [on|off] (each line of text on whole terminal rows), select [on|off] (select and copy picture text with the mouse), updates [on|off] (look for a new version once a day), theme [name] (list or switch themes), grab (copy the text of the selected pictures, or of the picture cells on the clipboard), dump (copy diagnostics)",+ argumentHint: '[on|off|size <percent>|width [columns|full]|height [rows|half|full]|grid [on|off]|select [on|off]|updates [on|off]|theme [name]|grab|dump]',}).catch(() => undefined)}diff --git a/hooks/lib/draw.tsx b/hooks/lib/draw.tsxindex 5e81c42..f35cbad 100644--- a/hooks/lib/draw.tsx+++ b/hooks/lib/draw.tsx@@ -11,6 +11,8 @@ import { DEFAULT_GRID, gridFirstRow, type GridMetrics } from './grid'import { FENCE, imagePathOf } from './markdown'import { linksOf, type LinkItem } from './links'import { fitBox, fitsInline, roomColumns } from './terminal'+import { overlayRows, picked, redrawSelection, selectColor, selectedRows, selectOn, stripFile, stripPlan, type SelRow } from './select'+import { cutStrips, hasStrips } from './strips'import { fsExists, fsRead, uiCopy, uiResolve, uiToast } from './api'// FNV-1a over the parts, twice with different offsets: 16 hex digits.@@ -178,8 +180,9 @@ export async function inlineOf($: EngineInterface, result: Result): Promise<stri// How a set of pages sits: `bullet` the reply bullet in the gutter (on the// first line's row), `gutter` its width (0: none), `marginTop` the rows above,// `keys` a prefix for its Images' and Buttons' keys (several sets in one-// tree), `links` the link rows under the pages (one row each).-export type PagesLayout = { bullet?: boolean; gutter?: number; marginTop?: number; keys?: string; links?: readonly ShownLink[] }+// tree), `links` the link rows under the pages (one row each), `id` the+// drawing's id in drawnAnswers (its pages take picture select).+export type PagesLayout = { bullet?: boolean; gutter?: number; marginTop?: number; keys?: string; links?: readonly ShownLink[]; id?: string }// The bullet's row when the page is drawn `factor` as wide.export function scaledBulletRow(bulletRow: number, factor: number): number {@@ -194,11 +197,22 @@ export function scaledBulletRow(bulletRow: number, factor: number): number {export function drawPages($: EngineInterface, e: ResolveInput<RenderComponent, 'terminal'> & { viewport?: RenderViewport }, result: Result, inline?: readonly string[], layout: PagesLayout = {},) {- const { Box, Text, Image } = uiResolve($, e)+ const table = uiResolve($, e)+ const { Box, Text, Image } = table+ // Absent in an engine with no Client element.+ const Client: typeof table.Client | undefined = table.Clientconst gutter = layout.gutter ?? GUTTERconst room = roomColumns(e.viewport)const first = result.pages[0]const factor = first && first.columns > room ? room / first.columns : 1+ const keys = layout.keys ?? ''+ const boxes = result.pages.map(page => fitBox(page, room))+ // Picture select: each page in a Box of its size, the selected rows over+ // it, and last the Client that takes the pointer (select.ts). Both in the+ // flow, pulled up over the page (marginTop), never absolute (overlayRows).+ const id = layout.id+ const cell = id && !inline ? selectOn({ hasClient: !!Client, isFullscreen: e.viewport?.isFullscreen }) : null+ const sel = id && cell && picked.now?.id === id ? selectedRows(picked.now, boxes) : nullreturn (<Box flexDirection="row" marginTop={layout.marginTop ?? MARGIN_TOP}>{gutter > 0 ? (@@ -208,25 +222,63 @@ export function drawPages() : null}<Box flexDirection="column" flexShrink={0}>{result.pages.map((page, i) => {- const box = fitBox(page, room)+ const box = boxes[i]!const shrunk = box.columns !== page.columns- return (+ const image = (<Image- key={`${layout.keys ?? ''}p${i}`}+ key={`${keys}p${i}`}source={inline ? { png: inline[i]! } : { file: page.file, format: 'png', ...(shrunk ? { generation: box.columns * 1000 + box.rows } : {}) }}columns={box.columns}rows={box.rows}alt={page.alt}/>)+ const plan = cell ? stripPlan(page, box, cell) : null+ if (!plan || !Client || !id) return image+ return (+ <Box key={`${keys}w${i}`} flexDirection="column" width={box.columns} height={box.rows} flexShrink={0}>+ {image}+ <Box key={`${keys}o${i}`} flexDirection="column" marginTop={-box.rows} width={box.columns} height={box.rows} flexShrink={0}>+ {drawSelected($, e, page, { ...plan, columns: box.columns }, sel?.[i] ?? [], `${keys}${i}`)}+ </Box>+ <Box key={`${keys}c${i}`} marginTop={-box.rows} width={box.columns} height={box.rows} flexShrink={0}>+ <Client key={`${keys}sel${i}`} module="../sel.tsx" width={box.columns} height={box.rows} props={{ id, page: i }} />+ </Box>+ </Box>+ )})}{result.note ? <Text dimColor wrap="truncate-end">Typst error: {result.note}</Text> : null}- {layout.links?.length ? drawLinks($, e, layout.links, layout.keys ?? '') : null}+ {layout.links?.length ? drawLinks($, e, layout.links, keys) : null}</Box></Box>)}+// The selected rows of a page ("clip, do not crop"): each row's strip (the+// page's own row) in a Box of the selected columns on the selection colour,+// shifted left so its cells stay over the page's. The page is transparent,+// so the colour shows under its text. Each row in the flow (overlayRows).+// Nothing while the strips are cut: they draw once there.+function drawSelected(+ $: EngineInterface, e: ResolveInput<RenderComponent, 'terminal'>, page: { file: string; width: number; height: number },+ plan: { rowPx: number; rows: number; columns: number }, rows: readonly SelRow[], keys: string,+) {+ if (!rows.length || !S.env) return null+ if (!hasStrips(page.file, plan.rowPx)) {+ cutStrips($, S.env, page, plan, () => redrawSelection($))+ return null+ }+ const { Box, Image } = uiResolve($, e)+ const bg = selectColor(S.theme, picked.gen)+ return overlayRows(rows).map(r => (+ <Box key={`${keys}r${r.row}`} marginTop={r.top} marginLeft={r.x0} width={r.x1 - r.x0 + 1} height={1} flexShrink={0} overflow="hidden" backgroundColor={bg}>+ <Box marginLeft={-r.x0} width={plan.columns} flexShrink={0}>+ <Image key={`${keys}s${r.row}`} source={{ file: stripFile(page.file, plan.rows, r.row), format: 'png' }} columns={plan.columns} rows={1} alt=" " />+ </Box>+ </Box>+ ))+}+// ---- Links ------------------------------------------------------------------// A click on a picture cannot open a link (an Image is a leaf: no Link holds// it, and a press on it gives no cell). So the links, file paths and commandsdiff --git a/hooks/lib/env.ts b/hooks/lib/env.tsindex c90f444..ae7bdc5 100644--- a/hooks/lib/env.ts+++ b/hooks/lib/env.ts@@ -16,20 +16,23 @@ import { registerCommand } from './command'import { readHostMode, termFontOf, watchHost } from './host'import { readFrame, setFrame, watchFrame } from './frame'import { offerSetup } from './setup'-import { findClipWay } from './clipboard'+import { findClipWay, noShims, setUpShims } from './clipboard'import { findTypst, measureFonts } from './typst'import { checkUpdates } from './updates'-import { configList, envGet, fsList, fsStat, pluginRoot, processRun, sessionCwd, storeGet, uiToast } from './api'+import { configs, envGet, fsList, fsStat, pluginRoot, processRun, sessionCwd, sessionVersion, storeGet, uiToast } from './api'// The `isOn` atom, which setUp turns off when the stored setting says so (a// copy of its own: see state.ts).const isOn = atom({ plugin: 'cc-intelligent-ui', key: 'isOn' } as const, true)-// Makes the cache folder, drops key folders not touched for 2 days, makes-// the designs folder (outside the tmp cache) and drops design files not-// touched for DESIGNS_KEPT_DAYS; prints the cache folder, the OS name and-// the designs folder.-export const CACHE_SH = `d="\${TMPDIR:-/tmp}"; d="\${d%/}/cc-intelligent-ui-$(id -u)"; g="\${XDG_CACHE_HOME:-$HOME/.cache}/cc-intelligent-ui/designs"; mkdir -p "$d" && chmod 700 "$d" && { find "$d" -mindepth 1 -maxdepth 1 -type d -mtime +2 ! -name pkg-cache -exec rm -rf {} + 2>/dev/null; mkdir -p "$g" 2>/dev/null && find "$g" -type f -mtime +${DESIGNS_KEPT_DAYS} -delete 2>/dev/null; printf '%s\\n%s\\n%s\\n' "$d" "$(uname -s)" "$g"; }`+// Makes the cache folder (stops when it is a link or not this user's),+// drops key folders not touched for 2 days (not the asks and shims of a+// live process), the ask FIFOs, request files and shims folders of a pid+// that is gone (and request files of 2 days), makes the designs folder+// (outside the tmp cache) and drops design files not touched for+// DESIGNS_KEPT_DAYS; prints the cache folder, the OS name and the designs+// folder (the OS name alone when the cache folder can not be made private).+export const CACHE_SH = `d="\${TMPDIR:-/tmp}"; d="\${d%/}/cc-intelligent-ui-$(id -u)"; g="\${XDG_CACHE_HOME:-$HOME/.cache}/cc-intelligent-ui/designs"; mkdir "$d" 2>/dev/null; [ ! -L "$d" ] && [ -d "$d" ] && [ -O "$d" ] && chmod 700 "$d" && { find "$d" -mindepth 1 -maxdepth 1 -type d -mtime +2 ! -name pkg-cache ! -name ask ! -name 'shims-*' -exec rm -rf {} + 2>/dev/null; for f in "$d"/ask-* "$d"/ask/* "$d"/shims-*; do n=\${f##*/}; n=\${n#ask-}; n=\${n#shims-}; n=\${n%%-*}; case $n in ''|*[!0-9]*) continue ;; esac; kill -0 "$n" 2>/dev/null || rm -rf -- "$f"; done; find "$d/ask" -type f -mtime +2 -delete 2>/dev/null; mkdir -p "$g" 2>/dev/null && find "$g" -type f -mtime +${DESIGNS_KEPT_DAYS} -delete 2>/dev/null; printf '%s\\n%s\\n%s\\n' "$d" "$(uname -s)" "$g"; } || printf '\\n%s\\n\\n' "$(uname -s)"`// Once per load: the cache folder, Typst, the terminal and its font, the// theme, the cell. A failed setup is tried again on the next call.@@ -45,19 +48,20 @@ export function setUp($: EngineInterface): Promise<Env> {const program = (await envGet($, 'TERM_PROGRAM')) ?? ''const term = (await envGet($, 'TERM')) ?? ''const ssh = !!(await envGet($, 'SSH_CONNECTION')) || !!(await envGet($, 'SSH_TTY'))+ const background = (await envGet($, 'CLAUDE_CODE_SESSION_KIND')) === 'bg'const canDraw = canDrawIn({program,term,kitty: !!(await envGet($, 'KITTY_WINDOW_ID')),tmux: !!(await envGet($, 'TMUX')) || !!(await envGet($, 'STY')) || /^tmux|^screen/i.test(term),ssh,- background: (await envGet($, 'CLAUDE_CODE_SESSION_KIND')) === 'bg',+ background,engineForced: isSet(await envGet($, 'CLAUDE_CODE_FORCE_TERMINAL_IMAGES')),modForced: isSet(await envGet($, 'IUI_FORCE')),})const root = pluginRoot($)const template = await fsStat($, `${root}/render/md.typ`).catch(() => null)- const rows = await configList($).catch(() => [])+ const rows = await configs($).catch(() => [])const raw = rows.find(row => row.key === 'theme')?.valueS.settingRaw = typeof raw === 'string' ? raw : ''S.setting = settingOf(raw)@@ -67,6 +71,14 @@ export function setUp($: EngineInterface): Promise<Env> {S.itermProfile = (await envGet($, 'ITERM_PROFILE')) || undefinedS.isKitty = /kitty/i.test(term) || !!(await envGet($, 'KITTY_WINDOW_ID')) || !!(await envGet($, 'KITTY_PID'))S.isRemote = ssh+ // On this computer, where pictures draw: the clipboard shims, before Claude Code copies.+ if (!ssh && (S.hostOs === 'Darwin' || S.hostOs === 'Linux')) {+ if (background) noShims('a background session')+ else if (!canDraw) noShims('no pictures in this terminal')+ // Never in the shared fallback folder.+ else if (made1.trim()) await setUpShims($, cache, S.hostOs === 'Darwin')+ else noShims()+ }S.debug = isSet(await envGet($, 'IUI_DEBUG'))S.isIterm = isItermIn({program,@@ -90,6 +102,9 @@ export function setUp($: EngineInterface): Promise<Env> {S.height = height === 'half' || height === 'full' ? height: Number.isInteger(height) && Number(height) >= MIN_HEIGHT && Number(height) <= MAX_HEIGHT ? Number(height) : DEFAULT_HEIGHTS.grid = (await storeGet($, 'grid').catch(() => undefined)) !== false+ S.select = (await storeGet($, 'select').catch(() => undefined)) !== false+ // An engine with no version call is older than picture select.+ S.engine = await Promise.resolve().then(() => sessionVersion($)).then(v => v.base ?? v.version, () => '')if ((await storeGet($, 'enabled').catch(() => undefined)) === false) await update($, isOn, () => false).catch(() => undefined)const isMac = os.trim() === 'Darwin'// Linux, on this computer: the clipboard tools, for the copy.diff --git a/hooks/lib/frame.ts b/hooks/lib/frame.tsindex 24a31f7..d28c843 100644--- a/hooks/lib/frame.ts+++ b/hooks/lib/frame.ts@@ -16,7 +16,7 @@ import { columnsFor, isGhosttyHere, sizeOf } from './terminal'import { CELL_SH, perlFor, termFontOf, WATCH_PL } from './host'import { clipWatchArgs } from './clipboard'import { measureFonts } from './typst'-import { clockAfter, processRun, processSpawn } from './api'+import { clockAfter, processRun, spawn } from './api'export type { Cell, Frame } from './state'@@ -253,7 +253,7 @@ export function watchFrame($: EngineInterface, clip?: ((count: number | null, fiwatcher = metry {const isMac = S.hostOs === 'Darwin'- const child = processSpawn($, { argv: [perlFor(isMac), '-e', WATCH_PL, ...(isClip ? ['clip', ...(clipWatchArgs(isMac) ?? [])] : [])] })+ const child = spawn($, { argv: [perlFor(isMac), '-e', WATCH_PL, ...(isClip ? ['clip', ...(clipWatchArgs(isMac) ?? [])] : [])] })void followWatcher(child, line => {if ('pid' in line) {pid = line.piddiff --git a/hooks/lib/pictures.tsx b/hooks/lib/pictures.tsxindex 852f052..71e8c15 100644--- a/hooks/lib/pictures.tsx+++ b/hooks/lib/pictures.tsx@@ -22,7 +22,7 @@ import { convertCall, logDebug, parseOd, pngSize } from './typst'import { pagesOf, type Drawing } from './answer'import { HEADERS_SH } from './host'import { frameFor, pictureColumns, shownFor, type Frame } from './frame'-import { clockAfter, fsExists, fsList, fsRead, fsStat, fsWrite, processRun, sessionId, sessionRoot, uiClose, uiInvalidate, uiOpen, uiResolve } from './api'+import { clockAfter, fsExists, fsList, fsRead, fsStat, fsWrite, processRun, sessionId, sessionRoot, uiClose, invalidate, uiOpen, uiResolve } from './api'export type Picture = { file: string; stamp: string; bytes: number; width: number; height: number }export type Shown = { source: ImageSource; columns: number; rows: number; alt: string }@@ -408,7 +408,7 @@ async function askPage($: EngineInterface, line: PageLine) {pageAsk.now = { row: line.row, file: line.file, box: line.box, value: String(line.page), note: '' }await uiOpen($, { id: PAGE_PANE, title: 'Page', focus: true, closeOnEscape: true, rows: 2, columns: 24 }).catch(() => undefined)// An open field (another row's) draws the new page.- uiInvalidate($, 'ui.render')+ invalidate($, 'ui.render')}// Enter in the page field: a page that exists shows in its row and the@@ -426,7 +426,7 @@ async function submitPage($: EngineInterface, text: string) {if (!page || made !== true) {const note = !page ? 'Type a page number' : made === false ? `No page ${page}` : `Page ${page}: ${made ?? 'Typst could not make it'}`pageAsk.now = { ...mine, note }- uiInvalidate($, 'ui.render')+ invalidate($, 'ui.render')return}keep(pdfPages, ask.row, page, PICTURES_KEPT)@@ -532,7 +532,7 @@ export function underRow($: EngineInterface, e: RowInput, row: RenderElement, unif (drawing.kind === 'blank') return <Box key={`blank-${i}`} />return (<Box key={`pages-${i}`} flexDirection="column">- {drawPages($, e, drawing.result, drawing.bytes, { gutter: 0, marginTop: 0, keys: `file${i}-`, links: s.links })}+ {drawPages($, e, drawing.result, drawing.bytes, { gutter: 0, marginTop: 0, keys: `file${i}-`, links: s.links, id: s.id })}</Box>)})}diff --git a/hooks/lib/plan.tsx b/hooks/lib/plan.tsxindex 8d5eabf..79e6dda 100644--- a/hooks/lib/plan.tsx+++ b/hooks/lib/plan.tsx@@ -15,7 +15,7 @@ import { S, writesLive } from './state'import { firstLine } from './markdown'import type { Drawing } from './answer'import { drawPages, type ShownLink } from './draw'-import { clockAfter, commandRun, envGet, fsRead, processRun, promptSubmit, turnAbort, uiAsk, uiResolve } from './api'+import { clockAfter, commandRun, envGet, fsRead, processRun, submit, turnAbort, uiAsk, uiResolve } from './api'// The question's chip and its two wordings.export const PLAN_HEADER = 'Plan'@@ -269,7 +269,7 @@ export function scheduleClear($: EngineInterface, plan: string, turnId: string)clockAfter($, CLEAR_DELAY_MS, async () => {if (turnId) await turnAbort($, { turnId }).catch(() => undefined)await commandRun($, { command: 'clear' }).catch(() => undefined)- await promptSubmit($, { text: `${PLAN_IMPLEMENT}\n\n${plan}`, asUser: true }).catch(() => undefined)+ await submit($, { text: `${PLAN_IMPLEMENT}\n\n${plan}`, asUser: true }).catch(() => undefined)})}@@ -279,15 +279,15 @@ type Terminal = ResolveInput<RenderComponent, 'terminal'> & { viewport?: RenderV// The ExitPlanMode ToolUse row: the engine's row, then the plan's pages and// their link rows (a dim line while they compile; the engine's row alone-// when they fail).-export function drawPlanUse($: EngineInterface, e: Terminal, row: RenderElement, drawing: Drawing, links: readonly ShownLink[] = []): RenderElement {+// when they fail). `id`: the drawing's (picture select).+export function drawPlanUse($: EngineInterface, e: Terminal, row: RenderElement, drawing: Drawing, links: readonly ShownLink[] = [], id?: string): RenderElement {if (drawing.kind === 'text') return rowconst { Box, Text } = uiResolve($, e)return (<Box flexDirection="column">{row}{drawing.kind === 'pages' ? (- drawPages($, e, drawing.result, drawing.bytes, { bullet: true, links })+ drawPages($, e, drawing.result, drawing.bytes, { bullet: true, links, ...(id ? { id } : {}) })) : (<Box marginTop={1} marginLeft={GUTTER}><Text dimColor>{PLAN_DRAWING}</Text>diff --git a/hooks/lib/select.ts b/hooks/lib/select.tsnew file mode 100644index 0000000..16b3bed--- /dev/null+++ b/hooks/lib/select.ts@@ -0,0 +1,212 @@+// Picture select: a mouse selection over the pictures of an answer, a plan+// or a Markdown file Claude sent. Since Claude Code 2.1.305 its own+// selection skips an Image's cells (no highlight, no text), so a Client+// (hooks/sel.tsx) over each page takes the pointer and posts the selection;+// the hooks module draws it over the page and copies its text on the+// button's up. The highlight is "clip, do not crop": each selected row is+// the page's own row (a strip PNG, strips.ts) on the selection colour, cut+// to the selected columns. The pure parts are here, with the selection now.+import type { EngineInterface } from 'claude-code'++import type { IuiTheme } from '../../types'+import { SELECT_SINCE } from './settings'+import { S, type Cell } from './state'+import { compareVersions } from './updates'+import { joinSegments, pageTopsOf, textOfRun, type Drawn, type Item, type RunLine, type TextBlock } from './copy'+import { invalidate } from './api'++export type Point = { x: number; y: number }+export type SelectPhase = 'down' | 'move' | 'up' | 'clear'+// A post of the Client (hooks/sel.tsx): the drawing's id (drawnAnswers),+// the page whose region posted, the anchor (where the button went down)+// and the head (the pointer now) in that region's cells, and the post's+// order (one count for every instance). `size`: the region's columns and+// rows as the Client has them (for the trace).+export type SelectPost = { id: string; page: number; anchor: Point; head: Point; phase: SelectPhase; seq: number; size?: Box }+// The selection: anchor and head in the drawing's rows (the page tops+// added), `isUp` once the button came up.+export type Selection = { id: string; anchor: Point; head: Point; isUp: boolean }+// One selected row of a page: its row in the page and its first and last+// columns (inclusive).+export type SelRow = { row: number; x0: number; x1: number }+type Box = { columns: number; rows: number }++// The one selection over the pictures (null: none), the order of the last+// post taken, its redraws (`gen`: the overlay colour toggles with it), and+// a Client that failed (`isBroken`: no select until a reload).+export const picked = { now: null as Selection | null, seq: 0, gen: 0, isBroken: false }++// Draws the selection again (its colour one step off: selectColor).+export function redrawSelection($: EngineInterface) {+ picked.gen+++ invalidate($, 'ui.render')+}++// The selection's part of a drawing's memo: its redraw count while the+// selection is on it, '' otherwise.+export const selStamp = (id: string) => (picked.now?.id === id ? `sel${picked.gen}` : '')++const isInt = (v: unknown): v is number => typeof v === 'number' && Number.isInteger(v)+const pointOf = (v: unknown): Point | null => {+ const p = v as Partial<Point> | null+ return p && isInt(p.x) && isInt(p.y) ? { x: p.x, y: p.y } : null+}++// A Client's post as a SelectPost; null when it is not one (data from code).+export function postOf(data: unknown): SelectPost | null {+ const d = data as Partial<Record<keyof SelectPost, unknown>> | null+ if (!d || typeof d !== 'object' || typeof d.id !== 'string' || !isInt(d.page) || d.page < 0 || !isInt(d.seq)) return null+ const anchor = pointOf(d.anchor)+ const head = pointOf(d.head)+ const phase = d.phase+ if (!anchor || !head || (phase !== 'down' && phase !== 'move' && phase !== 'up' && phase !== 'clear')) return null+ const size = d.size as Partial<Box> | null | undefined+ const known = size && isInt(size.columns) && isInt(size.rows) ? { size: { columns: size.columns, rows: size.rows } } : {}+ return { id: d.id, page: d.page, anchor, head, phase, seq: d.seq, ...known }+}++// The row each page starts at in its drawing (the pages one under the other).+export function topsOf(pages: readonly Box[]): number[] {+ let at = 0+ return pages.map(p => (at += p.rows) - p.rows)+}++// The selection after a post: a down, a move or an up selects from its+// anchor to its head (a down on another picture drops the old one); a+// clear (a click with no drag) leaves none. `tops`: the drawing's page tops.+export function selectionAfter(post: SelectPost, tops: readonly number[]): Selection | null {+ const top = tops[post.page]+ if (post.phase === 'clear' || top === undefined) return null+ const at = (p: Point) => ({ x: p.x, y: p.y + top })+ return { id: post.id, anchor: at(post.anchor), head: at(post.head), isUp: post.phase === 'up' }+}++// The selected cells of each page in stream order: the first row from the+// first point's column to its end, the rows between whole, the last row to+// the last point's column. A point above the drawing is its first cell, one+// below it its last. Anchor and head on one cell select nothing.+export function selectedRows(sel: { anchor: Point; head: Point }, pages: readonly Box[]): SelRow[][] {+ const out = pages.map((): SelRow[] => [])+ const total = pages.reduce((n, p) => n + p.rows, 0)+ if (!total || (sel.anchor.x === sel.head.x && sel.anchor.y === sel.head.y)) return out+ const before = (p: Point, q: Point) => p.y < q.y || (p.y === q.y && p.x < q.x)+ const [first, last] = before(sel.head, sel.anchor) ? [sel.head, sel.anchor] : [sel.anchor, sel.head]+ const inside = (p: Point): Point => (p.y < 0 ? { x: 0, y: 0 } : p.y >= total ? { x: Number.MAX_SAFE_INTEGER, y: total - 1 } : p)+ const a = inside(first)+ const b = inside(last)+ topsOf(pages).forEach((top, i) => {+ const { columns, rows } = pages[i]!+ const clamp = (x: number) => Math.max(0, Math.min(columns - 1, x))+ for (let y = Math.max(a.y, top); y <= Math.min(b.y, top + rows - 1); y++) {+ const x0 = y === a.y ? clamp(a.x) : 0+ const x1 = y === b.y ? clamp(b.x) : columns - 1+ if (x1 >= x0) out[i]!.push({ row: y - top, x0, x1 })+ }+ })+ return out+}++// The selected rows as the overlay draws them, in the flow of a Box over+// the page: each row `top` rows under the end of the one before (the first+// under the page's top). Not absolute Boxes: the engine moves an absolute+// Box whose top is above the screen down to row 0, children and pointer+// too, so a page scrolled part away had its rows and its Client too high.+export function overlayRows(rows: readonly SelRow[]): (SelRow & { top: number })[] {+ let next = 0+ return rows.map(r => {+ const top = r.row - next+ next = r.row + 1+ return { ...r, top }+ })+}++// The rows of a page scrolled away above the viewport: the message's first+// row on screen less the page's top in the message, 0 to the page's rows.+// Undefined when either is not known.+export function rowsAbove(first: number | undefined, top: number | undefined, rows: number): number | undefined {+ return first === undefined || top === undefined ? undefined : Math.max(0, Math.min(rows, first - top))+}++// The IUI_DEBUG trace of a post: the post as the Client sent it (its+// region's size), the drawing's last onScreen, the page's top in the+// message and its rows scrolled away (the Client's y counts from the+// page's top, scrolled or not: no offset is added), and the rows selected.+export function selectTrace(post: SelectPost, d: Drawn | undefined, sel: Selection | null): string {+ const p = (q: Point) => `${q.x},${q.y}`+ const size = post.size ? `${post.size.columns}x${post.size.rows}` : '?'+ const head = `select: ${post.phase} #${post.seq} ${post.id} page ${post.page} anchor ${p(post.anchor)} head ${p(post.head)} size ${size}`+ if (!d) return `${head} (no drawing)`+ const box = d.pages[post.page]+ const on = d.onScreen === undefined ? '?' : d.onScreen === null ? 'off' : `${d.onScreen.first}-${d.onScreen.last}/${d.onScreen.of}`+ const top = pageTopsOf(d)?.[post.page]+ const above = rowsAbove(d.onScreen?.first, top, box?.rows ?? 0)+ const drawn = box ? `${box.columns}x${box.rows}` : '?'+ const rows = sel ? selectedRows(sel, d.pages).flatMap((lines, i) => lines.length ? [`p${i} ${lines[0]!.row}..${lines[lines.length - 1]!.row} (${lines.length})`] : []) : []+ return `${head} drawn ${drawn} onScreen ${on} top ${top ?? '?'} above ${above ?? '?'} rows ${rows.join(', ') || 'none'}`+}++// The selection's runs, page by page: the lines a copy of the same cells+// decodes to (decodeSelection), so its text is read the same way.+export function runsOf(rows: readonly (readonly SelRow[])[]): { page: number; lines: RunLine[] }[] {+ return rows.flatMap((lines, page) => (lines.length ? [{ page, lines: lines.map(l => ({ row: l.row, colStart: l.x0, colEnd: l.x1 })) }] : []))+}++// The text of the runs, as a copy of their cells reads it: each page's+// words under its run (textOfRun), joined by new lines (joinSegments).+// Null when a page's words are not known.+export function textOfRuns(+ runs: readonly { page: number; lines: RunLine[] }[], words: (page: number) => { items: Item[]; blocks: TextBlock[] } | null,+ pages: readonly { width: number; height: number; columns: number; rows: number }[],+): string | null {+ const texts: (string | null)[] = []+ for (const run of runs) {+ const got = words(run.page)+ const page = pages[run.page]+ if (!got || !page) return null+ texts.push(textOfRun(run.lines, got.items, got.blocks, page) || null)+ }+ return joinSegments(runs.map(run => ({ kind: 'image', lines: run.lines })), texts)+}++// The strips of a page drawn at `box` in cells of `cell`: one PNG per cell+// row, `rowPx` tall, each drawn as a 1-row Image of `box.columns`. The+// terminal fits an image in its box with the aspect kept, so a strip shows+// its row of the page only when both are at one scale with no space above+// or below: the page is exactly `box.rows` x rowPx (a whole number) and+// fills its box's height (its width at most the box's at that scale).+// Null otherwise (a page drawn smaller, a cell of a fraction of a pixel):+// that page has no select.+export function stripPlan(page: { width: number; height: number }, box: Box, cell: Cell): { rowPx: number; rows: number } | null {+ if (box.rows < 1 || box.columns < 1 || page.width < 1) return null+ const rowPx = page.height / box.rows+ if (!Number.isInteger(rowPx)) return null+ return page.width * cell.height <= rowPx * box.columns * cell.width + 1e-6 ? { rowPx, rows: box.rows } : null+}++// The strips' folder of a page (beside it in its key folder, so the cache+// cleanup takes them with it), and the file of row `row` (0-based) as+// Typst names its pages: 1-based, padded to the count's digits.+export const stripDir = (file: string) => file.replace(/\.png$/, '') + '.strips'+export const stripFile = (file: string, rows: number, row: number) => `${stripDir(file)}/s-${String(row + 1).padStart(String(rows).length, '0')}.png`++// The selection's background: VS Code's selection colour of the mode, one+// step off on every other redraw (the engine repaints an overlay whole only+// when it changes).+export const selectColor = (mode: IuiTheme, gen: number) => (mode === 'light' ? (gen % 2 ? '#add6ff' : '#add6fe') : gen % 2 ? '#264f78' : '#264f79')++// Whether Claude Code is a version whose selection skips pictures+// (SELECT_SINCE or newer; a dev build by its release). Before it, its own+// selection copies them, and a Client over a picture would take that away.+export function needsSelect(version: string): boolean {+ const release = /^\d+\.\d+\.\d+/.exec(version.trim())?.[0]+ return !!release && (compareVersions(release, SELECT_SINCE) ?? -1) >= 0+}++// Whether a drawing's pages take picture select now: `/iui select` on, in+// fullscreen (on the main screen the terminal selects), pages drawn from+// files (not as bytes over ssh), the Client element there and a Claude Code+// that needs it, with the tty's cell known (the strips' scale).+export function selectOn(o: { hasClient: boolean; isFullscreen?: boolean }): Cell | null {+ if (!S.select || picked.isBroken || !o.hasClient || o.isFullscreen !== true || !S.env?.canDraw || S.env.inline || !needsSelect(S.engine)) return null+ return S.tty?.cell ?? null+}diff --git a/hooks/lib/settings.ts b/hooks/lib/settings.tsindex 547b3f5..b6bbcc0 100644--- a/hooks/lib/settings.ts+++ b/hooks/lib/settings.ts@@ -83,6 +83,11 @@ export const HOST_CHECK_MS = 2000// many pages are kept in memory (a copy probes only the pages it reads).export const CLIP_WAKE_MS = 300export const PROBED_KEPT = 64+// A copy of picture cells that the watcher sees waits this long for its shim's+// ask (which waits 1.5 s at most) before the /iui grab hint; an ask keeps+// its cells from the hint this long.+export const HINT_WAIT_MS = 2000+export const ASKED_MS = 5000// The base every theme merges over (its theme.json and syntax files).export const BASE_THEME = 'vscode'// The theme used when none is chosen, or when the chosen one is not usable.@@ -113,6 +118,10 @@ export const heightText = (height: Height) =>: `${height} rows`// The grid setting as words.export const gridText = (grid: boolean) => (grid ? 'on (each line of text on whole terminal rows)' : 'off')+// `/iui select`: picture select (select.ts), on by default; it takes effect+// from the Claude Code version whose own selection skips pictures.+export const SELECT_SINCE = '2.1.305'+export const selectText = (on: boolean) => (on ? 'on (select and copy picture text with the mouse, fullscreen, Claude Code 2.1.305+)' : 'off')export const PICTURES_KEPT = 64// The file paths of the link rows under a picture: whether each one is a// file, kept for this many paths (the oldest go).diff --git a/hooks/lib/state.ts b/hooks/lib/state.tsindex 3e8c3d6..b66fff6 100644--- a/hooks/lib/state.ts+++ b/hooks/lib/state.ts@@ -10,10 +10,10 @@ import type { PlanVerdict } from './plan'import type { Drawn } from './copy'import type { GridMetrics } from './grid'import type { Checked } from './updates'-import type { ClipWay } from './clipboard'+import type { ClipWay, ShimState } from './clipboard'import type { PdfFacts, PixelBox } from './images'import { DEFAULT_HEIGHT, DEFAULT_SIZE, DEFAULT_WIDTH, type Height, type Width } from './settings'-import { uiInvalidate } from './api'+import { invalidate } from './api'// The `done` atom (see types/index.d.ts) for refresh. The state scan of// `claude plugin validate` reads an atom only from a const in the file that@@ -147,6 +147,10 @@ export const S = {// `/iui grid`: each text line of a page on whole terminal rows, so a// mouse selection (whole cells) covers the text it copies. On by default.grid: true,+ // `/iui select`: picture select (select.ts), on by default; and Claude+ // Code's version ('' not known), read in setUp.+ select: true,+ engine: '',look: null as Look | null,// The usable themes the last scan found, for the typeahead.themeChoices: [] as ThemeInfo[],@@ -187,6 +191,11 @@ export const S = {// Linux: how the clipboard is read and watched (clipboard.ts; null: not// looked up, as on macOS and over ssh).clipWay: null as ClipWay | null,+ // The clipboard shims (clipboard.ts; null: not set up), this process's+ // FIFO for their asks and its shims folder ('' when the shims are off).+ shims: null as ShimState | null,+ askFifo: '',+ shimDir: '',// Streaming: model requests of the main loop in flight.stepsLive: 0,// The plan (plan.tsx): the main loop's running turn (`turn.start`), the@@ -283,9 +292,10 @@ export const DRAWN_KEPT = 64// yet; the first of each watcher child is only noted), whether the watcher// found no PerlObjCBridge (`none`), a read that runs (`busy`) and a change// that came during it (`again`), the clipboard text last seen and last-// written (so a write is never read back as new), the last text with-// placeholders with its row (`rawRow`, when known) and how it was read (for-// dump), and the order of the drawings (`seq`).+// written (so a write is never read back as new), the cells of the last+// shim asks with their times (`asked`: such a copy gets no hint), the last+// text with placeholders with its row (`rawRow`, when known) and how it was+// read (for dump), and the order of the drawings (`seq`).export const clip = {count: null as number | null,none: false,@@ -293,6 +303,7 @@ export const clip = {again: false,seen: '',wrote: '',+ asked: [] as { text: string; at: number }[],raw: '',rawRow: undefined as string | undefined,seq: 0,@@ -316,5 +327,5 @@ export function keep<T>(cache: Map<string, T>, key: string, value: T, max: numbe// Redraws every reader of `done`; an invalidate where the write is refused.export function refresh($: EngineInterface) {- void update($, done, n => n + 1).catch(() => uiInvalidate($, 'ui.render'))+ void update($, done, n => n + 1).catch(() => invalidate($, 'ui.render'))}diff --git a/hooks/lib/strips.ts b/hooks/lib/strips.tsnew file mode 100644index 0000000..b726605--- /dev/null+++ b/hooks/lib/strips.ts@@ -0,0 +1,71 @@+// Picture select's strips (select.ts): a page cut into one PNG per row by+// Typst (render/strips.typ, about 0.2 s a page), once, the first time a+// selection reaches it. They are kept beside the page in its key folder+// (stripDir), so the cache cleanup takes them with it, and a folder cut+// before is used at once. A PNG's rows can not be cut out of its bytes (its+// filters run across rows), so Typst draws each row again, pixel for pixel.+import type { EngineInterface } from 'claude-code'++import { COMPILE_TIMEOUT_MS } from './settings'+import { keep, type Env } from './state'+import { stripDir, stripFile } from './select'+import { logDebug, noNetworkEnv } from './typst'+import { fsExists, processRun } from './api'++// The Typst run that cuts `file` (`width` x `height` px) into rows of+// `rowPx`, into its strips' folder (made first).+export function stripsCall(o: { typst: string; root: string; cache: string; file: string; width: number; height: number; rowPx: number }): { argv: string[]; env: Record<string, string> } {+ const argv = [+ 'sh', '-c', 'mkdir -p "$1" && shift && exec "$@"', 'sh', stripDir(o.file),+ o.typst, 'compile',+ '--root', '/',+ '--format', 'png',+ '--diagnostic-format', 'short',+ '--ppi', '72',+ '--input', `src=${o.file}`,+ '--input', `w=${o.width}`,+ '--input', `h=${o.height}`,+ '--input', `row=${o.rowPx}`,+ `${o.root}/render/strips.typ`,+ `${stripDir(o.file)}/s-{0p}.png`,+ ]+ return { argv, env: noNetworkEnv(o.cache) }+}++// The pages whose strips are there (true) or failed (false), and the cuts+// that run now, by `<file>@<rowPx>`.+const STRIPS_KEPT = 256+const cut = new Map<string, boolean>()+const cutting = new Map<string, Promise<boolean>>()++const keyOf = (file: string, rowPx: number) => `${file}@${rowPx}`++// Whether a page's strips are there (cut in this load, or found).+export const hasStrips = (file: string, rowPx: number) => cut.get(keyOf(file, rowPx)) === true++// Cuts a page's strips once (a run that failed is not tried again in this+// load); `done` runs when they are there. Never throws.+export function cutStrips($: EngineInterface, env: Env, page: { file: string; width: number; height: number }, plan: { rowPx: number; rows: number }, done: () => void) {+ const key = keyOf(page.file, plan.rowPx)+ if (cut.has(key) || cutting.has(key) || !env.typst) return+ const typst = env.typst+ const run = (async () => {+ if (await fsExists($, stripFile(page.file, plan.rows, plan.rows - 1)).catch(() => false)) return true+ const { argv, env: vars } = stripsCall({ typst, root: env.root, cache: env.cache, file: page.file, width: page.width, height: page.height, rowPx: plan.rowPx })+ const ran = await processRun($, argv, { env: vars, timeoutMs: COMPILE_TIMEOUT_MS })+ if (ran.exitCode !== 0) await logDebug($, env.cache, `strips of ${page.file} failed:\n${ran.stderr}`)+ return ran.exitCode === 0+ })().catch(() => false)+ cutting.set(key, run)+ void run.then(isCut => {+ cutting.delete(key)+ keep(cut, key, isCut, STRIPS_KEPT)+ if (isCut) done()+ })+}++// Forgets the strips cut (for tests).+export function forgetStrips() {+ cut.clear()+ cutting.clear()+}diff --git a/hooks/lib/typst.ts b/hooks/lib/typst.tsindex e6d3b46..5024a1f 100644--- a/hooks/lib/typst.ts+++ b/hooks/lib/typst.ts@@ -12,7 +12,7 @@ import { HEADERS_SH } from './host'import { logDebug } from './log'import { positionsOf, type Mark, type Positions } from './copy'import { leanFontsFor } from './fonts'-import { clockAfter, envGet, fsExists, fsList, fsRead, fsWrite, processRun, processSpawn } from './api'+import { clockAfter, envGet, fsExists, fsList, fsRead, fsWrite, processRun, spawn } from './api'// The typst command and its environment. Network off (see NO_NETWORK) and a// package cache of our own, so only the vendored packages load, and the@@ -263,7 +263,7 @@ export async function runKillable(const timer = clockAfter($, o.timeoutMs, () => stop('timeout'))let code: number | null = 1try {- const child = processSpawn($, { argv: wrapPid(argv), ...(o.env ? { env: o.env } : {}) })+ const child = spawn($, { argv: wrapPid(argv), ...(o.env ? { env: o.env } : {}) })for (;;) {const got = await child.next()if (got.done) {diff --git a/hooks/register.tsx b/hooks/register.tsxindex c83d47a..03330ac 100644--- a/hooks/register.tsx+++ b/hooks/register.tsx@@ -18,13 +18,13 @@// 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, RenderViewport } from 'claude-code'+import type { EngineInterface, Register, RenderElement, RenderSurface, RenderViewport } from 'claude-code'import {- DEFAULT_THEME, gridText, heightText, IMAGE_MAX_COLUMNS, MARGIN_TOP, MAX_HEIGHT, MAX_SIZE, MAX_WIDTH, MIN_HEIGHT, MIN_SIZE, MIN_WIDTH, RESULTS_KEPT, TYPST_PROMPT,- typstHintOf, widthText,+ ASKED_MS, DEFAULT_THEME, gridText, heightText, IMAGE_MAX_COLUMNS, MARGIN_TOP, MAX_HEIGHT, MAX_SIZE, MAX_WIDTH, MIN_HEIGHT, MIN_SIZE, MIN_WIDTH, RESULTS_KEPT, TYPST_PROMPT,+ HINT_WAIT_MS, selectText, typstHintOf, widthText,} from './lib/settings'-import { clip, drawnAnswers, editedPlans, keep, queue, refresh, running, S, subagentPlans, writesLive, type Env } from './lib/state'+import { clip, drawnAnswers, editedPlans, keep, queue, refresh, running, S, subagentPlans, writesLive, type Env, type Job, type Result } from './lib/state'import { beginEpoch } from './lib/epoch'import { escapeControls, luminanceOf, modeLine, modeOfLuminance, parseOsc11, settingOf } from './lib/mode'import { applyTheme, themeListing, userThemesOf } from './lib/themes'@@ -33,18 +33,21 @@ import { runSetup } from './lib/setup'import { pollHost, PROBE_RUNS, probeOsc11, showMode, USE_OSC11, watchHost } from './lib/host'import { measureFonts } from './lib/typst'import { drawPages, linksFor, type ShownLink } from './lib/draw'+import { picked, postOf, redrawSelection, runsOf, selectedRows, selectionAfter, selectTrace, selStamp, stripPlan, textOfRuns, topsOf, type SelectPost, type Selection } from './lib/select'+import { cutStrips } from './lib/strips'+import { traceDebug } from './lib/log'import { finalizeAll } from './lib/stream'-import { noteDrawn, noteDrawnAgain, noteOnScreen, wordsOf } from './lib/probe'-import { codePointsOf, decodeSelection, hasPlaceholders, joinSegments, pageTopsOf, pickPages, textOfRun, type Pick, type Segment } from './lib/copy'+import { noteDrawn, noteDrawnAgain, noteOnScreen, wordsOf, type PageWords } from './lib/probe'+import { codePointsOf, decodeSelection, hasPlaceholders, joinSegments, pageTopsOf, pickPages, textOfRun, type Drawn, type Pick, type Segment } from './lib/copy'import { pagesOf, type Drawing } from './lib/answer'-import { autoCopyText, clipboardNow, clipWatchArgs, clipWayReady } from './lib/clipboard'+import { answerAsk, askGaveUp, askRequest, asking, autoCopyText, clipboardNow, clipWatchArgs, clipWayReady, endAsks, listenAsks } from './lib/clipboard'import { setUp } from './lib/env'import { frameFor, noteView, stopWatch, watchFrame, watchState } from './lib/frame'import { roomColumns } from './lib/terminal'import { onScreenOf, pastedImagePath, pastedNumbers, sentFiles } from './lib/images'import { drawPageField, pasteDir, pdfPagesOf, rowsAfter, sentOf, shownOf, underRow, type Shown, type Under } from './lib/pictures'import { checkUpdates, loadUpdates, updateLine, updateNotice, type UpdateState } from './lib/updates'-import { clockNow, fsWrite, sessionSurfaces, storeSet, uiCopy, uiResolve, uiSelection, uiToast } from './lib/api'+import { clockAfter, clockNow, fsWrite, sessionSurfaces, storeSet, uiCopy, uiLog, uiResolve, uiSelection, uiToast } from './lib/api'import {currentPlan, decidePlan, decisionOf, drawAskLine, drawPlanResult, drawPlanUse, isPlanQuestion, planFileOf, planInputOf, planOf,planShownOf, scheduleClear, type PlanVerdict,@@ -85,7 +88,10 @@ export { nextJob } from './lib/stream'export * from './lib/images'export { rowsAfter, type Under } from './lib/pictures'export * from './lib/copy'-export { autoCopyText, CLIP_TOOLS, clipToolsOf, clipWayOf, FIND_SH, GTK_READ_PY, GTK_WATCH_PY, WL_WATCH_SH, X11_WATCH_SH, type ClipTool, type ClipWay } from './lib/clipboard'+export {+ autoCopyText, CLIP_TOOLS, clipToolsOf, clipWayOf, FIND_SH, GTK_READ_PY, GTK_WATCH_PY, pathWith, SHIM_SETUP, SHIM_TOOLS, shimFindOf, shimText,+ shQuote, WL_WATCH_SH, X11_WATCH_SH, type ClipTool, type ClipWay, type ShimState,+} from './lib/clipboard'export {agoText, checkedOf, compareVersions, isDue, isNewer, parseVersion, shouldTell, toldOf, UPDATE_EVERY_MS, UPDATE_FETCH_MS, UPDATE_STEPS, UPDATE_TOAST_MS,UPDATE_URL, updateLine, updateNotice, updateToast, versionOf,@@ -122,7 +128,7 @@ async function pictureEnv($: EngineInterface, e: { viewport?: RenderViewport }):}// Once a session, where pictures draw on the main screen: fullscreen gives-// the automatic copy (the main screen has no selection to own a copy).+// the automatic copy (there Claude Code copies a selection itself).function tellFullscreen($: EngineInterface) {if (S.fullscreenTold) returnS.fullscreenTold = true@@ -154,9 +160,11 @@ const stampOf = (ready: { env: Env; done: number }, view?: RenderViewport) =>// `text` the row's text (a string kept as given, so the compare is mostly// one pointer), `extra` its other inputs.type Memo<T> = { text: string; extra: string; stamp: string; onScreen: boolean | undefined; value: T }-// An answer's tree is kept with its link rows' count (the copy counts them-// under its pages), a plan's drawing with its link rows.-const answers = new Map<string, Memo<{ tree: RenderElement; links: number }>>()+// An answer's tree is kept with its link rows (the copy counts them under+// its pages), its pages and its selection's stamp (selStamp: a new one+// draws the tree again from the pages), a plan's drawing with its link rows.+type AnswerMemo = { tree: RenderElement; links: readonly ShownLink[]; pages: { result: Result; bytes?: readonly string[] }; sel: string }+const answers = new Map<string, Memo<AnswerMemo>>()const plans = new Map<string, Memo<{ drawing: Drawing; links: ShownLink[] }>>()const unders = new Map<string, Memo<Under[]>>()const pasted = new Map<string, Memo<Shown[]>>()@@ -216,17 +224,19 @@ async function planEnv($: EngineInterface): Promise<Env | null> {// ---- Copy -----------------------------------------------------------------// A mouse copy over a picture puts kitty placeholder cells on the clipboard,-// and no hook sees that copy. The watcher (lib/frame.ts) prints a line when-// the clipboard changes (macOS: its change count; Linux: a notifier's-// line, lib/clipboard.ts); at a change the clipboard is read once (pbpaste,-// wl-paste, xclip, xsel or GTK) and, in fullscreen, when it holds placeholders,-// written again with the words under them; the main screen has /iui grab-// (see lib/copy.ts; the drawn pages and the words of-// a page are in lib/probe.ts). No timer reads the clipboard or the-// selection.+// and no hook sees that copy. Claude Code writes it with pbcopy (wl-copy,+// xclip or xsel), whose shim asks this process for the text of the cells+// (answerCopy; lib/clipboard.ts) and writes that text in their place (see+// lib/copy.ts; the drawn pages and the words of a page are in lib/probe.ts).+// The watcher (lib/frame.ts) prints a line when the clipboard changes; at a+// change the clipboard is read once (pbpaste, wl-paste, xclip, xsel or GTK),+// and picture cells that no shim asked for (the terminal's own copy on the+// main screen, another session's) get the /iui grab hint. No timer reads the+// clipboard or the selection.// Notes a drawing for a copy (noteDrawn, noteDrawnAgain) and turns on the-// watcher's clipboard part; a failure never stops the row from drawing.+// ask reader and the watcher's clipboard part; a failure never stops the row+// from drawing.function noteCopy($: EngineInterface, note: () => void) {try {note()@@ -262,30 +272,47 @@ async function readCopy($: EngineInterface, raw: string, requestId?: string): Prreturn read}-// Writes the text read for `raw` (placeholders) to the clipboard, once:-// our own write is never read back as new. Just before the write the-// clipboard is read again: a newer copy (made while the picture was read,-// or one the watcher has not seen yet) is not written over; it is read-// next (readClipboard), even when it is the same text again.-async function copyRead($: EngineInterface, raw: string, requestId?: string) {+// A shim's ask (request `id`, lib/clipboard.ts): the text of its picture+// cells, the selection's row when it is that copy. Each ask gets an answer:+// '' when there is no text (the shim then keeps its own bytes). A shim that+// gave up (1.5 s) wrote the cells: the clipboard is not written then (another+// session can copy the same cells), only a toast names /iui grab.+async function answerCopy($: EngineInterface, id: string) {+ let text: string | null = null+ let answered = false+ try {+ const raw = await askRequest($, id)+ if (raw !== null) {+ const now = await clockNow($).catch(() => Date.now())+ clip.asked = [{ text: raw, at: now }, ...clip.asked.filter(a => now - a.at < ASKED_MS)].slice(0, 8)+ text = await askedText($, raw)+ }+ } finally {+ answered = await answerAsk($, id, text ?? '')+ }+ if (answered) {+ if (text !== null) uiToast($, `Intelligent UI: copied ${text.length} chars of text`)+ return+ }+ await askGaveUp($, id)+ if (text !== null) uiToast($, 'Intelligent UI: the picture text was not ready in time; /iui grab copies it')+}++// The text of the cells in `raw`; null when they are not read (one toast).+async function askedText($: EngineInterface, raw: string): Promise<string | null> {+ if (!hasPlaceholders(raw) || !drawnAnswers.size) return null+ const selected = await uiSelection($).catch(() => undefined)clip.raw = raw- clip.rawRow = requestId- const read = await readCopy($, raw, requestId)+ clip.rawRow = selected && sameCopy(selected.text, raw) ? selected.requestId : undefined+ const read = await readCopy($, raw, clip.rawRow)if (!read.resolved) {- // Not read again from the clipboard (one toast).- clip.seen = rawuiToast($, 'Intelligent UI: could not read the selected picture (/iui dump says why)')- return- }- if ((await clipboardNow($)) !== raw) {- clip.seen = ''- clip.again = true- return+ return null}+ // The clipboard change of this text is not read again.clip.wrote = read.textclip.seen = read.text- await uiCopy($, { text: read.text })- uiToast($, `Intelligent UI: copied ${read.text.length} chars of text`)+ return read.text}// A selection and a clipboard text are the same copy (a copy can end in@@ -305,26 +332,29 @@ function clipChanged($: EngineInterface, count: number | null, first: boolean) {if (!first) void readClipboard($).catch(() => undefined)}-// Not fullscreen (or not known yet): no selection says whose copy it is, and-// two sessions can have the same cells, so the clipboard is not changed.-// Cells that fit a page drawn here get one toast that names /iui grab.-async function hintGrab($: EngineInterface) {- const text = await clipboardNow($)- if (text === null || text === clip.seen) return+// Cells that a shim of this process asked for lately.+async function askedFor($: EngineInterface, text: string): Promise<boolean> {+ const now = await clockNow($).catch(() => Date.now())+ return clip.asked.some(a => now - a.at < ASKED_MS && sameCopy(a.text, text))+}++// Picture cells that no shim of this process asked for (the terminal's own+// copy on the main screen, another session's): never written. Cells that+// fit a page drawn here get one toast that names /iui grab.+function hintGrab($: EngineInterface, text: string) {clip.seen = text- if (text === clip.wrote || !hasPlaceholders(text)) returnif (!pickPages(decodeSelection(text), [...drawnAnswers.values()]).some(Boolean)) returnclip.raw = textclip.rawRow = undefineduiToast($, 'Intelligent UI: picture cells copied; /iui grab puts their text on the clipboard')}-// The clipboard changed: one read (clipboardNow). Only in fullscreen, and-// only when this session's selection has placeholders and is the-// clipboard's text, it is read and written back as text: a copy with no-// selection here is another session's. A clipboard with no placeholders,-// and our own write, are never touched. A change while a read runs (a-// probe can take a second) reads once more after it.+// The clipboard changed: one read (clipboardNow). Picture cells that a shim+// of this process asked for are its own copy (answerCopy). Out of the main+// screen, the terminal's OSC 52 write of a copy comes before its shim asks:+// the cells wait HINT_WAIT_MS for the ask, then get the hint while the+// clipboard still has them. A clipboard with no placeholders, and our own+// write, are never touched. A change while a read runs reads once more after it.async function readClipboard($: EngineInterface) {if (clip.busy) {clip.again = true@@ -334,18 +364,24 @@ async function readClipboard($: EngineInterface) {try {do {clip.again = false- if (S.isFullscreen !== true) {- await hintGrab($)- continue- }- const selected = await uiSelection($).catch(() => undefined)- if (!(selected && hasPlaceholders(selected.text))) continueconst text = await clipboardNow($)if (text === null || text === clip.seen) continue- clip.seen = text- if (text === clip.wrote || !hasPlaceholders(text)) continue- if (!sameCopy(selected.text, text)) continue- await copyRead($, text, selected.requestId)+ if (text === clip.wrote || !hasPlaceholders(text) || (await askedFor($, text))) {+ clip.seen = text+ continue+ }+ if (S.isFullscreen !== false) {+ await new Promise<void>(done => clockAfter($, HINT_WAIT_MS, done))+ if (await askedFor($, text)) {+ clip.seen = text+ continue+ }+ if ((await clipboardNow($)) !== text) {+ clip.again = true+ continue+ }+ }+ hintGrab($, text)} while (clip.again)} finally {clip.busy = false@@ -359,9 +395,11 @@ async function readClipboard($: EngineInterface) {// flag is an argument): its first count is only noted. A child that runs// is kept; one that ended is started again (watchFrame caps it). With no// PerlObjCBridge (or a notifier that ended) the child goes on as it is-// (size only).+// (size only). The ask reader (lib/clipboard.ts) starts with the first picture.function watchClipboard($: EngineInterface) {- if (!S.env || S.isRemote || !drawnAnswers.size || clip.none || clipWatchArgs(S.env.isMac) === null) return+ if (!S.env || S.isRemote || !drawnAnswers.size) return+ listenAsks($, id => void answerCopy($, id).catch(() => undefined))+ if (clip.none || clipWatchArgs(S.env.isMac) === null) returnwatchFrame($, (count, first) => clipChanged($, count, first))}@@ -373,10 +411,9 @@ function stopClipboard($: EngineInterface) {// The automatic copy now, for /iui details and dump.function autoCopyOf(isOn: boolean): string {- const now = watchState()return autoCopyText({isMac: !!S.env?.isMac, hostOs: S.hostOs, isRemote: S.isRemote, isOn, isFullscreen: S.isFullscreen, none: clip.none, drawn: drawnAnswers.size > 0,- running: !!now?.clip && !now.ended, way: S.clipWay,+ asking: asking(), way: S.clipWay, shims: S.shims,})}@@ -455,6 +492,64 @@ async function dump($: EngineInterface, on: boolean): Promise<string> {].filter(Boolean).join('\n')}+// ---- Picture select -------------------------------------------------------+// The Client over each page (hooks/sel.tsx) posts the selection; it is drawn+// over the pages (lib/draw.tsx) and its text copied on the button's up, read+// as a shim's copy is (wordsOf, textOfRun). See lib/select.ts.++// A Client's post: the selection after it, drawn again (the redraw takes+// the newest selection, so posts that come faster than redraws merge). A+// down cuts its page's strips now, so they are there for the drag.+function onSelect($: EngineInterface, post: SelectPost, surface: RenderSurface) {+ if (post.seq <= picked.seq) return+ picked.seq = post.seq+ const d = drawnAnswers.get(post.id)+ const was = picked.now+ picked.now = d ? selectionAfter(post, topsOf(d.pages)) : null+ if (S.debug) traceDebug($, selectTrace(post, d, picked.now))+ if (was || picked.now) redrawSelection($)+ const page = d?.pages[post.page]+ const cell = S.tty?.cell+ if (post.phase === 'down' && page && cell && S.env) {+ const plan = stripPlan(page, page, cell)+ if (plan) cutStrips($, S.env, page, plan, () => redrawSelection($))+ }+ // The copy waits for the page's words (a probe): the next posts do not.+ if (post.phase === 'up' && d && picked.now) void copySelection($, d, picked.now, surface).catch(() => undefined)+}++// The text of a selection's cells, page by page, as a copy of those cells+// reads it (textOfRuns); null when a page's words can not be read.+async function selectionText($: EngineInterface, d: Drawn & { job?: Job }, sel: Omit<Selection, 'id' | 'isUp'>): Promise<string | null> {+ const runs = runsOf(selectedRows(sel, d.pages))+ const words = new Map<number, PageWords | null>()+ for (const run of runs) words.set(run.page, await wordsOf($, d, run.page))+ return textOfRuns(runs, page => words.get(page) ?? null, d.pages)+}++// The button came up: the selection's text on the clipboard, with the+// toast of a copy. No text under it: nothing is copied.+async function copySelection($: EngineInterface, d: Drawn & { job?: Job }, sel: Selection, surface: RenderSurface) {+ const text = await selectionText($, d, sel)+ if (text === null) {+ uiToast($, 'Intelligent UI: could not read the selected picture')+ return+ }+ if (!text) return+ // The clipboard change of this text is not read again.+ clip.wrote = text+ clip.seen = text+ const copied = await uiCopy($, { text, surface })+ uiToast($, copied.isCopied ? `Intelligent UI: copied ${text.length} chars of text` : `Intelligent UI: the copy failed: ${copied.reason}`)+}++// No selection drawn (a new turn, Intelligent UI or select off).+function clearSelection($: EngineInterface) {+ if (!picked.now) return+ picked.now = null+ redrawSelection($)+}+// ---- Hooks ----------------------------------------------------------------export const register: Register = on => {@@ -465,11 +560,14 @@ export const register: Register = on => {})// A /clear or a resume: the process goes on, with no clipboard reads until- // the next picture. Any other end (/exit): the watcher stops, and no new- // child starts.+ // the next picture. Any other end (/exit): the watcher and the ask reader+ // stop, no new child starts, and the FIFO and the request files go.on('session.end', async ($, e, next) => {if (e.reason === 'clear' || e.reason === 'resume') stopClipboard($)- else stopWatch()+ else {+ stopWatch()+ await endAsks($)+ }return next(e)}).catch(($, e, next) => next(e))@@ -489,6 +587,27 @@ export const register: Register = on => {return next({ ...e, text: read.text })}).catch(($, e, next) => next(e))+ // Picture select's posts (hooks/sel.tsx); any other post passes on.+ on('ui.message', async ($, e, next) => {+ const post = e.surface === 'terminal' ? postOf(e.data) : null+ if (!post) return next(e)+ try {+ onSelect($, post, e.surface)+ } catch {}+ return {}+ }).catch(($, e, next) => next(e))++ // A Client that failed: picture select is off until a reload (the engine+ // draws the row again, with no Client).+ on('ui.fault', async ($, e, next) => {+ if (e.module.endsWith('sel.tsx')) {+ picked.isBroken = true+ picked.now = null+ void Promise.resolve(uiLog($, `Intelligent UI: picture select is off (${e.phase}: ${e.reason})`, { to: 'debug' })).catch(() => undefined)+ }+ return next(e)+ }).catch(($, e, next) => next(e))+// Typeahead for /iui arguments; the theme list is the last scan's.on('prompt.autocomplete', async ($, e, next) => {const got = await next(e)@@ -496,7 +615,7 @@ export const register: Register = on => {if (!S.themeChoices.length) await setUp($).catch(() => null)await loadUpdates($)const mine = commandSuggestions(e.text, e.token, e.start, {- themes: S.themeChoices, active: S.look?.name ?? DEFAULT_THEME, size: S.size, width: S.width, height: S.height, grid: S.grid, updates: S.updates,+ themes: S.themeChoices, active: S.look?.name ?? DEFAULT_THEME, size: S.size, width: S.width, height: S.height, grid: S.grid, updates: S.updates, select: S.select,})return mine.length ? { suggestions: [...got.suggestions, ...mine] } : got})@@ -508,7 +627,10 @@ export const register: Register = on => {if (asked.kind === 'on' || asked.kind === 'off') {await update($, isOn, () => asked.kind === 'on')// On: the clipboard part starts with the next picture drawn.- if (asked.kind === 'off') stopClipboard($)+ if (asked.kind === 'off') {+ stopClipboard($)+ clearSelection($)+ }await storeSet($, 'enabled', asked.kind === 'on')return { text: `Intelligent UI is ${asked.kind}.` }}@@ -551,6 +673,17 @@ export const register: Register = on => {const head = asked.grid === undefined ? 'The row grid is' : 'The row grid is now'return { text: `${head} ${gridText(S.grid)}. With the grid on, a mouse selection over a picture covers the lines it copies. Use /iui grid on (the default), or /iui grid off.` }}+ if (asked.kind === 'select') {+ if (asked.on === undefined) await setUp($).catch(() => null)+ else {+ S.select = asked.on+ if (!S.select) clearSelection($)+ refresh($)+ await storeSet($, 'select', S.select)+ }+ const head = asked.on === undefined ? 'Picture select is' : 'Picture select is now'+ return { text: `${head} ${selectText(S.select)}. When on, a mouse drag over a picture selects its text and copies it; Claude Code 2.1.305 and newer no longer select pictures by themselves. Use /iui select on (the default), or /iui select off.` }+ }if (asked.kind === 'updates') {await loadUpdates($)if (asked.on !== undefined) {@@ -609,7 +742,7 @@ export const register: Register = on => {await clipWayReady()const auto = `Auto copy: ${autoCopyOf(state === 'on')}`const updates = `Update checks: ${updateLine(await updateState($))}`- return { text: `Intelligent UI is ${state}, theme ${theme}, ${modeLine(S.theme, S.setting, S.settingRaw, S.host)}, text size ${S.size}%, width ${widthText(S.width)}, image and drawing height ${heightText(S.height)}, row grid ${S.grid ? 'on' : 'off'}, ${font}. ${typst}; ${terminal}.\n${auto}\n${updates}` }+ return { text: `Intelligent UI is ${state}, theme ${theme}, ${modeLine(S.theme, S.setting, S.settingRaw, S.host)}, text size ${S.size}%, width ${widthText(S.width)}, image and drawing height ${heightText(S.height)}, row grid ${S.grid ? 'on' : 'off'}, picture select ${S.select ? 'on' : 'off'}, ${font}. ${typst}; ${terminal}.\n${auto}\n${updates}` }}// The short state: what is set, and only the problems that keep pictures away.const sized = (v: number | 'full' | 'half', unit: string) => (typeof v === 'number' ? `${v} ${unit}` : v)@@ -622,7 +755,7 @@ export const register: Register = on => {if (!ready.typst) lines.push(`Typst is not installed, so answers stay text. Install it: ${typstHintOf(ready.isMac)}`)const notice = updateNotice(await updateState($))if (notice) lines.push(notice)- lines.push('Change: /iui on | off | size | width | height | grid | updates | theme. More: /iui details')+ lines.push('Change: /iui on | off | size | width | height | grid | select | updates | theme. More: /iui details')return { text: bad + lines.join('\n') }})@@ -712,21 +845,26 @@ export const register: Register = on => {// under them; the rows on screen at each draw, a memo hit too.if (kept) {noteCopy($, () => {- noteDrawnAgain(e.requestId, roomColumns(e.viewport), { top: MARGIN_TOP, after: kept.links })+ noteDrawnAgain(e.requestId, roomColumns(e.viewport), { top: MARGIN_TOP, after: kept.links.length })noteOnScreen(e.requestId, e.props.onScreen)})+ const sel = selStamp(e.requestId)+ if (kept.sel !== sel) {+ kept.tree = drawPages($, e, kept.pages.result, kept.pages.bytes, { bullet, links: kept.links, id: e.requestId })+ kept.sel = sel+ }return kept.tree}const drawing = await pagesOf($, { id: e.requestId, text: e.props.text, frame: frameFor(e.viewport), env: ready.env, canStream: true, onScreen })if (drawing.kind === 'pages') {const links = await linksFor($, e.props.text, ready.env.cwd)- const tree = drawPages($, e, drawing.result, drawing.bytes, { bullet, links })+ const tree = drawPages($, e, drawing.result, drawing.bytes, { bullet, links, id: e.requestId })const { result } = drawingnoteCopy($, () => {noteDrawn(e.requestId, result, roomColumns(e.viewport), { top: MARGIN_TOP, after: links.length })noteOnScreen(e.requestId, e.props.onScreen)})- memoSet(answers, e.requestId, k, { tree, links: links.length })+ memoSet(answers, e.requestId, k, { tree, links, pages: { result, ...(drawing.bytes ? { bytes: drawing.bytes } : {}) }, sel: selStamp(e.requestId) })return tree}answers.delete(e.requestId)@@ -839,6 +977,7 @@ export const register: Register = on => {void checkUpdates($)// The clipboard part, if it is not on (a picture drawn while it was off).if (await read($, isOn)) watchClipboard($)+ if (e.agentId === undefined) clearSelection($)return next(e)}).catch(($, e, next) => next(e))@@ -866,7 +1005,7 @@ export const register: Register = on => {noteOnScreen(id, e.props.onScreen)})}- return drawPlanUse($, e, row, drawing, links)+ return drawPlanUse($, e, row, drawing, links, `plan:${e.props.tool_use_id}`)} catch {return row}diff --git a/hooks/sel.tsx b/hooks/sel.tsxnew file mode 100644index 0000000..818b64d--- /dev/null+++ b/hooks/sel.tsx@@ -0,0 +1,39 @@+// Picture select's Client (lib/select.ts): one over each page of a picture,+// in the cells of that page. It takes the pointer and posts the selection+// whole (anchor, head, phase and order), never a bare event: posts of one+// frame merge, the last one kept. A click with no drag posts `clear`. It+// draws only an empty Box: anything drawn would blank the picture's cells.+import type { ClientModule, JsonValue } from 'claude-code'++type Props = { id: string; page: number }+type Drag = { ax: number; ay: number; hx: number; hy: number; isDown: boolean }++// The posts' order, one count for every instance (the module is shared).+let seq = 0++const Select: ClientModule<Props, Drag> = (props, surface) => {+ const { Box } = surface.elements+ const post = (d: Drag, phase: 'down' | 'move' | 'up' | 'clear') => {+ const size = { columns: surface.columns, rows: surface.rows }+ const data: JsonValue = { id: props.id, page: props.page, anchor: { x: d.ax, y: d.ay }, head: { x: d.hx, y: d.hy }, phase, seq: ++seq, size }+ surface.post(data)+ }+ surface.onPointer(ev => {+ const now = surface.state+ if (ev.type === 'down') {+ if (ev.button && ev.button !== 'left') return+ const next = { ax: ev.x, ay: ev.y, hx: ev.x, hy: ev.y, isDown: true }+ surface.setState(next)+ post(next, 'down')+ return+ }+ if (!now?.isDown || (ev.type !== 'move' && ev.type !== 'up')) return+ const next = { ...now, hx: ev.x, hy: ev.y, isDown: ev.type === 'move' }+ if (ev.type === 'move' && next.hx === now.hx && next.hy === now.hy) return+ surface.setState(next)+ post(next, ev.type === 'move' ? 'move' : next.hx === next.ax && next.hy === next.ay ? 'clear' : 'up')+ })+ return <Box width={surface.columns || undefined} height={surface.rows || undefined} />+}++export default Selectdiff --git a/render/strips.typ b/render/strips.typnew file mode 100644index 0000000..f1a49ab--- /dev/null+++ b/render/strips.typ@@ -0,0 +1,16 @@+// Intelligent UI: a page PNG cut into one PNG per terminal row, for picture+// select (hooks/lib/strips.ts). At --ppi 72, 1 pt is 1 pixel, so each strip+// is the page's own pixels, unscaled.+//+// typst compile --root / --ppi 72 --input src=<page.png> --input w=<px>+// --input h=<px> --input row=<px> <render>/strips.typ <dir>/s-{0p}.png+#let src = sys.inputs.at("src")+#let w = int(sys.inputs.at("w"))+#let h = int(sys.inputs.at("h"))+#let row = int(sys.inputs.at("row"))+#set page(width: w * 1pt, height: row * 1pt, margin: 0pt, fill: none)+#let img = image(src, width: w * 1pt, height: h * 1pt, scaling: "pixelated")+#for k in range(calc.ceil(h / row)) {+ if k > 0 { pagebreak() }+ box(width: w * 1pt, height: row * 1pt, clip: true, place(dy: -k * row * 1pt, img))+}diff --git a/tests/command.test.ts b/tests/command.test.tsindex 30724b3..273eafe 100644--- a/tests/command.test.ts+++ b/tests/command.test.ts@@ -17,7 +17,7 @@ test('the first word completes as a subcommand', () => {expect(at('/iui h').map(r => r.text)).toEqual(['height'])expect(at('/iui g').map(r => r.text)).toEqual(['grid', 'grab'])expect(at('/iui o').map(r => r.text)).toEqual(['on', 'off'])- expect(at('/iui S').map(r => r.text)).toEqual(['size', 'setup'])+ expect(at('/iui S').map(r => r.text)).toEqual(['size', 'select', 'setup'])expect(at('/iui x')).toEqual([])expect(at('/iui t')[0]!.description).toBe('list the themes, or switch to one')})@@ -68,9 +68,17 @@ test('a theme name or size completes after its word', () => {expect(at('/iui grid on extra')).toEqual([])})+test('select completes its values; on is the default', () => {+ expect(at('/iui select').map(r => r.text)).toEqual(['select on', 'select off'])+ expect(at('/iui select').map(r => r.description)).toEqual(['in use', undefined])+ const off = (text: string) => commandSuggestions(text, /\S+$/.exec(text)![0], text.lastIndexOf(' ') + 1, { ...o, select: false })+ expect(off('/iui select o').map(r => r.description)).toEqual(['the default', 'in use'])+ expect(at('/iui select of').map(r => r.label)).toEqual(['off'])+})+test('the command name itself offers the subcommands, as rows that hold it', () => {const rows = at('/iui')- expect(rows.map(r => r.label)).toEqual(['on', 'off', 'size', 'width', 'height', 'grid', 'updates', 'theme', 'setup', 'details', 'grab', 'dump'])+ expect(rows.map(r => r.label)).toEqual(['on', 'off', 'size', 'width', 'height', 'grid', 'select', 'updates', 'theme', 'setup', 'details', 'grab', 'dump'])expect(rows.find(r => r.label === 'theme')!.text).toBe('/iui theme')expect(at(' /iui').map(r => r.text)).toContain('/iui grid')expect(at('tell me /iui')).toEqual([])@@ -125,6 +133,11 @@ const parses: [string, object][] = [['GRID Off', { kind: 'grid', grid: false }],['grid yes', BAD],['grid on off', BAD],+ // select: on, off, or nothing to show it+ ['select', { kind: 'select' }],+ ['select off', { kind: 'select', on: false }],+ ['SELECT On', { kind: 'select', on: true }],+ ['select maybe', BAD],// theme: a name kept as typed['theme', { kind: 'theme' }],['THEME', { kind: 'theme' }],@@ -174,7 +187,7 @@ test('a size row takes a % after the number, and marks the one in use and the tetest('right after the command name and a space, every subcommand is offered by its bare name', () => {const rows = commandSuggestions('/iui ', '', 5, o)- expect(rows.map(r => r.text)).toEqual(['on', 'off', 'size', 'width', 'height', 'grid', 'updates', 'theme', 'setup', 'details', 'grab', 'dump'])+ expect(rows.map(r => r.text)).toEqual(['on', 'off', 'size', 'width', 'height', 'grid', 'select', 'updates', 'theme', 'setup', 'details', 'grab', 'dump'])expect(rows.every(r => r.description)).toBe(true)})@@ -265,11 +278,20 @@ test('width, height and grid with no value show the setting and keep nothing', aexpect(store.get('width')).toBe(160)})+test('select off is kept and named; select with no value shows it', async ($, on) => {+ const { store } = world(on)+ expect(await run($, 'select')).toContain('Picture select is on (')+ expect(store.has('select')).toBe(false)+ expect(await run($, 'select off')).toContain('Picture select is now off.')+ expect(store.get('select')).toBe(false)+ expect(await run($, 'details')).toContain('picture select off')+})+test('a word that is not a subcommand gets the not-understood line, then the state', async ($, on) => {world(on)const text = await run($, ' bigger please ')expect(text.startsWith('Not understood: "bigger please".\nIntelligent UI is on: theme ')).toBe(true)- expect(text).toContain('Change: /iui on | off | size | width | height | grid | updates | theme. More: /iui details')+ expect(text).toContain('Change: /iui on | off | size | width | height | grid | select | updates | theme. More: /iui details')})test('the state tells when Typst is not found, and when the terminal shows no pictures', async ($, on) => {diff --git a/tests/copy.test.ts b/tests/copy.test.tsindex 4307db3..2694211 100644--- a/tests/copy.test.ts+++ b/tests/copy.test.ts@@ -2,11 +2,13 @@ import { expect, mock, test } from 'claude-code/testing'import {codePointsOf, commandSuggestions, decodeSelection, DIACRITICS, hasPlaceholders, isDrawnIn, joinSegments, pageTopsOf, pickPages,- placeholderCell, isSafePage, positionsOf, screenOf, textOfRun, WATCH_PL, type Drawn, type TextBlock, type Item, type Mark, type OnScreen,+ placeholderCell, isSafePage, positionsOf, screenOf, SHIM_SETUP, textOfRun, WATCH_PL, type Drawn, type TextBlock, type Item, type Mark, type OnScreen,type Place,} from '../hooks/register'import { joinItems } from '../hooks/lib/copy'import { WATCH_RESTART_MS } from '../hooks/lib/frame'+import { ANSWER_SH, ASK_CLEAN_SH, ASK_END_SH, ASK_MAX, ASK_PL, ASK_RESTART_MS, ASK_RESTARTS } from '../hooks/lib/clipboard'+import { HINT_WAIT_MS } from '../hooks/lib/settings'import { rowsAfter, type Under } from '../hooks/lib/pictures'import { forgetProbed, noteDrawn, noteDrawnAgain, noteOnScreen, wordsOf } from '../hooks/lib/probe'import { clip, drawnAnswers, lastKey, results, type Job, type Result } from '../hooks/lib/state'@@ -486,24 +488,44 @@ const inputsOf = (argv: readonly string[]) => Object.fromEntries(argv.flatMap((a// marks on page n (`hold` holds the next query until `release`; a kill ends// it), the watcher (WATCH_PL: its PID, then with `clip` the change count at// its start and at each change of the clipboard; `clip none` with-// `noBridge`), pbpaste reading `clipboard`, and the copies, toasts and-// selection the test sees. `copy` is a copy in another app (or a mouse-// copy): the clipboard and its change count change; while `selects`, a copy-// of picture cells is this session's selection too (a mouse copy in-// fullscreen).-function world(on: any, o: { failFirst?: boolean; noBridge?: boolean; env?: Record<string, string> } = {}) {+// `noBridge`), pbpaste reading `clipboard`, the shims (pbcopy found, perl,+// the FIFO ASK: IUI_COPY_ASK and PATH set, `envSets`), the ask reader+// (ASK_PL: its PID, then `ask <id>` lines) and the copies, toasts and+// selection the test sees. `copy` is Claude Code's own copy (copy on select+// in fullscreen, /copy, $.ui.copy): the terminal's OSC 52 write of the text+// (a clipboard change), then pbcopy through its shim: picture cells go to+// ask/<id> and the id to the reader; the answer (ANSWER_SH), or the cells+// when the shim gives up after 1.5 s (it leaves its mark), go on the+// clipboard. `copies` are the texts the mod put there. While `selects`, a+// copy of picture cells is this session's selection too. `foreign` is a copy+// with no ask (another session's, the terminal's own on the main screen,+// another app's); `change` is one more change with the same text.+// `tty`: the tty size line its read gives (none: no read), `version`+// Claude Code's (picture select); `strips` the strip cuts run.+function world(on: any, o: { failFirst?: boolean; noBridge?: boolean; env?: Record<string, string>; tty?: string; version?: string } = {}) {mock.store(on)const clock = mock.clock(on)- mock.env(on, o.env ?? { HOME: '/h', CLAUDE_CODE_FORCE_TERMINAL_IMAGES: '1' })+ const vars: Record<string, string | undefined> = { PATH: '/usr/bin:/bin', ...(o.env ?? { HOME: '/h', CLAUDE_CODE_FORCE_TERMINAL_IMAGES: '1' }) }+ const envSets: [string, string | undefined][] = []+ on('env.get', async ($: any, e: any) => ({ value: vars[e.name] }))+ on('env.set', async ($: any, e: any) => {+ envSets.push([e.name, e.value])+ vars[e.name] = e.value+ return { value: {} }+ })const files = new Map<string, string>()const compiles: Record<string, string>[] = []const queries: Record<string, string>[] = []const kills: string[] = []const killers = new Map<string, () => void>()- const watchers: { argv: string[]; pid: string; say: (line: string) => void; dead: boolean }[] = []+ type Child = { argv: string[]; pid: string; say: (line: string) => void; dead: boolean; end: (code: number) => void }+ const watchers: Child[] = []+ const readers: Child[] = []+ const waits = new Map<string, (text: string) => void>()const w = {- clock, files, compiles, queries, kills, watchers,- clipboard: 'nothing yet', count: 40, pastes: 0, copies: [] as string[], toasts: [] as string[],+ clock, files, compiles, queries, kills, watchers, readers, vars, envSets,+ ended: [] as string[][], answers: [] as string[], late: [] as string[], cleaned: [] as string[], asks: 0, reads: [] as string[],+ clipboard: 'nothing yet', count: 40, pastes: 0, copies: [] as string[], toasts: [] as string[], strips: [] as string[][], traces: [] as string[],selection: undefined as { text: string; requestId?: string } | undefined,selects: true,gate: null as Promise<void> | null, release: () => {},@@ -511,27 +533,63 @@ function world(on: any, o: { failFirst?: boolean; noBridge?: boolean; env?: Recow.gate = new Promise<void>(r => (w.release = r))},copy(text: string) {- w.clipboard = textif (w.selects && hasPlaceholders(text)) w.selection = { text }+ w.foreign(text)+ void w.shim(text)+ },+ // pbcopy through its shim: an ask for picture cells while a reader runs.+ async shim(text: string) {+ let out = text+ const reader = readers.find(c => !c.dead)+ if (hasPlaceholders(text) && reader) {+ const id = `4242-${++w.asks}-7`+ const request = `${ASK_DIR}/${id}`+ files.set(request, text)+ const answered = new Promise<string | null>(r => waits.set(id, r))+ reader.say(`ask ${id}`)+ const got = await Promise.race([answered, clock.sleep(1500).then(() => null)])+ waits.delete(id)+ files.delete(request)+ if (got === null) files.set(`${request}.answer`, '')+ // An empty answer: the shim keeps its own bytes.+ else if (got) out = got+ }+ if (out !== text) w.copies.push(out)+ w.foreign(out)+ },+ foreign(text: string) {+ w.clipboard = text+ w.change()+ },+ change() {w.count++for (const c of watchers) if (!c.dead && c.argv[3] === 'clip' && !o.noBridge) c.say(`clip ${w.count}`)},}let pids = 5000+ // A long-lived child: its PID, then the lines it is told; a kill ends it.+ async function* child(list: Child[], argv: string[], first: string[]) {+ const pid = String(pids++)+ const lines = [`pid ${pid}`, ...first]+ let wake = () => {}+ let code: number | null = null+ const me: Child = { argv, pid, dead: false, say: line => (lines.push(line), wake()), end: c => ((code = c), (me.dead = true), wake()) }+ list.push(me)+ killers.set(pid, () => ((me.dead = true), wake()))+ for (;;) {+ while (lines.length && !me.dead) yield { stream: 'stdout', text: `${lines.shift()}\n` }+ if (me.dead) return { value: { code, signal: code === null ? 'SIGTERM' : null } }+ await new Promise<void>(r => (wake = r))+ }+ }on('process.spawn', async function* ($: any, e: any) {const argv: string[] = e.argvif (argv[0] === '/usr/bin/perl' && argv[1] === '-e' && argv[2] === WATCH_PL) {- const pid = String(pids++)- const lines = [`pid ${pid}`, ...(argv[3] === 'clip' ? [o.noBridge ? 'clip none' : `clip ${w.count}`] : [])]- let wake = () => {}- const me = { argv, pid, dead: false, say: (line: string) => (lines.push(line), wake()) }- watchers.push(me)- killers.set(pid, () => ((me.dead = true), wake()))- for (;;) {- while (lines.length && !me.dead) yield { stream: 'stdout', text: `${lines.shift()}\n` }- if (me.dead) return { value: { code: null, signal: 'SIGTERM' } }- await new Promise<void>(r => (wake = r))- }+ return yield* child(watchers, argv, argv[3] === 'clip' ? [o.noBridge ? 'clip none' : `clip ${w.count}`] : [])+ }+ if (argv[0] === '/usr/bin/perl' && argv[1] === '-e' && argv[2] === ASK_PL) {+ expect(argv.slice(3)).toEqual([ASK])+ return yield* child(readers, argv, [])}if (argv[0] !== 'sh' || argv[2] !== WRAP || argv[4] !== '/usr/bin/typst') return { value: { code: 2, signal: null } }const pid = String(pids++)@@ -562,7 +620,48 @@ function world(on: any, o: { failFirst?: boolean; noBridge?: boolean; env?: Recokillers.get(argv[1]!)?.()return ok('')}+ // The answer: to the shim that waits, else refused (its mark).+ if (argv[0] === 'sh' && argv[2] === ANSWER_SH) {+ const request = argv[4]!+ const text: string = e.init?.stdin ?? ''+ const to = waits.get(request.slice(ASK_DIR.length + 1))+ if (!to || files.has(`${request}.answer`)) {+ w.late.push(text)+ return ok('', 1)+ }+ w.answers.push(text)+ to(text)+ return ok('')+ }+ // After a refused answer: the request and the mark go.+ if (argv[0] === 'sh' && argv[2] === ASK_CLEAN_SH) {+ files.delete(argv[4]!)+ files.delete(`${argv[4]}.answer`)+ w.cleaned.push(argv[4]!)+ return ok('')+ }+ if (argv[0] === 'sh' && argv[2] === ASK_END_SH) {+ w.ended.push(argv.slice(4))+ return ok('')+ }if (/cc-intelligent-ui-/.test(cmd)) return ok('/c\nDarwin\n')+ if (o.tty && argv[0] === 'sh' && cmd.includes('ioctl(T')) return ok(o.tty)+ if (argv[0] === 'sh' && argv.some(a => a.endsWith('/render/strips.typ'))) {+ w.strips.push(argv)+ return ok('')+ }+ if (argv[0] === 'sh' && cmd.includes('debug.log')) {+ w.traces.push(e.init?.stdin ?? '')+ return ok('')+ }+ if (argv[1]?.endsWith(SHIM_SETUP) && argv[2] === 'find') return ok(`pid 4242\npbcopy /usr/bin/pbcopy\nperl /usr/bin/perl\nask ${ASK}\n`)+ if (argv[1]?.endsWith(SHIM_SETUP) && argv[2] === 'install') {+ for (const f of argv.slice(5)) {+ files.set(f, files.get(`${f}${argv[4]}`) ?? '')+ files.delete(`${f}${argv[4]}`)+ }+ return ok('')+ }if (cmd.includes('command -v typst')) return ok('/usr/bin/typst\n')if (argv[0] === '/usr/bin/typst' && argv[1] === 'eval') return ok('{}')if (cmd.includes('od -An')) return ok(argv.filter(a => a.endsWith('.png')).map(() => `${HEAD}\n@\n`).join(''))@@ -576,7 +675,12 @@ function world(on: any, o: { failFirst?: boolean; noBridge?: boolean; env?: Recofiles.set(e.path, e.text)return { value: undefined }})- on('fs.read', async ($: any, e: any, next: any) => (files.has(e.path) ? { value: files.get(e.path) } : e.path.startsWith('/c/') ? { deny: 'ENOENT' } : next(e)))+ on('fs.stat', async ($: any, e: any, next: any) =>+ e.path.startsWith(`${ASK_DIR}/`) && files.has(e.path) ? { value: { kind: 'file', size: files.get(e.path)!.length, mtimeMs: 1, isLink: false } } : next(e))+ on('fs.read', async ($: any, e: any, next: any) => {+ w.reads.push(e.path)+ return e.path.endsWith('/hooks/clip-shim.sh') ? { value: SHIM_STUB } : files.has(e.path) ? { value: files.get(e.path) } : e.path.startsWith('/c/') ? { deny: 'ENOENT' } : next(e)+ })// One page per segment of the folder's doc.json.on('fs.list', async ($: any, e: any, next: any) => {if (!e.path.startsWith('/c/')) return next(e)@@ -590,6 +694,7 @@ function world(on: any, o: { failFirst?: boolean; noBridge?: boolean; env?: Recoreturn { value: { isCopied: true } }})on('ui.selection', async () => ({ value: w.selection }))+ on('session.version', async () => ({ value: { version: o.version ?? '2.1.306', base: o.version ?? '2.1.306' } }))on('ui.toast', async ($: any, e: any) => {w.toasts.push(String(e.text))return { value: undefined }@@ -598,13 +703,19 @@ function world(on: any, o: { failFirst?: boolean; noBridge?: boolean; env?: Recoreturn w}+// The shim template, as setUp reads it (the real one: tests/shim-test.sh).+const SHIM_STUB = 'tool=@TOOL@ real=@REAL@ dir=@DIR@ perl=@PERL@'+// The FIFO of this process (Claude Code's pid from the shim find: 4242) and the ask folder.+const ASK = '/c/ask-4242'+const ASK_DIR = '/c/ask'+const message = (requestId: string, text: string, isFullscreen: boolean | null = true) =>({plugin: 'cc-intelligent-ui', surface: 'terminal', component: 'AssistantMessage', requestId,props: { text, isFirstOfReply: true }, viewport: { columns: 100, rows: 40, ...(isFullscreen === null ? {} : { isFullscreen }) },}) as anyconst props = (text: string) => ({ text, isFirstOfReply: true }) as any-const names = (files: Map<string, string>) => [...new Set([...files.keys()].filter(k => k.startsWith('/c/')).map(k => k.replace(/^.*\//, '')))].sort()+const names = (files: Map<string, string>) => [...new Set([...files.keys()].filter(k => k.startsWith('/c/') && !k.startsWith('/c/shims-') && !k.startsWith(`${ASK_DIR}/`)).map(k => k.replace(/^.*\//, '')))].sort()// Draws `text` (SMALL_MD) as answer `id` (98 columns: the fixture's),// compiled; the watcher has its clipboard part (its first count noted).@@ -748,34 +859,39 @@ test('positions are kept in memory: copies of the same page share one probe run,expect(w.toasts.slice(toasts)).toEqual(['Intelligent UI: copied 35 chars of text'])})-test('a clipboard change runs pbpaste once and copies; no change, no read; our own write is not read as new', async ($, on) => {+test("a copy of picture cells gets its text through the shim's ask; the watcher reads each change once and never writes", async ($, on) => {const w = world(on)await $.command.run({ command: 'iui', args: '' } as any)- // No picture drawn: the watcher has no clipboard part.+ // No picture drawn: the watcher has no clipboard part, and no ask reader runs.await w.clock.advance(900)expect(w.watchers.map(c => c.argv.slice(3))).toEqual([[]])+ expect(w.readers.length).toBe(0)await drawAnswer($, w)+ expect(w.readers.length).toBe(1)// The first picture: the watcher again with `clip`, the first one killed.expect(w.watchers.map(c => c.argv.slice(3))).toEqual([[], ['clip']])expect(w.kills).toEqual([w.watchers[0]!.pid])// Its first count is only noted; no change, no read.await w.clock.advance(900)expect(w.pastes).toBe(0)- // A copy with no placeholders (and no selection of picture cells): no read, never written.- w.copy('copied in another app')+ // A copy with no placeholders: one read, never written.+ w.foreign('copied in another app')await w.clock.settle()- expect(w.pastes).toBe(0)- expect(w.copies).toEqual([])+ expect(w.pastes).toBe(1)w.copy(COPY)await w.clock.settle()+ // The shim got the text; the mod wrote nothing itself.+ expect(w.answers).toEqual([COPIED])expect(w.copies).toEqual([COPIED])+ expect(w.clipboard).toBe(COPIED)expect(w.toasts).toContain('Intelligent UI: copied 35 chars of text')- // One read before the write; our own write changes the count: read- // once, not copied again.- expect(w.pastes).toBe(3)- await w.clock.advance(900)- expect(w.pastes).toBe(3)- expect(w.copies.length).toBe(1)+ // The OSC 52 write of the cells and the shim's write: read, no hint.+ await w.clock.advance(3000)+ const pastes = w.pastes+ expect(pastes).toBeGreaterThan(1)+ expect(w.toasts.filter(t => t === GRAB_HINT)).toEqual([])+ // The request and the answer are gone.+ expect([...w.files.keys()].filter(k => k.startsWith(`${ASK_DIR}/`))).toEqual([])const dumped = await $.command.run({ command: 'iui', args: 'dump' } as any)expect(dumped.text).toContain('U+10EEEE[1,0]')expect(dumped.text).toContain('-> answer ans-1 page 0')@@ -788,7 +904,7 @@ test('a clipboard change runs pbpaste once and copies; no change, no read; our oexpect(w.kills).toEqual([w.watchers[0]!.pid, w.watchers[1]!.pid])w.copy(COPY)await w.clock.settle()- expect(w.pastes).toBe(3)+ expect(w.pastes).toBe(pastes)})test('/iui details says whether the automatic copy is on', async ($, on) => {@@ -799,9 +915,12 @@ test('/iui details says whether the automatic copy is on', async ($, on) => {expect(await details()).toBe('Auto copy: on')await $.command.run({ command: 'iui', args: 'off' } as any)expect(await details()).toBe('Auto copy: off (Intelligent UI is off)')+ // The shims were set up once: the FIFO, then PATH with the folder first.+ expect(w.envSets).toEqual([['IUI_COPY_ASK', ASK], ['PATH', '/c/shims-4242:/usr/bin:/bin']])+ expect(w.files.get('/c/shims-4242/pbcopy')).toBe(`tool='pbcopy' real='/usr/bin/pbcopy' dir='/c/shims-4242' perl='/usr/bin/perl'`)})-test('with no PerlObjCBridge (clip none) the copy is off: no read, no copy; details says why; grab still copies', async ($, on) => {+test('with no PerlObjCBridge (clip none) the shim still gets the text; no read, no hint; details says so; grab still copies', async ($, on) => {const w = world(on, { noBridge: true })await $.command.run({ command: 'iui', args: '' } as any)await drawAnswer($, w)@@ -809,9 +928,9 @@ test('with no PerlObjCBridge (clip none) the copy is off: no read, no copy; detaawait w.clock.advance(900)await w.clock.settle()expect(w.pastes).toBe(0)- expect(w.copies).toEqual([])+ expect(w.copies).toEqual([COPIED])const details = await $.command.run({ command: 'iui', args: 'details' } as any)- expect(details.text).toContain('\nAuto copy: off (no PerlObjCBridge in /usr/bin/perl)')+ expect(details.text).toContain('\nAuto copy: on (no /iui grab hint: no PerlObjCBridge in /usr/bin/perl)')// No new watcher for it: once is enough.await drawAnswer($, w, 'ans-2')expect(w.watchers.length).toBe(2)@@ -837,6 +956,9 @@ test('a copy of picture cells through $.ui.copy is rewritten to their text; otheexpect(w.copies).toEqual([COPIED])await $.command.run({ command: 'cp', args: 'plain text' } as any)expect(w.copies[1]).toBe('plain text')+ // The text went through the shim: no ask.+ await w.clock.settle()+ expect(w.answers).toEqual([])})test('a $.ui.copy of picture cells is not copied again at its clipboard change', { plugins: [COPIER] }, async ($, on) => {@@ -847,70 +969,83 @@ test('a $.ui.copy of picture cells is not copied again at its clipboard change',await $.command.run({ command: 'cp', args: COPY } as any)await w.clock.advance(900)await w.clock.settle()- expect(w.pastes).toBe(1)+ expect(w.pastes).toBe(2)expect(w.copies).toEqual([COPIED])expect(w.toasts.filter(t => t.startsWith('Intelligent UI: copied')).length).toBe(0)})-test('a clipboard with picture cells that is not this session\'s selection is not rewritten (another session\'s copy)', async ($, on) => {+const GRAB_HINT = 'Intelligent UI: picture cells copied; /iui grab puts their text on the clipboard'++test("another session's copy of the same cells (no ask here) is never rewritten, even with this session's old selection of them; one hint", async ($, on) => {const w = world(on)await $.command.run({ command: 'iui', args: '' } as any)await drawAnswer($, w)- w.selects = false- // This session has a selection of its own picture cells, and the- // clipboard has other picture cells (another session's).- w.selection = { text: cells(2, 0, 97), requestId: 'ans-1' }- w.copy([cells(1, 0, 97), cells(3, 0, 21)].join('\n'))+ // This session copied these cells a while ago (answered then).+ w.copy(COPY)await w.clock.settle()- expect(w.pastes).toBe(1)- expect(w.copies).toEqual([])- // The same copy as the selection, with a last line break: rewritten.- w.selection = { text: COPY, requestId: 'ans-1' }- w.copy(`${COPY}\n`)+ expect(w.copies).toEqual([COPIED])+ await w.clock.advance(60000)+ // The selection stays; another session copies the same cells.+ expect(w.selection?.text).toBe(COPY)+ w.foreign(COPY)await w.clock.settle()+ // A shim of this session could still ask: no hint yet.+ expect(w.toasts.filter(t => t === GRAB_HINT)).toEqual([])+ await w.clock.advance(HINT_WAIT_MS)+ expect(w.copies).toEqual([COPIED])+ expect(w.clipboard).toBe(COPY)+ expect(w.toasts.filter(t => t === GRAB_HINT)).toEqual([GRAB_HINT])+ // Its OSC 52 write of the same text: no second hint.+ w.change()+ await w.clock.advance(HINT_WAIT_MS)+ expect(w.toasts.filter(t => t === GRAB_HINT).length).toBe(1)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) => {+test('cells the watcher sees before their ask comes (a slow shim start): the ask in the wait stops the hint', async ($, on) => {const w = world(on)await $.command.run({ command: 'iui', args: '' } as any)await drawAnswer($, w)- w.selects = false- // 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`)+ // The OSC 52 write only, then pbcopy's shim 300 ms later.+ w.foreign(COPY)+ await w.clock.advance(300)+ void w.shim(COPY)await w.clock.settle()expect(w.copies).toEqual([COPIED])+ await w.clock.advance(HINT_WAIT_MS)+ expect(w.toasts.filter(t => t === GRAB_HINT)).toEqual([])+ expect(w.clipboard).toBe(COPIED)+ // A copy that changes in the wait: no hint for the old one.+ w.foreign(COPY.replace(cells(3, 0, 21), cells(3, 0, 20)))+ await w.clock.advance(500)+ w.foreign('copied in another app')+ await w.clock.advance(HINT_WAIT_MS)+ expect(w.toasts.filter(t => t === GRAB_HINT)).toEqual([])})-test('a watcher that starts again does not copy the clipboard that was there before (its first count is only noted)', async ($, on) => {+test("the selection gives the row (its requestId) only when it is that copy", async ($, on) => {const w = world(on)- // Picture cells on the clipboard before this load (a hot reload).- w.clipboard = COPYawait $.command.run({ command: 'iui', args: '' } as any)+ // Two answers that both fit the cells: with no row, the newer one.await drawAnswer($, w)- await w.clock.advance(900)- expect(w.pastes).toBe(0)- expect(w.copies).toEqual([])- // A new copy is copied.+ await drawAnswer($, w, 'ans-2', `${SMALL_MD}\n\nEnd.`)+ w.selects = false+ const picked = async () => ((await $.command.run({ command: 'iui', args: 'dump' } as any)).text ?? '').match(/-> answer (\S+) page/)?.[1]+ // A selection of other cells (and a row) does not name this copy's row.+ w.selection = { text: cells(9, 0, 9), requestId: 'ans-1' }w.copy(COPY)await w.clock.settle()expect(w.copies).toEqual([COPIED])+ expect(await picked()).toBe('ans-2')+ // The selection of this copy (a last line break less): its row.+ w.selection = { text: COPY, requestId: 'ans-1' }+ w.copy(`${COPY}\n`)+ await w.clock.settle()+ expect(w.copies).toEqual([COPIED, COPIED])+ expect(await picked()).toBe('ans-1')})-test('a copy that can not be read gives one toast; the same text again gives none', async ($, on) => {+test('cells that can not be read: an empty answer at once (no wait; the shim keeps its cells), one toast for each copy', async ($, on) => {const w = world(on)await $.command.run({ command: 'iui', args: '' } as any)await drawAnswer($, w)@@ -918,33 +1053,114 @@ test('a copy that can not be read gives one toast; the same text again gives nonconst text = [cells(1, 0, 150), cells(2, 0, 150)].join('\n')w.copy(text)await w.clock.settle()+ expect(w.answers).toEqual([''])+ expect(w.clipboard).toBe(text)+ expect(w.copies).toEqual([])+ expect(w.toasts.filter(t => t.startsWith('Intelligent UI: could not read')).length).toBe(1)+ // No /iui grab hint for it: its own shim asked.+ await w.clock.advance(HINT_WAIT_MS)+ expect(w.toasts.filter(t => t === GRAB_HINT)).toEqual([])w.copy(text)await w.clock.settle()- expect(w.pastes).toBe(2)+ expect(w.answers).toEqual(['', ''])+ expect(w.toasts.filter(t => t.startsWith('Intelligent UI: could not read')).length).toBe(2)+})++test('a request over ASK_MAX bytes is not read: an empty answer at once', async ($, on) => {+ const w = world(on)+ await $.command.run({ command: 'iui', args: '' } as any)+ await drawAnswer($, w)+ w.reads.length = 0+ const big = `${COPY}${' '.repeat(ASK_MAX)}`+ void w.shim(big)+ await w.clock.settle()+ expect(w.answers).toEqual([''])+ expect(w.reads.filter(p => p.startsWith(`${ASK_DIR}/`))).toEqual([])+ expect(w.clipboard).toBe(big)expect(w.copies).toEqual([])- expect(w.toasts.filter(t => t.startsWith('Intelligent UI: could not read')).length).toBe(1)})-test('a clipboard change while a copy is read: the newer clipboard is read after it and is not written over', async ($, on) => {+test('a throw while the text is read (a bad selection): an empty answer all the same', async ($, on) => {+ const w = world(on)+ await $.command.run({ command: 'iui', args: '' } as any)+ await drawAnswer($, w)+ w.selects = false+ w.selection = { text: 5 } as any+ void w.shim(COPY)+ await w.clock.settle()+ expect(w.answers).toEqual([''])+ expect(w.clipboard).toBe(COPY)+ expect(w.copies).toEqual([])+})++test('a bad id is never read or answered; an id with no request file gets an empty answer', async ($, on) => {+ const w = world(on)+ await $.command.run({ command: 'iui', args: '' } as any)+ await drawAnswer($, w)+ w.reads.length = 0+ w.files.set('/c/secret', COPY)+ w.files.set(`${ASK_DIR}/.hidden`, COPY)+ for (const id of ['../secret', '.hidden', '..', 'a/b', 'x'.repeat(200), '4242-1-1']) w.readers[0]!.say(`ask ${id}`)+ await w.clock.settle()+ expect(w.reads.filter(p => p.startsWith('/c/'))).toEqual([])+ expect(w.answers).toEqual([])+ // Only 4242-1-1 (no shim waits for it): refused, its files go.+ expect(w.late).toEqual([''])+ expect(w.cleaned).toEqual([`${ASK_DIR}/4242-1-1`])+ expect(w.copies).toEqual([])+ expect(w.toasts.filter(t => t.includes('not ready'))).toEqual([])+})++const NOT_READY = 'Intelligent UI: the picture text was not ready in time; /iui grab copies it'++test('a late answer (the shim gave up at 1.5 s) writes nothing: the cells stay, one toast names /iui grab', async ($, on) => {const w = world(on)await $.command.run({ command: 'iui', args: '' } as any)await drawAnswer($, w)+ // A slow probe: the shim gives up and writes the cells, with its mark.w.hold()w.copy(COPY)await w.clock.settle()- expect(w.queries.length).toBe(1)- w.copy('copied in another app')- await w.clock.settle()- expect(w.pastes).toBe(1)+ await w.clock.advance(1500)+ expect(w.clipboard).toBe(COPY)+ expect([...w.files.keys()].filter(k => k.startsWith(`${ASK_DIR}/`))).toEqual([`${ASK_DIR}/4242-1-7.answer`])w.release()await w.clock.settle()- // One read before the write (not the copy now: no write), one of the newer.- expect(w.pastes).toBe(3)+ // The answer meets the mark: no write, the files go, one toast.+ expect(w.late).toEqual([COPIED])expect(w.copies).toEqual([])- expect(w.clipboard).toBe('copied in another app')+ expect(w.clipboard).toBe(COPY)+ expect([...w.files.keys()].filter(k => k.startsWith(`${ASK_DIR}/`))).toEqual([])+ expect(w.toasts.filter(t => t === NOT_READY)).toEqual([NOT_READY])+ expect(w.toasts.filter(t => t.startsWith('Intelligent UI: copied'))).toEqual([])+ // Its own shim asked for these cells: no /iui grab hint as well.+ await w.clock.advance(HINT_WAIT_MS)+ expect(w.toasts.filter(t => t === GRAB_HINT)).toEqual([])+ // /iui grab then copies the text.+ expect((await $.command.run({ command: 'iui', args: 'grab' } as any)).text).toContain('copied')+ expect(w.copies).toEqual([COPIED])})-test('the same copy again while its picture is read: copied once, one toast', async ($, on) => {+test('session A in fullscreen keeps an old selection equal to the cells; the same cells come on the clipboard not through its shim: no write by A, at most one hint', async ($, on) => {+ const w = world(on)+ await $.command.run({ command: 'iui', args: '' } as any)+ await drawAnswer($, w)+ // An old selection of A, with the text of these cells.+ w.selection = { text: COPY }+ await w.clock.advance(60000)+ // Another session copies identical cells (no ask comes to A).+ w.foreign(COPY)+ await w.clock.settle()+ await w.clock.advance(HINT_WAIT_MS)+ w.change()+ await w.clock.advance(HINT_WAIT_MS)+ expect(w.copies).toEqual([])+ expect(w.answers).toEqual([])+ expect(w.clipboard).toBe(COPY)+ expect(w.toasts.filter(t => t === GRAB_HINT).length).toBeLessThanOrEqual(1)+})++test('the same copy again while its picture is read: one probe, each copy gets the text', async ($, on) => {const w = world(on)await $.command.run({ command: 'iui', args: '' } as any)await drawAnswer($, w)@@ -956,52 +1172,64 @@ test('the same copy again while its picture is read: copied once, one toast', asw.release()await w.clock.settle()expect(w.queries.length).toBe(1)- expect(w.copies).toEqual([COPIED])- expect(w.clipboard).toBe(COPIED)- expect(w.toasts.filter(t => t.startsWith('Intelligent UI: copied'))).toEqual(['Intelligent UI: copied 35 chars of text'])- // A copy of it after the write is a new copy: copied again (no probe).- w.copy(COPY)- await w.clock.settle()expect(w.copies).toEqual([COPIED, COPIED])- expect(w.queries.length).toBe(1)+ expect(w.clipboard).toBe(COPIED)+ expect(w.toasts.filter(t => t.startsWith('Intelligent UI: copied')).length).toBe(2)})-test('a newer clipboard that the watcher has not said yet is not written over; it is read next', async ($, on) => {+test('a watcher that starts again gives no hint for 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).+ w.clipboard = COPYawait $.command.run({ command: 'iui', args: '' } as any)await drawAnswer($, w)- // Other picture cells: copied as their own text, not the first copy's.- w.hold()- w.copy([cells(2, 0, 97), cells(3, 0, 21)].join('\n'))- await w.clock.settle()- w.clipboard = COPY- w.selection = { text: COPY }- w.release()+ await w.clock.advance(3000)+ expect(w.pastes).toBe(0)+ expect(w.toasts.filter(t => t === GRAB_HINT)).toEqual([])+ // A new copy is answered.+ w.copy(COPY)await w.clock.settle()expect(w.copies).toEqual([COPIED])- expect(w.clipboard).toBe(COPIED)- // Text with no picture cells (a page not probed yet): kept as it is.- await drawAnswer($, w, 'ans-2', `${SMALL_MD}\n\nEnd.`)- w.gate = null- w.hold()+})++test('the ask reader that ends on its own is started again, ASK_RESTARTS times at most; a FIFO that is not safe (exit 3) is final', async ($, on) => {+ const w = world(on)+ await $.command.run({ command: 'iui', args: '' } as any)+ await drawAnswer($, w)+ const details = async () => ((await $.command.run({ command: 'iui', args: 'details' } as any)).text ?? '').split('\n').find(l => l.startsWith('Auto copy:'))+ for (let i = 0; i < ASK_RESTARTS; i++) {+ w.readers.at(-1)!.end(0)+ await w.clock.settle()+ expect(await details()).toBe('Auto copy: off (the ask reader does not run)')+ await w.clock.advance(ASK_RESTART_MS)+ expect(w.readers.length).toBe(i + 2)+ expect(await details()).toBe('Auto copy: on')+ }+ // The new reader gets the asks.w.copy(COPY)await w.clock.settle()- expect(w.queries.length).toBe(2)- w.clipboard = 'copied in another app'- w.release()- await w.clock.settle()expect(w.copies).toEqual([COPIED])- expect(w.clipboard).toBe('copied in another app')- expect(w.toasts.filter(t => t.startsWith('Intelligent UI: copied')).length).toBe(1)- // Its change comes late: one read, the same text, nothing more.- const pastes = w.pastes- w.copy('copied in another app')+ // The cap: no more.+ w.readers.at(-1)!.end(0)+ await w.clock.advance(ASK_RESTART_MS * 3)+ expect(w.readers.length).toBe(ASK_RESTARTS + 1)+ // No reader: the shim writes the cells at once (its FIFO open fails).+ w.copy(COPY)await w.clock.settle()- expect(w.pastes).toBe(pastes + 1)+ expect(w.clipboard).toBe(COPY)expect(w.copies).toEqual([COPIED])})-test('at an exit the watcher is killed and no child takes its place; a /clear starts it again with no clipboard part', async ($, on) => {+test('an unsafe FIFO (the reader exits 3) is not read again', async ($, on) => {+ const w = world(on)+ await $.command.run({ command: 'iui', args: '' } as any)+ await drawAnswer($, w)+ w.readers[0]!.end(3)+ await w.clock.advance(ASK_RESTART_MS * 3)+ expect(w.readers.length).toBe(1)+})++test('at an exit the watcher and the ask reader are killed, no child takes their place, the FIFO and the requests go; a /clear keeps the reader', async ($, on) => {const w = world(on)on('session.end', async ($: any, e: any) => ({ sessionId: e.sessionId }))await $.command.run({ command: 'iui', args: '' } as any)@@ -1010,18 +1238,24 @@ test('at an exit the watcher is killed and no child takes its place; a /clear stawait $.session.end({ reason: 'clear', sessionId: 's1' } as any)await w.clock.settle()expect(w.watchers.map(c => c.argv.slice(3))).toEqual([[], ['clip'], []])+ // The process goes on: the reader and its FIFO stay.+ expect(w.ended).toEqual([])+ expect(w.readers.map(c => c.dead)).toEqual([false])expect(w.kills).toEqual([w.watchers[0]!.pid, w.watchers[1]!.pid])w.copy(COPY)await w.clock.settle()expect(w.pastes).toBe(0)+ expect(w.copies).toEqual([COPIED])await $.session.end({ reason: 'prompt_input_exit', sessionId: 's2' } as any)await w.clock.settle()await w.clock.advance(5000)expect(w.watchers.length).toBe(3)- expect(w.kills).toEqual(w.watchers.map(c => c.pid))+ expect(w.readers.length).toBe(1)+ expect(w.kills).toEqual([...w.watchers.map(c => c.pid), w.readers[0]!.pid])+ expect(w.ended).toEqual([[ASK, ASK_DIR, '4242', '/c/shims-4242']])})-test('a clip child that ends on its own is started again; its old clipboard is not written', async ($, on) => {+test('a clip child that ends on its own is started again; its old clipboard gets no hint', async ($, on) => {const w = world(on)await $.command.run({ command: 'iui', args: '' } as any)await drawAnswer($, w)@@ -1037,14 +1271,14 @@ test('a clip child that ends on its own is started again; its old clipboard is nawait w.clock.settle()expect(w.watchers.map(c => c.argv.slice(3))).toEqual([[], ['clip'], ['clip']])expect(w.pastes).toBe(0)- w.copy(COPY)- await w.clock.settle()- expect(w.copies).toEqual([COPIED])+ // The new child sees a later copy of another session.+ w.foreign([cells(1, 0, 97), cells(2, 0, 97), cells(3, 0, 21)].join('\n'))+ await w.clock.advance(HINT_WAIT_MS)+ expect(w.toasts.filter(t => t === GRAB_HINT)).toEqual([GRAB_HINT])})-// ---- The main screen: no selection, so no automatic copy ---------------------+// ---- The main screen: the terminal copies, so no automatic copy -------------const GRAB_TOAST = 'Intelligent UI: picture cells copied; /iui grab puts their text on the clipboard'const FULLSCREEN_TOAST = 'Intelligent UI works best in fullscreen mode: run /tui fullscreen'test('main screen: picture cells on the clipboard are never rewritten (their session is not known); one toast names /iui grab', async ($, on) => {@@ -1052,24 +1286,25 @@ test('main screen: picture cells on the clipboard are never rewritten (their sesw.selects = falseawait $.command.run({ command: 'iui', args: '' } as any)await drawAnswer($, w, 'ans-1', SMALL_MD, false)- // Another session's cells: no write, no probe, one toast.- w.copy(COPY)+ // The terminal's own copy (or another session's): no ask, so no write,+ // no probe, one toast at once.+ w.foreign(COPY)await w.clock.settle()await w.clock.advance(1000)expect(w.pastes).toBe(1)expect(w.copies).toEqual([])expect(w.queries).toEqual([])- expect(w.toasts.filter(t => t.includes('/iui grab'))).toEqual([GRAB_TOAST])+ expect(w.toasts.filter(t => t.includes('/iui grab'))).toEqual([GRAB_HINT])// dump shows them (its read probes the page).const dumped = await $.command.run({ command: 'iui', args: 'dump' } as any)expect(dumped.text).toContain('auto copy off on the main screen (use /iui grab)')expect(dumped.text).toContain('-> answer ans-1 page 0')// The same cells again: no second toast.- w.copy(COPY)+ w.foreign(COPY)await w.clock.settle()expect(w.toasts.filter(t => t.includes('/iui grab')).length).toBe(1)// Cells that fit no page drawn here (column 150): no toast.- w.copy([cells(1, 0, 150), cells(2, 0, 150)].join('\n'))+ w.foreign([cells(1, 0, 150), cells(2, 0, 150)].join('\n'))await w.clock.settle()expect(w.toasts.filter(t => t.includes('/iui grab')).length).toBe(1)expect(w.copies).toEqual([])@@ -1081,7 +1316,7 @@ test('main screen: /iui grab with no selection reads the clipboard and writes itw.selects = falseawait $.command.run({ command: 'iui', args: '' } as any)await drawAnswer($, w, 'ans-1', SMALL_MD, false)- w.copy(COPY)+ w.foreign(COPY)await w.clock.settle()const grabbed = await $.command.run({ command: 'iui', args: 'grab' } as any)expect(grabbed.text).toBe('Intelligent UI: copied 35 chars (1 of 1 picture runs read).')@@ -1093,28 +1328,41 @@ test('main screen: /iui grab with no selection reads the clipboard and writes itconst again = await $.command.run({ command: 'iui', args: 'grab' } as any)expect(again.text).toBe('Intelligent UI: the clipboard has the text already.')expect(w.copies).toEqual([COPIED])- w.copy('plain text')+ w.foreign('plain text')await w.clock.settle()const none = await $.command.run({ command: 'iui', args: 'grab' } as any)expect(none.text).toBe('Intelligent UI: no mouse selection and no picture cells on the clipboard; select over a picture and copy first.')expect(w.copies).toEqual([COPIED])})-test('fullscreen or main screen not known yet: the clipboard is never written', async ($, on) => {+test('main screen: /copy or $.ui.copy of picture cells gets their text (the ui.copy hook), with no hint', { plugins: [COPIER] }, async ($, on) => {+ const w = world(on)+ w.selects = false+ await $.command.run({ command: 'iui', args: '' } as any)+ await drawAnswer($, w, 'ans-1', SMALL_MD, false)+ await $.command.run({ command: 'cp', args: COPY } as any)+ await w.clock.advance(HINT_WAIT_MS)+ expect(w.copies).toEqual([COPIED])+ expect(w.clipboard).toBe(COPIED)+ expect(w.toasts.filter(t => t === GRAB_HINT)).toEqual([])+})++// The ask says whose copy it is, so a write is safe before the layout is known.+test('fullscreen or main screen not known yet: a copy through the shim gets its text, another one gets the hint', async ($, on) => {const w = world(on)w.selects = falseawait $.command.run({ command: 'iui', args: '' } as any)await drawAnswer($, w, 'ans-1', SMALL_MD, null)- // Even with a selection that is the copy.- w.selection = { text: COPY, requestId: 'ans-1' }+ w.foreign(COPY)+ await w.clock.advance(HINT_WAIT_MS)+ expect(w.copies).toEqual([])+ expect(w.toasts.filter(t => t === GRAB_HINT)).toEqual([GRAB_HINT])w.copy(COPY)await w.clock.settle()- await w.clock.advance(1000)- expect(w.copies).toEqual([])- expect(w.queries).toEqual([])+ expect(w.copies).toEqual([COPIED])const details = await $.command.run({ command: 'iui', args: 'details' } as any)- expect(details.text).toContain('\nAuto copy: off (fullscreen or main screen is not known yet)')- // No advice either: the layout is not known.+ expect(details.text).toContain('\nAuto copy: on')+ // No advice: the layout is not known.expect(w.toasts).not.toContain(FULLSCREEN_TOAST)})@@ -1147,3 +1395,110 @@ test('the fullscreen advice is not shown with Intelligent UI off', async ($, on)await drawAnswer($, w, 'ans-1', SMALL_MD, false)expect(w.toasts).not.toContain(FULLSCREEN_TOAST)})++// ---- Picture select -----------------------------------------------------------++// The tty of the fixture's page: 100 columns (98 for the picture) of 16 x 32 px.+const TTY = '40 100 1600 1280'+const tree = (node: any): any[] => (node && typeof node === 'object' ? [node, ...(node.children ?? []).flatMap(tree)] : [])+const stripImages = (drawn: any) => tree(drawn).filter(n => n.type === 'Image' && /\.strips\//.test(n.props.source?.file ?? ''))+// The selected rows drawn: each strip's row (its Box in the flow, its+// marginTop the rows after the row before), and its Box's columns.+const selectedOf = (drawn: any) => {+ let next = 0+ return tree(drawn)+ .filter(n => n.type === 'Box' && n.props.overflow === 'hidden' && n.props.height === 1 && n.props.backgroundColor)+ .map(n => {+ const row = next + n.props.marginTop+ next = row + 1+ return { row, x0: n.props.marginLeft, x1: n.props.marginLeft + n.props.width - 1 }+ })+}++test('picture select: a drag over a page draws its rows over it and copies their text on the up', async ($, on) => {+ const w = world(on, { tty: TTY })+ await $.command.run({ command: 'iui', args: '' } as any)+ const ui = await drawAnswer($, w)+ const client = tree(await ui.drawn()).find(n => n.type === 'Client')+ expect(client?.props).toMatchObject({ key: 'sel0', width: 98, height: 16, props: { id: 'ans-1', page: 0 } })+ await ui.pointer({ type: 'down', x: 0, y: 1, button: 'left', in: 'sel0' })+ await w.clock.settle()+ // The page's strips are cut at the down: rows of 32 px.+ expect(w.strips.length).toBe(1)+ expect(w.strips[0]).toContain('row=32')+ await ui.pointer({ type: 'move', x: 10, y: 2, button: 'left', in: 'sel0' })+ await ui.pointer({ type: 'up', x: 21, y: 3, button: 'left', in: 'sel0' })+ await w.clock.settle()+ expect(w.copies).toEqual([COPIED])+ expect(w.toasts).toContain(`Intelligent UI: copied ${COPIED.length} chars of text`)+ const drawn = await ui.drawn()+ expect(selectedOf(drawn)).toEqual([{ row: 1, x0: 0, x1: 97 }, { row: 2, x0: 0, x1: 97 }, { row: 3, x0: 0, x1: 21 }])+ expect(stripImages(drawn).map(n => n.props.source.file.replace(/^.*\//, ''))).toEqual(['s-02.png', 's-03.png', 's-04.png'])+ expect(stripImages(drawn).every(n => n.props.columns === 98 && n.props.rows === 1)).toBe(true)+ // The rows, then the Client, pulled up over the page in the flow: no+ // absolute Box (the engine moves one scrolled above the screen to row 0).+ const page = tree(drawn).find(n => n.type === 'Box' && n.props.flexDirection === 'column' && n.props.width === 98 && n.props.height === 16)+ expect(page.children.map((c: any) => c.props.marginTop)).toEqual([undefined, -16, -16])+ expect(page.children[2].children[0].type).toBe('Client')+ expect(tree(page).some(n => n.props?.position === 'absolute')).toBe(false)+ // A click with no drag clears it, and copies nothing.+ await ui.pointer({ type: 'down', x: 5, y: 5, button: 'left', in: 'sel0' })+ await ui.pointer({ type: 'up', x: 5, y: 5, button: 'left', in: 'sel0' })+ await w.clock.settle()+ expect(selectedOf(await ui.drawn())).toEqual([])+ expect(w.copies).toEqual([COPIED])+ // The strips are cut once.+ expect(w.strips.length).toBe(1)+})++test('picture select: with IUI_DEBUG, each post writes a trace line (the post, its region, the rows selected)', async ($, on) => {+ const w = world(on, { tty: TTY, env: { HOME: '/h', CLAUDE_CODE_FORCE_TERMINAL_IMAGES: '1', IUI_DEBUG: '1' } })+ await $.command.run({ command: 'iui', args: '' } as any)+ const ui = await drawAnswer($, w)+ await ui.pointer({ type: 'down', x: 0, y: 1, button: 'left', in: 'sel0' })+ await ui.pointer({ type: 'move', x: 10, y: 2, button: 'left', in: 'sel0' })+ await w.clock.settle()+ const lines = w.traces.filter(t => t.includes('select: '))+ expect(lines.length).toBe(2)+ expect(lines[0]).toContain('select: down #')+ // The kit gives a Client the terminal's size, the engine its region's.+ expect(lines[0]).toMatch(/ans-1 page 0 anchor 0,1 head 0,1 size \d+x\d+ drawn 98x16 onScreen /)+ expect(lines[0]).toContain('rows none')+ expect(lines[1]).toContain('anchor 0,1 head 10,2 size ')+ expect(lines[1]).toContain('rows p0 1..2 (2)')+})++test('picture select: with no IUI_DEBUG, a post writes no trace line', async ($, on) => {+ const w = world(on, { tty: TTY })+ await $.command.run({ command: 'iui', args: '' } as any)+ const ui = await drawAnswer($, w)+ await ui.pointer({ type: 'down', x: 0, y: 1, button: 'left', in: 'sel0' })+ await w.clock.settle()+ expect(w.traces.filter(t => t.includes('select: '))).toEqual([])+})++test('picture select: none before Claude Code 2.1.305', async ($, on) => {+ const old = world(on, { tty: TTY, version: '2.1.304' })+ await $.command.run({ command: 'iui', args: '' } as any)+ const ui = await drawAnswer($, old)+ expect(tree(await ui.drawn()).some(n => n.type === 'Client')).toBe(false)+})++test('picture select: none on the main screen', async ($, on) => {+ const w = world(on, { tty: TTY })+ await $.command.run({ command: 'iui', args: '' } as any)+ const ui = await drawAnswer($, w, 'ans-1', SMALL_MD, false)+ expect(tree(await ui.drawn()).some(n => n.type === 'Client')).toBe(false)+})++test('picture select: /iui select off takes the Client away', async ($, on) => {+ const w = world(on, { tty: TTY })+ await $.command.run({ command: 'iui', args: '' } as any)+ const ui = await drawAnswer($, w)+ expect(tree(await ui.drawn()).some(n => n.type === 'Client')).toBe(true)+ await $.command.run({ command: 'iui', args: 'select off' } as any)+ await w.clock.settle()+ await ui.redraw(props(SMALL_MD))+ await w.clock.settle()+ expect(tree(await ui.drawn()).some(n => n.type === 'Client')).toBe(false)+})diff --git a/tests/linux.test.ts b/tests/linux.test.tsindex b79f154..d346987 100644--- a/tests/linux.test.ts+++ b/tests/linux.test.ts@@ -2,10 +2,11 @@ import { expect, mock, test } from 'claude-code/testing'import {autoCopyText, clipToolsOf, clipWayOf, editorArgv, familyOfFc, FIND_SH, GHOSTTY_DEFAULT_FONT, GTK_READ_PY, GTK_WATCH_PY, guiEditorOf, LINUX_EDITORS,- placeholderCell, schemeOfPortal, SETUP_NOT_MAC, WATCH_PL, WL_WATCH_SH, X11_WATCH_SH, type ClipWay,+ placeholderCell, schemeOfPortal, SETUP_NOT_MAC, WATCH_PL, WL_WATCH_SH, X11_WATCH_SH, type ClipWay, type ShimState,} from '../hooks/register'import { CACHE_SH } from '../hooks/lib/env'-import { clipboardNow, clipWatchArgs, clipWayReady, findClipWay } from '../hooks/lib/clipboard'+import { ANSWER_SH, ASK_END_SH, ASK_PL, clipboardNow, clipWatchArgs, clipWayReady, findClipWay, SHIM_SETUP } from '../hooks/lib/clipboard'+import { ASKED_MS, HINT_WAIT_MS } from '../hooks/lib/settings'import { S } from '../hooks/lib/state'import { SMALL_MARKS, SMALL_MD } from './copy-fixture'@@ -75,24 +76,35 @@ test('the GTK scripts: X11 events, a line at the start, a text read that fails wexpect(GTK_READ_PY).toContain('if t is None: sys.exit(1)')})-test('the automatic copy line: the macOS texts as they were, the Linux way or why it is off', () => {- const base = { isMac: true, hostOs: 'Darwin', isRemote: false, isOn: true, isFullscreen: true as boolean | null, none: false, drawn: true, running: true, way: null }+test('the automatic copy line: the shims and the ask reader say on or off; the watcher only the /iui grab hint', () => {+ const base = {+ isMac: true, hostOs: 'Darwin', isRemote: false, isOn: true, isFullscreen: true as boolean | null, none: false, drawn: true, asking: true, way: null,+ shims: { tools: ['pbcopy'] } as ShimState | null,+ }expect(autoCopyText(base)).toBe('on')// Only fullscreen copies by itself: the main screen has /iui grab.expect(autoCopyText({ ...base, isFullscreen: false })).toBe('off on the main screen (use /iui grab)')expect(autoCopyText({ ...base, isFullscreen: false, isOn: false })).toBe('off (Intelligent UI is off)')- expect(autoCopyText({ ...base, isFullscreen: null })).toBe('off (fullscreen or main screen is not known yet)')+ // The layout not known yet: the ask says whose copy it is, so it is on.+ expect(autoCopyText({ ...base, isFullscreen: null })).toBe('on')+ // The shims: not set up yet, or off with the reason.+ expect(autoCopyText({ ...base, shims: null })).toBe('off (the clipboard shims are not set up yet)')+ expect(autoCopyText({ ...base, shims: { off: 'no pbcopy' } })).toBe('off (no pbcopy)')+ expect(autoCopyText({ ...base, shims: { off: 'no perl' } })).toBe('off (no perl)')+ expect(autoCopyText({ ...base, shims: { off: 'no pbcopy' }, isRemote: true })).toBe('off (ssh)')expect(autoCopyText({ ...base, isFullscreen: null, drawn: false })).toBe('off (no picture yet)')- expect(autoCopyText({ ...base, none: true })).toBe('off (no PerlObjCBridge in /usr/bin/perl)')+ expect(autoCopyText({ ...base, asking: false })).toBe('off (the ask reader does not run)')+ expect(autoCopyText({ ...base, none: true })).toBe('on (no /iui grab hint: no PerlObjCBridge in /usr/bin/perl)')expect(autoCopyText({ ...base, isRemote: true })).toBe('off (ssh)')- expect(autoCopyText({ ...base, running: false })).toBe('off (the watcher does not run)')const wl = clipWayOf({ wayland: true, x11: false, desktop: 'sway', found: ALL })const linux = { ...base, isMac: false, hostOs: 'Linux', way: wl }expect(autoCopyText(linux)).toBe('on, with wl-paste --watch (Wayland)')- expect(autoCopyText({ ...linux, none: true })).toBe('off (wl-paste --watch (Wayland) stopped: the compositor may not have the data-control protocol)')- expect(autoCopyText({ ...linux, way: clipWayOf({ wayland: false, x11: true, desktop: '', found: ALL }), none: true })).toBe('off (clipnotify and xclip (X11) stopped)')- expect(autoCopyText({ ...linux, way: { off: 'no Wayland or X11 display' } })).toBe('off (no Wayland or X11 display)')- expect(autoCopyText({ ...linux, way: null })).toBe('off (the clipboard tools are not looked up yet)')+ expect(autoCopyText({ ...linux, none: true })).toBe('on (no /iui grab hint: wl-paste --watch (Wayland) stopped: the compositor may not have the data-control protocol)')+ expect(autoCopyText({ ...linux, way: clipWayOf({ wayland: false, x11: true, desktop: '', found: ALL }), none: true })).toBe('on (no /iui grab hint: clipnotify and xclip (X11) stopped)')+ expect(autoCopyText({ ...linux, way: { off: 'no Wayland or X11 display' } })).toBe('on (no /iui grab hint: no Wayland or X11 display)')+ expect(autoCopyText({ ...linux, way: null })).toBe('on')+ const addon = { off: 'no wl-copy, xclip or xsel: Claude Code copies with its own addon' }+ expect(autoCopyText({ ...linux, shims: addon })).toBe('off (no wl-copy, xclip or xsel: Claude Code copies with its own addon)')expect(autoCopyText({ ...linux, drawn: false })).toBe('off (no picture yet)')expect(autoCopyText({ ...linux, isFullscreen: false })).toBe('off on the main screen (use /iui grab)')expect(autoCopyText({ ...linux, hostOs: 'FreeBSD' })).toBe('off (not macOS or Linux)')@@ -215,21 +227,54 @@ const cells = (row: number, from: number, to: number, lead = ' ') =>// A Linux computer in kitty on Wayland (sway): uname says Linux, the tools// are wl-paste only, the portal says `scheme`, kitty's config names no font// (fontconfig gives one), and the watcher is `perl` with the notifier after-// `clip`. `copy` changes the clipboard: each clip child prints a line.-function linuxWorld(on: any, o: { scheme?: number; noPortal?: boolean; env?: Record<string, string>; noTypst?: boolean; jetbrains?: boolean } = {}) {+// `clip`. The clipboard write tools found are `writers` (wl-copy by default),+// with perl and the FIFO L_ASK; the ask reader is `perl` too. `copy` is+// Claude Code's own copy: the OSC 52 write, then the shim, whose ask for+// picture cells the reader gets and the mod answers (ANSWER_SH). `foreign`+// is a copy with no ask (another session's, the terminal's): each changes+// the clipboard, and each clip child prints a line.+function linuxWorld(on: any, o: { scheme?: number; noPortal?: boolean; env?: Record<string, string>; noTypst?: boolean; jetbrains?: boolean; writers?: string[] } = {}) {mock.store(on)const clock = mock.clock(on)- mock.env(on, o.env ?? { HOME: '/h', TERM: 'xterm-kitty', KITTY_WINDOW_ID: '1', WAYLAND_DISPLAY: 'wayland-1', XDG_CURRENT_DESKTOP: 'sway' })+ const vars: Record<string, string | undefined> = {+ PATH: '/usr/local/bin:/usr/bin', ...(o.env ?? { HOME: '/h', TERM: 'xterm-kitty', KITTY_WINDOW_ID: '1', WAYLAND_DISPLAY: 'wayland-1', XDG_CURRENT_DESKTOP: 'sway' }),+ }+ on('env.get', async ($: any, e: any) => ({ value: vars[e.name] }))+ on('env.set', async ($: any, e: any) => {+ vars[e.name] = e.value+ return { value: {} }+ })const files = new Map<string, string>()const ran: string[][] = []const watchers: { argv: string[]; say: (line: string) => void; dead: boolean; n: number }[] = []+ const readers: { argv: string[]; say: (line: string) => void; dead: boolean }[] = []+ const waits = new Map<string, (text: string) => void>()const w = {- clock, files, ran, watchers, clipboard: 'old', reads: 0, copies: [] as string[], toasts: [] as string[],+ clock, files, ran, watchers, readers, vars, clipboard: 'old', reads: 0, asks: 0, copies: [] as string[], toasts: [] as string[], ended: [] as string[][],selection: undefined as { text: string } | undefined,copy(text: string) {- w.clipboard = text// A mouse copy of picture cells in fullscreen is this session's selection.if (text.includes('\u{10EEEE}')) w.selection = { text }+ w.foreign(text)+ void w.shim(text)+ },+ async shim(text: string) {+ let out = text+ const reader = readers.find(c => !c.dead)+ if (text.includes('\u{10EEEE}') && reader) {+ const id = `77-${++w.asks}-1`+ files.set(`/c/ask/${id}`, text)+ const answered = new Promise<string | null>(r => waits.set(id, r))+ reader.say(`ask ${id}`)+ out = (await Promise.race([answered, clock.sleep(1500).then(() => null)])) ?? text+ waits.delete(id)+ files.delete(`/c/ask/${id}`)+ }+ if (out !== text) w.copies.push(out)+ w.foreign(out)+ },+ foreign(text: string) {+ w.clipboard = textfor (const c of watchers) if (!c.dead && c.argv[3] === 'clip') c.say(`clip ${c.n++}`)},}@@ -252,6 +297,19 @@ function linuxWorld(on: any, o: { scheme?: number; noPortal?: boolean; env?: Recawait new Promise<void>(r => (wake = r))}}+ if (argv[0] === 'perl' && argv[1] === '-e' && argv[2] === ASK_PL && argv[3] === L_ASK) {+ const pid = String(pids++)+ const lines = [`pid ${pid}`]+ let wake = () => {}+ const me = { argv, dead: false, say: (line: string) => (lines.push(line), wake()) }+ readers.push(me)+ killers.set(pid, () => ((me.dead = true), wake()))+ for (;;) {+ while (lines.length && !me.dead) yield { stream: 'stdout', text: `${lines.shift()}\n` }+ if (me.dead) return { value: { code: null, signal: 'SIGTERM' } }+ await new Promise<void>(r => (wake = r))+ }+ }if (argv[0] !== 'sh' || argv[2] !== WRAP || argv[4] !== '/usr/bin/typst') return { value: { code: 2, signal: null } }yield { stream: 'stdout', text: `${pids++}\n` }const typst = argv.slice(4)@@ -271,6 +329,23 @@ function linuxWorld(on: any, o: { scheme?: number; noPortal?: boolean; env?: Recif (/cc-intelligent-ui-/.test(cmd)) return ok('/c\nLinux\n/h/.cache/cc-intelligent-ui/designs\n')if (cmd.includes('command -v typst')) return o.noTypst ? ok('', 1) : ok('/usr/bin/typst\n')if (argv[0] === 'sh' && argv[2] === FIND_SH) return ok('wl-paste /usr/bin/wl-paste\n')+ if (argv[0] === 'sh' && argv[1]?.endsWith(SHIM_SETUP) && argv[2] === 'find') {+ return ok(`pid 77\n${(o.writers ?? ['wl-copy']).map(t => `${t} /usr/bin/${t}\n`).join('')}perl /usr/bin/perl\nask ${L_ASK}\n`)+ }+ if (argv[0] === 'sh' && argv[2] === ANSWER_SH) {+ const to = waits.get(argv[4]!.slice('/c/ask/'.length))+ if (!to) return ok('', 1)+ to(e.init?.stdin ?? '')+ return ok('')+ }+ if (argv[0] === 'sh' && argv[2] === ASK_END_SH) {+ w.ended.push(argv.slice(4))+ return ok('')+ }+ if (argv[0] === 'sh' && argv[1]?.endsWith(SHIM_SETUP) && argv[2] === 'install') {+ for (const f of argv.slice(5)) files.set(f, files.get(`${f}${argv[4]}`) ?? '')+ return ok('')+ }if (argv[0] === '/usr/bin/typst' && argv[1] === 'eval') return ok('{}')if (cmd.includes('od -An')) return ok(argv.filter(a => a.endsWith('.png')).map(() => `${HEAD}\n@\n`).join(''))if (argv[0] === 'gdbus') return o.noPortal ? ok('', 1) : ok(`(<<uint32 ${o.scheme ?? 1}>>,)\n`)@@ -288,7 +363,9 @@ function linuxWorld(on: any, o: { scheme?: number; noPortal?: boolean; env?: Recfiles.set(e.path, e.text)return { value: undefined }})- on('fs.read', async ($: any, e: any, next: any) => (files.has(e.path) ? { value: files.get(e.path) } : e.path.startsWith('/c/') || e.path.startsWith('/h/') ? { deny: 'ENOENT' } : next(e)))+ on('fs.stat', async ($: any, e: any, next: any) =>+ e.path.startsWith('/c/ask/') && files.has(e.path) ? { value: { kind: 'file', size: 1, mtimeMs: 1, isLink: false } } : next(e))+ on('fs.read', async ($: any, e: any, next: any) => (e.path.endsWith('/hooks/clip-shim.sh') ? { value: 'tool=@TOOL@ real=@REAL@' } : files.has(e.path) ? { value: files.get(e.path) } : e.path.startsWith('/c/') || e.path.startsWith('/h/') ? { deny: 'ENOENT' } : next(e)))on('fs.exists', async ($: any, e: any, next: any) => (o.noTypst || e.path.startsWith('/h/') ? { value: false } : next(e)))on('fs.list', async ($: any, e: any, next: any) => {if (!e.path.startsWith('/c/')) return next(e)@@ -306,9 +383,13 @@ function linuxWorld(on: any, o: { scheme?: number; noPortal?: boolean; env?: Recreturn { value: undefined }})on('ui.render', () => ({ type: 'Text', props: {}, children: ['engine drawing'] }) as any)+ on('session.end', async ($: any, e: any) => ({ sessionId: e.sessionId }))return w}+// The FIFO of this process (pid 77).+const L_ASK = '/c/ask-77'+const message = (requestId: string, text: string, isFullscreen = true) =>({plugin: 'cc-intelligent-ui', surface: 'terminal', component: 'AssistantMessage', requestId,@@ -317,7 +398,7 @@ const message = (requestId: string, text: string, isFullscreen = true) =>const autoLine = async ($: any) => ((await $.command.run({ command: 'iui', args: 'details' } as any)).text ?? '').split('\n').find((l: string) => l.startsWith('Auto copy:'))-test('Linux on Wayland: the watcher runs wl-paste --watch after clip, and a copy of picture cells is read with wl-paste and written as text', async ($, on) => {+test('Linux on Wayland: the reader (perl) gets the asks of the wl-copy shim, and the watcher runs wl-paste --watch after clip for the hint', async ($, on) => {const w = linuxWorld(on)await $.command.run({ command: 'iui', args: '' } as any)expect(await autoLine($)).toBe('Auto copy: off (no picture yet)')@@ -329,6 +410,7 @@ test('Linux on Wayland: the watcher runs wl-paste --watch after clip, and a copyawait w.clock.settle()// The size watcher (perl from the PATH), then the one with the notifier.expect(w.watchers.map(c => c.argv.slice(3))).toEqual([[], ['clip', '/usr/bin/wl-paste', '--watch', 'sh', '-c', WL_WATCH_SH]])+ expect(w.readers.map(c => c.argv[0])).toEqual(['perl'])expect(await autoLine($)).toBe('Auto copy: on, with wl-paste --watch (Wayland)')// The first line (the notifier's start) is not a change: no read.expect(w.reads).toBe(0)@@ -340,8 +422,45 @@ test('Linux on Wayland: the watcher runs wl-paste --watch after clip, and a copyw.copy('plain text')await w.clock.settle()expect(w.copies.length).toBe(1)+ // Another session's copy of picture cells (no ask here, none of them lately): never written, one hint after the wait.+ await w.clock.advance(ASKED_MS)+ w.foreign([cells(1, 0, 97), cells(2, 0, 97), cells(3, 0, 21)].join('\n'))+ await w.clock.advance(HINT_WAIT_MS)+ expect(w.copies.length).toBe(1)+ expect(w.toasts).toContain('Intelligent UI: picture cells copied; /iui grab puts their text on the clipboard')// pbpaste is never run on Linux.expect(w.ran.some(argv => argv[0] === 'pbpaste')).toBe(false)+ // The wl-copy shim only (the tool found), its folder first on PATH.+ expect([...w.files.keys()].filter(k => k.startsWith('/c/shims') && !k.includes('.new-'))).toEqual(['/c/shims-77/wl-copy'])+ expect(w.vars.PATH).toBe('/c/shims-77:/usr/local/bin:/usr/bin')+ expect(w.vars.IUI_COPY_ASK).toBe(L_ASK)+ // The end of the session: the FIFO, the requests and the shims of pid 77 go.+ await $.session.end({ reason: 'prompt_input_exit', sessionId: 's1' } as any)+ await w.clock.settle()+ expect(w.ended).toEqual([[L_ASK, '/c/ask', '77', '/c/shims-77']])+ expect(w.readers.map(c => c.dead)).toEqual([true])+})++test('Linux with no wl-copy, xclip or xsel: Claude Code copies with its addon, so no shim, no automatic copy; the hint and /iui grab stay', async ($, on) => {+ const w = linuxWorld(on, { writers: [] })+ await $.command.run({ command: 'iui', args: '' } as any)+ const ui = await $.ui.mount(message('ans-1', SMALL_MD))+ await w.clock.settle()+ await ui.redraw({ text: SMALL_MD, isFirstOfReply: true } as any)+ await w.clock.settle()+ await w.clock.advance(300)+ await w.clock.settle()+ expect(await autoLine($)).toBe('Auto copy: off (no wl-copy, xclip or xsel: Claude Code copies with its own addon)')+ expect(w.vars.PATH).toBe('/usr/local/bin:/usr/bin')+ expect(w.vars.IUI_COPY_ASK).toBeUndefined()+ expect(w.readers).toEqual([])+ w.copy([cells(1, 0, 97), cells(2, 0, 97), cells(3, 0, 21)].join('\n'))+ await w.clock.advance(HINT_WAIT_MS)+ expect(w.copies).toEqual([])+ expect(w.toasts).toContain('Intelligent UI: picture cells copied; /iui grab puts their text on the clipboard')+ w.selection = undefined+ const grabbed = await $.command.run({ command: 'iui', args: 'grab' } as any)+ expect(grabbed.text).toBe('Intelligent UI: copied 35 chars (1 of 1 picture runs read).')})test('Linux on Wayland, main screen: a copy of picture cells is not changed; /iui grab reads it with wl-paste and writes its text', async ($, on) => {@@ -354,8 +473,8 @@ test('Linux on Wayland, main screen: a copy of picture cells is not changed; /iuawait w.clock.advance(300)await w.clock.settle()expect(await autoLine($)).toBe('Auto copy: off on the main screen (use /iui grab)')- w.copy([cells(1, 0, 97, '● '), cells(2, 0, 97), cells(3, 0, 21)].join('\n'))- w.selection = undefined+ // The terminal's own copy: no ask, the hint at once.+ w.foreign([cells(1, 0, 97, '● '), cells(2, 0, 97), cells(3, 0, 21)].join('\n'))await w.clock.settle()expect(w.copies).toEqual([])expect(w.toasts).toContain('Intelligent UI: picture cells copied; /iui grab puts their text on the clipboard')diff --git a/tests/select.test.ts b/tests/select.test.tsnew file mode 100644index 0000000..688d08e--- /dev/null+++ b/tests/select.test.ts@@ -0,0 +1,210 @@+import { expect, test } from 'claude-code/testing'++import { decodeSelection, placeholderCell, positionsOf, textOfRun, type Mark } from '../hooks/register'+import {+ needsSelect, overlayRows, postOf, rowsAbove, runsOf, selectColor, selectedRows, selectionAfter, selectTrace, stripDir, stripFile, stripPlan, textOfRuns, topsOf,+ type SelectPost,+} from '../hooks/lib/select'+import { stripsCall } from '../hooks/lib/strips'+import { SMALL_MARKS, SMALL_PAGE, SMALL_PPI } from './copy-fixture'++// ---- The posts --------------------------------------------------------------++const post = (o: Partial<SelectPost> = {}): SelectPost => ({ id: 'ans-1', page: 0, anchor: { x: 2, y: 1 }, head: { x: 7, y: 3 }, phase: 'move', seq: 1, ...o })++test('a post is read only when it is whole: an id, a page, two points of whole cells, a phase and an order', () => {+ expect(postOf(post())).toEqual(post())+ expect(postOf({ ...post(), extra: 1 })).toEqual(post())+ expect(postOf(null)).toBe(null)+ expect(postOf('down')).toBe(null)+ expect(postOf({ ...post(), id: 3 })).toBe(null)+ expect(postOf({ ...post(), page: -1 })).toBe(null)+ expect(postOf({ ...post(), anchor: { x: 1.5, y: 0 } })).toBe(null)+ expect(postOf({ ...post(), head: null })).toBe(null)+ expect(postOf({ ...post(), phase: 'drag' })).toBe(null)+ expect(postOf({ ...post(), seq: '1' })).toBe(null)+})++test('a post selects in the drawing\'s rows (its page\'s top added); a clear leaves none', () => {+ const tops = topsOf([{ columns: 98, rows: 16 }, { columns: 98, rows: 10 }, { columns: 98, rows: 4 }])+ expect(tops).toEqual([0, 16, 26])+ expect(selectionAfter(post({ phase: 'down', head: { x: 2, y: 1 } }), tops)).toEqual({ id: 'ans-1', anchor: { x: 2, y: 1 }, head: { x: 2, y: 1 }, isUp: false })+ // Page 1's region: its rows from 16; a head past its edge goes on into page 2.+ expect(selectionAfter(post({ page: 1, head: { x: 7, y: 12 } }), tops)).toEqual({ id: 'ans-1', anchor: { x: 2, y: 17 }, head: { x: 7, y: 28 }, isUp: false })+ expect(selectionAfter(post({ phase: 'up' }), tops)?.isUp).toBe(true)+ expect(selectionAfter(post({ phase: 'clear' }), tops)).toBe(null)+ // A page the drawing does not have.+ expect(selectionAfter(post({ page: 3 }), tops)).toBe(null)+})++test('a post keeps its region\'s size when it is whole cells; a bad one is dropped, the post kept', () => {+ expect(postOf({ ...post(), size: { columns: 98, rows: 16 } })).toEqual(post({ size: { columns: 98, rows: 16 } }))+ expect(postOf({ ...post(), size: { columns: 98 } })).toEqual(post())+ expect(postOf({ ...post(), size: { columns: 98, rows: 1.5 } })).toEqual(post())+ expect(postOf({ ...post(), size: null })).toEqual(post())+})++// ---- Scrolled pages -------------------------------------------------------------++test('the overlay rows are in the flow: each row\'s marginTop is the rows after the one before', () => {+ expect(overlayRows([])).toEqual([])+ expect(overlayRows([{ row: 0, x0: 3, x1: 9 }])).toEqual([{ row: 0, x0: 3, x1: 9, top: 0 }])+ // Rows 5, 6 and 7: 5 rows down, then each under the one before.+ expect(overlayRows([{ row: 5, x0: 4, x1: 97 }, { row: 6, x0: 0, x1: 97 }, { row: 7, x0: 0, x1: 20 }]).map(r => r.top)).toEqual([5, 0, 0])+ // A gap (rows that are not selected) is the margin; out of order still lands on its row.+ expect(overlayRows([{ row: 2, x0: 0, x1: 1 }, { row: 9, x0: 0, x1: 1 }]).map(r => r.top)).toEqual([2, 6])+ expect(overlayRows([{ row: 9, x0: 0, x1: 1 }, { row: 2, x0: 0, x1: 1 }]).map(r => r.top)).toEqual([9, -8])+ // Each row's top: the sum of the margins and rows before it.+ let at = 0+ const rows = [3, 4, 10, 15].map(row => ({ row, x0: 0, x1: 1 }))+ expect(overlayRows(rows).map(r => (at += r.top + 1) - 1)).toEqual([3, 4, 10, 15])+})++test('the rows of a page above the viewport: the message\'s first row on screen less the page\'s top, 0 to its rows', () => {+ // Page at row 1 of the message (the top margin), 16 rows.+ expect(rowsAbove(0, 1, 16)).toBe(0)+ expect(rowsAbove(1, 1, 16)).toBe(0)+ expect(rowsAbove(8, 1, 16)).toBe(7)+ // All of it scrolled away.+ expect(rowsAbove(40, 1, 16)).toBe(16)+ // The second page, at row 17: the first one is scrolled away first.+ expect(rowsAbove(10, 17, 10)).toBe(0)+ expect(rowsAbove(20, 17, 10)).toBe(3)+ expect(rowsAbove(undefined, 1, 16)).toBe(undefined)+ expect(rowsAbove(3, undefined, 16)).toBe(undefined)+})++test('the trace line of a post: the post, its region, onScreen, the page\'s top and rows above, and the rows selected', () => {+ const pages = [{ file: '/c/p-1.png', width: 1568, height: 512, columns: 98, rows: 16 }, { file: '/c/p-2.png', width: 1568, height: 320, columns: 98, rows: 10 }]+ // The answer's pages over its one link row, 8 rows of it scrolled away.+ const d = { id: 'ans-1', seq: 1, dir: '/c', pages, place: { top: 1, below: 1 }, onScreen: { first: 8, last: 27, of: 28 } }+ const p = post({ page: 0, anchor: { x: 2, y: 9 }, head: { x: 7, y: 11 }, size: { columns: 98, rows: 16 } })+ const sel = selectionAfter(p, topsOf(pages))+ expect(selectTrace(p, d, sel)).toBe('select: move #1 ans-1 page 0 anchor 2,9 head 7,11 size 98x16 drawn 98x16 onScreen 8-27/28 top 1 above 7 rows p0 9..11 (3)')+ // Page 1, its top at row 17: nothing of it above.+ const q = post({ page: 1, anchor: { x: 0, y: 0 }, head: { x: 3, y: 2 } })+ expect(selectTrace(q, d, selectionAfter(q, topsOf(pages)))).toBe('select: move #1 ans-1 page 1 anchor 0,0 head 3,2 size ? drawn 98x10 onScreen 8-27/28 top 17 above 0 rows p1 0..2 (3)')+ expect(selectTrace(p, { ...d, onScreen: null }, null)).toContain('onScreen off top ? above ? rows none')+ expect(selectTrace(p, undefined, null)).toBe('select: move #1 ans-1 page 0 anchor 2,9 head 7,11 size 98x16 (no drawing)')+})++// ---- Stream order -------------------------------------------------------------++const PAGE = { columns: 10, rows: 5 }+const sel = (ax: number, ay: number, hx: number, hy: number) => ({ anchor: { x: ax, y: ay }, head: { x: hx, y: hy } })++test('stream order: the first row from the anchor to its end, the rows between whole, the last row to the head', () => {+ expect(selectedRows(sel(3, 1, 6, 3), [PAGE])).toEqual([[{ row: 1, x0: 3, x1: 9 }, { row: 2, x0: 0, x1: 9 }, { row: 3, x0: 0, x1: 6 }]])+ // One row: from the anchor to the head.+ expect(selectedRows(sel(3, 2, 6, 2), [PAGE])).toEqual([[{ row: 2, x0: 3, x1: 6 }]])+})++test('stream order: a drag up or to the left selects the same cells as the drag the other way', () => {+ expect(selectedRows(sel(6, 3, 3, 1), [PAGE])).toEqual(selectedRows(sel(3, 1, 6, 3), [PAGE]))+ expect(selectedRows(sel(6, 2, 3, 2), [PAGE])).toEqual([[{ row: 2, x0: 3, x1: 6 }]])+})++test('anchor and head on one cell select nothing (a click)', () => {+ expect(selectedRows(sel(4, 2, 4, 2), [PAGE])).toEqual([[]])+})++test('a head past the edges: left and right are the row\'s ends, above is the first cell, below the last', () => {+ expect(selectedRows(sel(3, 1, 40, 1), [PAGE])).toEqual([[{ row: 1, x0: 3, x1: 9 }]])+ expect(selectedRows(sel(3, 1, -5, 2), [PAGE])).toEqual([[{ row: 1, x0: 3, x1: 9 }, { row: 2, x0: 0, x1: 0 }]])+ expect(selectedRows(sel(3, 1, 5, -4), [PAGE])).toEqual([[{ row: 0, x0: 0, x1: 9 }, { row: 1, x0: 0, x1: 3 }]])+ expect(selectedRows(sel(3, 3, 0, 9), [PAGE])).toEqual([[{ row: 3, x0: 3, x1: 9 }, { row: 4, x0: 0, x1: 9 }]])+})++test('a selection over two pages: each page gets its own rows', () => {+ const pages = [PAGE, { columns: 8, rows: 3 }]+ expect(selectedRows(sel(4, 3, 2, 6), pages)).toEqual([+ [{ row: 3, x0: 4, x1: 9 }, { row: 4, x0: 0, x1: 9 }],+ [{ row: 0, x0: 0, x1: 7 }, { row: 1, x0: 0, x1: 2 }],+ ])+ expect(selectedRows(sel(1, 5, 30, 7), pages)).toEqual([[], [{ row: 0, x0: 1, x1: 7 }, { row: 1, x0: 0, x1: 7 }, { row: 2, x0: 0, x1: 7 }]])+})++// ---- The text ------------------------------------------------------------------++const REAL = positionsOf(SMALL_MARKS as unknown as Mark[], SMALL_PPI)+// The cells as Claude Code's own copy puts them on the clipboard (one line a row).+const cellsOf = (rows: { row: number; x0: number; x1: number }[]) =>+ rows.map(r => Array.from({ length: r.x1 - r.x0 + 1 }, (_, i) => placeholderCell(r.row, r.x0 + i)).join('')).join('\n')++test('a selection reads as a copy of the same cells: the same runs, the same text', () => {+ // From the heading's start to "jumps" (its centre in column 21) in the paragraph.+ const rows = selectedRows(sel(0, 1, 21, 3), [SMALL_PAGE])+ const runs = runsOf(rows)+ const decoded = decodeSelection(cellsOf(rows[0]!))+ expect(decoded).toEqual([{ kind: 'image', lines: runs[0]!.lines }])+ const text = textOfRuns(runs, () => ({ items: REAL.pages[0]!, blocks: REAL.blocks }), [SMALL_PAGE])+ expect(text).toBe('Copy test\nThe quick brown fox jumps')+ expect(text).toBe(textOfRun(runs[0]!.lines, REAL.pages[0]!, REAL.blocks, SMALL_PAGE))+})++test('a selection over two pages is the text of each, one line apart; a page with no words read is no text', () => {+ const pages = [SMALL_PAGE, SMALL_PAGE]+ // Page 0 from "the lazy dog" (row 3, column 30) to its end, page 1 its heading.+ const runs = runsOf(selectedRows(sel(30, 3, 97, 17), pages))+ expect(runs.map(r => r.page)).toEqual([0, 1])+ const words = () => ({ items: REAL.pages[0]!, blocks: REAL.blocks })+ expect(textOfRuns(runs, words, pages)).toBe(+ 'the lazy dog, then keeps running across the wide green field until the well-known path ends.\n- first item\n- second item\necho hello world\nls -la\nCopy test',+ )+ expect(textOfRuns(runs, page => (page === 1 ? null : words()), pages)).toBe(null)+ expect(textOfRuns([], words, pages)).toBe('')+})++// ---- The strips ------------------------------------------------------------------++test('strips: the page must be exactly rows x rowPx (a whole number) at the cell\'s aspect', () => {+ // Ghostty, 16 x 36 px cells: 212 columns of 16 px, 53 rows of 36 px.+ expect(stripPlan({ width: 3392, height: 1908 }, { columns: 212, rows: 53 }, { width: 16, height: 36 })).toEqual({ rowPx: 36, rows: 53 })+ expect(stripPlan({ width: 1568, height: 512 }, { columns: 98, rows: 16 }, { width: 16, height: 32 })).toEqual({ rowPx: 32, rows: 16 })+ // A page not a whole number of rows (a page cut at the most rows an Image takes).+ expect(stripPlan({ width: 3392, height: 1909 }, { columns: 212, rows: 53 }, { width: 16, height: 36 })).toBe(null)+ // A page wider than its box at that scale: the terminal would fit it by+ // its width, with space above and below (strips would not line up).+ expect(stripPlan({ width: 3408, height: 1908 }, { columns: 212, rows: 53 }, { width: 16, height: 36 })).toBe(null)+ // A cell of a fraction of a pixel (16.062 px a column): the page rounded up is wider than its box.+ expect(stripPlan({ width: 3406, height: 1855 }, { columns: 212, rows: 53 }, { width: 16.062, height: 35 })).toBe(null)+ // A page drawn smaller (an old one in an open epoch): its rows are not whole.+ expect(stripPlan({ width: 3392, height: 1908 }, { columns: 150, rows: 38 }, { width: 16, height: 36 })).toBe(null)+ // A page narrower than its box at that scale: space at the side only, the same for the strips.+ expect(stripPlan({ width: 3380, height: 1908 }, { columns: 212, rows: 53 }, { width: 16, height: 36 })).toEqual({ rowPx: 36, rows: 53 })+})++test('strips: each row is a page of its own, named as Typst numbers pages (1-based, padded to the count)', () => {+ expect(stripDir('/c/k/a-1.png')).toBe('/c/k/a-1.strips')+ expect(stripFile('/c/k/a-1.png', 53, 0)).toBe('/c/k/a-1.strips/s-01.png')+ expect(stripFile('/c/k/a-1.png', 53, 52)).toBe('/c/k/a-1.strips/s-53.png')+ expect(stripFile('/c/k/b-2.png', 120, 7)).toBe('/c/k/b-2.strips/s-008.png')+ expect(stripFile('/c/k/a-1.png', 9, 3)).toBe('/c/k/a-1.strips/s-4.png')+ const { argv, env } = stripsCall({ typst: '/usr/bin/typst', root: '/p', cache: '/c', file: '/c/k/a-1.png', width: 3392, height: 1908, rowPx: 36 })+ expect(argv.slice(0, 5)).toEqual(['sh', '-c', 'mkdir -p "$1" && shift && exec "$@"', 'sh', '/c/k/a-1.strips'])+ expect(argv.slice(5)).toEqual([+ '/usr/bin/typst', 'compile', '--root', '/', '--format', 'png', '--diagnostic-format', 'short', '--ppi', '72',+ '--input', 'src=/c/k/a-1.png', '--input', 'w=3392', '--input', 'h=1908', '--input', 'row=36', '/p/render/strips.typ', '/c/k/a-1.strips/s-{0p}.png',+ ])+ expect(env.TYPST_PACKAGE_CACHE_PATH).toBe('/c/pkg-cache')+})++// ---- The gate and the colour ---------------------------------------------------++test('picture select is for Claude Code 2.1.305 and newer (a dev build by its release)', () => {+ expect(needsSelect('2.1.305')).toBe(true)+ expect(needsSelect('2.1.306')).toBe(true)+ expect(needsSelect('2.2.0')).toBe(true)+ expect(needsSelect('2.1.306-dev')).toBe(true)+ expect(needsSelect('2.1.304')).toBe(false)+ expect(needsSelect('2.0.999')).toBe(false)+ expect(needsSelect('')).toBe(false)+ expect(needsSelect('dev')).toBe(false)+})++test('the selection colour is one step off on every other redraw, in both modes', () => {+ expect(selectColor('dark', 1)).toBe('#264f78')+ expect(selectColor('dark', 2)).toBe('#264f79')+ expect(selectColor('light', 1)).toBe('#add6ff')+ expect(selectColor('light', 2)).toBe('#add6fe')+})diff --git a/tests/shim-test.sh b/tests/shim-test.shnew file mode 100755index 0000000..0c2d770--- /dev/null+++ b/tests/shim-test.sh@@ -0,0 +1,529 @@+#!/bin/sh+# Runs the clipboard shims (hooks/clip-shim.sh) against fake tools and a fake+# mod (the reader of the ask FIFO), on macOS and Linux: what counts as a+# clipboard write, the ask for picture cells and its limits, the bytes and+# arguments the real tool gets, the exit status, and no recursion. Then the+# setup script (hooks/clip-shim-setup.sh) and the cache sweep (CACHE_SH).+# Usage: sh tests/shim-test.sh (also dash, bash --posix; exit 0 when all pass)+set -u+here=$(cd "$(dirname "$0")/.." && pwd)+template=$here/hooks/clip-shim.sh+tmp=$(mktemp -d "${TMPDIR:-/tmp}/iui-shim-test.XXXXXX")+mod=+trap '[ -z "$mod" ] || kill "$mod" 2>/dev/null; rm -rf "$tmp"' EXIT+real=$tmp/real+shims=$tmp/shims+log=$tmp/log+bin=$tmp/bin+stub=$tmp/stub+c=$tmp/c+mkdir -p "$real" "$shims" "$log" "$bin" "$stub" "$c/ask"+chmod 700 "$c" "$c/ask"+# The mod's FIFO. `launch` names it for its own pid while a shim runs.+fifo=$c/ask-mod+mkfifo -m 600 "$fifo"+pass=0+fail=0++ok() { pass=$((pass + 1)); }+bad() { fail=$((fail + 1)); echo "FAIL: $*"; }++# The tools the shims and the fake tools use, with no perl among them.+for t in sh dash bash cat grep rm mv ln chmod basename dirname env sleep cp mkdir ls kill sed; do+ p=$(command -v "$t") && ln -s "$p" "$bin/$t"+done+perl=$(command -v perl)+# A perl that notes each run, then runs the real one (the shims' @PERL@).+printf '#!/bin/sh\necho run >>"%s/perl.runs"\nexec "%s" "$@"\n' "$log" "$perl" >"$stub/perl"+chmod 755 "$stub/perl"+P=$shims:$real:$bin++# The fake real tools: they keep their arguments (one per line) and their+# stdin, then exit with $FAKE_EXIT or die of $FAKE_SIG.+for t in pbcopy wl-copy xclip xsel; do+ cat >"$real/$t" <<EOF+#!/bin/sh+l='$log'/\$(basename "\$0")+printf '%s\n' "\$@" >"\$l.args"+cat >"\$l.in"+[ -z "\${FAKE_SIG:-}" ] || kill -"\$FAKE_SIG" \$\$+exit "\${FAKE_EXIT:-0}"+EOF+ chmod 755 "$real/$t"+done+# make_shim <folder> <tool> <perl>: the shim as the mod writes it (each value quoted for sh).+make_shim() {+ sed -e "s|@TOOL@|'$2'|" -e "s|@REAL@|'$real/$2'|" -e "s|@DIR@|'$1'|" -e "s|@PERL@|'$3'|" "$template" >"$1/$2"+ chmod 755 "$1/$2"+}+for t in pbcopy wl-copy xclip xsel; do make_shim "$shims" "$t" "$stub/perl"; done++# The fake mod: reads ids from the FIFO (open to read and write, as the real+# reader), notes them, and answers after $2 seconds ("ANSWER for N bytes",+# or nothing with $4 = empty): a temp file, then a link. A link that fails+# meets the shim's mark: then it notes `late <id>`, and the files go.+cat >"$tmp/mod.pl" <<'EOF'+use Time::HiRes ();+my ($fifo, $wait, $log, $kind) = @ARGV;+open(my $f, "+<", $fifo) or die "fifo: $!";+(my $dir = $fifo) =~ s{/[^/]*$}{/ask};+$| = 1;+while (my $id = <$f>) {+ chomp $id;+ open(my $n, ">>", "$log/mod.ids"); print $n "$id\n"; close $n;+ open(my $r, "<:raw", "$dir/$id") or next;+ my $raw = do { local $/; <$r> }; close $r;+ Time::HiRes::sleep($wait);+ open(my $w, ">:raw", "$dir/$id.answer.tmp");+ print $w "ANSWER for " . length($raw) . " bytes" unless ($kind // "") eq "empty";+ close $w;+ if (!link("$dir/$id.answer.tmp", "$dir/$id.answer")) {+ unlink "$dir/$id", "$dir/$id.answer";+ open(my $l, ">>", "$log/mod.late"); print $l "late $id\n"; close $l;+ }+ unlink "$dir/$id.answer.tmp";+}+EOF+start_mod() {+ stop_mod+ "$perl" "$tmp/mod.pl" "$fifo" "$1" "$log" "${2:-}" &+ mod=$!+ # Until it has the FIFO open (a writer's open then works).+ n=0+ while [ "$n" -lt 100 ]; do+ "$perl" -MFcntl -e 'sysopen(my $f, $ARGV[0], O_WRONLY | O_NONBLOCK) or exit 1' "$fifo" && return+ n=$((n + 1)); sleep 0.05 2>/dev/null || sleep 1+ done+}+stop_mod() {+ [ -z "$mod" ] || { kill "$mod" 2>/dev/null; wait "$mod" 2>/dev/null; }+ mod=+}+now() { "$perl" -MTime::HiRes=time -e 'printf "%.3f", time'; }+# The seconds from $1 to now, under $2: true or false.+under() { "$perl" -MTime::HiRes=time -e 'exit(time - $ARGV[0] < $ARGV[1] ? 0 : 1)' "$1" "$2"; }++# launch <ask> <cmd>...: runs <cmd> as a child of this shell (as Claude Code+# runs the tool), with IUI_COPY_ASK=<ask>, PID in it replaced by this pid.+# LAUNCH (default fifo) puts there the mod's FIFO, a file, a link to it, or+# nothing; LAUNCH_DEEP=1 runs <cmd> one shell further down (not a child).+cat >"$tmp/launch" <<EOF+#!/bin/sh+a=\$(printf '%s' "\$1" | sed "s/PID/\$\$/"); shift+case \${LAUNCH:-fifo} in+ fifo) mv '$fifo' "\$a" ;;+ file) : >"\$a" ;;+ link) ln -s '$fifo' "\$a" ;;+esac+IUI_COPY_ASK=\$a; export IUI_COPY_ASK+if [ -n "\${LAUNCH_DEEP:-}" ]; then sh -c '"\$@"; exit \$?' sh "\$@"; else "\$@"; fi+r=\$?+case \${LAUNCH:-fifo} in+ fifo) mv "\$a" '$fifo' ;;+ file|link) rm -f "\$a" ;;+esac+exit \$r+EOF+chmod 755 "$tmp/launch"+L=$tmp/launch++input=$tmp/input+cells=$tmp/cells+answer=$tmp/answer+printf 'Copy test\nThe quick brown fox jumps' >"$input"+# Two picture cells (U+10EEEE, a row and a column diacritic) between text.+printf 'Copy \364\216\273\256\314\205\314\205\364\216\273\256\314\205\314\215 test\n' >"$cells"+printf 'ANSWER for %s bytes' "$(wc -c <"$cells" | tr -d ' ')" >"$answer"++# The ask folder has nothing left: no request, answer or mark.+clean() { [ -z "$(ls -A "$c/ask")" ] && ok || { bad "$1: files left in ask/: $(ls -A "$c/ask" | tr '\n' ' ')"; rm -f "$c/ask"/*; }; }+reset_log() { rm -f "$log"/*.args "$log"/*.in "$log/perl.runs" "$log/mod.ids" "$log/mod.late"; }++# run <shell> <file> <tool> [args...]: the shim with <file> on stdin, as+# Claude Code's exec finds it (on PATH $P, shim folder first), with the ask.+run() {+ sh_=$1 in_=$2 tool=$3+ shift 3+ reset_log+ if [ "$sh_" = exec ]; then+ PATH=$P "$L" "$c/ask-PID" "$tool" "$@" <"$in_"+ elif [ "$sh_" = bash ]; then+ PATH=$P "$L" "$c/ask-PID" bash --posix "$shims/$tool" "$@" <"$in_"+ else+ PATH=$P "$L" "$c/ask-PID" "$sh_" "$shims/$tool" "$@" <"$in_"+ fi+}++# check <shell> <write 0|1> <tool> [args...]: a write runs perl once, and+# picture cells get the mod's answer; plain text, and any other call, pass+# as they are (no perl for another call). The same arguments and exit+# status each time, and nothing left in ask/.+check() {+ sh_=$1 want=$2 tool=$3+ shift 3+ printf '%s\n' "$@" >"$tmp/want.args"+ code=${FAKE_EXIT:-0}+ what="$sh_ $tool $*"+ for kind in plain cells; do+ if [ "$kind" = plain ]; then in_=$input; else in_=$cells; fi+ run "$sh_" "$in_" "$tool" "$@"+ got=$?+ [ "$got" = "$code" ] && ok || bad "$what ($kind): exit $got, not $code"+ cmp -s "$log/$tool.args" "$tmp/want.args" && ok || bad "$what ($kind): the real tool did not get the same arguments"+ if [ "$kind" = cells ] && [ "$want" = 1 ]; then+ cmp -s "$log/$tool.in" "$answer" && ok || bad "$what: the real tool did not get the answer ($(cat "$log/$tool.in"))"+ else+ cmp -s "$log/$tool.in" "$in_" && ok || bad "$what ($kind): the real tool did not get the same stdin"+ fi+ if [ "$want" = 1 ]; then+ [ "$(cat "$log/perl.runs" 2>/dev/null)" = run ] && ok || bad "$what ($kind): not one perl run"+ else+ [ ! -e "$log/perl.runs" ] && ok || bad "$what ($kind): perl ran"+ fi+ clean "$what ($kind)"+ done+}++shells=exec+for s in sh dash bash; do command -v "$s" >/dev/null 2>&1 && shells="$shells $s"; done++start_mod 0+for s in $shells; do+ # pbcopy: a write of the general pasteboard.+ check "$s" 1 pbcopy+ check "$s" 1 pbcopy -pboard general+ check "$s" 0 pbcopy -pboard find+ check "$s" 0 pbcopy -help+ # wl-copy: a write of text unless primary, clear, or text in the arguments.+ check "$s" 1 wl-copy+ check "$s" 1 wl-copy -n --type 'text/plain;charset=utf-8'+ check "$s" 1 wl-copy --type=text/plain+ check "$s" 1 wl-copy -t UTF8_STRING+ check "$s" 0 wl-copy -t image/png+ check "$s" 0 wl-copy --type=image/png+ check "$s" 0 wl-copy --primary+ check "$s" 0 wl-copy -p+ check "$s" 0 wl-copy --clear+ check "$s" 0 wl-copy hello+ # xclip: input mode and the clipboard selection (its default is primary).+ check "$s" 1 xclip -selection clipboard+ check "$s" 1 xclip -sel c+ check "$s" 1 xclip -i -selection clipboard -t text/plain+ check "$s" 1 xclip -selection clipboard -target STRING+ check "$s" 0 xclip -selection clipboard -t image/png+ check "$s" 0 xclip -selection clipboard -target text/html+ check "$s" 0 xclip -selection primary+ check "$s" 0 xclip+ check "$s" 0 xclip -selection clipboard -o+ check "$s" 0 xclip -selection clipboard -t text/plain -o+ check "$s" 0 xclip -selection clipboard -t TARGETS -o+ check "$s" 0 xclip -selection clipboard -t image/png -o+ check "$s" 0 xclip -selection clipboard file.txt+ # xsel: input (or append) and the clipboard.+ check "$s" 1 xsel --clipboard --input+ check "$s" 1 xsel -bi+ check "$s" 1 xsel -b -i+ check "$s" 0 xsel --primary --input+ check "$s" 0 xsel --clipboard --output+ check "$s" 0 xsel -bo+ check "$s" 0 xsel --clipboard+ check "$s" 0 xsel+done++# The exit status of the real tool, for a write and for a read, each tool.+for t in pbcopy wl-copy; do FAKE_EXIT=3 check exec 1 "$t"; done+FAKE_EXIT=3 check dash 1 xclip -selection clipboard+FAKE_EXIT=3 check sh 1 xsel -bi+FAKE_EXIT=4 check exec 0 xclip -selection clipboard -o+# (An assignment before a function call stays in some shells.)+unset FAKE_EXIT++# A real tool that a signal ends: the same signal status (128 + n), with+# perl (a write) and with no perl (a read).+for a in "1 pbcopy" "0 xsel -bo"; do+ for kind in plain cells; do+ if [ "$kind" = plain ]; then in_=$input; else in_=$cells; fi+ set -- $a+ FAKE_SIG=TERM run dash "$in_" $2 ${3:-} 2>/dev/null+ got=$?+ [ "$got" = 143 ] && ok || bad "signal ($2 $kind): exit $got, not 143"+ clean "signal ($2 $kind)"+ done+done+unset FAKE_SIG+# The same signal itself, not only its status, for its parent.+for t in pbcopy "xsel -bo"; do+ reset_log+ FAKE_SIG=TERM PATH=$P "$perl" -e 'my ($f, @cmd) = @ARGV; (my $a = $f) =~ s/-mod$/-$$/; rename $f, $a; $ENV{IUI_COPY_ASK} = $a; system(@cmd); my $s = $?; rename $a, $f; exit(($s & 127) == 15 ? 0 : 1)' "$fifo" $t <"$cells"+ [ $? = 0 ] && ok || bad "signal ($t): its parent did not see SIGTERM"+ clean "signal itself ($t)"+done++# A binary type passes untouched: all bytes, no ask.+printf '\211PNG\r\n\032\n\364\216\273\256\000\377' >"$tmp/png"+run exec "$tmp/png" wl-copy -t image/png+cmp -s "$log/wl-copy.in" "$tmp/png" && [ ! -e "$log/mod.ids" ] && [ ! -e "$log/perl.runs" ] && ok || bad "image/png: not all bytes untouched"++# 4 MiB of any bytes (the limit), text with no last line break, and none.+dd if=/dev/urandom of="$tmp/big" bs=1024 count=4096 2>/dev/null+for f in big: nolf:no\ line\ break empty:; do+ name=${f%%:*}+ [ "$name" = big ] || printf '%s' "${f#*:}" >"$tmp/$name"+ run exec "$tmp/$name" xsel -bi+ cmp -s "$log/xsel.in" "$tmp/$name" && ok || bad "$name input: the text did not pass"+ clean "$name input"+done+# Picture cells in more than 4 MiB: no ask, all bytes pass (also after the+# first 4 MiB); exactly 4 MiB with cells: asked.+{ cat "$cells"; cat "$tmp/big"; } >"$tmp/big5"+run exec "$tmp/big5" pbcopy+cmp -s "$log/pbcopy.in" "$tmp/big5" && [ ! -e "$log/mod.ids" ] && ok || bad "over 4 MiB: not all bytes, or asked"+clean "over 4 MiB"+{ cat "$cells"; head -c $((4194304 - $(wc -c <"$cells"))) "$tmp/big"; } >"$tmp/big4"+run exec "$tmp/big4" pbcopy+[ -s "$log/mod.ids" ] && ok || bad "4 MiB with cells: not asked"+clean "4 MiB with cells"++# An empty answer (the mod has no text): the shim's own cells.+start_mod 0 empty+run sh "$cells" pbcopy+cmp -s "$log/pbcopy.in" "$cells" && [ -s "$log/mod.ids" ] && ok || bad "empty answer: not the cells"+clean "empty answer"+start_mod 0++# Not a direct child of the FIFO's process: no ask, no perl, the cells.+reset_log+PATH=$P LAUNCH_DEEP=1 "$L" "$c/ask-PID" pbcopy <"$cells"+got=$?+[ "$got" = 0 ] && cmp -s "$log/pbcopy.in" "$cells" && [ ! -e "$log/perl.runs" ] && [ ! -e "$log/mod.ids" ] && ok || bad "not a child: asked, or not the cells ($got)"+clean "not a child"++# IUI_COPY_ASK not set, or not a FIFO path: the cells pass, no perl.+for a in '' "$c/foo" "$c/ask-x1"; do+ reset_log+ PATH=$P LAUNCH=none "$L" "$a" pbcopy <"$cells"+ cmp -s "$log/pbcopy.in" "$cells" && [ ! -e "$log/perl.runs" ] && ok || bad "IUI_COPY_ASK '$a': the cells did not pass as they are"+done++# No reader (the mod stopped): the cells at once.+stop_mod+t0=$(now)+run exec "$cells" pbcopy+under "$t0" 0.3 && ok || bad "no reader: the shim waited"+cmp -s "$log/pbcopy.in" "$cells" && ok || bad "no reader: not the cells"+clean "no reader"++# A slow mod (1.8 s): the cells after the limit (1.5 s), well before Claude+# Code's 2 s. The late answer meets the shim's mark, and the files go.+start_mod 1.8+t0=$(now)+run dash "$cells" xclip -selection clipboard+under "$t0" 1.4 && bad "slow mod: the shim did not wait for the answer" || ok+under "$t0" 2 && ok || bad "slow mod: the shim took 2 s or more"+cmp -s "$log/xclip.in" "$cells" && ok || bad "slow mod: not the cells"+n=0+while [ ! -s "$log/mod.late" ] && [ "$n" -lt 40 ]; do sleep 0.1 2>/dev/null || sleep 1; n=$((n + 1)); done+grep -q "^late " "$log/mod.late" 2>/dev/null && ok || bad "slow mod: its answer was not refused"+clean "slow mod"+stop_mod++# No perl (the shim's perl is not there): the cells, the exit status.+start_mod 0+mkdir -p "$tmp/noperl"+make_shim "$tmp/noperl" pbcopy "$tmp/no/perl"+for s in exec sh; do+ reset_log+ if [ "$s" = exec ]; then+ PATH=$tmp/noperl:$real:$bin FAKE_EXIT=5 "$L" "$c/ask-PID" pbcopy <"$cells"+ else+ PATH=$tmp/noperl:$real:$bin FAKE_EXIT=5 "$L" "$c/ask-PID" sh "$tmp/noperl/pbcopy" <"$cells"+ fi+ got=$?+ [ "$got" = 5 ] && ok || bad "$s no perl: exit $got, not 5"+ cmp -s "$log/pbcopy.in" "$cells" && [ ! -e "$log/mod.ids" ] && ok || bad "$s no perl: not the cells"+ clean "$s no perl"+done++# A FIFO that is a regular file, or a link to the mod's FIFO: the cells, and+# the file is not written, the mod gets no id.+for kind in file link; do+ reset_log+ PATH=$P LAUNCH=$kind "$L" "$c/ask-PID" wl-copy <"$cells"+ cmp -s "$log/wl-copy.in" "$cells" && ok || bad "a $kind as the FIFO: not the cells"+ [ ! -e "$log/mod.ids" ] && ok || bad "a $kind as the FIFO: the mod got an id"+ clean "a $kind as the FIFO"+done++# An ask folder that is a link, or not this user's (root's /usr): no ask,+# the cells as they are.+mkdir -p "$tmp/l"; ln -s "$c/ask" "$tmp/l/ask"+for a in "fifo $tmp/l/ask-PID" "none /usr/ask-PID"; do+ reset_log+ PATH=$P LAUNCH=${a%% *} "$L" "${a#* }" pbcopy <"$cells"+ cmp -s "$log/pbcopy.in" "$cells" && [ ! -e "$log/mod.ids" ] && ok || bad "ask folder of ${a#* }: not the cells as they are"+done+clean "ask folder links"++# The request can not be written (ask/ read-only): all of the text, and the+# exit status.+if [ "$(id -u)" != 0 ]; then+ chmod 500 "$c/ask"+ for s in exec dash; do+ reset_log+ if [ "$s" = exec ]; then+ PATH=$P FAKE_EXIT=6 "$L" "$c/ask-PID" xsel -bi <"$cells"+ else+ PATH=$P FAKE_EXIT=6 "$L" "$c/ask-PID" dash "$shims/xsel" -bi <"$cells"+ fi+ got=$?+ [ "$got" = 6 ] && ok || bad "$s read-only ask/: exit $got, not 6"+ cmp -s "$log/xsel.in" "$cells" && [ ! -e "$log/mod.ids" ] && ok || bad "$s read-only ask/: the text did not pass"+ done+ chmod 700 "$c/ask"+fi+stop_mod++# The real tool moved: found on PATH without the shim folder, still asked.+start_mod 0+mv "$real" "$tmp/moved"+reset_log+PATH=$shims:$tmp/moved:$bin "$L" "$c/ask-PID" xclip -selection clipboard <"$cells"+got=$?+[ "$got" = 0 ] && ok || bad "a moved tool: exit $got"+cmp -s "$log/xclip.in" "$answer" && ok || bad "a moved tool: not the answer"+clean "a moved tool"+stop_mod++# No real tool, and the shim folder on PATH again in another spelling: the+# shim stops (127), it never runs itself in a loop; also through perl.+for a in none fifo; do+ start_mod 0+ out=$(PATH="$shims:$tmp/./shims:$bin" LAUNCH=$a "$L" "$c/ask-PID" xsel -bi <"$input" 2>&1)+ got=$?+ [ "$got" = 127 ] && ok || bad "no real tool ($a): exit $got, not 127 ($out)"+ stop_mod+done+mv "$tmp/moved" "$real"++# ---- The setup script: only a private folder of this user gets shims ----+# Each call runs as a child of this shell, so the pid it prints is $$.+setup=$here/hooks/clip-shim-setup.sh+sleep 60 &+other=$!+for s in $shells; do+ [ "$s" = exec ] && continue+ [ "$s" = bash ] && s='bash --posix'+ k=$(echo "$s" | tr -d ' -')+ d=$tmp/cache-$k+ m=$d/shims-$$+ mkdir -p "$d/shims" "$d/shims-$other"+ # Shims of another process (and an old shims folder) on PATH: found outside them.+ cp "$shims/pbcopy" "$d/shims/pbcopy"+ cp "$shims/pbcopy" "$d/shims-$other/pbcopy"+ PATH="$d/shims-$other:$d/shims:$real:/usr/bin:/bin" $s "$setup" find "$d" pbcopy xclip nosuchtool >"$tmp/out"+ [ $? = 0 ] && ok || bad "$s setup find: a private folder fails"+ out=$(cat "$tmp/out")+ echo "$out" | grep -qx "pid $$" && ok || bad "$s setup find: not the pid of its parent ($out)"+ echo "$out" | grep -qx "pbcopy $real/pbcopy" && ok || bad "$s setup find: pbcopy not found outside the shim folders ($out)"+ echo "$out" | grep -q nosuchtool && bad "$s setup find: a tool that is not there" || ok+ echo "$out" | grep -q '^perl /' && ok || bad "$s setup find: no perl line"+ [ -d "$m" ] && [ ! -L "$m" ] && ok || bad "$s setup find: no shims folder of the pid"+ case $(ls -ld "$m") in drwx------*) ok ;; *) bad "$s setup find: shims folder not 700" ;; esac+ # The ask folder (700) and the FIFO of the pid (600).+ echo "$out" | grep -qx "ask $d/ask-$$" && ok || bad "$s setup find: no ask line ($out)"+ case $(ls -ld "$d/ask") in drwx------*) ok ;; *) bad "$s setup find: ask folder not 700" ;; esac+ case $(ls -l "$d/ask-$$") in prw-------*) ok ;; *) bad "$s setup find: the FIFO is not a FIFO of mode 600" ;; esac+ rm -f "$d/ask-$$"+ # A file or a link in the FIFO's place (the parent's pid): a new FIFO, the target stays.+ for kind in file link; do+ printf 'keep' >"$tmp/target"+ out=$($s -c 'if [ "$1" = file ]; then printf x >"$2/ask-$$"; else ln -s "$3" "$2/ask-$$"; fi; shift 3; "$@"; :' \+ sh "$kind" "$d" "$tmp/target" $s "$setup" find "$d" pbcopy)+ p2=$(echo "$out" | sed -n 's/^pid \([0-9][0-9]*\)$/\1/p')+ [ -n "$p2" ] && [ -p "$d/ask-$p2" ] && [ ! -L "$d/ask-$p2" ] && echo "$out" | grep -qx "ask $d/ask-$p2" && ok || bad "$s setup find: no new FIFO for a $kind ($out)"+ [ "$(cat "$tmp/target")" = keep ] && ok || bad "$s setup find: the link target changed"+ rm -rf "$d/ask-$p2" "$d/shims-$p2"+ done+ # An ask folder that is a link: no ask line, the shims still set up.+ c5=$tmp/c5-$k; mkdir -p "$c5" "$tmp/other5-$k"; ln -s "$tmp/other5-$k" "$c5/ask"+ $s "$setup" find "$c5" pbcopy >"$tmp/out"; got=$?+ [ "$got" = 0 ] && ! grep -q '^ask ' "$tmp/out" && ok || bad "$s setup find: an ask link gives $got ($(cat "$tmp/out"))"+ # Install: a new shim takes its place, executable; `-file` removes one.+ printf 'x' >"$m/xclip.new-1"+ printf 'y' >"$m/xsel"+ $s "$setup" install "$d" .new-1 "$m/xclip" "-$m/xsel"+ [ $? = 0 ] && [ -x "$m/xclip" ] && [ ! -e "$m/xclip.new-1" ] && [ ! -e "$m/xsel" ] && ok || bad "$s setup install"+ # The shims of another process: never installed or removed by this one.+ printf 'z' >"$d/shims-$other/xclip.new-1"+ $s "$setup" install "$d" .new-1 "$d/shims-$other/xclip" 2>/dev/null; got=$?+ $s "$setup" install "$d" .new-1 "-$d/shims-$other/pbcopy" 2>/dev/null; got2=$?+ [ "$got" != 0 ] && [ "$got2" != 0 ] && [ ! -e "$d/shims-$other/xclip" ] && cmp -s "$d/shims-$other/pbcopy" "$shims/pbcopy" && ok || bad "$s setup install: the shims of another process changed ($got $got2)"+ # A temp file that is a link: refused, the target is not changed.+ printf 'keep' >"$tmp/target"+ ln -s "$tmp/target" "$m/pbcopy.new-2"+ $s "$setup" install "$d" .new-2 "$m/pbcopy" 2>/dev/null+ got=$?+ [ "$got" != 0 ] && [ "$(cat "$tmp/target")" = keep ] && [ ! -e "$m/pbcopy" ] && ok || bad "$s setup install: a link was used ($got)"+ # A cache folder that others can write: ours, so it is made 700 and used.+ c2=$tmp/open-$k; mkdir -p "$c2"; chmod 777 "$c2"+ $s "$setup" find "$c2" pbcopy >/dev/null+ [ $? = 0 ] && case $(ls -ld "$c2") in drwx------*) true ;; *) false ;; esac && ok || bad "$s setup: an open folder of this user was not made private"+ # A link as the cache folder, or as its shims folder: exit 3.+ ln -s "$d" "$tmp/link-$k"+ $s "$setup" find "$tmp/link-$k" pbcopy >/dev/null; got=$?+ [ "$got" = 3 ] && ok || bad "$s setup: a linked cache folder gives $got, not 3"+ c3=$tmp/c3-$k; mkdir -p "$c3" "$tmp/other-$k"; ln -s "$tmp/other-$k" "$c3/shims-$$"+ $s "$setup" find "$c3" pbcopy >/dev/null; got=$?+ [ "$got" = 3 ] && ok || bad "$s setup: a linked shims folder gives $got, not 3"+ $s "$setup" install "$c3" .new-1 "$c3/shims-$$/pbcopy" >/dev/null 2>&1; got=$?+ [ "$got" = 3 ] && ok || bad "$s setup install: a linked shims folder gives $got, not 3"+ # A link in the shims folder (it could point anywhere): exit 3.+ c4=$tmp/c4-$k; mkdir -p "$c4/shims-$$"; ln -s /usr/bin/true "$c4/shims-$$/pbcopy"+ $s "$setup" find "$c4" pbcopy >/dev/null; got=$?+ [ "$got" = 3 ] && ok || bad "$s setup: a link in the shims folder gives $got, not 3"+ # A file as the cache folder, and a folder of another user (root's /usr): exit 3.+ printf '' >"$tmp/file-$k"+ $s "$setup" find "$tmp/file-$k" pbcopy >/dev/null; got=$?+ [ "$got" = 3 ] && ok || bad "$s setup: a file as the cache folder gives $got, not 3"+ if [ "$(id -u)" != 0 ]; then+ $s "$setup" find /usr pbcopy >/dev/null 2>&1; got=$?+ [ "$got" = 3 ] && ok || bad "$s setup: a folder of another user gives $got, not 3"+ fi+done++# ---- The cache sweep (CACHE_SH in hooks/lib/env.ts) ----+# The TypeScript text as sh: ${DESIGNS_KEPT_DAYS} as 30, the escapes undone.+cache_sh=$("$perl" -ne 'if (/^export const CACHE_SH = `(.*)`$/) { $_ = $1; s/(?<!\\)\$\{DESIGNS_KEPT_DAYS\}/30/g; s/\\\$/\$/g; s/\\\\/\\/g; print }' "$here/hooks/lib/env.ts")+[ -n "$cache_sh" ] && ok || bad "CACHE_SH: not found in env.ts"+sh -c 'exit 0' &+dead=$!+wait "$dead"+uid=$(id -u)+for s in $shells; do+ [ "$s" = exec ] && continue+ [ "$s" = bash ] && s='bash --posix'+ k=$(echo "$s" | tr -d ' -')+ # A link as the cache folder (to a folder of the victim): nothing there changes.+ td=$tmp/td-$k; v=$tmp/victim-$k+ mkdir -p "$td" "$v/old" "$v/ask"; chmod 755 "$v"+ : >"$v/ask-$dead"; : >"$v/ask/$dead-1-2"+ touch -t 202001010000 "$v/old"+ ln -s "$v" "$td/cc-intelligent-ui-$uid"+ out=$(TMPDIR=$td XDG_CACHE_HOME=$tmp/xdg-$k $s -c "$cache_sh")+ [ -z "$(echo "$out" | sed -n 1p)" ] && ok || bad "$s CACHE_SH: a linked cache folder was used ($out)"+ [ -d "$v/old" ] && [ -e "$v/ask-$dead" ] && [ -e "$v/ask/$dead-1-2" ] && case $(ls -ld "$v") in drwxr-xr-x*) true ;; *) false ;; esac && ok || bad "$s CACHE_SH: the link target changed"+ # A cache folder of this user: the files of a dead pid go, of a live pid stay.+ td=$tmp/te-$k; d=$td/cc-intelligent-ui-$uid+ mkdir -p "$d/ask" "$d/shims-$dead" "$d/shims-$$" "$d/old" "$d/pkg-cache"+ : >"$d/ask-$dead"; : >"$d/ask-$$"; : >"$d/ask/$dead-1-2"; : >"$d/ask/$dead-1-2.answer"; : >"$d/ask/$$-1-2"+ : >"$d/shims-$dead/pbcopy"+ touch -t 202001010000 "$d/old" "$d/shims-$$" "$d/pkg-cache"+ out=$(TMPDIR=$td XDG_CACHE_HOME=$tmp/xdg-$k $s -c "$cache_sh")+ [ "$(echo "$out" | sed -n 1p)" = "$d" ] && ok || bad "$s CACHE_SH: not the cache folder ($out)"+ [ ! -e "$d/ask-$dead" ] && [ ! -e "$d/ask/$dead-1-2" ] && [ ! -e "$d/ask/$dead-1-2.answer" ] && [ ! -e "$d/shims-$dead" ] && [ ! -e "$d/old" ] && ok || bad "$s CACHE_SH: files of a dead pid or an old folder stayed ($(ls -A "$d" "$d/ask" | tr '\n' ' '))"+ [ -e "$d/ask-$$" ] && [ -e "$d/ask/$$-1-2" ] && [ -d "$d/shims-$$" ] && [ -d "$d/pkg-cache" ] && ok || bad "$s CACHE_SH: files of a live pid went"+done+kill "$other" 2>/dev/null; wait "$other" 2>/dev/null++echo "shim-test: $pass passed, $fail failed (shells: $shells)"+[ "$fail" = 0 ]diff --git a/tests/shims.test.ts b/tests/shims.test.tsnew file mode 100644index 0000000..a7c0ea9--- /dev/null+++ b/tests/shims.test.ts@@ -0,0 +1,212 @@+import { expect, test } from 'claude-code/testing'++import { pathWith, SHIM_SETUP, SHIM_TOOLS, shimFindOf, shimText, shQuote } from '../hooks/register'+import { setUpShims } from '../hooks/lib/clipboard'+import { S } from '../hooks/lib/state'++const ok = (stdout: string, exitCode = 0) => ({ value: { exitCode, stdout, stderr: '', isStdoutTruncated: false, isStderrTruncated: false } })+const TEMPLATE = 'tool=@TOOL@\nreal=@REAL@\ndir=@DIR@\nperl=@PERL@\n'++test('a shim is the template with the tool, the real tool, the folder and perl, each quoted for sh', () => {+ expect(shQuote("it's")).toBe(`'it'\\''s'`)+ expect(shimText(TEMPLATE, 'xclip', '/usr/bin/xclip', "/t/a'b/shims-1", '/usr/bin/perl'))+ .toBe(`tool='xclip'\nreal='/usr/bin/xclip'\ndir='/t/a'\\''b/shims-1'\nperl='/usr/bin/perl'\n`)+ // A `$` in a path stays as it is.+ expect(shimText('@REAL@', 'pbcopy', '/x/$HOME', '/d', '/p')).toBe(`'/x/$HOME'`)+ expect(SHIM_TOOLS).toEqual({ mac: ['pbcopy'], linux: ['wl-copy', 'xclip', 'xsel'] })+})++test("the find script's output: the pid, only absolute tools outside the shim folders, perl, and the FIFO in the cache folder", () => {+ const out = 'pid 123\nwl-copy /usr/bin/wl-copy\nxclip /c/shims-7/xclip\nxsel xsel\nfoo /usr/bin/foo\nperl /usr/bin/perl\nask /c/ask-123\n'+ expect(shimFindOf('xclip /c/shims/xclip\n', SHIM_TOOLS.linux, '/c').found).toEqual({})+ expect(shimFindOf(out, SHIM_TOOLS.linux, '/c')).toEqual({ pid: '123', found: { 'wl-copy': '/usr/bin/wl-copy' }, perl: '/usr/bin/perl', ask: '/c/ask-123' })+ expect(shimFindOf('', SHIM_TOOLS.mac, '/c')).toEqual({ pid: '', found: {}, perl: '', ask: '' })+ // A FIFO outside the cache folder, or not of a pid: none.+ for (const ask of ['/tmp/ask-1', '/c/ask-', '/c/ask-1/x', '/c/x/ask-1', 'perl']) expect(shimFindOf(`ask ${ask}\n`, SHIM_TOOLS.mac, '/c').ask).toBe('')+ expect(shimFindOf('perl perl\n', SHIM_TOOLS.mac, '/c').perl).toBe('')+ expect(SHIM_SETUP).toBe('hooks/clip-shim-setup.sh')+})++test('PATH gets the shim folder first, once', () => {+ expect(pathWith('/usr/bin:/bin', '/c/shims')).toBe('/c/shims:/usr/bin:/bin')+ expect(pathWith('/c/shims:/usr/bin:/bin', '/c/shims')).toBe(null)+ // Not first, or twice: moved to the front, once.+ expect(pathWith('/usr/bin:/c/shims:/bin', '/c/shims')).toBe('/c/shims:/usr/bin:/bin')+ expect(pathWith('/c/shims:/usr/bin:/c/shims', '/c/shims')).toBe('/c/shims:/usr/bin')+ expect(pathWith('', '/c/shims')).toBe('/c/shims')+})++// A computer for setUpShims (a $ of its own): `found` the tools the find+// script prints, the files in `files`, PATH in `vars`; `runs` and `sets`+// keep the calls.+function shimWorld(o: {+ found: string[]; files?: Record<string, string>; chmodFails?: boolean; denyPath?: boolean; path?: string; unsafe?: 'find' | 'install'; noPerl?: boolean; noAsk?: boolean+ noPid?: boolean+}) {+ const files = new Map<string, string>(Object.entries(o.files ?? {}))+ const vars: Record<string, string | undefined> = { PATH: o.path ?? '/usr/bin:/bin' }+ const runs: string[][] = []+ const sets: [string, string | undefined][] = []+ const run = async (argv: string[]) => {+ runs.push(argv)+ if (o.unsafe === argv[2]) return ok('unsafe /c\n', 3).value+ if (argv[1]?.endsWith(SHIM_SETUP) && argv[2] === 'find') {+ return ok(`${o.noPid ? '' : 'pid 99\n'}${o.found.map(t => `${t} /usr/bin/${t}\n`).join('')}${o.noPerl ? '' : 'perl /usr/bin/perl\n'}${o.noAsk ? '' : 'ask /c/ask-99\n'}`).value+ }+ if (argv[1]?.endsWith(SHIM_SETUP) && argv[2] === 'install') {+ if (o.chmodFails) return ok('', 1).value+ for (const f of argv.slice(5)) {+ if (f.startsWith('-')) files.delete(f.slice(1))+ else {+ files.set(f, files.get(`${f}${argv[4]}`)!)+ files.delete(`${f}${argv[4]}`)+ }+ }+ return ok('').value+ }+ return ok('', 1).value+ }+ const $ = {+ plugin: { root: '/p' },+ process: { run },+ env: {+ get: async (name: string) => vars[name],+ set: async (name: string, value: string | undefined) => {+ sets.push([name, value])+ if (o.denyPath && name === 'PATH') return { deny: 'held by the organization' }+ vars[name] = value+ return {}+ },+ },+ fs: {+ read: async (path: string) => {+ if (path === '/p/hooks/clip-shim.sh') return TEMPLATE+ if (files.has(path)) return files.get(path)+ throw new Error('ENOENT')+ },+ write: async (path: string, text: string) => void files.set(path, text),+ exists: async (path: string) => files.has(path),+ },+ session: { id: async () => 'sess-1' },+ } as any+ return { $, files, vars, runs, sets }+}++const shimsOf = (files: Map<string, string>) => [...files.keys()].filter(k => k.startsWith('/c/shims-99/')).sort()++test('macOS: the pbcopy shim, then IUI_COPY_ASK and PATH; again (a hot reload): nothing written or set', async () => {+ const w = shimWorld({ found: ['pbcopy'] })+ expect(await setUpShims(w.$, '/c', true)).toEqual({ tools: ['pbcopy'] })+ expect(w.runs[0]).toEqual(['sh', '/p/hooks/clip-shim-setup.sh', 'find', '/c', 'pbcopy'])+ // The shims folder of this process (pid 99), with the perl that find gave.+ expect(shimsOf(w.files)).toEqual(['/c/shims-99/pbcopy'])+ expect(w.files.get('/c/shims-99/pbcopy')).toBe(shimText(TEMPLATE, 'pbcopy', '/usr/bin/pbcopy', '/c/shims-99', '/usr/bin/perl'))+ // The FIFO first, so the first shim run has it.+ expect(w.sets).toEqual([['IUI_COPY_ASK', '/c/ask-99'], ['PATH', '/c/shims-99:/usr/bin:/bin']])+ expect(S.askFifo).toBe('/c/ask-99')+ expect(S.shimDir).toBe('/c/shims-99')+ const runs = w.runs.length+ expect(await setUpShims(w.$, '/c', true)).toEqual({ tools: ['pbcopy'] })+ // Only the find: the shim is the same, PATH and the FIFO are set.+ expect(w.runs.length).toBe(runs + 1)+ expect(w.sets.length).toBe(2)+ expect(w.vars.PATH).toBe('/c/shims-99:/usr/bin:/bin')+})++test('a find with no pid: off, nothing written or set', async () => {+ const w = shimWorld({ found: ['pbcopy'], noPid: true })+ expect(await setUpShims(w.$, '/c', true)).toEqual({ off: 'the shim folder could not be made' })+ expect(w.files.size).toBe(0)+ expect(w.sets).toEqual([])+})++test('no perl, or no FIFO: off with the reason; PATH and IUI_COPY_ASK not set', async () => {+ for (const [o, off] of [[{ noPerl: true }, 'no perl'], [{ noAsk: true }, 'the ask FIFO could not be made']] as const) {+ const w = shimWorld({ found: ['pbcopy'], ...o })+ expect(await setUpShims(w.$, '/c', true)).toEqual({ off })+ expect(w.sets).toEqual([])+ expect(S.askFifo).toBe('')+ }+})++test('Linux: a shim for each write tool found, none for the others; the shim of a tool that is gone goes', async () => {+ // Another process's shims folder is not touched.+ const w = shimWorld({ found: ['wl-copy', 'xsel'], files: { '/c/shims-99/xclip': 'old', '/c/shims-5/xclip': 'other' } })+ expect(await setUpShims(w.$, '/c', false)).toEqual({ tools: ['wl-copy', 'xsel'] })+ expect(w.runs[0]!.slice(4)).toEqual(['wl-copy', 'xclip', 'xsel'])+ expect(shimsOf(w.files)).toEqual(['/c/shims-99/wl-copy', '/c/shims-99/xsel'])+ expect(w.files.get('/c/shims-5/xclip')).toBe('other')+ expect(w.runs[1]!.slice(2)).toEqual(['install', '/c', '.new-99', '/c/shims-99/wl-copy', '-/c/shims-99/xclip', '/c/shims-99/xsel'])+})++test('Linux with no write tool: no shim, PATH not changed, off with the reason; S.askFifo stays empty', async () => {+ const w = shimWorld({ found: [] })+ expect(await setUpShims(w.$, '/c', false)).toEqual({ off: 'no wl-copy, xclip or xsel: Claude Code copies with its own addon' })+ expect(w.sets).toEqual([])+ expect(S.askFifo).toBe('')+})++test('a chmod that fails: off, with no FIFO', async () => {+ const w = shimWorld({ found: ['pbcopy'], chmodFails: true })+ expect(await setUpShims(w.$, '/c', true)).toEqual({ off: 'the clipboard shims could not be installed' })+ expect(w.sets).toEqual([])+ expect(S.askFifo).toBe('')+})++test('a PATH write the engine holds: off, with the reason', async () => {+ const w = shimWorld({ found: ['pbcopy'], denyPath: true })+ expect(await setUpShims(w.$, '/c', true)).toEqual({ off: 'PATH not set: held by the organization' })+ expect(S.askFifo).toBe('')+})++test('over ssh no shim is made and PATH is not changed; details say why', async ($, on) => {+ const runs: string[][] = []+ const sets: string[] = []+ on('env.get', async ($: any, e: any) => ({ value: ({ HOME: '/h', PATH: '/usr/bin', SSH_CONNECTION: '1.2.3.4 5 6.7.8.9 22' } as Record<string, string>)[e.name] }))+ on('env.set', async ($: any, e: any) => (sets.push(e.name), { value: {} }))+ on('process.run', async ($: any, e: any) => (runs.push(e.argv), /cc-intelligent-ui-/.test(e.argv.join(' ')) ? ok('/c\nDarwin\n') : ok('', 1)))+ const ran = await $.command.run({ command: 'iui', args: 'details' } as any)+ expect(ran.text).toContain('\nAuto copy: off (ssh)')+ expect(runs.some(argv => argv[1]?.endsWith(SHIM_SETUP) && argv[2] === 'find')).toBe(false)+ expect(sets).toEqual([])+})++test('a cache or shims folder that is not private (exit 3 at find or at install): no shims, PATH and IUI_COPY_ASK as they were', async () => {+ for (const unsafe of ['find', 'install'] as const) {+ const w = shimWorld({ found: ['pbcopy'], unsafe })+ expect(await setUpShims(w.$, '/c', true)).toEqual({ off: 'the cache folder is not private' })+ expect(w.sets).toEqual([])+ expect(w.vars.PATH).toBe('/usr/bin:/bin')+ expect(S.askFifo).toBe('')+ }+})++test('no private cache folder (the cache script failed): no shim step, PATH not changed; details say why', async ($, on) => {+ const runs: string[][] = []+ const sets: string[] = []+ on('env.get', async ($: any, e: any) => ({ value: ({ HOME: '/h', PATH: '/usr/bin', TERM_PROGRAM: 'ghostty' } as Record<string, string>)[e.name] }))+ on('env.set', async ($: any, e: any) => (sets.push(e.name), { value: {} }))+ // The cache script gives only the OS name: the shared fallback is for Typst only.+ on('process.run', async ($: any, e: any) => (runs.push(e.argv), /cc-intelligent-ui-/.test(e.argv.join(' ')) ? ok('\nDarwin\n\n') : ok('', 1)))+ const ran = await $.command.run({ command: 'iui', args: 'details' } as any)+ expect(ran.text).toContain('\nAuto copy: off (the cache folder is not private)')+ expect(runs.some(argv => argv[1]?.endsWith(SHIM_SETUP))).toBe(false)+ expect(sets).toEqual([])+})++for (const [what, vars, off] of [+ ['a background session', { TERM_PROGRAM: 'ghostty', CLAUDE_CODE_SESSION_KIND: 'bg' }, 'a background session'],+ ['a terminal with no pictures', { TERM_PROGRAM: 'Apple_Terminal' }, 'no pictures in this terminal'],+] as const) {+ test(`${what}: no shims, PATH not changed; details say why`, async ($, on) => {+ const runs: string[][] = []+ const sets: string[] = []+ on('env.get', async ($: any, e: any) => ({ value: ({ HOME: '/h', PATH: '/usr/bin', ...vars } as Record<string, string>)[e.name] }))+ on('env.set', async ($: any, e: any) => (sets.push(e.name), { value: {} }))+ on('process.run', async ($: any, e: any) => (runs.push(e.argv), /cc-intelligent-ui-/.test(e.argv.join(' ')) ? ok('/c\nDarwin\n') : ok('', 1)))+ const ran = await $.command.run({ command: 'iui', args: 'details' } as any)+ expect(ran.text).toContain(`\nAuto copy: off (${off})`)+ expect(runs.some(argv => argv[1]?.endsWith(SHIM_SETUP))).toBe(false)+ expect(sets).toEqual([])+ })+}