SLOPSHOPPER

image-preview

Hover a pasted [Image #N] to see the picture: above the prompt while you write, and on the prompts you sent.

bandspinnerrowspromptprocesstimer
★ 210v0.2.2MITupdated 2026-10-22midudev/better-claude/plugins/image-preview
A shopper browsing a rack in a slop shop
README

Image Preview

A Claude Code mod: hover a pasted [Image #1] and the picture opens by the pointer, in a small window of its own or drawn in the terminal. Click it and the image opens in the system's viewer.

[Image #1] hover to preview · click to open        ← hover: the picture opens by the pointer
──────────────────────────────────────────────────
❯ what is wrong in this screenshot? [Image #1]
  • While you write: paste an image and a row above the prompt lists its [Image #N]. Put the pointer on one and its preview opens; move it off and the preview closes. Click it and the image opens in Preview on macOS, in the system's viewer elsewhere.
  • After you send it: put the pointer on an [Image #N] in the first line of the prompt you sent, or on the ⎿ [Image #N] rows Claude Code lists under it, and its preview opens. Click the [Image #N] in the prompt to open the image.

The preview shows only while the pointer is on the [Image #N] that opened it. Typing, sending the prompt or a click closes it, and so does the transcript moving under a sent prompt's; moving the pointer on the [Image #N] again opens it again.

Hovering needs the mouse, so it needs Claude Code's fullscreen interface ("tui": "fullscreen" in your settings, or /config).

In a window, by the pointer

Where the terminal cannot draw a picture itself (Warp, iTerm2, Terminal.app, VS Code), the preview is a small borderless window outside the terminal: above the pointer and to its right, under it or to its left where the screen has no room, never over the pointer. It never takes the focus or the pointer, so you keep typing and hovering in the terminal, and it stands over every window, full-screen ones too. It shows in about a third of a second.

The picture shows as it is, sharp: a screenshot of text reads as it was. It is shrunk only to fit 800 points on its longest side and 70% of the screen, and never enlarged. Each system draws the window with what it has:

SystemThe windowNeeds
macOSAn AppKit panel with the HUD material, round corners and a shadow, run by osascript (JavaScript for Automation)Nothing to install
WindowsA Windows Forms tool window, run by PowerShellNothing to install
WSLThe same Windows Forms window, run by powershell.exe from WSL, by the Windows pointerNothing to install
LinuxA Tk tooltip window on X11, the picture shrunk to a PNGPython's Tk (python3-tk on Debian and Ubuntu, python3-tkinter on Fedora) and ImageMagick

Turn it off with the Preview in a window option (/config) to keep the preview in the terminal. Over SSH the window would open on the other machine, so there the terminal draws it. Where the window's program cannot show one, the terminal draws the previews for the rest of the session.

In the terminal

In Ghostty and kitty the terminal draws the picture itself, through the kitty graphics protocol. Elsewhere, without the window, it is drawn in colored block elements: each cell is sampled at eight by eight points, in linear light, and drawn with the half, eighth or quarter block (▀ ▂ ▎ ▚ …) that splits those points best, in their two colors, in any terminal with true color. Cells are coarse: a screenshot of text shows its layout, not text you can read.

  12
╭──────────────────────────────────────────╮
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│
│▃▁▁▘▘▁▃▀▀▖▗▀▖▗▀▏▊▎▗▘▃▗▀▖▀▂▁▉▚▉▘▏▗▀▖▗▀▖▀▀▀│   ← the picture, in color, over the transcript
│[Image #1] PNG · 151 KB                   │
╰──────────────────────────────────────────╯
[Image #1] hover to preview · click to open

The card floats over the transcript and moves nothing: the transcript does not scroll, the prompt does not jump, and the [Image #N] that opened it stays uncovered. A card from the row above the prompt floats over the lowest rows of the transcript; on a screen too short for that, it opens in that row, which grows to hold it. One from a sent prompt opens above the prompt or under its list, wherever there are more rows; where neither has six, it does not open.

Warp speaks the kitty graphics protocol, but not the Unicode placeholders Claude Code draws pictures with, so Warp cannot draw it in the terminal. CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1 makes this mod send the picture to a terminal Claude Code does not know draws pictures; it only helps in one that supports those placeholders. It sends a PNG of up to 1024 pixels and 2 MB; a larger one is drawn in cells.

Install

/plugin marketplace add midudev/better-claude
/plugin install image-preview@better-claude

Limits

  • The text inside the prompt box can only be colored by a mod, not hovered, so while you write the pointer target is the row above it, not the [Image #N] in the box itself.
  • In a sent prompt, the [Image #N] takes the pointer and the click in the prompt's first line only, before it would wrap: past that, the mod cannot tell where the line puts it. The ⎿ [Image #N] rows show the preview but take no click, which Claude Code keeps for itself.
  • A window stays two minutes at most, even with the pointer still on its [Image #N]: move off and back on to open it again.
  • A click opens the image with open -a Preview on macOS, explorer on Windows, explorer.exe under WSL (the path turned into its Windows form by wslpath -w), xdg-open elsewhere.
  • On Linux the window needs X11 (or XWayland): under Wayland alone there is none, and the terminal draws the card. Under XWayland the window may not stand by the pointer when the terminal is a Wayland window.
  • On Windows the first window compiles a small helper into the temp folder, which takes a second. The Windows window and converter have been written and checked (the scripts parse, the commands are tested) and have been run on Windows from WSL.
  • Under WSL (WSL_DISTRO_NAME is set) the session is Linux but the desktop is Windows', so the window, the converter and the viewer are Windows': a WSLg window could not tell where the pointer is over a Windows terminal.
  • Hovering needs the terminal to report mouse motion. A terminal multiplexer between Claude Code and the terminal must pass it on: in testing, herdr 0.9.1 did not, and no preview opened.
  • It reads the images Claude Code stores for the session, in its temp folder (<CLAUDE_CODE_TMPDIR or /tmp>/claude-<uid>/…/images; on Windows <CLAUDE_CODE_TMPDIR or %TEMP%>\claude\…\images), and keeps its shrunk copies in an image-preview folder beside them. Images from a session started before you installed the mod are found too; images from another session are not.
  • A session resumed with --continue draws its rows all at once, its prompts apart from the rest; the mod puts them back in order by turns. A turn with no done line (one interrupted) can put a card a few rows off.
  • It shrinks the picture with sips on macOS, ImageMagick (magick or convert) on Linux, and ImageMagick or PowerShell on Windows and under WSL (there powershell.exe, its variables passed through WSLENV). Without them, the card says it cannot read it.

How it works

One hooks module (hooks/register.tsx), a surface module for the pointer targets (hooks/chip.tsx), a decoder (hooks/pixels.ts) and the window programs (hooks/window.ts):

  • prompt.edit and a look at the box every half second (a paste puts the placeholder there without an edit event) keep the list of [Image #N] in the draft.
  • Each image is found in the session's temp folder, shrunk to a BMP of 800 pixels with sips, ImageMagick or PowerShell, and parsed into pixels; each cell is sampled from them in linear light at eight by eight points.
  • Each [Image #N] is a Client: in the band, a chip it draws; in a sent prompt, an unpainted region laid over the [Image #N] Claude Code draws, in the prompt and in the rows under it. It posts when the pointer comes onto it, moves on it and leaves it, and when it is clicked; a ui.message hook opens and closes the preview, or opens the image.
  • In a window, a hover starts the system's program with $.process.spawn, told the picture, its largest side and how long it may stay (IMAGE_PREVIEW_FILE, IMAGE_PREVIEW_SIZE, IMAGE_PREVIEW_LIFE). It says shown once the window is up, then a . every tenth of a second, so the mod wakes to end it when the pointer leaves: it stops reading, and the program ends. One that ends without saying shown hands the previews back to the terminal.
  • In the terminal, a ui.render hook on every transcript row notes which of its rows show. The card is drawn position: "absolute" (out of the flow, so it moves nothing) in the row that holds it: the lowest on screen for a chip of the band, the sent prompt itself or the lowest row for its list. It covers only rows drawn before that row, never the [Image #N] that opened it. Only the row the card leaves and the one it joins draw again (a state family, one flag per row).

Development

claude plugin validate ./plugins/image-preview   # from the repository root
claude plugin test ./plugins/image-preview       # runs hooks/register.test.tsx against the engine

MIT © Miguel Ángel Durán (midudev)

Source 5 files
hooks/register.tsx 767 lines
1import { atom, memberOf, read, update } from 'claude-code'
2import type { Elements, EngineInterface, EventOf, Register, RenderElement } from 'claude-code'
3
4import { fit, parseBmp, toCells } from './pixels'
5import type { Pixels } from './pixels'
6import { LINUX, MACOS, WINDOWS, WINDOWS_CONVERT, powershell } from './window'
7import type { Picture, Shown } from '../types'
8
9type Engine = EngineInterface
10type RenderOf<C extends string> = Extract<EventOf['ui.render'], { component: C, surface: 'terminal' }>
11type Bounds = { columns: number; rows: number }
12type Ui = Elements['terminal']
13
14// A transcript row: a card floats over the rows above the end of one.
15type Row = RenderOf<'UserMessage' | 'AssistantMessage' | 'ToolUse' | 'ToolResult' | 'ToolGroup' | 'CommandOutput' | 'TurnDuration' | 'InfoNotice'>
16type OnScreen = NonNullable<Row['props']['onScreen']>
17
18const draft = atom({ plugin: 'image-preview', key: 'draft' } as const, [] as number[])
19const pictures = atom({ plugin: 'image-preview', key: 'pictures' } as const, {} as Record<string, Picture>)
20const shown = atom({ plugin: 'image-preview', key: 'shown' } as const, null as Shown | null)
21const hosting = atom({ plugin: 'image-preview', key: 'hosting' } as const, false)
22
23const TOKEN = /\[Image #(\d+)\]/g
24const NOT_STORED = 'not stored in this session'
25// The longest side a picture is shrunk to before it is sampled into cells, eight points a cell across.
26const SOURCE_PIXELS = 800
27const MAX_COLUMNS = 160
28const MAX_ROWS = 48
29// What the screen keeps under the transcript (the band's row, the prompt and its lines), and above
30// its first row (the logo); the fewest rows a card is worth floating in, from the band's chips and
31// from a sent prompt's.
32const BOTTOM_ROWS = 7
33const HEADER_ROWS = 4
34const MIN_FLOAT_ROWS = 10
35const MIN_CARD_ROWS = 6
36// What a card adds around the picture: the border, and the caption under it.
37const CARD_COLUMNS = 2
38const CARD_ROWS = 3
39// Under a sent prompt the engine lists its images, one a row, each after `  ⎿  `; the prompt's own
40// first line starts after `❯ `, on the row after the blank one above it.
41const LISTED_AT = 5
42const PROMPT_AT = 2
43const WAIT_MS = 200
44// Pasting an image puts its placeholder in the box with no edit event: the box is looked at this often.
45const POLL_MS = 500
46const WAIT_TRIES = 15
47// The longest side of the picture a terminal is sent to draw itself, and the most bytes it may take.
48const VIEW_PIXELS = 1024
49const VIEW_BYTES = 2 * 1024 * 1024
50// The longest side of the picture in a window of its own, in points, and the most seconds it stays.
51const WINDOW_SIZE = 800
52const WINDOW_LIFE = 120
53
54/** The ids of the `[Image #N]` placeholders in a text, each once, in order. */
55export const idsIn = (text: string) => [...new Set([...text.matchAll(TOKEN)].map(match => Number(match[1])))]
56
57const sameIds = (a: readonly number[], b: readonly number[] = []) => a.length === b.length && a.every((id, i) => id === b[i])
58
59const sizeOf = (bytes: number) =>
60  bytes < 1024 ? `${bytes} B` : bytes < 1024 * 1024 ? `${Math.round(bytes / 1024)} KB` : `${(bytes / 1024 / 1024).toFixed(1)} MB`
61
62// How the terminal draws the picture itself: Ghostty and kitty read the file; one Claude Code was
63// told draws pictures (CLAUDE_CODE_FORCE_TERMINAL_IMAGES) is sent its bytes; any other gets it in
64// colored cells.
65let graphics: 'none' | 'file' | 'bytes' = 'none'
66let sessionId: string | undefined
67let folder: string | undefined
68let madeFolder: string | undefined
69// Claude Code's temp folder for every session of this user.
70let temp: string | undefined
71type Platform = 'macos' | 'linux' | 'windows' | 'other'
72// The system the session runs on: what converts, opens and shows a picture there.
73let platform: Promise<Platform> | undefined
74type Tool = 'sips' | 'magick' | 'convert' | 'powershell'
75type Command = { argv: string[]; env?: Record<string, string> }
76// The converters each system is tried with, in order: sips on macOS, ImageMagick, and on Windows
77// PowerShell's System.Drawing (there `convert` is the system's disk tool, never ImageMagick's).
78const TOOLS: Record<Platform, Tool[]> = { macos: ['sips', 'magick', 'convert'], linux: ['magick', 'convert'], windows: ['magick', 'powershell'], other: ['magick', 'convert'] }
79// The converter that worked here.
80let tool: Tool | undefined
81// Where a card opens in a window of its own, by the pointer: the system whose program shows it.
82// Unset, the terminal draws the card. And the window open now, if any.
83let windowing: Platform | undefined
84let window: { id: number; isOpen: boolean } | undefined
85// The pictures read, shrunk, by id; the cells drawn of them, by id and size.
86const thumbs = new Map<number, Pixels>()
87const loading = new Set<number>()
88const failed = new Set<number>()
89const cells = new Map<string, string>()
90// The PNG bytes, base64, sent to a terminal that is not known to read files, by id.
91const views = new Map<number, string>()
92// Each transcript row drawn, by requestId: which it is, when first drawn, whether in the batch the
93// session (or a reload) starts by drawing, and which of its rows show. Whether that batch is still
94// coming in. How many times a row was added or moved, and how many when the card opened. And the
95// screen's height, as the last drawing said.
96type Noted = { component: Row['component']; seen: number; isBatch: boolean; onScreen: OnScreen | null | undefined }
97const rows = new Map<string, Noted>()
98let seenRows = 0
99let cachedOrder: Map<string, number> | undefined
100let cachedOrderSeen = -1
101// The module loads as the session starts or reloads, and the engine draws every row right after.
102let isBatching = true
103let seenAtLastLook = 0
104let moves = 0
105let movesShown = 0
106let screenRows = 40
107let draftIds: number[] = []
108let hasShown = false
109let poll: { cancel: () => void } | undefined
110
111/** The system the session runs on, asked once. */
112function platformOf ($: Engine) {
113  platform ??= (async (): Promise<Platform> => {
114    if ((await $.env.get('OS')) === 'Windows_NT') return 'windows'
115    const name = (await $.process.run(['uname', '-s']).catch(() => undefined))?.stdout.trim()
116    return name === 'Darwin' ? 'macos' : name === 'Linux' ? 'linux' : 'other'
117  })()
118  return platform
119}
120
121// Whether the session runs under WSL: Linux, on a Windows desktop.
122let wsl: Promise<boolean> | undefined
123
124/**
125 * Whether the session runs under WSL, asked once. There the session is Linux, but the desktop, the
126 * pointer and the viewer are Windows': the picture is converted, shown and opened by Windows' own.
127 */
128function wslOf ($: Engine) {
129  wsl ??= (async () => (await platformOf($)) === 'linux' && (await $.env.get('WSL_DISTRO_NAME')) !== undefined)()
130  return wsl
131}
132
133/**
134 * A PowerShell command as WSL runs it: as `powershell.exe`, its variables crossing over through
135 * WSLENV, the paths among them (`/p`) turned into their Windows form. Elsewhere, as it is.
136 */
137async function crossing ($: Engine, command: Command): Promise<Command> {
138  if (command.argv[0] !== 'powershell' || !(await wslOf($))) return command
139  const names = Object.keys(command.env ?? {}).map(name => (/^IMAGE_PREVIEW_(FILE|OUT)$/.test(name) ? `${name}/p` : name))
140  const inherited = await $.env.get('WSLENV')
141  return { argv: ['powershell.exe', ...command.argv.slice(1)], env: { ...command.env, WSLENV: [inherited, ...names].filter(Boolean).join(':') } }
142}
143
144/** Forgets what was read in another session: after /clear the ids start again from 1. */
145async function follow ($: Engine) {
146  const id = await $.session.id()
147  if (id === sessionId) return
148  // The rows drawn so far are this session's, unless another came before it.
149  if (sessionId !== undefined) {
150    rows.clear()
151    cachedOrder = undefined
152    cachedOrderSeen = -1
153    moves++
154  }
155  sessionId = id
156  folder = undefined
157  madeFolder = undefined
158  thumbs.clear()
159  failed.clear()
160  cells.clear()
161  views.clear()
162  await update($, pictures, () => ({}))
163  await change($, () => null)
164}
165
166/**
167 * Claude Code's temp folder: `<CLAUDE_CODE_TMPDIR or /tmp>/claude-<uid>`. Windows has no uid: there
168 * it is `<CLAUDE_CODE_TMPDIR or the system's temp folder>\claude`.
169 */
170async function tempFolder ($: Engine) {
171  if ((await platformOf($)) !== 'windows') {
172    const base = ((await $.env.get('CLAUDE_CODE_TMPDIR')) || '/tmp').replace(/\/+$/, '')
173    return `${base}/claude-${(await $.process.run(['id', '-u'])).stdout.trim()}`
174  }
175  // The system's temp folder as Node finds it: %TEMP%, %TMP%, or the one in Windows' own folder.
176  const system = (await $.env.get('TEMP')) || (await $.env.get('TMP')) || `${(await $.env.get('SystemRoot')) || 'C:\\Windows'}\\temp`
177  const base = ((await $.env.get('CLAUDE_CODE_TMPDIR')) || system).replace(/[\\/]+$/, '')
178  return `${base}/claude`
179}
180
181/**
182 * Claude Code's temp folder for this session, where a pasted image is stored as
183 * `images/<N>.<ext>`: `<temp folder>/<project path, dashed>/<session id>`.
184 */
185async function sessionFolder ($: Engine) {
186  if (folder) return folder
187  const id = await $.session.id()
188  const root = (temp ??= await tempFolder($))
189  const names = new Set([await $.session.root(), await $.session.cwd()].map(path => path.replace(/[^a-zA-Z0-9]/g, '-')))
190  for (const name of names) {
191    if (await $.fs.exists(`${root}/${name}/${id}/images`)) return (folder = `${root}/${name}/${id}`)
192  }
193  // A project path too long for a folder name gets a hash after it: look for the session instead.
194  for (const entry of await $.fs.list(root).catch(() => [])) {
195    if (entry.kind === 'dir' && (await $.fs.exists(`${root}/${entry.name}/${id}/images`))) {
196      return (folder = `${root}/${entry.name}/${id}`)
197    }
198  }
199  return undefined
200}
201
202async function fileOf ($: Engine, id: number) {
203  const found = await sessionFolder($)
204  if (!found) return undefined
205  const name = (await $.fs.list(`${found}/images`).catch(() => [])).find(entry => entry.name.startsWith(`${id}.`))?.name
206  return name && /^\d+\.[a-z]+$/.test(name) ? `${found}/images/${name}` : undefined
207}
208
209/** Runs the first converter that works here, and remembers it. */
210async function convert ($: Engine, commandOf: (tool: Tool) => Command) {
211  // Under WSL, Windows' PowerShell converts where ImageMagick is not installed.
212  const tools = (await wslOf($)) ? [...TOOLS.linux, 'powershell' as const] : TOOLS[await platformOf($)]
213  for (const name of tool ? [tool] : tools) {
214    const { argv, env } = await crossing($, commandOf(name))
215    const result = await $.process.run(argv, env && { env }).catch(() => undefined)
216    if (result?.exitCode === 0) {
217      tool = name
218      return true
219    }
220  }
221  return false
222}
223
224/**
225 * How `tool` writes `file` as `format` to `out`, its longest side `side` pixels (`n` stretched or
226 * shrunk to it, `n>` only shrunk; none, as it is).
227 */
228function shrink (tool: Tool, file: string, out: string, format: 'bmp' | 'png', side?: string): Command {
229  if (tool === 'sips') return { argv: ['sips', '-s', 'format', format, ...(side ? ['-Z', side.replace('>', '')] : []), file, '--out', out] }
230  if (tool === 'powershell') {
231    return { argv: powershell(WINDOWS_CONVERT), env: { IMAGE_PREVIEW_FILE: file, IMAGE_PREVIEW_OUT: out, IMAGE_PREVIEW_FORMAT: format, ...(side && { IMAGE_PREVIEW_SIZE: side.replace('>', '') }) } }
232  }
233  const size = side && `${side.replace('>', '')}x${side}`
234  return { argv: [tool, file, ...(size ? ['-resize', size] : []), `${format === 'bmp' ? 'BMP3' : 'PNG'}:${out}`] }
235}
236
237/** Makes the mod's cache folder. */
238async function makeFolder ($: Engine, path: string) {
239  if (madeFolder === path) return
240  if ((await platformOf($)) !== 'windows') await $.process.run(['mkdir', '-p', path])
241  else await $.process.run(powershell('New-Item -ItemType Directory -Force -Path $env:IMAGE_PREVIEW_OUT | Out-Null'), { env: { IMAGE_PREVIEW_OUT: path } })
242  madeFolder = path
243}
244
245async function decode ($: Engine, file: string, out: string): Promise<Pixels> {
246  const isConverted = await convert($, name => shrink(name, file, out, 'bmp', String(SOURCE_PIXELS)))
247  if (!isConverted) throw new Error('needs sips (macOS), ImageMagick or PowerShell (Windows) to read it')
248  const { base64 } = await $.fs.read(out, { as: 'bytes' })
249  return parseBmp(Uint8Array.fromBase64(base64))
250}
251
252/** A PNG of the picture for the terminal to draw itself: the file, or a copy converted beside the cache. */
253async function pngOf ($: Engine, file: string, out: string) {
254  if (file.endsWith('.png')) return file
255  return (await convert($, name => shrink(name, file, out, 'png'))) ? out : undefined
256}
257
258/** The picture shrunk to a PNG, base64, for a terminal sent the bytes: none past what one may take. */
259async function viewOf ($: Engine, file: string, out: string) {
260  const isConverted = await convert($, name => shrink(name, file, out, 'png', `${VIEW_PIXELS}>`))
261  if (!isConverted || (await $.fs.stat(out)).size > VIEW_BYTES) return undefined
262  return (await $.fs.read(out, { as: 'bytes' })).base64
263}
264
265/** Reads image `id` once: its file, then its pixels, shrunk; the drawings that wait on it redraw. */
266async function load ($: Engine, id: number) {
267  if (thumbs.has(id) || loading.has(id) || failed.has(id)) return
268  const loadingIn = sessionId
269  loading.add(id)
270  let picture: Picture
271  try {
272    // The engine writes the file just after the paste puts the placeholder in the box.
273    let file = await fileOf($, id)
274    for (let tries = 0; !file && tries < WAIT_TRIES; tries++) {
275      await $.clock.sleep(WAIT_MS)
276      file = await fileOf($, id)
277    }
278    if (!file) throw new Error(NOT_STORED)
279
280    const cache = `${await sessionFolder($)}/image-preview`
281    await makeFolder($, cache)
282    const pixels = await decode($, file, `${cache}/${id}.bmp`)
283    let png: string | undefined
284    if (graphics === 'file') png = await pngOf($, file, `${cache}/${id}.png`)
285    if (graphics === 'bytes') {
286      const view = await viewOf($, file, `${cache}/${id}.view.png`)
287      if (view) {
288        views.set(id, view)
289        png = `${cache}/${id}.view.png`
290      }
291    }
292    const { size } = await $.fs.stat(file)
293    if (sessionId === loadingIn) thumbs.set(id, pixels)
294    const format = file.slice(file.lastIndexOf('.') + 1)
295    picture = { file, format, bytes: size, width: pixels.width, height: pixels.height, ...(png && { png }) }
296  } catch (error) {
297    if (sessionId === loadingIn) failed.add(id)
298    picture = { error: error instanceof Error ? error.message : String(error) }
299  } finally {
300    loading.delete(id)
301  }
302  if (sessionId !== loadingIn) return
303  // Read in the background: a module unloaded meanwhile has no state to write to.
304  await update($, pictures, all => ({ ...all, [id]: picture })).catch(() => undefined)
305}
306
307async function fileFor ($: Engine, id: number) {
308  const picture = (await read($, pictures))[id]
309  return picture && !('error' in picture) ? picture.file : await fileOf($, id)
310}
311
312/** Opens image `id` in the system's viewer: Preview on macOS, the desktop's own elsewhere. */
313async function openImage ($: Engine, id: number) {
314  const file = await fileFor($, id)
315  if (!file) return
316  if (await wslOf($)) {
317    const path = (await $.process.run(['wslpath', '-w', file]).catch(() => undefined))?.stdout.trim()
318    if (path) await $.process.run(['explorer.exe', path]).catch(() => undefined)
319    return
320  }
321  const system = await platformOf($)
322  const argv = system === 'macos' ? ['open', '-a', 'Preview', file] : system === 'windows' ? ['explorer', file.replaceAll('/', '\\')] : ['xdg-open', file]
323  await $.process.run(argv).catch(() => undefined)
324}
325
326/** The program that shows image `id` in a window of its own here, and what it is told. */
327async function windowCommand ($: Engine, id: number): Promise<Command | undefined> {
328  const file = await fileFor($, id)
329  if (!file) return undefined
330  const env = { IMAGE_PREVIEW_FILE: file, IMAGE_PREVIEW_SIZE: String(WINDOW_SIZE), IMAGE_PREVIEW_LIFE: String(WINDOW_LIFE) }
331  if (windowing === 'macos') return { argv: ['osascript', '-l', 'JavaScript', '-e', MACOS], env }
332  if (windowing === 'windows') return crossing($, { argv: powershell(WINDOWS), env })
333  // Tk reads a PNG: the picture shrunk to one, once.
334  const cache = `${await sessionFolder($)}/image-preview`
335  const out = `${cache}/${id}.window.png`
336  if (!(await $.fs.exists(out))) {
337    await makeFolder($, cache)
338    if (!(await convert($, name => shrink(name, file, out, 'png', `${WINDOW_SIZE}>`)))) return undefined
339  }
340  return { argv: ['python3', '-c', LINUX], env: { ...env, IMAGE_PREVIEW_FILE: out } }
341}
342
343/**
344 * Shows image `id` in a window of its own till `closeWindow`. The program says `.` ten times a
345 * second, so the loop wakes to see it was closed, and leaving the loop ends the program. One that
346 * ends without having shown a window says the system shows none: the card goes back to the terminal.
347 */
348async function openWindow ($: Engine, id: number) {
349  if (window?.id === id && window.isOpen) return
350  closeWindow()
351  const opening = { id, isOpen: true }
352  window = opening
353  const command = await windowCommand($, id)
354  let isShown = false
355  let said = ''
356  if (command && opening.isOpen) {
357    try {
358      for await (const { text } of $.process.spawn(command)) {
359        if (said.length < 2000) said += text
360        if (/^shown$/m.test(text)) isShown = true
361        if (!opening.isOpen) break
362      }
363    } catch (error) {
364      said += error instanceof Error ? error.message : String(error)
365    }
366  }
367  if (window === opening) window = undefined
368  if (isShown || !opening.isOpen) return
369  $.ui.log(`image-preview: no window (${said.replace(/\.\n/g, '').trim().slice(0, 300) || 'the program could not start'}); the terminal draws the cards`, { to: 'debug' })
370  windowing = undefined
371  await change($, now => (now?.edge === 'window' ? placeFor(now.id, now.target) : now))
372}
373
374function closeWindow () {
375  if (window) window.isOpen = false
376  window = undefined
377}
378
379/**
380 * The picture of image `id` in a bordered card, as large as the bounds allow, its caption under it.
381 * It floats over the transcript, which shows through any cell it leaves unwritten: every line is
382 * written to the card's width, spaces and all.
383 */
384function card ({ Box, Image, Raster, Text }: Ui, all: Record<string, Picture>, id: number, bounds: Bounds) {
385  const label = `[Image #${id}]`
386  const picture = all[id]
387  const line = (text: string, width: number) => <Text dimColor wrap="truncate-end">{text.padEnd(width)}</Text>
388  if (!picture || (!('error' in picture) && !thumbs.has(id))) return line(`${label} loading…`, bounds.columns)
389  if ('error' in picture) return line(`${label} no preview: ${picture.error}`, bounds.columns)
390
391  const { columns, rows } = fit(picture.width, picture.height, bounds.columns - CARD_COLUMNS, bounds.rows - CARD_ROWS)
392  const view = views.get(id)
393  const source = !picture.png ? undefined : graphics === 'file' ? { file: picture.png, format: 'png' as const } : view && { png: view }
394  const key = `${id}:${columns}x${rows}`
395  let painted = cells.get(key)
396  if (!source && painted === undefined) {
397    painted = toCells(thumbs.get(id)!, columns, rows)
398    cells.set(key, painted)
399  }
400  const caption = `${label} ${picture.format.toUpperCase()} · ${sizeOf(picture.bytes)}`
401  // A picture narrower than its caption is centered, the rows beside it written blank.
402  const width = Math.max(columns, Math.min(caption.length, bounds.columns - CARD_COLUMNS))
403  const side = (n: number) => n > 0 && <Text>{Array.from({ length: rows }, () => ' '.repeat(n)).join('\n')}</Text>
404  const left = Math.floor((width - columns) / 2)
405  return (
406    <Box key={`card-${id}`} flexDirection="column" borderStyle="round" borderDimColor flexShrink={0} width={width + CARD_COLUMNS}>
407      <Box flexDirection="row">
408        {side(left)}
409        {source
410          ? <Image key={`image-${id}`} source={source} columns={columns} rows={rows} alt={label} />
411          : <Raster key={`image-${id}`} columns={columns} rows={rows} cells={painted!} />}
412        {side(width - columns - left)}
413      </Box>
414      {line(caption, width)}
415    </Box>
416  )
417}
418
419const sameShown = (a: Shown | null, b: Shown | null) =>
420  a === b || (a !== null && b !== null && a.id === b.id && a.target === b.target && a.host === b.host && a.rows === b.rows && a.edge === b.edge)
421
422let changing = Promise.resolve()
423
424/**
425 * Opens, moves or closes (null) the card, one change at a time, each `next` handed the card the one
426 * before left: the pointer leaving one chip and coming onto the next are two changes, in either
427 * order. The row the card leaves and the one it joins draw again.
428 */
429function change ($: Engine, next: (now: Shown | null) => Shown | null) {
430  const run = changing.then(async () => {
431    const last = await read($, shown)
432    const to = next(last)
433    movesShown = moves
434    hasShown = to !== null
435    if (sameShown(last, to)) return
436    await update($, shown, () => to)
437    if (last?.host && last.host !== to?.host) await update($, memberOf(hosting, { requestId: last.host }), () => false)
438    if (to?.host && to.host !== last?.host) await update($, memberOf(hosting, { requestId: to.host }), () => true)
439    if (to?.edge === 'window') void openWindow($, to.id)
440    else closeWindow()
441  })
442  changing = run.catch(() => undefined)
443  return run
444}
445
446/** Closes the card a chip of the band opened, if one did. */
447const closeChipCard = (now: Shown | null) => (now && !isFromPrompt(now) ? null : now)
448
449const isFromPrompt = (now: Shown) => /\/(sent|token):/.test(now.target)
450
451/** Where the card a target opened goes: a target is `<site>/<chip key>`, the site a sent prompt's for its chips. */
452const placeFor = (id: number, target: string) => placeOf(id, target, /\/chip:/.test(target) ? undefined : target.slice(0, target.indexOf('/')))
453
454/** Notes a transcript row, and which of its rows show: before anything is awaited, so in the transcript's order. */
455function note (e: Row) {
456  if (e.viewport && e.viewport.rows !== screenRows) {
457    screenRows = e.viewport.rows
458    moves++
459  }
460  const onScreen = e.props.onScreen
461  const row = rows.get(e.requestId)
462  if (!row) {
463    rows.set(e.requestId, { component: e.component, seen: seenRows++, isBatch: isBatching, onScreen })
464    moves++
465  } else if (row.onScreen?.first !== onScreen?.first || row.onScreen?.last !== onScreen?.last || row.onScreen?.of !== onScreen?.of) {
466    row.onScreen = onScreen
467    moves++
468  }
469}
470
471/**
472 * The rows in the transcript's order, as best known. A row drawn as it came is in place. The batch
473 * a start or a reload draws comes in two runs, the sent prompts apart from the rest (each run in
474 * order): each prompt is put back at the start of a turn, the row after a turn's duration line,
475 * the newest prompt at the newest turn.
476 */
477function transcriptOrder () {
478  if (cachedOrder && cachedOrderSeen === seenRows) return cachedOrder
479  const bySeen = (a: [string, Noted], b: [string, Noted]) => a[1].seen - b[1].seen
480  const batch = [...rows].filter(([, row]) => row.isBatch).sort(bySeen)
481  const notices = batch.filter(([, row]) => row.component === 'InfoNotice')
482  const prompts = batch.filter(([, row]) => row.component === 'UserMessage')
483  const others = batch.filter(([, row]) => row.component !== 'InfoNotice' && row.component !== 'UserMessage')
484  const starts = [0, ...others.flatMap(([, row], i) => (row.component === 'TurnDuration' && i + 1 < others.length ? [i + 1] : []))]
485  const before = new Map<number, string[]>()
486  prompts.forEach(([id], i) => {
487    const at = starts[Math.max(0, starts.length - (prompts.length - i))]!
488    before.set(at, [...(before.get(at) ?? []), id])
489  })
490  const ordered = [...notices.map(([id]) => id), ...others.flatMap(([id], i) => [...(before.get(i) ?? []), id])]
491  if (others.length === 0) ordered.push(...prompts.map(([id]) => id))
492  const order = new Map(ordered.map((id, i) => [id, i]))
493  for (const [id, row] of rows) if (!row.isBatch) order.set(id, ordered.length + row.seen)
494  cachedOrder = order
495  cachedOrderSeen = seenRows
496  return order
497}
498
499/**
500 * Where the card of image `id` floats, from what the transcript's rows said of themselves; it never
501 * covers the chip that opened it, and covers only rows drawn before the one it stands on (a row drawn
502 * after paints over it).
503 *
504 * From the band's chip: over the rows above the end of the lowest row on screen; in the band
505 * itself where too few rows show. From the list under a sent prompt (`prompt`, its requestId): over
506 * the rows above the prompt, or over those under its list, whichever holds more; none where neither
507 * holds a card. The rows counted are the rows the mod knows of, so a count is never too high.
508 */
509function placeOf (id: number, target: string, prompt?: string): Shown | null {
510  if (windowing) return { id, target, host: null, rows: 0, edge: 'window' }
511  const order = transcriptOrder()
512  const showing = [...rows]
513    .filter(([, row]) => !!row.onScreen)
514    .map(([id, row]) => [id, { order: order.get(id)!, onScreen: row.onScreen! }] as const)
515  // A row first drawn late (as one scrolled into view) still has its place where it is cut: the row
516  // cut at the top is the first on screen, the one cut at the foot the last.
517  const placeOn = ({ order, onScreen }: { order: number; onScreen: OnScreen }) =>
518    onScreen.first > 0 ? -Infinity : onScreen.last < onScreen.of - 1 ? Infinity : order
519  showing.sort((a, b) => placeOn(a[1]) - placeOn(b[1]) || a[1].order - b[1].order)
520  const room = screenRows - BOTTOM_ROWS
521  const lowest = showing.at(-1)
522  const top = showing[0]?.[1]
523  // A row off the screen, or the first on it cut, is a transcript that fills the screen.
524  const isFull = top !== undefined && (top.onScreen.first > 0 || [...rows.values()].some(row => row.onScreen === null))
525  const count = (list: typeof showing) => list.reduce((sum, [, row]) => sum + row.onScreen.last - row.onScreen.first + 1, 0)
526
527  if (prompt === undefined) {
528    // One row kept free: the count of the rows above is a guess.
529    const above = !lowest ? 0 : (isFull ? room : Math.min(room, HEADER_ROWS + count(showing))) - 1
530    return above >= MIN_FLOAT_ROWS
531      ? { id, target, host: lowest![0], rows: above, edge: 'last' }
532      : { id, target, host: null, rows: 0, edge: 'last' }
533  }
534  const at = showing.findIndex(([requestId]) => requestId === prompt)
535  if (at === -1) return null
536  const message = showing[at]![1].onScreen
537  // Above: the rows before it that show, the logo's in a short transcript, and its own blank first row.
538  const above = message.first > 0 ? 0 : Math.min(room, count(showing.slice(0, at)) + (isFull ? 0 : HEADER_ROWS) + 1)
539  // Under: the rows after it, between its list and the foot of the lowest.
540  const under = Math.min(room, count(showing.slice(at + 1)))
541  if (Math.max(above, under) < MIN_CARD_ROWS) return null
542  return above >= under
543    ? { id, target, host: prompt, rows: above, edge: 'first' }
544    : { id, target, host: lowest![0], rows: under, edge: 'last' }
545}
546
547/**
548 * Keeps the open card in step with the transcript. A sent prompt's list moves with it, and a chip
549 * that moves from under a resting pointer says nothing: its card closes when anything moves (a move
550 * of the pointer opens it again). The band's chips stay put: their card follows the lowest row.
551 */
552async function settle ($: Engine) {
553  if (seenRows === seenAtLastLook) isBatching = false
554  seenAtLastLook = seenRows
555  if (!hasShown || moves === movesShown) return
556  await change($, now => !now ? null : !isFromPrompt(now) ? placeFor(now.id, now.target) : moves === movesShown ? now : null)
557}
558
559/** A transcript row, drawn with the open card where it holds it: out of the flow, so it moves nothing. */
560async function float ($: Engine, e: Row, drawn: RenderElement, overlays: RenderElement[] = []) {
561  const now = (await read($, memberOf(hosting, e))) ? await read($, shown) : null
562  const onScreen = e.props.onScreen
563  const isHost = now !== null && now.host === e.requestId && !!onScreen
564  if (!isHost && overlays.length === 0) return drawn
565  const ui = $.ui.resolve(e)
566  const { Box } = ui
567  let floating: RenderElement | undefined
568  if (isHost) {
569    const bounds = { columns: Math.max(8, Math.min(MAX_COLUMNS, (e.viewport?.columns ?? 80) - 4)), rows: Math.min(MAX_ROWS, now.rows) }
570    // On the last row of this one that shows (the rest are past the screen's edge), or ending on
571    // its first, the blank row above a message.
572    const bottom = now.edge === 'last' ? onScreen.of - 1 - onScreen.last : onScreen.of - 1
573    floating = (
574      <Box key={`float-${now.id}`} position="absolute" bottom={bottom} left={0}>
575        {card(ui, await read($, pictures), now.id, bounds)}
576      </Box>
577    )
578  }
579  return (
580    <Box flexDirection="column">
581      {drawn}
582      {overlays}
583      {floating}
584    </Box>
585  )
586}
587
588/** Keeps the band's list of the draft's images in step with the prompt box. */
589async function track ($: Engine, text: string) {
590  await follow($)
591  const ids = idsIn(text)
592  if (!sameIds(ids, draftIds)) {
593    draftIds = ids
594    await update($, draft, () => ids)
595    if (hasShown) await change($, closeChipCard)
596  }
597  for (const id of ids) void load($, id)
598}
599
600export const register: Register = (on, options) => {
601  on('session.start', async ($, e, next) => {
602    poll?.cancel()
603    poll = undefined
604    const result = await next(e)
605    const [program, term, kittyWindow, forceImages, sshConnection, sshTty] = await Promise.all([
606      $.env.get('TERM_PROGRAM'),
607      $.env.get('TERM'),
608      $.env.get('KITTY_WINDOW_ID'),
609      $.env.get('CLAUDE_CODE_FORCE_TERMINAL_IMAGES'),
610      $.env.get('SSH_CONNECTION'),
611      $.env.get('SSH_TTY')
612    ])
613    const isNative = program === 'ghostty' || term === 'xterm-ghostty' || term === 'xterm-kitty' || kittyWindow !== undefined
614    const isForced = /^(1|true|yes|on)$/i.test(forceImages ?? '')
615    graphics = isNative ? 'file' : isForced ? 'bytes' : 'none'
616    // Where the terminal cannot draw the picture itself, a card opens in a window of its own, by
617    // the pointer, on the desktop the session runs on: not over SSH, where that is another machine's.
618    windowing = undefined
619    const isRemote = sshConnection !== undefined || sshTty !== undefined
620    if (options.window !== false && graphics === 'none' && e.isInteractive && !isRemote) {
621      const system = await platformOf($)
622      if (system === 'macos' || system === 'windows') windowing = system
623      // Under WSL the window is Windows', by the pointer: not WSLg's, which cannot tell where it is.
624      if (await wslOf($)) windowing = 'windows'
625      else if (system === 'linux' && (await $.env.get('DISPLAY'))) {
626        const tk = await $.process.run(['python3', '-c', 'import tkinter']).catch(() => undefined)
627        if (tk?.exitCode === 0) windowing = 'linux'
628      }
629    }
630    // A reload starts with an empty cache: read again what is in the box.
631    await follow($)
632    const [initialDraft, initialShown, box] = await Promise.all([read($, draft), read($, shown), $.prompt.read()])
633    draftIds = initialDraft
634    hasShown = initialShown !== null
635    await track($, box.text)
636    if (e.isInteractive) {
637      // A look that fails (the module unloading under it) waits for the next.
638      poll = $.clock.every(POLL_MS, () => {
639        void $.prompt.read().then(box => track($, box.text)).then(() => settle($)).catch(() => undefined)
640      })
641    }
642    return result
643  })
644
645  on('session.end', ($, e, next) => {
646    poll?.cancel()
647    poll = undefined
648    closeWindow()
649    return next(e)
650  })
651
652  // Typing closes the card a chip opened: the box may grow and move the chip from under the pointer.
653  on('prompt.edit', async ($, e, next) => {
654    const box = await next(e)
655    if (hasShown) await change($, closeChipCard)
656    await track($, box.text)
657    return box
658  })
659
660  on('prompt.submit', async ($, e, next) => {
661    const result = await next(e)
662    if (e.origin.kind !== 'composer') return result
663    draftIds = []
664    await update($, draft, () => [])
665    await change($, () => null)
666    for (const id of idsIn(e.text)) void load($, id)
667    return result
668  }).catch(($, e, next) => next(e))
669
670  // A chip says when the pointer comes onto it (or moves on it) and when it leaves: its card opens
671  // and closes. Clicked, it opens the image in the system's viewer.
672  on('ui.message', async ($, e, next) => {
673    const result = await next(e)
674    const chip = /^(chip|sent|token):(\d+)$/.exec(e.element)
675    if (!chip) return result
676    const target = `${e.requestId}/${e.element}`
677    const data = e.data as { isOver?: unknown; isClicked?: unknown } | null
678    // Clicked, the image opens in its viewer, over which a card left open would stand.
679    if (data?.isClicked === true) {
680      void openImage($, Number(chip[2]))
681      await change($, () => null)
682      return result
683    }
684    const isOver = data?.isOver === true
685    await change($, now => (!isOver ? (now?.target === target ? null : now) : now?.target === target ? now : placeFor(Number(chip[2]), target)))
686    return result
687  })
688
689  // Every transcript row is noted in one hook, so rows drawn again together (as after a reload) are
690  // noted in the transcript's order. A sent prompt: under it the engine lists its images, `⎿ [Image #N]`
691  // a row; each gets a chip of its own over it, unpainted, so the engine's row shows as it was and
692  // the pointer on it opens the card.
693  on('ui.render', { component: ['UserMessage', 'AssistantMessage', 'ToolUse', 'ToolResult', 'ToolGroup', 'CommandOutput', 'TurnDuration', 'InfoNotice'] }, async ($, e, next) => {
694    if (e.surface !== 'terminal' || e.viewport?.isFullscreen === false) return next(e)
695    note(e)
696    if (e.component !== 'UserMessage') return float($, e, await next(e))
697    const ids = e.props.origin.kind === 'composer' ? idsIn(e.props.text) : []
698    if (ids.length === 0) return float($, e, await next(e))
699    const all = await read($, pictures)
700    for (const id of ids) if (!all[id]) void load($, id)
701    const drawn = await next(e)
702    // The engine lists the images it was sent, in the order pasted: each one stored, read or not.
703    const listed = ids.filter(id => { const picture = all[id]; return picture && !('error' in picture && picture.error === NOT_STORED) })
704    listed.sort((a, b) => a - b)
705    const { Box, Client } = $.ui.resolve(e)
706    const overlays: RenderElement[] = []
707    listed.forEach((id, i) => {
708      if ('error' in all[id]!) return
709      overlays.push(
710        <Box key={`listed-${id}`} position="absolute" bottom={-1 - i} left={LISTED_AT}>
711          <Client key={`sent:${id}`} module="./chip.tsx" width={`[Image #${id}]`.length} height={1} />
712        </Box>
713      )
714    })
715    // The rows the engine lists the images in take the pointer but not a click, which stays the
716    // engine's: a click opens an image from its `[Image #N]` in the prompt's first line, where the
717    // place of each can be told (before the line wraps, after text one cell a character).
718    const line = e.props.text.split('\n')[0]!
719    const width = (e.viewport?.columns ?? 80) - 1
720    const placed = new Set<number>()
721    for (const match of line.matchAll(TOKEN)) {
722      const id = Number(match[1])
723      const at = PROMPT_AT + match.index
724      const picture = all[id]
725      if (placed.has(id) || !picture || 'error' in picture || !/^[\u0020-\u02ff]*$/.test(line.slice(0, match.index)) || at + match[0].length > width) continue
726      placed.add(id)
727      overlays.push(
728        <Box key={`token-${id}`} position="absolute" top={1} left={at}>
729          <Client key={`token:${id}`} module="./chip.tsx" width={match[0].length} height={1} />
730        </Box>
731      )
732    }
733    return float($, e, drawn, overlays)
734  })
735
736  // Above the prompt: the draft's images, each a chip that opens its card while the pointer is on
737  // it. The card floats over the transcript; where too few of its rows show, the band holds it.
738  on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
739    // Off fullscreen the terminal reports no pointer: nothing could be hovered.
740    if (e.surface !== 'terminal' || e.props.hasSurvey || e.viewport?.isFullscreen === false) return next(e)
741    const [ids, now] = await Promise.all([read($, draft), read($, shown)])
742    const inBand = now && now.host === null && now.edge !== 'window' ? now.id : undefined
743    if (ids.length === 0 && inBand === undefined) return next(e)
744    if (e.viewport) screenRows = e.viewport.rows
745
746    const ui = $.ui.resolve(e)
747    const { Box, Client, Text } = ui
748    const below: RenderElement = await next(e)
749    const bounds = {
750      columns: Math.max(8, Math.min(MAX_COLUMNS, e.props.bodyColumns)),
751      rows: Math.max(4, Math.min(MAX_ROWS, e.props.maxRows - 1))
752    }
753    return (
754      <Box flexDirection="column">
755        {below}
756        {inBand !== undefined && card(ui, await read($, pictures), inBand, bounds)}
757        {ids.length > 0 && (
758          <Box flexDirection="row" columnGap={1}>
759            {ids.map(id => <Client key={`chip:${id}`} module="./chip.tsx" props={{ label: `[Image #${id}]` }} />)}
760            <Text dimColor>hover to preview · click to open</Text>
761          </Box>
762        )}
763      </Box>
764    )
765  })
766}
767
hooks/pixels.ts 284 lines
1/** A picture decoded to 8-bit RGBA, row-major from the top-left pixel. */
2export type Pixels = { width: number; height: number; data: Uint8Array }
3
4const UPPER_HALF = 0x2580 // ▀
5const LEFT_HALF = 0x258c // ▌
6const SPACE = 0x20
7const DEFAULT_COLOR = 0x01000000
8
9/** The lowest set bit of a channel mask, and how many bits wide the channel is. */
10function channelOf (mask: number) {
11  if (mask === 0) return null
12  let shift = 0
13  while (((mask >>> shift) & 1) === 0) shift++
14  let bits = 0
15  while (((mask >>> (shift + bits)) & 1) === 1) bits++
16  return { shift, max: 2 ** bits - 1 }
17}
18
19/** Reads an uncompressed 24- or 32-bit BMP, the kind `sips` and ImageMagick write. */
20export function parseBmp (bytes: Uint8Array): Pixels {
21  if (bytes.length < 54 || bytes[0] !== 0x42 || bytes[1] !== 0x4d) throw new Error('not a BMP file')
22  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
23  const offset = view.getUint32(10, true)
24  const headerSize = view.getUint32(14, true)
25  const width = view.getInt32(18, true)
26  const signedHeight = view.getInt32(22, true)
27  const bits = view.getUint16(28, true)
28  const compression = view.getUint32(30, true)
29  const height = Math.abs(signedHeight)
30
31  if (width <= 0 || height === 0) throw new Error('an empty BMP')
32  if (bits !== 24 && bits !== 32) throw new Error(`a ${bits}-bit BMP`)
33  if (compression !== 0 && compression !== 3) throw new Error('a compressed BMP')
34
35  const stride = Math.ceil((width * bits) / 32) * 4
36  if (offset + stride * height > bytes.length) throw new Error('a truncated BMP')
37
38  // BI_BITFIELDS keeps its masks right after the 40-byte header, or inside a longer one.
39  const masks = compression === 3
40    ? [54, 58, 62, 66].map(at => (at === 66 && headerSize < 56) || at + 4 > bytes.length ? 0 : view.getUint32(at, true))
41    : bits === 32 ? [0xff0000, 0xff00, 0xff, 0xff000000] : null
42  const channels = masks?.map(channelOf)
43
44  const data = new Uint8Array(width * height * 4)
45  let hasAlpha = false
46  for (let y = 0; y < height; y++) {
47    const start = offset + (signedHeight < 0 ? y : height - 1 - y) * stride
48    for (let x = 0; x < width; x++) {
49      const out = (y * width + x) * 4
50      if (channels) {
51        const value = view.getUint32(start + x * 4, true)
52        for (let c = 0; c < 4; c++) {
53          const channel = channels[c]
54          data[out + c] = channel ? Math.round((((value >>> channel.shift) & channel.max) * 255) / channel.max) : 255
55        }
56        if (data[out + 3] !== 0) hasAlpha = true
57      } else {
58        const at = start + x * 3
59        data[out] = bytes[at + 2]!
60        data[out + 1] = bytes[at + 1]!
61        data[out + 2] = bytes[at]!
62        data[out + 3] = 255
63      }
64    }
65  }
66  // A writer that leaves the alpha byte unused fills it with zeros: the picture is opaque.
67  if (channels && !hasAlpha) for (let i = 3; i < data.length; i += 4) data[i] = 255
68
69  return { width, height, data }
70}
71
72/**
73 * The largest box of cells within the bounds that keeps the picture's shape. A cell is about
74 * twice as tall as it is wide, and holds two rows of pixels.
75 */
76export function fit (width: number, height: number, maxColumns: number, maxRows: number) {
77  const columnsCap = Math.max(1, Math.min(maxColumns, width))
78  let columns = columnsCap
79  let rows = Math.max(1, Math.round((columns * height) / width / 2))
80  if (rows > maxRows) {
81    rows = Math.max(1, maxRows)
82    columns = Math.max(1, Math.min(columnsCap, Math.round((rows * 2 * width) / height)))
83  }
84  return { columns, rows }
85}
86
87// Light adds up in linear terms, not in the gamma-encoded bytes: averaging the bytes darkens a
88// light line on a dark ground until it is gone.
89const LINEAR = Float64Array.from({ length: 256 }, (_, i) => {
90  const value = i / 255
91  return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4
92})
93const ENCODED = Uint8Array.from({ length: 4096 }, (_, i) => {
94  const value = i / 4095
95  return Math.round(255 * (value <= 0.0031308 ? value * 12.92 : 1.055 * value ** (1 / 2.4) - 0.055))
96})
97const encode = (linear: number) => ENCODED[Math.min(4095, Math.max(0, Math.round(linear * 4095)))]!
98
99// Each cell is sampled as a grid of SIDE by SIDE points, each the average of the pixels it covers.
100const SIDE = 8
101const HALF = SIDE / 2
102const POINTS = SIDE * SIDE
103
104/** A block element, and which of a cell's points its foreground covers. */
105type Shape = { glyph: number; inside: Uint8Array; count: number }
106
107const shape = (glyph: number, covers: (x: number, y: number) => boolean): Shape => {
108  const inside = Uint8Array.from({ length: POINTS }, (_, i) => (covers(i % SIDE, Math.floor(i / SIDE)) ? 1 : 0))
109  return { glyph, inside, count: inside.reduce((sum, bit) => sum + bit, 0) }
110}
111
112// The ways a cell can be split in two colors: the halves, the eighths from the bottom and from the
113// left (each also the rest from the top or right, the colors swapped), the quarters and the
114// diagonals. The halves first, so a cell they draw as well as another is drawn with them.
115const SHAPES: Shape[] = [
116  shape(UPPER_HALF, (_, y) => y < HALF),
117  shape(LEFT_HALF, x => x < HALF),
118  ...[1, 2, 3, 5, 6, 7].map(k => shape(0x2580 + k, (_, y) => y >= SIDE - k)), // ▁▂▃▅▆▇
119  ...[1, 2, 3, 5, 6, 7].map(k => shape(0x2590 - k, x => x < k)), // ▏▎▍▋▊▉
120  shape(0x2598, (x, y) => x < HALF && y < HALF), // ▘
121  shape(0x259d, (x, y) => x >= HALF && y < HALF), // ▝
122  shape(0x2596, (x, y) => x < HALF && y >= HALF), // ▖
123  shape(0x2597, (x, y) => x >= HALF && y >= HALF), // ▗
124  shape(0x259a, (x, y) => (x < HALF) === (y < HALF)), // ▚
125  shape(0x259e, (x, y) => (x < HALF) !== (y < HALF)) // ▞
126]
127
128/** One cell's points: gamma-encoded, to choose a shape by; linear, to mix its colors in; and which show. */
129type Points = { encoded: Float64Array; linear: Float64Array; shows: Uint8Array; shown: number }
130
131const SCRATCH_POINTS: Points = {
132  encoded: new Float64Array(POINTS * 3),
133  linear: new Float64Array(POINTS * 3),
134  shows: new Uint8Array(POINTS),
135  shown: 0
136}
137
138/** Averages, in linear light, the pixels under each of the points of the cell at `column`, `row`. */
139function pointsOf ({ width, height, data }: Pixels, columns: number, rows: number, column: number, row: number, points: Points) {
140  const across = columns * SIDE
141  const down = rows * SIDE
142  const { encoded, linear, shows } = points
143  points.shown = 0
144  for (let py = 0; py < SIDE; py++) {
145    const y0 = Math.floor(((row * SIDE + py) * height) / down)
146    const y1 = Math.max(y0 + 1, Math.floor(((row * SIDE + py + 1) * height) / down))
147    for (let px = 0; px < SIDE; px++) {
148      const x0 = Math.floor(((column * SIDE + px) * width) / across)
149      const x1 = Math.max(x0 + 1, Math.floor(((column * SIDE + px + 1) * width) / across))
150      let r = 0
151      let g = 0
152      let b = 0
153      let alpha = 0
154      for (let y = y0; y < y1; y++) {
155        let at = (y * width + x0) * 4
156        for (let x = x0; x < x1; x++) {
157          const a = data[at + 3]!
158          r += LINEAR[data[at]!]! * a
159          g += LINEAR[data[at + 1]!]! * a
160          b += LINEAR[data[at + 2]!]! * a
161          alpha += a
162          at += 4
163        }
164      }
165      const i = py * SIDE + px
166      const isShown = alpha / ((y1 - y0) * (x1 - x0)) >= 128
167      shows[i] = isShown ? 1 : 0
168      if (!isShown) continue
169      points.shown++
170      const out = i * 3
171      const linR = r / alpha
172      const linG = g / alpha
173      const linB = b / alpha
174      linear[out] = linR
175      linear[out + 1] = linG
176      linear[out + 2] = linB
177      encoded[out] = encode(linR)
178      encoded[out + 1] = encode(linG)
179      encoded[out + 2] = encode(linB)
180    }
181  }
182}
183
184/**
185 * The shape that splits the points in the two groups closest to their own averages (least squared
186 * error, measured on the encoded values, which is closer to what the eye sees); none where one
187 * color is as close. Part see-through, the shape that best parts the points that show from the rest.
188 */
189function shapeOf ({ encoded, shows, shown }: Points) {
190  let best: Shape | undefined
191  if (shown < POINTS) {
192    let bestMatches = -1
193    for (const candidate of SHAPES) {
194      let matches = 0
195      for (let i = 0; i < POINTS; i++) matches += candidate.inside[i] === shows[i] ? 1 : 0
196      if (matches > bestMatches) {
197        bestMatches = matches
198        best = candidate
199      }
200    }
201    return best
202  }
203  // Each split's error is the total of the squares less what its two groups' averages explain: the
204  // largest |sum|² / count over the two groups is the least error.
205  let total0 = 0
206  let total1 = 0
207  let total2 = 0
208  for (let i = 0; i < POINTS * 3; i += 3) {
209    total0 += encoded[i]!
210    total1 += encoded[i + 1]!
211    total2 += encoded[i + 2]!
212  }
213  let bestScore = (total0 ** 2 + total1 ** 2 + total2 ** 2) / POINTS
214  for (const candidate of SHAPES) {
215    let sum0 = 0
216    let sum1 = 0
217    let sum2 = 0
218    const { inside, count } = candidate
219    for (let i = 0; i < POINTS; i++) {
220      if (inside[i] === 0) continue
221      const at = i * 3
222      sum0 += encoded[at]!
223      sum1 += encoded[at + 1]!
224      sum2 += encoded[at + 2]!
225    }
226    const out = POINTS - count
227    const score =
228      sum0 ** 2 / count + (total0 - sum0) ** 2 / out +
229      sum1 ** 2 / count + (total1 - sum1) ** 2 / out +
230      sum2 ** 2 / count + (total2 - sum2) ** 2 / out
231    if (score > bestScore + 1e-6) {
232      bestScore = score
233      best = candidate
234    }
235  }
236  return best
237}
238
239/** The color of the points that show on one side of a shape (all of them with none), mixed in linear light. */
240function colorOf ({ linear, shows }: Points, shape: Shape | undefined, side: number) {
241  let sum0 = 0
242  let sum1 = 0
243  let sum2 = 0
244  let count = 0
245  for (let i = 0; i < POINTS; i++) {
246    if (shows[i] === 0 || (shape ? shape.inside[i] : 1) !== side) continue
247    const at = i * 3
248    sum0 += linear[at]!
249    sum1 += linear[at + 1]!
250    sum2 += linear[at + 2]!
251    count++
252  }
253  return count === 0 ? DEFAULT_COLOR : (encode(sum0 / count) << 16) | (encode(sum1 / count) << 8) | encode(sum2 / count)
254}
255
256/**
257 * The picture as a `Raster`'s cells: `columns × rows` block elements, each cell sampled at eight
258 * by eight points and drawn with the half, eighth or quarter block that splits them best, in their
259 * two colors; the terminal's own color where the picture is see-through.
260 */
261export function toCells (pixels: Pixels, columns: number, rows: number): string {
262  const words = new Uint32Array(columns * rows * 3)
263  const points = SCRATCH_POINTS
264  for (let row = 0; row < rows; row++) {
265    for (let column = 0; column < columns; column++) {
266      pointsOf(pixels, columns, rows, column, row, points)
267      const at = (row * columns + column) * 3
268      if (points.shown === 0) {
269        words[at] = SPACE
270        words[at + 1] = DEFAULT_COLOR
271        words[at + 2] = DEFAULT_COLOR
272        continue
273      }
274      const best = shapeOf(points)
275      const inside = colorOf(points, best, 1)
276      const outside = !best ? inside : points.shown < POINTS ? DEFAULT_COLOR : colorOf(points, best, 0)
277      words[at] = best?.glyph ?? UPPER_HALF
278      words[at + 1] = inside
279      words[at + 2] = outside
280    }
281  }
282  return new Uint8Array(words.buffer).toBase64()
283}
284
hooks/window.ts 256 lines
1// The programs that show a picture in a window of its own, by the pointer, outside the terminal:
2// one a system, each run with what it needs in the environment (IMAGE_PREVIEW_FILE, the picture;
3// IMAGE_PREVIEW_SIZE, its longest side at most; IMAGE_PREVIEW_LIFE, how many seconds at most it
4// stays). Each writes `shown` once the window is up, or a line starting `error`, then a `.` every
5// tenth of a second, so the one reading it is woken to end it, and it ends when nobody reads. The
6// window never takes the focus or the pointer: the terminal keeps both.
7
8/** macOS: JavaScript for Automation, run by `osascript`, drawing an AppKit panel. */
9export const MACOS = String.raw`
10ObjC.import('Cocoa')
11
12function run () {
13  const env = $.NSProcessInfo.processInfo.environment
14  const read = key => { const value = env.objectForKey(key); return value.isNil() ? '' : ObjC.unwrap(value) }
15  const say = text => $.NSFileHandle.fileHandleWithStandardOutput.writeData($(text + '\n').dataUsingEncoding($.NSUTF8StringEncoding))
16  const largest = Number(read('IMAGE_PREVIEW_SIZE')) || 800
17  // An accessory app: no Dock icon, never the active one.
18  $.NSApplication.sharedApplication.setActivationPolicy(1)
19  const image = $.NSImage.alloc.initWithContentsOfFile(read('IMAGE_PREVIEW_FILE'))
20  if (image.isNil() || image.representations.count === 0) { say('error: cannot read the image'); return }
21  const pixels = image.representations.firstObject
22  const mouse = $.NSEvent.mouseLocation
23  const screens = $.NSScreen.screens
24  let screen = $.NSScreen.mainScreen
25  for (let i = 0; i < screens.count; i++) {
26    const frame = screens.objectAtIndex(i).frame
27    if (mouse.x >= frame.origin.x && mouse.x < frame.origin.x + frame.size.width && mouse.y >= frame.origin.y && mouse.y < frame.origin.y + frame.size.height) screen = screens.objectAtIndex(i)
28  }
29  const area = screen.visibleFrame
30  let width = pixels.pixelsWide / screen.backingScaleFactor
31  let height = pixels.pixelsHigh / screen.backingScaleFactor
32  const fit = Math.min(1, largest / Math.max(width, height), (area.size.width * 0.7) / width, (area.size.height * 0.7) / height)
33  width = Math.round(width * fit)
34  height = Math.round(height * fit)
35  const pad = 6
36  const gap = 18
37  const outer = { width: width + pad * 2, height: height + pad * 2 }
38  const top = area.origin.y + area.size.height
39  const right = area.origin.x + area.size.width
40  // Above the pointer and to its right; under it, or to its left, where there is no room: never
41  // over the pointer, which stays on the terminal.
42  let x = mouse.x + gap
43  let y = mouse.y + gap
44  if (y + outer.height > top) y = mouse.y - gap - outer.height
45  if (x + outer.width > right) x = mouse.x - gap - outer.width
46  x = Math.max(area.origin.x, Math.min(x, right - outer.width))
47  y = Math.max(area.origin.y, Math.min(y, top - outer.height))
48  // Borderless and non-activating; over every app window, on every space, full-screen ones too.
49  const panel = $.NSPanel.alloc.initWithContentRectStyleMaskBackingDefer($.NSMakeRect(x, y, outer.width, outer.height), 128, 2, false)
50  panel.level = 25
51  panel.ignoresMouseEvents = true
52  panel.hasShadow = true
53  panel.opaque = false
54  panel.backgroundColor = $.NSColor.clearColor
55  panel.collectionBehavior = 1 | 16 | 256
56  const backdrop = $.NSVisualEffectView.alloc.initWithFrame($.NSMakeRect(0, 0, outer.width, outer.height))
57  backdrop.material = 13
58  backdrop.state = 1
59  backdrop.wantsLayer = true
60  backdrop.layer.cornerRadius = 10
61  backdrop.layer.masksToBounds = true
62  const view = $.NSImageView.alloc.initWithFrame($.NSMakeRect(pad, pad, width, height))
63  view.image = image
64  view.imageScaling = 3
65  backdrop.addSubview(view)
66  panel.contentView = backdrop
67  panel.orderFrontRegardless
68  say('shown')
69  const end = Date.now() + (Number(read('IMAGE_PREVIEW_LIFE')) || 120) * 1000
70  while (Date.now() < end) {
71    $.NSRunLoop.currentRunLoop.runUntilDate($.NSDate.dateWithTimeIntervalSinceNow(0.1))
72    say('.')
73  }
74}
75`
76
77/** Linux and other X11 desktops: Python's Tk, an undecorated tooltip window. The picture is a PNG. */
78export const LINUX = String.raw`
79import math, os, sys
80try:
81    import tkinter as tk
82except ImportError:
83    print('error: no tkinter', flush=True)
84    sys.exit(1)
85
86largest = int(os.environ.get('IMAGE_PREVIEW_SIZE') or 800)
87life = float(os.environ.get('IMAGE_PREVIEW_LIFE') or 120)
88try:
89    root = tk.Tk()
90except tk.TclError as error:
91    print('error: no display: %s' % error, flush=True)
92    sys.exit(1)
93root.withdraw()
94root.overrideredirect(True)
95for name, value in (('-topmost', True), ('-type', 'tooltip')):
96    try:
97        root.attributes(name, value)
98    except tk.TclError:
99        pass
100try:
101    image = tk.PhotoImage(file=os.environ.get('IMAGE_PREVIEW_FILE', ''))
102except tk.TclError:
103    print('error: cannot read the image', flush=True)
104    sys.exit(1)
105screen_width, screen_height = root.winfo_screenwidth(), root.winfo_screenheight()
106step = max(1, math.ceil(max(image.width() / min(largest, screen_width * 0.7), image.height() / min(largest, screen_height * 0.7))))
107if step > 1:
108    image = image.subsample(step)
109pad, gap = 6, 18
110width, height = image.width() + pad * 2, image.height() + pad * 2
111root.configure(background='#202020')
112tk.Label(root, image=image, borderwidth=0, highlightthickness=0, background='#202020').pack(padx=pad, pady=pad)
113mouse_x, mouse_y = root.winfo_pointerxy()
114# Above the pointer and to its right; under it, or to its left, where there is no room: never over
115# the pointer, which stays on the terminal.
116x, y = mouse_x + gap, mouse_y - gap - height
117if y < 0:
118    y = mouse_y + gap
119if x + width > screen_width:
120    x = mouse_x - gap - width
121x = max(0, min(x, screen_width - width))
122y = max(0, min(y, screen_height - height))
123root.geometry('%dx%d+%d+%d' % (width, height, x, y))
124root.deiconify()
125root.update_idletasks()
126print('shown', flush=True)
127
128
129def beat():
130    try:
131        print('.', flush=True)
132    except (BrokenPipeError, OSError):
133        # Nobody reads: end, the pipe's last flush going nowhere.
134        os.dup2(os.open(os.devnull, os.O_WRONLY), sys.stdout.fileno())
135        root.destroy()
136        return
137    root.after(100, beat)
138
139
140beat()
141root.after(int(life * 1000), root.destroy)
142root.mainloop()
143`
144
145/** Windows: PowerShell and Windows Forms, a tool window that is never activated. */
146export const WINDOWS = String.raw`
147$ErrorActionPreference = 'Stop'
148Add-Type -AssemblyName System.Windows.Forms, System.Drawing
149$largest = if ($env:IMAGE_PREVIEW_SIZE) { [int]$env:IMAGE_PREVIEW_SIZE } else { 800 }
150$life = if ($env:IMAGE_PREVIEW_LIFE) { [int]$env:IMAGE_PREVIEW_LIFE } else { 120 }
151# A form that shows without taking the focus, as a tool window, topmost, the pointer passing through.
152# Compiled once into the temp folder, then loaded from there: compiling takes a second.
153$assembly = Join-Path $env:TEMP 'image-preview-popup-1.dll'
154if (-not (Test-Path $assembly)) {
155  try {
156  Add-Type -OutputAssembly $assembly -OutputType Library -ReferencedAssemblies System.Windows.Forms, System.Drawing -TypeDefinition @'
157public class ImagePreviewPopup : System.Windows.Forms.Form {
158  protected override bool ShowWithoutActivation { get { return true; } }
159  protected override System.Windows.Forms.CreateParams CreateParams {
160    get {
161      System.Windows.Forms.CreateParams p = base.CreateParams;
162      p.ExStyle |= 0x08000000 | 0x00000080 | 0x00000008 | 0x00000020;
163      return p;
164    }
165  }
166}
167'@
168  } catch {
169    # Another preview compiling it at the same time: its file is used.
170  }
171}
172Add-Type -Path $assembly
173try { $image = [System.Drawing.Image]::FromFile($env:IMAGE_PREVIEW_FILE) } catch { [Console]::Out.WriteLine('error: cannot read the image'); exit 1 }
174$cursor = [System.Windows.Forms.Cursor]::Position
175$area = [System.Windows.Forms.Screen]::FromPoint($cursor).WorkingArea
176$fit = [Math]::Min(1.0, [Math]::Min($largest / [Math]::Max($image.Width, $image.Height), [Math]::Min($area.Width * 0.7 / $image.Width, $area.Height * 0.7 / $image.Height)))
177$pad = 6
178$gap = 18
179$width = [int]($image.Width * $fit) + $pad * 2
180$height = [int]($image.Height * $fit) + $pad * 2
181# Above the pointer and to its right; under it, or to its left, where there is no room: never over
182# the pointer, which stays on the terminal.
183$x = $cursor.X + $gap
184$y = $cursor.Y - $gap - $height
185if ($y -lt $area.Top) { $y = $cursor.Y + $gap }
186if ($x + $width -gt $area.Right) { $x = $cursor.X - $gap - $width }
187$x = [Math]::Max($area.Left, [Math]::Min($x, $area.Right - $width))
188$y = [Math]::Max($area.Top, [Math]::Min($y, $area.Bottom - $height))
189$form = New-Object ImagePreviewPopup
190$form.FormBorderStyle = 'None'
191$form.ShowInTaskbar = $false
192$form.TopMost = $true
193$form.StartPosition = 'Manual'
194$form.Bounds = New-Object System.Drawing.Rectangle($x, $y, $width, $height)
195$form.BackColor = [System.Drawing.Color]::FromArgb(32, 32, 32)
196$form.Padding = New-Object System.Windows.Forms.Padding($pad)
197$form.Opacity = 0.99
198$box = New-Object System.Windows.Forms.PictureBox
199$box.Dock = 'Fill'
200$box.SizeMode = 'Zoom'
201$box.Image = $image
202$form.Controls.Add($box)
203$form.add_Shown({ [Console]::Out.WriteLine('shown'); [Console]::Out.Flush() })
204$timer = New-Object System.Windows.Forms.Timer
205$timer.Interval = $life * 1000
206$timer.add_Tick({ $form.Close() })
207$timer.Start()
208$beat = New-Object System.Windows.Forms.Timer
209$beat.Interval = 100
210$beat.add_Tick({ try { [Console]::Out.WriteLine('.'); [Console]::Out.Flush() } catch { $form.Close() } })
211$beat.Start()
212[System.Windows.Forms.Application]::Run($form)
213`
214
215/**
216 * Windows: PowerShell and System.Drawing, the converter where ImageMagick is not installed. Writes
217 * IMAGE_PREVIEW_FILE shrunk to IMAGE_PREVIEW_SIZE at most, as IMAGE_PREVIEW_FORMAT (`bmp` or `png`),
218 * to IMAGE_PREVIEW_OUT.
219 */
220export const WINDOWS_CONVERT = String.raw`
221$ErrorActionPreference = 'Stop'
222Add-Type -AssemblyName System.Drawing
223$source = [System.Drawing.Image]::FromFile($env:IMAGE_PREVIEW_FILE)
224$largest = if ($env:IMAGE_PREVIEW_SIZE) { [int]$env:IMAGE_PREVIEW_SIZE } else { [Math]::Max($source.Width, $source.Height) }
225$fit = [Math]::Min(1.0, $largest / [Math]::Max($source.Width, $source.Height))
226$width = [Math]::Max(1, [int]($source.Width * $fit))
227$height = [Math]::Max(1, [int]($source.Height * $fit))
228$picture = New-Object System.Drawing.Bitmap($width, $height, [System.Drawing.Imaging.PixelFormat]::Format32bppArgb)
229$canvas = [System.Drawing.Graphics]::FromImage($picture)
230$canvas.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
231$canvas.DrawImage($source, 0, 0, $width, $height)
232$canvas.Dispose()
233$format = if ($env:IMAGE_PREVIEW_FORMAT -eq 'png') { [System.Drawing.Imaging.ImageFormat]::Png } else { [System.Drawing.Imaging.ImageFormat]::Bmp }
234$picture.Save($env:IMAGE_PREVIEW_OUT, $format)
235`
236
237const encodedCache = new Map<string, string>()
238
239/** A PowerShell script as `-EncodedCommand` takes it: UTF-16LE, base64. */
240export function encodedCommand (script: string) {
241  const cached = encodedCache.get(script)
242  if (cached !== undefined) return cached
243  const bytes = new Uint8Array(script.length * 2)
244  for (let i = 0; i < script.length; i++) {
245    const code = script.charCodeAt(i)
246    bytes[i * 2] = code & 0xff
247    bytes[i * 2 + 1] = code >> 8
248  }
249  const encoded = bytes.toBase64()
250  encodedCache.set(script, encoded)
251  return encoded
252}
253
254/** How PowerShell runs one of these scripts: no profile, no prompts, no script policy in the way. */
255export const powershell = (script: string) => ['powershell', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand', encodedCommand(script)]
256
hooks/chip.tsx 45 lines
1// The surface module of the pointer targets: a chip that says when the pointer is on it, so the
2// hooks module shows that image's card, and hides it when the pointer leaves; and when it is
3// clicked, so the hooks module opens the image.
4import type { ClientModule } from 'claude-code'
5
6/** `label`, the chip's text; none (or no props), an unpainted region over a row the engine draws. */
7type Props = { label?: string } | null
8
9const REPEAT_MS = 50
10
11const Chip: ClientModule<Props, { isOver: boolean; isPressed?: boolean }> = (props, surface) => {
12  const label = props?.label
13  surface.onPointer(event => {
14    // A click is the left button let go over the chip it went down on.
15    if (event.type === 'down') {
16      if (event.button === 'left') surface.setState({ isOver: true, isPressed: true })
17      return
18    }
19    if (event.type === 'up') {
20      // A region not laid out yet (0 by 0) says nothing of where the pointer is.
21      const isInside = surface.columns === 0 || (event.x >= 0 && event.y >= 0 && event.x < surface.columns && event.y < surface.rows)
22      if (surface.state?.isPressed && isInside) surface.post({ isOver: true, isClicked: true })
23      surface.setState({ isOver: isInside })
24      return
25    }
26    // A move over the chip says so again: a card hidden while the pointer stayed comes back.
27    const isOver = event.type !== 'leave'
28    surface.post({ isOver })
29    if (isOver !== (surface.state?.isOver ?? false)) surface.setState({ isOver, isPressed: surface.state?.isPressed })
30    // Going straight from one chip to the next, the leave and the enter post in one frame, and the
31    // later replaces the other: the enter says so again a frame on.
32    if (event.type === 'enter') {
33      const stop = surface.every(REPEAT_MS, () => {
34        stop()
35        if (surface.state?.isOver) surface.post({ isOver: true })
36      })
37    }
38  })
39  const { Box, Text } = surface.elements
40  if (label === undefined) return <Box />
41  return <Text color="claude" inverse={surface.state?.isOver === true}>{label}</Text>
42}
43
44export default Chip
45
types/index.d.ts 28 lines
1/** A pasted image as the mod read it: its file, format, size and pixel shape, or why it has no preview. */
2export type Picture =
3  | { file: string; format: string; bytes: number; width: number; height: number; png?: string }
4  | { error: string }
5
6/**
7 * The card on show: its image; the pointer target that showed it (`<site>/<chip key>`); the
8 * transcript row it floats on, by `requestId` (null: the band holds it, or a window); how many rows
9 * it may take; and whether it stands on that row's last row on screen, ends just above its first,
10 * or is a window of its own, outside the terminal.
11 */
12export type Shown = { id: number; target: string; host: string | null; rows: number; edge: 'last' | 'first' | 'window' }
13
14declare module 'claude-code' {
15  interface PluginState {
16    'image-preview': {
17      /** The `[Image #N]` ids in the prompt box, in the order typed. */
18      draft: number[]
19      /** Each image read so far, by id. */
20      pictures: Record<string, Picture>
21      /** The card the pointer has open, and where it floats; null while none is. */
22      shown: Shown | null
23      /** Whether a transcript row, by its `requestId`, holds the card: so only the rows it leaves and joins draw again. */
24      hosting: StateFamily<boolean>
25    }
26  }
27}
28