SLOPSHOPPER

monocle

Colorful Claude Code replies, with mermaid diagrams and images drawn as real pixels in kitty.

spinnerrowscommandtoastpromptprocess
★ 2v0.19.0MITupdated 2026-10-19DimitrisMilonopoulos/monocle
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · monocle
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ fix the failing auth test and add an audit log call │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ⏺ Read(src/auth.ts) ⎿ Read 6 lines Edited src/auth.ts ● ⏺ Bash(bun test) ⎿ 3 pass, 1 fail Done. I made refresh reject expired claims and added an audit call. ✻ Worked for 42s › /monocle Commands ━━━━━━━━ [ ⧉ md ] [ ⧉ art ] ┌───────────────────────┬───────────────────────────────────────────────────────────────────────────────────────────── ────────────────────┐ │ Command │ Does │ ╞═══════════════════════╪═════════════════════════════════════════════════════════════════════════════════════════════ ════════════════════╡ │ /monocle theme <name> │ Switch theme on the spot (/monocle <name> works too), random for a new dark one now and each time the mod loads │ ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Turn line
✻ Worked for 42s
Your message
╭──────────────────────────────────────────────────────────────────────────────────────────────────╮ │ fix the failing auth test and add an audit log call │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
Claude's reply
Done. I made refresh reject expired claims and added an audit call.
Tool row
Edited src/auth.ts ●
Command output
Commands ━━━━━━━━ [ ⧉ md ] [ ⧉ art ] ┌───────────────────────┬─────────────────────────────────────────────────────────────────────────── ──────────────────────────────────────┐ │ Command │ Does ╞═══════════════════════╪═══════════════════════════════════════════════════════════════════════════ ══════════════════════════════════════╡ │ /monocle theme <name> │ Switch theme on the spot (/monocle <name> works too), random for a new dar ├───────────────────────┼─────────────────────────────────────────────────────────────────────────── ──────────────────────────────────────┤ │ /monocle copy │ Copy the last reply, or copy code for its last code block ├───────────────────────┼─────────────────────────────────────────────────────────────────────────── ──────────────────────────────────────┤ │ /monocle doctor │ Check what images, diagrams and big headings need on this machine ├───────────────────────┼─────────────────────────────────────────────────────────────────────────── ──────────────────────────────────────┤ │ /monocle demo │ Full showcase, every element and diagram ├───────────────────────┼─────────────────────────────────────────────────────────────────────────── ──────────────────────────────────────┤ │ /monocle demo-rtl │ Hebrew right-to-left showcase └───────────────────────┴─────────────────────────────────────────────────────────────────────────── ──────────────────────────────────────┘ 17 themes [ ⧉ copy ] • Dark: catppuccin-mocha, dracula, nord, tokyo-night, gruvbox-dark, rose-pine, everforest, github-dark, one-dark, solarized-dark • Light: catppuccin-latte, gruvbox-light, rose-pine-dawn, github-light, solarized-light • Plain: mono, no color, only bold and dim • Terminal: ansi, your terminal's own colors, following its theme as it changes Docs and issues: https://github.com/DimitrisMilonopoulos/monocle ╭──────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Tip │ │ Any color slot beats the theme. Set headingColor or numberColor to a hex value in /config. │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────╯ Task lists [ ⧉ copy ] [✓] Tables, diagrams and charts drawn in the terminal [✓] Copy tables as Markdown, art or HTML (macOS/Linux) [ ] Pick a taskStyle in /config: checks, ticks, box or progress [ ⧉ source ] [ ⧉ art ] ┌───────┐ ┌───────┐ ┌──────┐ ┌────────┐ │ Reply ├──►│ Parse ├──►│ Draw ├──►│ Screen │ └───────┘ └───────┘ └──────┘ └────────┘ ╭──────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Caution │ │ Claude Code refuses trees over 20000 nodes. A code block past about 500 highlighted lines falls │ │ back to plain text. │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
README

<h1 align="center"><img src="docs/brand/banner.png" alt="monocle" width="640"></h1>

<a href="https://github.com/DimitrisMilonopoulos/monocle/releases"><img src="https://img.shields.io/github/v/release/DimitrisMilonopoulos/monocle?style=flat-square&color=cba6f7" alt="Release"></a> <a href="https://github.com/DimitrisMilonopoulos/monocle/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/DimitrisMilonopoulos/monocle/ci.yml?branch=main&style=flat-square&label=ci" alt="CI"></a> <a href="LICENSE"><img src="https://img.shields.io/github/license/DimitrisMilonopoulos/monocle?style=flat-square&color=cba6f7" alt="License"></a> <img src="https://img.shields.io/badge/Claude%20Code-2.1.287%2B-cba6f7?style=flat-square" alt="Claude Code 2.1.287 or later">

<a href="#install"><strong>Install</strong></a> · <a href="#images-in-kitty-and-ghostty">Images</a> · <a href="#configure">Configure</a> · <a href="docs/REFERENCE.md">Reference</a> · <a href="docs/CHANGELOG.md">Changelog</a>

A reply drawn by monocle in kitty: a big heading, a colored table and a mermaid flowchart drawn as an image

What it does

  • Claude Code prints mermaid as source, tables as pipes and tool calls as walls of text. monocle draws them.
  • In kitty and Ghostty you get real pixels: every mermaid diagram type as an image, big # and ## headings in your terminal's font, the images you paste under your prompt, and optional LaTeX math.
  • Everywhere else, colored text art: flowcharts, sequence diagrams and charts, colored tables and code, and quiet one-line tool rows.
  • 15 palettes, mono, and ansi, which follows your terminal's theme as you switch it.
  • No network calls, nothing installed for you. Turn it off in /plugin and Claude Code's own renderer comes back.

Install

Needs Claude Code 2.1.287 or later. In Claude Code, run:

/plugin marketplace add DimitrisMilonopoulos/monocle
/plugin install monocle@monocle

Then /monocle demo shows the main features. To update, run this and then /reload in every open session:

claude plugin marketplace update monocle && claude plugin update monocle@monocle

To uninstall:

claude plugin uninstall monocle@monocle

What each feature needs

monocle installs nothing. It uses a program only if it finds it on your PATH, and a feature whose program is missing falls back to text. Run /monocle doctor to see what's ready on your machine; docs/SETUP.md has the install commands for macOS (Homebrew) and Linux.

FeatureTerminalNeeds
Colors, tables, code, text-art diagrams, tool rowsanynothing
Mermaid diagrams as imageskitty or Ghosttymmdc (mermaid-cli) and Chrome or Chromium
Big # and ## headingskitty or GhosttyImageMagick and fc-match (fontconfig)
Pasted images under the promptkitty or Ghosttynothing for PNG; ImageMagick for JPEG, GIF and WebP
Click a diagram or image to zoomkittykitty remote control on
ansi colors in diagrams and headingskitty or Ghosttykitty: remote control on
LaTeX math as imageskitty or GhosttyRaTeX's ratex-render, and latex set to true
⧉ html table copylocal macOS, or Linux desktopLinux: CopyQ running

Images never show inside tmux, which doesn't pass them through.

Images in kitty and Ghostty

Mermaid diagrams drawn as images in kitty: a sequence diagram, a pie chart and a bar and line chart

  • Diagrams: every mermaid type renders with mmdc in the theme's colors, including pie, gantt, mindmap, timeline and git graphs. A diagram shows as text art first and turns into an image about two seconds later.
  • Zoom: in kitty, click a diagram or press ⤢ zoom to open it full screen, rendered at twice the resolution. Enter closes it.
  • Big headings: # and ## draw three and two rows tall in your terminal's bold font.
  • Pasted images show under the prompt that carried them instead of only [Image #1].

A pasted image shown under the prompt that carried it

Everything else it draws

Tablescolored header and rules, alignment, colored numbers, columns that shrink to fit
Codea language header and copy button, highlighting for two dozen languages, colored shell commands
Text-art diagramsflowcharts, sequence, state, class and ER diagrams, bar and line charts, in any terminal
Textbold, italic, links, numbers, versions, durations and paths in their own colors
Headings, lists, quotesfour heading styles, nested and task lists, GitHub alerts as colored boxes
Tool rowsRan npm test, Read src/app.ts, groups summed up in one line; toolOutput: quiet hides what Claude only read
Your promptsa bubble, bar or chevron so you find your turns, and an optional clock
Copy buttons⧉ md, ⧉ art and ⧉ html on tables, ⧉ copy on code, lists, quotes, diagrams and whole replies
Right to leftHebrew and Arabic read right to left, with bullets, quotes and tables mirrored
Slash commands/cost, /context and plugin output drawn like replies
Diagram hintsa short note only the model reads, so Claude reaches for diagrams when they help

Each one, with its options, is in docs/REFERENCE.md.

Configure

/monocle theme <name> switches the theme on the spot:

  • Dark: catppuccin-mocha (default), dracula, nord, tokyo-night, gruvbox-dark, rose-pine, everforest, github-dark, one-dark, solarized-dark
  • Light: catppuccin-latte, gruvbox-light, rose-pine-dawn, github-light, solarized-light
  • ansi follows your terminal's own colors as you switch its theme (how)
  • mono uses only bold and dim, and random picks a dark theme each time the mod loads

Everything else is a row in /config, or a key in ~/.claude/settings.json, followed by /reload:

{
  "pluginConfigs": {
    "monocle@monocle": {
      "options": { "theme": "ansi", "tableStyle": "grid", "toolOutput": "quiet", "clock": "24h" }
    }
  }
}
OptionValuesDefault
tableStylebox, rules, grid, minimalbox
headingStylebanner, bold, underline, uppercasebanner
promptStylebubble, bar, chevron, offbubble
toolStylechat, tree-dim, tree-bold, classicchat
toolOutputfull, quietfull
clockoff, 24h, 12hoff
mermaidImages, headingImagestrue, falsetrue
latextrue, falsefalse

Every option, and the 20 color slots you can override one by one, are in docs/REFERENCE.md.

Troubleshooting

  • Something that needs a program doesn't show: run /monocle doctor, install what it lists, then /reload.
  • A setting or update didn't take: run /reload. A session keeps what it loaded.
  • A diagram shows as code: it's over 80 lines, wider than the window, doesn't parse, or mermaid is off.
  • Clicking a copy button selects the word "copy": your terminal copies on select. Use /monocle copy.

To change the code, read AGENTS.md instead.

  • What it is: a Claude Code mod (function hooks in TypeScript) that redraws assistant replies, slash-command output, tool rows, the turn footer and user prompts. It changes how things look. The one thing it sends the model is the optional diagram hint below.
  • Install: /plugin marketplace add DimitrisMilonopoulos/monocle, then /plugin install monocle@monocle. Needs Claude Code 2.1.287+. Changes apply after /reload.
  • Configure: /monocle theme <name> is the only setter at runtime. Every other option is a key under pluginConfigs["monocle@monocle"].options in ~/.claude/settings.json (or a row in /config), then /reload. Options and defaults are in docs/REFERENCE.md; the machine-readable source is userConfig in .claude-plugin/plugin.json. Color values that don't parse are ignored.
  • Side effects: no network. Programs it runs: the ⧉ html clipboard helper (/usr/bin/osascript on macOS, copyq on Linux, 5 second timeout, only on a click), ratex-render while latex is on, mmdc (which starts a headless Chrome with its network turned off) for mermaid blocks while mermaidImages is on, in kitty or Ghostty, kitty @ launch when you zoom a diagram, and magick, fc-match and kitty +runpy (to find your font) for big headings while headingImages is on. With diagramHints on, it adds about 190 tokens of model-only context to each typed prompt.
  • Turning it off: "enabled": false, or disable the plugin in /plugin.
  • For other mods: $.monocle.markdown({ surface, text, columns }), typed in types/index.d.ts. See docs/MODS.md.

Develop

git clone https://github.com/DimitrisMilonopoulos/monocle
claude --plugin-dir ./monocle

Edits hot-reload in that session. AGENTS.md has the rules and the checks to run before a commit, and docs/MODS.md shows how other mods can draw markdown with monocle.

Credits

monocle is made by Dimitrios Milonopoulos. It started as a fork of prismantis by Nahum Litvin, whose work covers everything up to 0.17.1 in the changelog.

License

MIT. Bundled third-party code is listed in THIRD_PARTY_NOTICES.md.

Source 21 files
hooks/register.tsx 968 lines
1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register, RenderElement, RenderSurface } from 'claude-code'
3
4import { clockText, endedAt, promptKey, sentAt, withRecord } from './clock'
5import type { Diagram, Picture } from './diagram'
6import { BROWSERS, MAX_DIAGRAMS, MMDC_SCALE, batchMarkdown, batchOutput, colorHex, diagramKey, fitDiagram, inlinable, mermaidConfig, mermaidOf, puppeteerConfig } from './diagram'
7import type { HeadingSource } from './heading'
8import type { Facts, Platform } from './doctor'
9import { doctorText } from './doctor'
10import type { Palette } from './palette'
11import { ghosttyPalette, kittyPalette, paintTheme, samePalette, usesNames } from './palette'
12import type { PastedSource } from './paste'
13import { fitPreview, imageRefs, pastedImages } from './paste'
14import { HEADING_PX_PER_ROW, KITTY_FONT, fitHeading, ghosttyFont, headingColor, headingRows, headingSource, labelText, parseHeadingSource, wrapHeading } from './heading'
15import type { Formula } from './latex'
16import { LATEX_DPR, LATEX_FONT_PX, fitsImage, keepFormulas, mathOf, padFor, pickFormula, pngSize, ratexColor, renderedOf } from './latex'
17import type { Block } from './markdown'
18import { inlineText, parse, texText } from './markdown'
19import type { ClipboardBackend } from './clipboard'
20import { clipboardCommand } from './clipboard'
21import { boxArt, inlineMarks, mermaidText, shortenEdgeLabels, unsupportedKind } from './mermaid'
22import type { Drawn, Fold } from './render'
23import { isReadOnlyCall, remember, rememberCall, renderBlocks, renderDiffCard, renderExpandedShell, renderFailure, renderShellResult, renderToolGroup, renderToolRow, renderTurnDuration, renderUserPrompt, width } from './render'
24import { helpText, rtlShowcaseText, showcaseText } from './help'
25import { PRESET_NAMES } from './presets'
26import type { Style } from './theme'
27import { RANDOM_THEMES, pickTheme, resolveStyle } from './theme'
28import type { Terminal } from './rtl'
29import { TERMINALS, hasRtl } from './rtl'
30
31const HINT = [
32  'Replies in this session are drawn by the monocle mod, which runs inside Claude Code and is not a command or tool to call: when the user asks to show something with monocle, write it as markdown in the reply.',
33  'Markdown tables, GitHub alerts (> [!WARNING], > [!NOTE]), fenced code with a language tag, and ```mermaid blocks render as colored terminal graphics:',
34  'flowcharts, sequence diagrams and xychart-beta bar or line charts.',
35  'When a reply carries a numeric series or a flow that is easier to see than read, add one small diagram or chart with short labels.',
36  'Skip diagrams for simple answers.',
37  'Put any command or snippet the user may run or copy in a fenced block with a language tag, never inline code: fenced blocks get a copy button, inline code does not.',
38].join(' ')
39
40const MERMAID_HINT = 'This terminal also renders ```mermaid blocks as real images, so every mermaid diagram type works here: pie, gantt, mindmap, timeline, quadrantChart, gitGraph and journey as well as flowcharts, sequence, state, class, ER and xychart-beta.'
41
42const LATEX_HINT = 'This terminal typesets LaTeX math: a formula in $$…$$ on lines of its own, or in a ```math block, renders as an image (KaTeX syntax). Inline $…$ does not render, so write inline math as plain text or Unicode.'
43
44type Host = Terminal | 'herdr'
45
46const detectHost = async ($: EngineInterface): Promise<Host | null> => {
47  const program = await $.env.get('TERM_PROGRAM')
48  const term = await $.env.get('TERM')
49  if (program === 'herdr') return 'herdr'
50  if ((await $.env.get('KITTY_WINDOW_ID')) || term === 'xterm-kitty') return 'kitty'
51  if (program === 'Apple_Terminal') return 'apple-terminal'
52  if (program === 'WarpTerminal') return 'warp'
53  if (program === 'ghostty' || term === 'xterm-ghostty' || (await $.env.get('GHOSTTY_RESOURCES_DIR'))) return 'ghostty'
54  if (program === 'WezTerm') return 'wezterm'
55  if (program === 'vscode') return 'vscode'
56  if (program === 'iTerm.app') return 'iterm'
57  if (term === 'alacritty' || (await $.env.get('ALACRITTY_WINDOW_ID'))) return 'alacritty'
58  if (await $.env.get('WT_SESSION')) return 'windows-terminal'
59  if (await $.env.get('VTE_VERSION')) return 'gnome'
60  if (await $.env.get('KONSOLE_VERSION')) return 'konsole'
61  return null
62}
63
64const terminalOf = (host: Host | null): Terminal | null => (host === 'herdr' ? null : host)
65
66const applyRtl = (style: Style, terminal: Terminal | null): Terminal | null => {
67  if (style.rtl === 'off') return null
68  if (style.rtl !== 'auto') return style.rtl
69  style.reorder = terminal !== null
70  if (terminal) style.shape = TERMINALS[terminal]
71  return terminal
72}
73
74const expandedCalls = new Set<string>()
75const readOnlyCalls = new Set<string>()
76
77const markExpanded = (id: string) => {
78  expandedCalls.delete(id)
79  expandedCalls.add(id)
80  if (expandedCalls.size > 500) expandedCalls.delete(expandedCalls.values().next().value!)
81}
82
83const columnsOf = (viewport?: { columns?: number }) => Math.max(20, (viewport?.columns ?? 100) - 4)
84
85const formulas = atom({ plugin: 'monocle', key: 'formulas' } as const, {})
86const latexDir = atom({ plugin: 'monocle', key: 'dir' } as const, '')
87const diagrams = atom({ plugin: 'monocle', key: 'diagrams' } as const, {})
88const diagramDir = atom({ plugin: 'monocle', key: 'diagramDir' } as const, '')
89const headings = atom({ plugin: 'monocle', key: 'headings' } as const, {})
90const MAX_HEADINGS = 100
91const unfolded = atom({ plugin: 'monocle', key: 'unfolded' } as const, [] as string[])
92const zoomLoad = atom({ plugin: 'monocle', key: 'zoomLoad' } as const, '')
93const sent = atom({ plugin: 'monocle', key: 'sent' } as const, [])
94const turnEnds = atom({ plugin: 'monocle', key: 'turnEnds' } as const, [])
95
96const firstDrawn = async ($: EngineInterface, id: string): Promise<number> => {
97  const ref = { plugin: 'monocle', key: 'drawn', id } as const
98  const { value } = await $.state.get(ref)
99  if (value !== undefined) return value
100  const now = await $.clock.now()
101  $.clock.after(0, () => void $.state.set(ref, now, { ifVersion: 0 }))
102  return now
103}
104
105type Typeset = Exclude<Formula, { error: true }> & { tex: string }
106
107const forcedImages = async ($: EngineInterface): Promise<boolean> => /^(1|true|yes|on)$/i.test((await $.env.get('CLAUDE_CODE_FORCE_TERMINAL_IMAGES')) ?? '')
108
109const inBackground = async ($: EngineInterface): Promise<boolean> => (await $.env.get('CLAUDE_CODE_SESSION_KIND')) === 'bg'
110
111const viewsImages = async ($: EngineInterface, host: Host | null): Promise<boolean> => {
112  if ((await $.env.get('TMUX')) || (await inBackground($))) return false
113  if (host === 'herdr') return forcedImages($)
114  return host === 'kitty' || host === 'ghostty'
115}
116
117const showsImages = async ($: EngineInterface): Promise<boolean> => viewsImages($, await detectHost($))
118
119type Latex = { command: string; dir: string }
120type LatexSession = { style: Style; color: string; pending: Set<string>; wanted: Set<string>; batch: string[]; engine?: Promise<Latex | null>; ready?: Latex | null; stopped?: boolean; queue: Promise<void> }
121
122const formulaKey = (latex: LatexSession, tex: string) => `${latex.color}\0${tex}`
123
124const typeset = async ($: EngineInterface, latex: LatexSession, engine: Latex, texs: string[], fontSize = LATEX_FONT_PX, dpr = LATEX_DPR): Promise<Formula[]> => {
125  const { stdout } = await $.process.run(
126    [engine.command, '--output-dir', engine.dir, '--color', latex.color, '--background-color', 'transparent', '--font-size', String(fontSize), '--dpr', String(dpr)],
127    { stdin: `${texs.join('\n')}\n`, timeoutMs: 2000 },
128  )
129  const rendered = renderedOf(stdout)
130  return Promise.all(texs.map(async (_, i): Promise<Formula> => {
131    if (!rendered.has(i + 1)) return { error: true }
132    const png = await $.fs.read(`${engine.dir}/${String(i + 1).padStart(4, '0')}.png`, { as: 'bytes' }).then(r => r.base64, () => '')
133    const size = png ? pngSize(png) : null
134    return size && fitsImage(png) ? { png, ...size } : { error: true }
135  }))
136}
137
138const withPadding = async ($: EngineInterface, latex: LatexSession, engine: Latex, texs: string[], results: Formula[]): Promise<Formula[]> => {
139  const padded: Formula[] = []
140  for (const [i, result] of results.entries()) {
141    const pad = 'png' in result ? padFor(result) : null
142    const [again] = pad ? await typeset($, latex, engine, [texs[i]!], pad.fontSize, pad.dpr).catch((): Formula[] => []) : []
143    padded.push(again && 'png' in again && 'png' in result ? { ...result, padded: again } : result)
144  }
145  return padded
146}
147
148const startLatex = async ($: EngineInterface, latex: LatexSession): Promise<Latex | null> => {
149  if (!latex.style.latex || !(await $.session.surfaces()).includes('terminal') || !(await showsImages($))) return null
150  const tmp = (await $.env.get('TMPDIR')) ?? (await $.env.get('TEMP')) ?? '/tmp'
151  const suffix = (await read($, latexDir)) || crypto.randomUUID()
152  await update($, latexDir, () => suffix)
153  const engine = { command: 'ratex-render', dir: `${tmp.replace(/[\\/]+$/, '')}/monocle-latex-${await $.session.id()}-${suffix}` }
154  const [check] = await typeset($, latex, engine, ['x^2'])
155  return check && 'png' in check ? engine : null
156}
157
158const latexEngine = ($: EngineInterface, latex: LatexSession): Promise<Latex | null> =>
159  (latex.engine ??= startLatex($, latex)
160    .catch(() => null)
161    .then(engine => {
162      latex.ready = engine
163      if (engine) $.ui.invalidate('ui.render')
164      return engine
165    }))
166
167const stopLatex = (latex: LatexSession) => {
168  latex.stopped = true
169  latex.pending.clear()
170  latex.batch = []
171}
172
173const typesetLater = async ($: EngineInterface, latex: LatexSession, texs: string[]): Promise<void> => {
174  const engine = await latexEngine($, latex)
175  if (!engine || latex.stopped) return
176  const results = await typeset($, latex, engine, texs).then(
177    done => withPadding($, latex, engine, texs, done),
178    () => null,
179  )
180  if (!results) return stopLatex(latex)
181  const fresh = texs.map((tex, i) => [formulaKey(latex, tex), results[i]!] as const)
182  await update($, formulas, store => keepFormulas(store, fresh, latex.wanted))
183  latex.wanted.clear()
184  for (const tex of texs) latex.pending.delete(formulaKey(latex, tex))
185}
186
187const mathOfBlocks = async ($: EngineInterface, latex: LatexSession, surface: string, blocks: Block[]): Promise<Map<number, Typeset>> => {
188  const maths = [...blocks.entries()].flatMap(([i, block]) => {
189    const tex = mathOf(block)
190    return tex === null ? [] : [{ i, tex, key: formulaKey(latex, tex) }]
191  })
192  if (surface !== 'terminal' || maths.length === 0) return new Map()
193  void latexEngine($, latex)
194  if (!latex.ready) return new Map()
195  for (const { key } of maths) latex.wanted.add(key)
196  const store = (await read($, formulas)) as Record<string, Formula>
197  const missing = latex.stopped ? [] : [...new Map(maths.map(({ key, tex }) => [key, tex]))].filter(([key]) => !store[key] && !latex.pending.has(key))
198  if (missing.length) {
199    if (latex.batch.length === 0) {
200      $.clock.after(0, () => {
201        const texs = latex.batch
202        latex.batch = []
203        latex.queue = latex.queue.then(() => typesetLater($, latex, texs)).catch(() => stopLatex(latex))
204      })
205    }
206    for (const [key, tex] of missing) {
207      latex.pending.add(key)
208      latex.batch.push(tex)
209    }
210  }
211  return new Map(maths.flatMap(({ i, tex, key }) => {
212    const formula = store[key]
213    return formula && 'png' in formula ? [[i, { tex, ...formula }] as const] : []
214  }))
215}
216
217type Mermaid = { dir: string; puppeteer: string }
218type Job = { key: string; source: string }
219type Painter = {
220  style: Style
221  limit: number
222  kind: 'mermaid' | 'headings'
223  pending: Set<string>
224  wanted: Set<string>
225  batch: Job[]
226  engine?: Promise<Mermaid | Headings | null>
227  ready?: Mermaid | Headings | null
228  stopped?: boolean
229  queue: Promise<void>
230}
231type MermaidSession = Painter
232
233const painter = (style: Style, kind: Painter['kind'], limit: number): Painter =>
234  ({ style, kind, limit, pending: new Set(), wanted: new Set(), batch: [], queue: Promise.resolve() })
235
236const themeKeys = new WeakMap<Style['theme'], string>()
237
238const hashOf = (text: string): string => {
239  let hash = 2166136261
240  for (const ch of text) hash = Math.imul(hash ^ ch.charCodeAt(0), 16777619) >>> 0
241  return hash.toString(36)
242}
243
244const themeKeyOf = (theme: Style['theme']): string => {
245  const known = themeKeys.get(theme)
246  if (known) return known
247  const key = hashOf(JSON.stringify(mermaidConfig(theme)))
248  themeKeys.set(theme, key)
249  return key
250}
251
252const tmpDir = async ($: EngineInterface): Promise<string> => ((await $.env.get('TMPDIR')) ?? (await $.env.get('TEMP')) ?? '/tmp').replace(/[\\/]+$/, '')
253
254const workDir = async ($: EngineInterface, kind: string): Promise<string> => {
255  const suffix = (await read($, diagramDir)) || crypto.randomUUID()
256  await update($, diagramDir, () => suffix)
257  return `${await tmpDir($)}/monocle-${kind}-${await $.session.id()}-${suffix}`
258}
259
260const drawsImages = async ($: EngineInterface): Promise<boolean> => (await $.session.surfaces()).includes('terminal') && (await showsImages($))
261
262const startMermaid = async ($: EngineInterface, style: Style): Promise<Mermaid | null> => {
263  if (!style.mermaidImages || !(await drawsImages($))) return null
264  const version = await $.process.run(['mmdc', '--version'], { timeoutMs: 15000 })
265  if (version.exitCode !== 0) return null
266  const dir = await workDir($, 'mermaid')
267  const browser = await findBrowser($)
268  const puppeteer = `${dir}/puppeteer.json`
269  await $.fs.write(puppeteer, JSON.stringify(puppeteerConfig(browser?.kind === 'system' ? browser.path : undefined)))
270  return { dir, puppeteer }
271}
272
273const findBrowser = async ($: EngineInterface): Promise<{ kind: 'puppeteer' } | { kind: 'system'; path: string } | null> => {
274  const home = await $.env.get('HOME')
275  const bundled = (await $.env.get('PUPPETEER_CACHE_DIR')) ?? (home ? `${home}/.cache/puppeteer` : '')
276  if ((await $.env.get('PUPPETEER_EXECUTABLE_PATH')) || (bundled && (await $.fs.exists(bundled)))) return { kind: 'puppeteer' }
277  for (const path of BROWSERS) if (await $.fs.exists(path)) return { kind: 'system', path }
278  return null
279}
280
281const commandPath = async ($: EngineInterface, name: string): Promise<string | null> => {
282  const result = await $.process.run(['sh', '-c', 'command -v "$1"', 'which', name], { timeoutMs: 5000 }).catch(() => null)
283  return (result?.exitCode === 0 && result.stdout.trim()) || null
284}
285
286const platformOf = async ($: EngineInterface): Promise<Platform> => {
287  const result = await $.process.run(['uname', '-s'], { timeoutMs: 5000 }).catch(() => null)
288  const name = result?.stdout.trim()
289  return name === 'Darwin' ? 'mac' : name === 'Linux' ? 'linux' : 'other'
290}
291
292const doctorFacts = async ($: EngineInterface, style: Style): Promise<Facts> => {
293  const host = await detectHost($)
294  const [platform, mmdc, browser, magick, fcMatch, ratex] = await Promise.all([platformOf($), commandPath($, 'mmdc'), findBrowser($), imageMagick($), commandPath($, 'fc-match'), commandPath($, 'ratex-render')])
295  return {
296    platform,
297    terminal: host,
298    forced: await forcedImages($),
299    background: await inBackground($),
300    tmux: !!(await $.env.get('TMUX')),
301    mmdc,
302    browser: browser ? (browser.kind === 'system' ? browser.path : "puppeteer's own Chrome") : null,
303    magick,
304    fcMatch,
305    font: magick && fcMatch ? ((await fontFile($, await terminalFont($)))?.split('/').pop() ?? null) : null,
306    kittyRemote: host === 'kitty' ? !!(await $.env.get('KITTY_LISTEN_ON')) : null,
307    ratex,
308    latex: style.latex,
309  }
310}
311
312const painterEngine = ($: EngineInterface, p: Painter): Promise<Mermaid | Headings | null> =>
313  (p.engine ??= (p.kind === 'mermaid' ? startMermaid($, p.style) : startHeadings($, p.style))
314    .catch(() => null)
315    .then(engine => {
316      p.ready = engine
317      if (engine) $.ui.invalidate('ui.render')
318      return engine
319    }))
320
321const stopPainter = (p: Painter) => {
322  p.stopped = true
323  p.pending.clear()
324  p.batch = []
325}
326
327const pictureAt = async ($: EngineInterface, file: string): Promise<Diagram> => {
328  const png = await $.fs.read(file, { as: 'bytes' }).then(r => r.base64, () => '')
329  const size = png ? pngSize(png) : null
330  return size ? { file, ...size, ...(inlinable(png) ? { png } : {}) } : { error: true }
331}
332
333const renderDiagrams = async ($: EngineInterface, engine: Mermaid, theme: Style['theme'], sources: readonly string[]): Promise<Diagram[]> => {
334  const batch = crypto.randomUUID().slice(0, 8)
335  const input = `${engine.dir}/${batch}.md`
336  const config = `${engine.dir}/${batch}.json`
337  await $.fs.write(config, JSON.stringify(mermaidConfig(theme)))
338  await $.fs.write(input, batchMarkdown(sources))
339  const argv = ['mmdc', '-q', '-i', input, '-o', `${engine.dir}/${batch}.png`, '-c', config, '-b', 'transparent', '-s', String(MMDC_SCALE), '-j', '1', '-p', engine.puppeteer]
340  const { exitCode } = await $.process.run(argv, { timeoutMs: Math.min(120000, 20000 + 5000 * sources.length) })
341  const results = await Promise.all(sources.map((_, i) => pictureAt($, batchOutput(engine.dir, batch, i))))
342  if (exitCode === 0 || sources.length === 1) return results
343  const retried: Diagram[] = []
344  for (const [i, result] of results.entries()) retried.push('error' in result ? (await renderDiagrams($, engine, theme, [sources[i]!]))[0]! : result)
345  return retried
346}
347
348type Headings = { dir: string; font: string; magick: string }
349
350const GHOSTTY_APP = '/Applications/Ghostty.app/Contents/MacOS/ghostty'
351
352const fontTerminal = async ($: EngineInterface): Promise<'kitty' | 'ghostty' | null> => {
353  const host = await detectHost($)
354  if (host === 'kitty' || host === 'ghostty') return host
355  if (host !== 'herdr') return null
356  if (await $.env.get('GHOSTTY_RESOURCES_DIR')) return 'ghostty'
357  return (await $.env.get('KITTY_PID')) ? 'kitty' : null
358}
359
360const terminalFont = async ($: EngineInterface): Promise<string | null> => {
361  const terminal = await fontTerminal($)
362  if (terminal === 'kitty') {
363    const result = await $.process.run(['kitty', '+runpy', KITTY_FONT], { timeoutMs: 10000 }).catch(() => null)
364    const name = result?.exitCode === 0 ? result.stdout.trim() : ''
365    return name && name !== 'monospace' ? name : null
366  }
367  if (terminal !== 'ghostty') return null
368  for (const ghostty of ['ghostty', GHOSTTY_APP]) {
369    const result = await $.process.run([ghostty, '+show-config'], { timeoutMs: 10000 }).catch(() => null)
370    if (result?.exitCode === 0) return ghosttyFont(result.stdout)
371  }
372  return null
373}
374
375const terminalColors = async ($: EngineInterface): Promise<Palette | null> => {
376  const terminal = await fontTerminal($)
377  if (terminal === 'kitty') {
378    const window = await $.env.get('KITTY_WINDOW_ID')
379    const result = await $.process.run(['kitty', '@', 'get-colors', ...(window ? ['--match', `id:${window}`] : [])], { timeoutMs: 5000 }).catch(() => null)
380    return result?.exitCode === 0 ? kittyPalette(result.stdout) : null
381  }
382  if (terminal !== 'ghostty') return null
383  for (const ghostty of ['ghostty', GHOSTTY_APP]) {
384    const result = await $.process.run([ghostty, '+show-config'], { timeoutMs: 10000 }).catch(() => null)
385    if (result?.exitCode === 0) return ghosttyPalette(result.stdout)
386  }
387  return null
388}
389
390type Tones = { palette: Palette | null; loading?: Promise<void> }
391
392const refreshTones = ($: EngineInterface, style: Style, latex: LatexSession, tones: Tones): Promise<void> => {
393  if (!style.followsTerminal && !usesNames(style.theme)) return Promise.resolve()
394  return (tones.loading ??= terminalColors($)
395    .catch(() => null)
396    .then(found => {
397      tones.loading = undefined
398      if (!found || samePalette(found, tones.palette)) return
399      tones.palette = found
400      style.paletteRead = true
401      style.imageTheme = paintTheme(style.theme, found, style.followsTerminal)
402      latex.color = ratexColor(style.imageTheme.diagramText ?? style.imageTheme.codeText ?? '#808080')
403      $.ui.invalidate('ui.render')
404    }))
405}
406
407const imageMagick = async ($: EngineInterface): Promise<string | null> => {
408  for (const command of ['magick', 'convert']) {
409    const result = await $.process.run([command, '-version'], { timeoutMs: 10000 }).catch(() => null)
410    if (result?.exitCode === 0 && result.stdout.includes('ImageMagick')) return command
411  }
412  return null
413}
414
415const fontFile = async ($: EngineInterface, family: string | null): Promise<string | null> => {
416  for (const pattern of [...(family ? [`${family}:bold`] : []), 'monospace:bold']) {
417    const result = await $.process.run(['fc-match', '-f', '%{file}', pattern], { timeoutMs: 5000 }).catch(() => null)
418    const file = result?.exitCode === 0 ? result.stdout.trim() : ''
419    if (file) return file
420  }
421  return null
422}
423
424const startHeadings = async ($: EngineInterface, style: Style): Promise<Headings | null> => {
425  if (!style.headingImages || !(await drawsImages($))) return null
426  const magick = await imageMagick($)
427  if (!magick) return null
428  const font = await fontFile($, await terminalFont($))
429  if (!font) return null
430  const dir = await workDir($, 'headings')
431  await $.fs.write(`${dir}/.monocle`, '')
432  return { dir, font, magick }
433}
434
435const renderHeadings = async ($: EngineInterface, engine: Headings, sources: readonly string[]): Promise<Diagram[]> => {
436  const results: Diagram[] = []
437  for (const source of sources) {
438    const heading = parseHeadingSource(source)
439    const file = `${engine.dir}/${crypto.randomUUID().slice(0, 8)}.png`
440    const argv = [engine.magick, '-background', 'none', '-fill', heading.color, '-font', engine.font, '-pointsize', String(HEADING_PX_PER_ROW * headingRows(heading.level)), `label:${labelText(heading.lines)}`, file]
441    const { exitCode } = await $.process.run(argv, { timeoutMs: 10000 })
442    results.push(exitCode === 0 ? await pictureAt($, file) : { error: true })
443  }
444  return results
445}
446
447const paintLater = async ($: EngineInterface, p: Painter, items: Job[]): Promise<void> => {
448  const engine = await painterEngine($, p)
449  if (!engine || p.stopped) return
450  const sources = items.map(item => item.source)
451  const results = await (p.kind === 'mermaid' ? renderDiagrams($, engine as Mermaid, p.style.imageTheme, sources) : renderHeadings($, engine as Headings, sources)).catch(() => null)
452  if (!results) return stopPainter(p)
453  const fresh = items.map((item, i) => [item.key, results[i]!] as const)
454  const keep = (store: Record<string, unknown>) => keepFormulas(store, fresh, p.wanted, p.limit)
455  await (p.kind === 'mermaid' ? update($, diagrams, keep) : update($, headings, keep))
456  p.wanted.clear()
457  for (const item of items) p.pending.delete(item.key)
458}
459
460const picturesOf = async ($: EngineInterface, p: Painter, found: (Job & { i: number })[]): Promise<Map<number, Picture>> => {
461  if (found.length === 0) return new Map()
462  void painterEngine($, p)
463  if (!p.ready) return new Map()
464  for (const { key } of found) p.wanted.add(key)
465  const store = (p.kind === 'mermaid' ? await read($, diagrams) : await read($, headings)) as Record<string, Diagram>
466  const missing = p.stopped ? [] : [...new Map(found.map(({ key, source }) => [key, source]))].filter(([key]) => !store[key] && !p.pending.has(key))
467  if (missing.length) {
468    if (p.batch.length === 0) {
469      $.clock.after(0, () => {
470        const items = p.batch
471        p.batch = []
472        p.queue = p.queue.then(() => paintLater($, p, items)).catch(() => stopPainter(p))
473      })
474    }
475    for (const [key, source] of missing) {
476      p.pending.add(key)
477      p.batch.push({ key, source })
478    }
479  }
480  return new Map(found.flatMap(({ i, key }) => {
481    const picture = store[key]
482    return picture && !('error' in picture) ? [[i, picture] as const] : []
483  }))
484}
485
486const diagramsOfBlocks = async ($: EngineInterface, mermaid: MermaidSession, surface: string, blocks: Block[]): Promise<Map<number, Picture>> => {
487  if (surface !== 'terminal' || !mermaid.style.mermaidImages) return new Map()
488  const themeKey = themeKeyOf(mermaid.style.imageTheme)
489  return picturesOf($, mermaid, [...blocks.entries()].flatMap(([i, block]) => {
490    const source = mermaidOf(block)
491    return source === null ? [] : [{ i, source, key: diagramKey(themeKey, source) }]
492  }))
493}
494
495const headingsOfBlocks = async ($: EngineInterface, headed: Painter, surface: string, blocks: Block[], columns: number): Promise<Map<number, Picture & { heading: HeadingSource }>> => {
496  const style = headed.style
497  if (surface !== 'terminal' || !style.headingImages) return new Map()
498  const found = blocks.flatMap((block, i) => {
499    const rows = block.kind === 'heading' ? headingRows(block.level) : 0
500    if (block.kind !== 'heading' || !rows || i === blocks.length - 1) return []
501    const text = inlineText(block.inline).trim()
502    if (!text || hasRtl(text)) return []
503    const heading = { level: block.level, color: colorHex(headingColor(style.imageTheme, block.level), '#808080'), lines: wrapHeading(style.headingStyle === 'uppercase' && block.level === 1 ? text.toUpperCase() : text, Math.max(4, Math.floor(columns / rows))) }
504    const source = headingSource(heading)
505    return [{ i, source, key: source, heading }]
506  })
507  const pictures = await picturesOf($, headed, found)
508  return new Map(found.flatMap(({ i, heading }) => {
509    const picture = pictures.get(i)
510    return picture ? [[i, { ...picture, heading }] as const] : []
511  }))
512}
513
514const ZOOM_SCALE = 4
515const ZOOM_VIEW = `kitty +kitten icat --scale-up --align center "$1"; printf '\\n  [Enter to close] '; read -r _`
516
517type ZoomTarget = { kind: 'mermaid'; source: string } | { kind: 'image'; id: number }
518
519const zoomable = {
520  targets: new Map<string, ZoomTarget>(),
521  session: undefined as MermaidSession | undefined,
522  add(target: ZoomTarget): string {
523    const id = target.kind === 'mermaid' ? hashOf(target.source) : `image-${target.id}`
524    this.targets.delete(id)
525    this.targets.set(id, target)
526    if (this.targets.size > 200) this.targets.delete(this.targets.keys().next().value!)
527    return id
528  },
529}
530
531type Preview = Picture & PastedSource
532const pastes = { images: new Map<number, Preview | { error: true }>(), loading: false, triedAt: -Infinity, dir: '' }
533
534const pasteDir = async ($: EngineInterface): Promise<string> => {
535  if (pastes.dir) return pastes.dir
536  const dir = await workDir($, 'paste')
537  await $.fs.write(`${dir}/.monocle`, '')
538  pastes.dir = dir
539  return dir
540}
541
542const DECODE = '{ base64 -d < "$1" 2>/dev/null || base64 -D < "$1"; } > "$2.raw" || exit 1; if [ "$3" = png ]; then mv "$2.raw" "$2"; else im=$(command -v magick || command -v convert) && "$im" "$2.raw" "png:$2"; fi'
543
544const pasteFile = async ($: EngineInterface, id: number, source: PastedSource): Promise<string | null> => {
545  const file = `${await pasteDir($)}/image-${id}.png`
546  await $.fs.write(`${file}.b64`, source.data)
547  const kind = source.mediaType === 'image/png' ? 'png' : 'other'
548  const result = await $.process.run(['sh', '-c', DECODE, 'paste', `${file}.b64`, file, kind], { timeoutMs: 20000 }).catch(() => null)
549  return result?.exitCode === 0 ? file : null
550}
551
552const previewOf = async ($: EngineInterface, id: number, source: PastedSource): Promise<Preview | { error: true }> => {
553  const size = source.mediaType === 'image/png' ? pngSize(source.data) : null
554  if (size && inlinable(source.data)) return { png: source.data, file: '', ...size, ...source }
555  const file = await pasteFile($, id, source)
556  const png = file ? await $.fs.read(file, { as: 'bytes' }).then(r => r.base64, () => '') : ''
557  const read = png ? pngSize(png) : null
558  return file && read ? { file, ...read, ...(inlinable(png) ? { png } : {}), ...source } : { error: true }
559}
560
561const PASTE_RETRY_MS = 250
562const PASTE_TRIES = 8
563
564const loadPastes = async ($: EngineInterface, wanted: number[], tries: number): Promise<void> => {
565  const messages = await $.session.messages({ as: 'api' })
566  let added = false
567  for (const [id, source] of pastedImages(messages)) {
568    if (pastes.images.has(id)) continue
569    pastes.images.set(id, await previewOf($, id, source))
570    added = true
571  }
572  if (added) $.ui.invalidate('ui.render')
573  if (tries <= 1 || wanted.every(id => pastes.images.has(id))) return
574  await new Promise<void>(resolve => $.clock.after(PASTE_RETRY_MS, () => void loadPastes($, wanted, tries - 1).catch(() => undefined).finally(resolve)))
575}
576
577const previewsOf = async ($: EngineInterface, text: string): Promise<{ id: number; preview: Preview }[]> => {
578  const ids = imageRefs(text)
579  if (ids.length === 0) return []
580  const now = await $.clock.now()
581  if (ids.some(id => !pastes.images.has(id)) && !pastes.loading && now - pastes.triedAt > 1000) {
582    pastes.loading = true
583    pastes.triedAt = now
584    $.clock.after(0, () => void loadPastes($, ids, PASTE_TRIES).catch(() => undefined).finally(() => (pastes.loading = false)))
585  }
586  return ids.flatMap(id => {
587    const preview = pastes.images.get(id)
588    return preview && !('error' in preview) ? [{ id, preview }] : []
589  })
590}
591
592const zoomFile = async ($: EngineInterface, target: ZoomTarget): Promise<{ file: string } | { failure: string }> => {
593  if (target.kind === 'image') {
594    const preview = pastes.images.get(target.id)
595    if (!preview || 'error' in preview) return { failure: 'That image is gone' }
596    const file = preview.file || (await pasteFile($, target.id, preview))
597    return file ? { file } : { failure: 'Could not write the image' }
598  }
599  const mermaid = zoomable.session
600  const engine = mermaid ? ((await painterEngine($, mermaid)) as Mermaid | null) : null
601  if (!mermaid || !engine) return { failure: 'mmdc is not available' }
602  const name = `${engine.dir}/zoom-${crypto.randomUUID().slice(0, 8)}`
603  await $.fs.write(`${name}.json`, JSON.stringify(mermaidConfig(mermaid.style.imageTheme)))
604  await $.fs.write(`${name}.mmd`, target.source)
605  const render = await $.process.run(['mmdc', '-q', '-i', `${name}.mmd`, '-o', `${name}.png`, '-c', `${name}.json`, '-b', 'transparent', '-s', String(ZOOM_SCALE), '-j', '1', '-p', engine.puppeteer], { timeoutMs: 60000 })
606  return render.exitCode === 0 ? { file: `${name}.png` } : { failure: `mmdc failed: ${render.stderr.trim().split('\n')[0] ?? render.exitCode}` }
607}
608
609const zoomIn = async ($: EngineInterface, target: ZoomTarget): Promise<string | null> => {
610  const window = await $.env.get('KITTY_WINDOW_ID')
611  if (!window) return 'Full screen needs kitty'
612  const shown = await zoomFile($, target)
613  if ('failure' in shown) return shown.failure
614  const launch = await $.process.run(['kitty', '@', 'launch', '--match', `window_id:${window}`, '--next-to', `id:${window}`, '--type=overlay', `--title=${target.kind === 'image' ? 'image' : 'diagram'}`, '--', 'sh', '-c', ZOOM_VIEW, 'zoom', shown.file], { timeoutMs: 10000 })
615  return launch.exitCode === 0 ? null : `kitty refused the overlay: ${launch.stderr.trim().split('\n')[0] ?? launch.exitCode}`
616}
617
618const openZoom = async ($: EngineInterface, id: string): Promise<void> => {
619  const target = zoomable.targets.get(id)
620  if (!target) return
621  $.ui.toast(target.kind === 'image' ? 'Opening the image full screen…' : 'Opening the diagram full screen…')
622  const failure = await zoomIn($, target).catch(() => 'Could not open it')
623  if (failure) $.ui.toast(failure)
624}
625
626const zoomBox = (el: ReturnType<EngineInterface['ui']['resolve']>, style: Style, image: RenderElement, id: string, fit: { columns: number; rows: number }, key: string): RenderElement =>
627  'Client' in el && style.zoom ? (
628    <el.Box key={key} width={fit.columns} height={fit.rows}>
629      {image}
630      <el.Box position="absolute" top={0} left={0}>
631        <el.Client key={`zoom-${id}`} module="./zoom.tsx" props={{ id, columns: fit.columns, rows: fit.rows }} width={fit.columns} height={fit.rows} />
632      </el.Box>
633    </el.Box>
634  ) : image
635
636const drawPreviews = ($: EngineInterface, el: ReturnType<EngineInterface['ui']['resolve']>, style: Style, previews: { id: number; preview: Preview }[], columns: number): RenderElement | null => {
637  const Image = 'Image' in el ? el.Image : null
638  if (!Image || previews.length === 0) return null
639  const shown = previews.flatMap(({ id, preview }) => {
640    const fit = fitPreview(preview, columns - 2)
641    if (!fit) return []
642    const image = <Image key={`img${id}${style.zoomLoad ?? ''}`} source={preview.png ? { png: preview.png } : { file: preview.file, format: 'png' }} columns={fit.columns} rows={fit.rows} alt={`[Image #${id}]`} />
643    return [zoomBox(el, style, image, zoomable.add({ kind: 'image', id }), fit, `paste${id}`)]
644  })
645  return shown.length ? <el.Box key="pasted" flexDirection="column" rowGap={1} paddingLeft={2}>{shown}</el.Box> : null
646}
647
648const locate = async ($: EngineInterface, style: Style) => {
649  style.cwd = await $.session.root().catch(() => style.cwd)
650  style.home ||= (await $.env.get('HOME').catch(() => undefined)) ?? (await $.env.get('USERPROFILE').catch(() => undefined)) ?? ''
651}
652
653const htmlBackend = async ($: EngineInterface): Promise<ClipboardBackend | null> => {
654  if (await $.env.get('SSH_CONNECTION') || await $.env.get('SSH_TTY')) return null
655  const helper = await $.fs.stat('/usr/bin/osascript').catch(() => null)
656  if (helper?.kind === 'file') return 'macos'
657  return await $.env.get('WAYLAND_DISPLAY') || await $.env.get('DISPLAY') ? 'linux' : null
658}
659
660const zoomBackend = async ($: EngineInterface): Promise<boolean> =>
661  !!(await $.env.get('KITTY_WINDOW_ID')) && !(await $.env.get('TMUX')) && !(await $.env.get('SSH_CONNECTION')) && !(await $.env.get('SSH_TTY'))
662
663type Probes = { detected?: Promise<Host | null>; terminal?: Promise<Terminal | null>; htmlCopy?: Promise<ClipboardBackend | null>; zoom?: Promise<boolean>; images?: Promise<boolean> }
664
665const probe = async ($: EngineInterface, style: Style, probes: Probes): Promise<Terminal | null> => {
666  probes.detected ??= detectHost($)
667  const detected = await probes.detected
668  probes.terminal ??= Promise.resolve(applyRtl(style, terminalOf(detected)))
669  const terminal = await probes.terminal
670  probes.htmlCopy ??= htmlBackend($)
671  style.htmlCopy = await probes.htmlCopy
672  probes.zoom ??= zoomBackend($)
673  style.zoom = await probes.zoom
674  probes.images ??= viewsImages($, detected)
675  style.images = await probes.images
676  return terminal
677}
678
679const copyTable = async ($: EngineInterface, backend: ClipboardBackend, html: string, text: string): Promise<string | null> => {
680  const command = clipboardCommand(backend, html, text)
681  const result = await $.process.run(command.argv, { stdin: command.stdin, timeoutMs: 5000 }).catch(() => null)
682  if (!result) return command.failure
683  return result.exitCode === 0 ? null : result.stderr.trim() || command.failure
684}
685
686const folder = ($: EngineInterface, el: ReturnType<EngineInterface['ui']['resolve']>, open?: string[]): Fold => (id, hidden, key) => {
687  if (!open) return null
688  const folded = !open.includes(id)
689  return { folded, element: <el.Button key={key} variant="secondary" label={folded ? `+${hidden} more lines` : 'show less'} onPress={() => update($, unfolded, ids => (folded ? [...ids, id].slice(-500) : ids.filter(x => x !== id)))} /> }
690}
691
692const drawMarkdown = ($: EngineInterface, el: ReturnType<EngineInterface['ui']['resolve']>, style: Style, blocks: ReturnType<typeof parse>, columns: number, math: Map<number, Typeset> = new Map(), reply?: string, open?: string[], pictures: Map<number, Picture> = new Map(), titles: Map<number, Picture & { heading: HeadingSource }> = new Map()): RenderElement[] => {
693  const { Button } = el
694  const copy = (text: string | (() => string), key: string, label = '⧉ copy', html?: () => string) => {
695    const copied = async (surface: RenderSurface): Promise<string> => {
696      const content = typeof text === 'function' ? text() : text
697      const backend = style.htmlCopy
698      const failure = html && backend ? await copyTable($, backend, html(), content) : undefined
699      if (failure === null) return 'Copied formatted table'
700      const result = await $.ui.copy({ text: content, surface })
701      if (!result.isCopied) return `Copy failed: ${result.reason}`
702      return failure ? `Copied as plain text (${failure})` : 'Copied'
703    }
704    return style.copyButtons && (!html || style.htmlCopy) ? (
705      <Button
706        key={key}
707        variant="primary"
708        label={label}
709        onPress={async press => $.ui.toast(await copied(press.surface).catch(() => 'Copy failed'))}
710      />
711    ) : null
712  }
713  const drawn: Drawn = new Map()
714  const Image = 'Image' in el ? el.Image : null
715  if (style.mermaid) {
716    for (const [i, block] of blocks.entries()) {
717      if (block.kind !== 'code' || block.lang.toLowerCase() !== 'mermaid') continue
718      const source = block.lines.join('\n')
719      const picture = Image ? pictures.get(i) : undefined
720      const fit = picture ? fitDiagram(picture, columns) : null
721      if (Image && picture && fit) {
722        const alt = `mermaid: ${source.trim().split('\n')[0]!.trim()}`
723        const id = zoomable.add({ kind: 'mermaid', source })
724        const image = <Image key={`img${i}${style.zoomLoad ?? ''}`} source={picture.png ? { png: picture.png } : { file: picture.file, format: 'png' }} columns={fit.columns} rows={fit.rows} alt={alt} />
725        const element = <el.Box key={`b${i}`} paddingLeft={2}>{zoomBox(el, style, image, id, fit, `zoom${i}`)}</el.Box>
726        const button = style.zoom ? <el.Button key={`zoombtn${i}`} variant="primary" label="⤢ zoom" onPress={() => void openZoom($, id)} /> : undefined
727        drawn.set(i, { element, button })
728        continue
729      }
730      let narrowest = Infinity
731      for (const max of [Infinity, 24, 12]) {
732        const art = mermaidText(max === Infinity ? source : shortenEdgeLabels(source, max), style.mermaidAscii, columns)
733        const wide = art === null ? Infinity : Math.max(...art.split('\n').map(width))
734        narrowest = Math.min(narrowest, wide)
735        if (art === null || wide > columns - 2) continue
736        drawn.set(i, { element: boxArt(el, style, art, `b${i}`, inlineMarks(source)), art })
737        break
738      }
739      if (!drawn.has(i) && !block.isOpen) {
740        const kind = unsupportedKind(source)
741        drawn.set(i, { note: kind ? `${kind} diagrams draw as source for now` : narrowest === Infinity ? 'diagram failed to render' : `diagram is ${narrowest} cols, terminal is ${columns}` })
742      }
743    }
744  }
745  if (Image) {
746    for (const [i, title] of titles) {
747      const fit = fitHeading(title, title.heading, columns)
748      if (!fit) continue
749      const image = <Image key={`b${i}`} source={title.png ? { png: title.png } : { file: title.file, format: 'png' }} columns={fit.columns} rows={fit.rows} alt={title.heading.lines.join(' ')} />
750      const rule = title.heading.level === 2 && style.headingStyle === 'banner' ? <el.Text key="rule" color={title.heading.color}>{'━'.repeat(fit.columns)}</el.Text> : null
751      drawn.set(i, { element: rule ? <el.Box key={`b${i}`} flexDirection="column" alignSelf="flex-start">{image}{rule}</el.Box> : image })
752    }
753    for (const [i, formula] of math) {
754      const picked = pickFormula(formula, columns)
755      if (picked) drawn.set(i, { element: <Image key={`b${i}`} source={{ png: picked.picture.png }} columns={picked.fit.columns} rows={picked.fit.rows} alt={formula.tex} /> })
756    }
757  }
758  for (const [i, block] of blocks.entries()) {
759    const tex = drawn.has(i) ? null : mathOf(block)
760    const text = tex === null ? null : texText(tex)
761    if (text && !text.includes('\\')) drawn.set(i, { element: <el.Box key={`b${i}`} paddingLeft={2}><el.Text italic color={style.theme.emphasis}>{text}</el.Text></el.Box> })
762  }
763  const elements = renderBlocks(el, style, blocks, columns, drawn, copy, folder($, el, open))
764  const button = reply === undefined ? null : copy(reply, 'reply', '⧉ copy reply')
765  return button ? [...elements, <el.Box key="reply" alignSelf="flex-end">{button}</el.Box>] : elements
766}
767
768export const register: Register = (on, options) => {
769  on('engine.create', async (_$, e, next) => ({ ...(await next(e)), monocle: { markdown: async () => undefined } }))
770  if (options.enabled === false) return
771  let themeName = pickTheme(options.theme)
772  const style = resolveStyle({ ...options, theme: themeName })
773  const parsed = new Map<string, ReturnType<typeof parse>>()
774  const parseCached = (text: string, cache = parsed, limit?: number) => remember(cache, text, () => parse(text, { numbers: style.highlightNumbers, paths: style.highlightPaths }), limit)
775  const shared = new Map<string, ReturnType<typeof parse>>()
776  let terminal: Terminal | null = null
777  const fit = (viewport?: { isFullscreen?: boolean }): Style => (terminal === 'apple-terminal' && viewport?.isFullscreen ? { ...style, shape: 'inverse' } : style)
778  const mermaid: MermaidSession = painter(style, 'mermaid', MAX_DIAGRAMS)
779  const headed = painter(style, 'headings', MAX_HEADINGS)
780  zoomable.session = mermaid
781  const latex: LatexSession = { style, color: ratexColor(style.imageTheme.diagramText ?? style.imageTheme.codeText ?? '#808080'), pending: new Set(), wanted: new Set(), batch: [], queue: Promise.resolve() }
782  const forSurface = (base: Style, surface: RenderSurface): Style => (surface === 'terminal' || !base.htmlCopy ? base : { ...base, htmlCopy: null })
783  const fullWidth = (text: string) => (style.reorder && hasRtl(text) ? { width: '100%' as const } : {})
784  const probes: Probes = {}
785  const tones: Tones = { palette: null }
786
787  const quiet = style.toolOutput === 'quiet'
788  if (options.toolRows !== false) {
789    on('ui.render', { component: 'ToolGroup' }, async ($, e, next) => {
790      await locate($, style)
791      if (quiet) for (const call of e.props.calls) if (call.tool_use_id && isReadOnlyCall(call.tool, call.input)) rememberCall(readOnlyCalls, call.tool_use_id)
792      if (e.props.isExpanded) {
793        for (const call of e.props.calls) if (call.tool_use_id) markExpanded(call.tool_use_id)
794        return next(e)
795      }
796      return renderToolGroup($.ui.resolve(e), fit(e.viewport), e.props.calls, e.props.isActive, e.viewport?.columns)
797    })
798    on('ui.render', { component: 'ToolUse' }, async ($, e, next) => {
799      await locate($, style)
800      if (quiet && isReadOnlyCall(e.props.tool, e.props.input)) rememberCall(readOnlyCalls, e.props.tool_use_id)
801      if (!expandedCalls.has(e.props.tool_use_id)) return renderToolRow($.ui.resolve(e), fit(e.viewport), e.props, e.viewport?.columns)
802      return e.props.tool === 'Bash' || e.props.tool === 'PowerShell' ? renderExpandedShell($.ui.resolve(e), fit(e.viewport), e.props) : next(e)
803    })
804    on('ui.render', { component: 'ToolResult' }, async ($, e, next) => {
805      if ((e.props.tool === 'Edit' || e.props.tool === 'Write') && !e.props.isErrored) {
806        const el = $.ui.resolve(e)
807        return renderDiffCard(el, fit(e.viewport), e.props.output, folder($, el, await read($, unfolded)), e.props.tool_use_id) ?? next(e)
808      }
809      if (!quiet) return next(e)
810      await locate($, style)
811      const el = $.ui.resolve(e)
812      const shell = e.props.tool === 'Bash' || e.props.tool === 'PowerShell'
813      const out = (e.props.output ?? {}) as Record<string, unknown>
814      if (shell && (out.backgroundTaskId || out.isImage || out.interrupted)) return next(e)
815      if (e.props.isErrored) return renderFailure(el, fit(e.viewport), e.props.output) ?? next(e)
816      if (readOnlyCalls.has(e.props.tool_use_id)) return <el.Box />
817      return shell ? renderShellResult(el, fit(e.viewport), e.props.output) : next(e)
818    })
819  }
820
821  on('session.start', async ($, e, next) => {
822    terminal = await probe($, style, probes)
823    await refreshTones($, style, latex, tones)
824    void latexEngine($, latex)
825    void painterEngine($, mermaid)
826    void painterEngine($, headed)
827    const started = await next(e)
828    await $.command
829      .register({ name: 'monocle', description: 'Switch the monocle theme, copy the last reply, show the demo, or check dependencies', argumentHint: '[theme [<name> | random] | copy [code] | demo | doctor]' })
830      .catch(() => undefined)
831    return started
832  })
833
834  on('command.run', { command: 'monocle' }, async ($, e) => {
835    const [first, second] = e.args.trim().split(/\s+/)
836    const themeArgs = [...PRESET_NAMES, 'random']
837    const [sub, name] = themeArgs.includes(first ?? '') ? ['theme', first] : [first, second]
838    if (sub === 'demo') return { text: showcaseText(PRESET_NAMES) }
839    if (sub === 'doctor') return { text: doctorText(await doctorFacts($, style)) }
840    if (sub === 'copy') {
841      const reply = (await $.session.messages()).findLast(m => m.role === 'assistant' && m.text.trim())
842      if (!reply) return { text: 'Nothing to copy yet.' }
843      const code = name === 'code' ? parseCached(reply.text).flatMap(b => (b.kind === 'list' ? b.items.flatMap(it => it.blocks ?? []) : [b])).findLast(b => b.kind === 'code') : undefined
844      if (name === 'code' && code?.kind !== 'code') return { text: 'The last reply has no code block.' }
845      const result = await $.ui.copy({ text: code?.kind === 'code' ? code.lines.join('\n') : reply.text })
846      return { text: result.isCopied ? `Copied the last ${code ? 'code block' : 'reply'}.` : `Copy failed: ${result.reason}` }
847    }
848    if (sub === 'demo-rtl') {
849      terminal = await probe($, style, probes)
850      return { text: rtlShowcaseText() }
851    }
852    if (sub !== 'theme') return { text: helpText(PRESET_NAMES) }
853    if (!name) return { text: options.theme === 'random' ? `current: ${themeName} (random)\nkeep it: /monocle theme ${themeName}` : `current: ${themeName}` }
854    if (!themeArgs.includes(name)) return { text: `Unknown theme "${name}". Themes: ${PRESET_NAMES.join(', ')}, random` }
855    const result = await $.config.set({ key: `${$.plugin.name}.theme`, value: name })
856    if (result.deny) return { text: `Could not switch theme: ${result.deny}` }
857    if (name !== 'random' || options.theme !== 'random') return { text: `Theme set to ${name}.` }
858    const others = RANDOM_THEMES.filter(t => t !== themeName)
859    themeName = others[Math.floor(Math.random() * others.length)]!
860    Object.assign(style, resolveStyle({ ...options, theme: themeName }))
861    tones.palette = null
862    $.ui.invalidate('ui.render')
863    return { text: `Theme set to random: ${themeName} for now.\nkeep it: /monocle theme ${themeName}` }
864  })
865
866  on('ui.render', { component: 'TurnDuration' }, async ($, e) => {
867    const at = style.clock === 'off' ? undefined : endedAt(await read($, turnEnds), e.props.durationMs, await firstDrawn($, e.requestId))
868    const time = at === undefined ? undefined : clockText(at, await $.clock.now(), style.clock)
869    return renderTurnDuration($.ui.resolve(e), style, e.props.word, e.props.durationMs, time)
870  })
871
872  on('turn.complete', async ($, e, next) => {
873    if (style.clock !== 'off' && e.agentId === undefined) {
874      const at = await $.clock.now()
875      await update($, turnEnds, list => withRecord(list, { durationMs: e.durationMs, at }))
876    }
877    return next(e)
878  })
879
880  on('prompt.submit', async ($, e, next) => {
881    if (style.clock !== 'off' && (e.origin.kind === 'composer' || e.origin.kind === 'bridge')) {
882      const at = await $.clock.now()
883      await update($, sent, list => withRecord(list, { text: promptKey(e.text), at }))
884    }
885    terminal = await probe($, style, probes)
886    void refreshTones($, style, latex, tones)
887    if (!style.diagramHints || (e.origin.kind !== 'composer' && e.origin.kind !== 'bridge')) return next(e)
888    void latexEngine($, latex)
889    void painterEngine($, mermaid)
890    const hints = [HINT, ...(mermaid.ready && !mermaid.stopped ? [MERMAID_HINT] : []), ...(latex.ready && !latex.stopped ? [LATEX_HINT] : [])]
891    return next({ ...e, context: [...(e.context ?? []), ...hints] })
892  })
893
894  on('ui.render', { component: 'CommandOutput' }, async ($, e, next) => {
895    if (e.props.isErrored) return next(e)
896    if (e.props.text.includes('\u001b')) return next(e)
897    const blocks = parseCached(e.props.text)
898    if (blocks.length === 0) return next(e)
899    const el = $.ui.resolve(e)
900    const { Box } = el
901    style.zoomLoad = await read($, zoomLoad)
902    const math = await mathOfBlocks($, latex, e.surface, blocks)
903    const pictures = await diagramsOfBlocks($, mermaid, e.surface, blocks)
904    const titles = await headingsOfBlocks($, headed, e.surface, blocks, columnsOf(e.viewport))
905    const open = await read($, unfolded)
906    return <Box flexDirection="column" rowGap={1} {...fullWidth(e.props.text)}>{drawMarkdown($, el, forSurface(fit(e.viewport), e.surface), blocks, columnsOf(e.viewport), math, undefined, open, pictures, titles)}</Box>
907  })
908
909  on('ui.render', { component: 'UserMessage' }, async ($, e, next) => {
910    const kind = e.props.origin.kind
911    const own = kind === 'composer' || kind === 'bridge' || (kind === 'unclassified' && !e.props.from && !e.props.task)
912    if (!own) return next(e)
913    const at = style.clock === 'off' ? undefined : sentAt(await read($, sent), e.props.text, await firstDrawn($, e.requestId))
914    const time = at === undefined ? undefined : clockText(at, await $.clock.now(), style.clock)
915    const prompt = style.promptStyle === 'off'
916      ? await (time === undefined ? next(e) : next({ ...e, props: { ...e.props, text: `[${time}] ${e.props.text}` } }))
917      : renderUserPrompt($.ui.resolve(e), fit(e.viewport), e.props.text, columnsOf(e.viewport), time)
918    const el = $.ui.resolve(e)
919    style.zoomLoad = await read($, zoomLoad)
920    const pasted = e.surface === 'terminal' && style.images ? drawPreviews($, el, style, await previewsOf($, e.props.text), columnsOf(e.viewport)) : null
921    return pasted ? <el.Box flexDirection="column" rowGap={1}>{prompt}{pasted}</el.Box> : prompt
922  })
923
924  on('ui.render', { component: 'AssistantMessage' }, async ($, e, next) => {
925    const blocks = parseCached(e.props.text)
926    if (blocks.length === 0) return next(e)
927    const el = $.ui.resolve(e)
928    const { Box, Text } = el
929    const narration = style.toolStyle === 'tree-bold' && blocks.length === 1 && blocks[0]!.kind === 'paragraph'
930    style.zoomLoad = await read($, zoomLoad)
931    const math = await mathOfBlocks($, latex, e.surface, blocks)
932    const pictures = await diagramsOfBlocks($, mermaid, e.surface, blocks)
933    const titles = await headingsOfBlocks($, headed, e.surface, blocks, columnsOf(e.viewport))
934    const surfaceStyle = forSurface(fit(e.viewport), e.surface)
935    const open = await read($, unfolded)
936    return (
937      <Box flexDirection="row">
938        <Box width={2} flexShrink={0}>
939          <Text color={style.theme.accent}>{e.props.isFirstOfReply ? '●' : ' '}</Text>
940        </Box>
941        <Box flexDirection="column" rowGap={1} flexGrow={1}>
942          {drawMarkdown($, el, narration ? { ...surfaceStyle, narration } : surfaceStyle, blocks, columnsOf(e.viewport), math, blocks.length > 1 || hasRtl(e.props.text) ? e.props.text : undefined, open, pictures, titles)}
943        </Box>
944      </Box>
945    )
946  })
947
948  on('ui.message', async ($, e, next) => {
949    const data = e.data as { zoom?: unknown; loaded?: unknown } | null
950    if (data && typeof data === 'object' && typeof data.loaded === 'string') {
951      const loaded = data.loaded
952      await update($, zoomLoad, () => loaded)
953      return {}
954    }
955    if (!data || typeof data !== 'object' || typeof data.zoom !== 'string') return next(e)
956    void openZoom($, data.zoom)
957    return {}
958  })
959
960  on('monocle.markdown', ($, e, next) => {
961    const blocks = parseCached(e.text, shared, 20)
962    if (blocks.length === 0) return next(e)
963    const el = $.ui.resolve({ surface: e.surface, component: 'AssistantMessage' })
964    const { Box } = el
965    return { value: <Box flexDirection="column" rowGap={1} {...fullWidth(e.text)}>{drawMarkdown($, el, { ...style, copyButtons: false }, blocks, Math.max(20, e.columns || 0))}</Box> }
966  })
967}
968
hooks/clock.ts 39 lines
1import type { Clock } from './theme'
2
3export type Sent = { text: string; at: number }
4export type TurnEnd = { durationMs: number; at: number }
5
6const KEEP = 500
7const SLACK_MS = 2000
8const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
9
10const two = (n: number) => String(n).padStart(2, '0')
11
12export const clockText = (at: number, now: number, clock: Clock): string => {
13  const d = new Date(at)
14  const h = d.getHours()
15  const time = clock === '12h' ? `${h % 12 === 0 ? 12 : h % 12}:${two(d.getMinutes())} ${h < 12 ? 'AM' : 'PM'}` : `${two(h)}:${two(d.getMinutes())}`
16  const today = new Date(now)
17  const isToday = d.getFullYear() === today.getFullYear() && d.getMonth() === today.getMonth() && d.getDate() === today.getDate()
18  return isToday ? time : `${MONTHS[d.getMonth()]} ${d.getDate()} ${time}`
19}
20
21export const promptKey = (text: string) => text.trim().replace(/\s+/g, ' ').slice(0, 300)
22
23export const withRecord = <T>(list: readonly T[], record: T): T[] => [...list, record].slice(-KEEP)
24
25export const sentAt = (sent: readonly Sent[], text: string, drawnAt: number): number | undefined => {
26  const key = promptKey(text)
27  return sent.findLast(one => one.text === key && one.at <= drawnAt)?.at
28}
29
30export const endedAt = (ends: readonly TurnEnd[], durationMs: number, drawnAt: number): number | undefined => {
31  let best: TurnEnd | undefined
32  for (const end of ends) {
33    if (end.at > drawnAt) continue
34    const gap = Math.abs(end.durationMs - durationMs)
35    if (gap <= SLACK_MS && (best === undefined || gap <= Math.abs(best.durationMs - durationMs))) best = end
36  }
37  return best?.at
38}
39
hooks/diagram.ts 145 lines
1import type { Block } from './markdown'
2import type { Theme } from './theme'
3import { ratexColor } from './latex'
4
5export type Picture = { png?: string; file: string; width: number; height: number }
6
7export type Diagram = Picture | { error: true }
8
9type Fit = { columns: number; rows: number }
10
11export const MMDC_SCALE = 2
12const PX_PER_ROW = 22 * MMDC_SCALE
13const CELL_RATIO = 0.46
14const MAX_ROWS = 60
15const MIN_SCALE = 0.5
16export const MAX_DIAGRAMS = 40
17const MAX_SOURCE = 20000
18const MAX_INLINE_BYTES = 2 * 1024 * 1024
19const DIAGRAM_TYPES = ['flowchart', 'sequence', 'gantt', 'journey', 'timeline', 'class', 'state', 'er', 'pie', 'quadrantChart', 'xyChart', 'requirement', 'mindmap', 'kanban', 'gitGraph', 'c4', 'sankey', 'packet', 'block', 'radar', 'architecture']
20
21export const OFFLINE_CHROME = ['--proxy-server=127.0.0.1:9', '--proxy-bypass-list=<-loopback>', '--host-resolver-rules=MAP * ~NOTFOUND']
22
23export const puppeteerConfig = (executablePath?: string): Record<string, unknown> => ({ ...(executablePath ? { executablePath } : {}), args: OFFLINE_CHROME })
24
25export const BROWSERS = [
26  '/usr/bin/chromium',
27  '/usr/bin/chromium-browser',
28  '/usr/bin/google-chrome-stable',
29  '/usr/bin/google-chrome',
30  '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
31  '/Applications/Chromium.app/Contents/MacOS/Chromium',
32]
33
34export const mermaidOf = (block: Block): string | null => {
35  if (block.kind !== 'code' || block.lang.toLowerCase() !== 'mermaid' || block.isOpen) return null
36  const source = block.lines.join('\n').trim()
37  return source === '' || source.length > MAX_SOURCE ? null : source
38}
39
40const channels = (color: string): [number, number, number] | null => {
41  const hex = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(ratexColor(color))?.[1]
42  if (!hex) return null
43  const full = hex.length === 3 ? [...hex].map(c => c + c).join('') : hex
44  return [0, 2, 4].map(i => parseInt(full.slice(i, i + 2), 16)) as [number, number, number]
45}
46
47export const colorHex = (color: string | undefined, fallback: string): string => hexOf(color, fallback)
48
49const hexOf = (color: string | undefined, fallback: string): string => {
50  const rgb = color ? channels(color) : null
51  return rgb ? `#${rgb.map(n => n.toString(16).padStart(2, '0')).join('')}` : fallback
52}
53
54const luminance = (color: string): number => {
55  const rgb = channels(color)
56  return rgb ? (0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]) / 255 : 1
57}
58
59export const mermaidConfig = (theme: Theme): Record<string, unknown> => {
60  const text = hexOf(theme.diagramText ?? theme.codeText, '#808080')
61  const darkMode = luminance(text) > 0.5
62  const line = hexOf(theme.diagram ?? theme.accent, text)
63  const fill = hexOf(theme.rule ?? theme.tableRule, darkMode ? '#3a3a3a' : '#e0e0e0')
64  const border = hexOf(theme.tableRule ?? theme.diagram, line)
65  const palette = [...new Set([theme.link, theme.number, theme.heading, theme.emphasis, theme.path, theme.codeFlag, theme.accent].flatMap(c => (c && channels(c) ? [hexOf(c, text)] : [])))]
66  const pies = Object.fromEntries((palette.length ? palette : [line]).flatMap((c, i, all) => [i, i + all.length].filter(n => n < 12).map(n => [`pie${n + 1}`, c])))
67  return {
68    ...Object.fromEntries(DIAGRAM_TYPES.map(type => [type, { useMaxWidth: false }])),
69    theme: 'base',
70    themeVariables: {
71      darkMode,
72      fontFamily: 'ui-sans-serif, system-ui, sans-serif',
73      primaryColor: fill,
74      primaryTextColor: text,
75      primaryBorderColor: border,
76      secondaryColor: fill,
77      secondaryTextColor: text,
78      tertiaryColor: fill,
79      tertiaryTextColor: text,
80      mainBkg: fill,
81      nodeBorder: line,
82      nodeTextColor: text,
83      textColor: text,
84      lineColor: line,
85      titleColor: hexOf(theme.heading, text),
86      clusterBkg: fill,
87      clusterBorder: border,
88      edgeLabelBackground: fill,
89      actorBkg: fill,
90      actorBorder: line,
91      actorTextColor: text,
92      actorLineColor: border,
93      signalColor: text,
94      signalTextColor: text,
95      labelBoxBkgColor: fill,
96      labelBoxBorderColor: line,
97      labelTextColor: text,
98      loopTextColor: text,
99      activationBkgColor: fill,
100      activationBorderColor: line,
101      noteBkgColor: fill,
102      noteBorderColor: hexOf(theme.emphasis, line),
103      noteTextColor: text,
104      ...pies,
105      pieTitleTextColor: text,
106      pieSectionTextColor: darkMode ? '#000000' : '#ffffff',
107      pieLegendTextColor: text,
108      pieStrokeColor: fill,
109      pieOuterStrokeColor: border,
110      xyChart: {
111        backgroundColor: 'transparent',
112        titleColor: text,
113        xAxisLabelColor: text,
114        xAxisTitleColor: text,
115        xAxisTickColor: border,
116        xAxisLineColor: border,
117        yAxisLabelColor: text,
118        yAxisTitleColor: text,
119        yAxisTickColor: border,
120        yAxisLineColor: border,
121        plotColorPalette: (palette.length ? palette : [line]).join(','),
122      },
123    },
124  }
125}
126
127export const diagramKey = (themeKey: string, source: string) => `${themeKey}\0${source}`
128
129export const batchMarkdown = (sources: readonly string[]): string => sources.map(s => `\`\`\`mermaid\n${s}\n\`\`\`\n`).join('\n')
130
131export const batchOutput = (dir: string, batch: string, index: number) => `${dir}/${batch}-${index + 1}.png`
132
133export const inlinable = (base64: string): boolean => (base64.length * 3) / 4 <= MAX_INLINE_BYTES
134
135export const fitDiagram = (size: { width: number; height: number }, columns: number, maxRows = MAX_ROWS): Fit | null => {
136  const room = Math.min(255, columns - 2)
137  if (room < 8 || size.width < 1 || size.height < 1) return null
138  const aspect = size.width / size.height / CELL_RATIO
139  const natural = size.height / PX_PER_ROW
140  let rows = Math.max(1, Math.min(255, maxRows, Math.round(natural)))
141  if (Math.round(aspect * rows) > room) rows = Math.max(1, Math.floor(room / aspect))
142  if (rows < natural * MIN_SCALE) return null
143  return { columns: Math.max(1, Math.min(room, Math.round(aspect * rows))), rows }
144}
145
hooks/heading.ts 54 lines
1import type { Theme } from './theme'
2
3export const HEADING_PX_PER_ROW = 34
4const CELL_RATIO = 0.46
5
6export const KITTY_FONT = 'from kitty.config import load_config; from kitty.constants import config_dir; import os; print(load_config(os.path.join(config_dir, "kitty.conf")).font_family.created_from_string)'
7
8export const labelText = (lines: readonly string[]): string =>
9  lines.map((line, i) => {
10    const escaped = line.replace(/\\/g, '\\\\').replace(/%/g, '%%')
11    return i === 0 && escaped.startsWith('@') ? `\\${escaped}` : escaped
12  }).join('\\n')
13
14export const ghosttyFont = (config: string): string | null =>
15  [...config.matchAll(/^font-family[ \t]*=[ \t]*(.*?)[ \t]*$/gm)].map(m => m[1]!.replace(/^"(.*)"$/, '$1')).find(Boolean) ?? null
16
17export const headingColor = (t: Theme, level: number): string | undefined =>
18  [t.heading, t.accent, t.link, t.number, t.emphasis, t.emphasis][level - 1] ?? t.heading
19
20export const headingRows = (level: number): number => (level === 1 ? 3 : level === 2 ? 2 : 0)
21
22export const wrapHeading = (text: string, maxChars: number): string[] => {
23  const lines: string[] = []
24  let line: string[] = []
25  for (const word of text.trim().split(/\s+/).filter(Boolean)) {
26    let chars = [...word]
27    const room = maxChars - line.length - (line.length ? 1 : 0)
28    if (chars.length > room && line.length) {
29      lines.push(line.join(''))
30      line = []
31    }
32    while (chars.length > maxChars) {
33      lines.push(chars.slice(0, maxChars).join(''))
34      chars = chars.slice(maxChars)
35    }
36    line = line.length ? [...line, ' ', ...chars] : chars
37  }
38  if (line.length) lines.push(line.join(''))
39  return lines
40}
41
42export type HeadingSource = { level: number; color: string; lines: string[] }
43
44export const headingSource = (heading: HeadingSource): string => JSON.stringify(heading)
45
46export const parseHeadingSource = (source: string): HeadingSource => JSON.parse(source) as HeadingSource
47
48export const fitHeading = (size: { width: number; height: number }, heading: HeadingSource, columns: number): { columns: number; rows: number } | null => {
49  const rows = heading.lines.length * headingRows(heading.level)
50  if (rows < 1 || size.width < 1 || size.height < 1) return null
51  const width = Math.max(1, Math.round((rows * size.width) / size.height / CELL_RATIO))
52  return width <= Math.min(255, columns) && rows <= 255 ? { columns: width, rows } : null
53}
54
hooks/doctor.ts 87 lines
1export type Platform = 'mac' | 'linux' | 'other'
2
3export type Facts = {
4  platform: Platform
5  terminal: string | null
6  tmux: boolean
7  forced: boolean
8  background: boolean
9  mmdc: string | null
10  browser: string | null
11  magick: string | null
12  fcMatch: string | null
13  font: string | null
14  kittyRemote: boolean | null
15  ratex: string | null
16  latex: boolean
17}
18
19type Row = { feature: string; ok: boolean; status: string; fix?: string }
20
21const install = (platform: Platform, mac: string, linux: string) => (platform === 'mac' ? mac : linux)
22
23const VIEWERS: Record<string, string> = { kitty: 'kitty', ghostty: 'Ghostty', herdr: 'herdr' }
24
25const rows = (f: Facts): Row[] => {
26  const viewer = f.tmux ? undefined : VIEWERS[f.terminal ?? '']
27  const imageRow: Row = f.background
28    ? { feature: 'Images in the terminal', ok: false, status: 'background session, where Claude Code draws no images', fix: 'start Claude Code in the foreground, not with `--bg` or `/bg`' }
29    : f.terminal === 'herdr' && !f.forced && !f.tmux
30    ? { feature: 'Images in the terminal', ok: false, status: 'herdr, which Claude Code does not draw images in by itself', fix: 'start Claude Code with `CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1` inside herdr' }
31    : viewer
32    ? { feature: 'Images in the terminal', ok: true, status: viewer }
33    : {
34        feature: 'Images in the terminal',
35        ok: false,
36        status: f.tmux ? 'inside tmux' : `${f.terminal ?? 'this terminal'} has no kitty graphics`,
37        fix: f.tmux ? 'run Claude Code outside tmux' : 'use kitty, Ghostty or herdr',
38      }
39  const browserFix = install(f.platform, '`brew install --cask google-chrome`', '`npm install -g @mermaid-js/mermaid-cli` fetches one, or install Chromium')
40  return [
41    imageRow,
42    f.mmdc
43      ? f.browser
44        ? { feature: 'Mermaid as images', ok: true, status: `mmdc, ${f.browser}` }
45        : { feature: 'Mermaid as images', ok: false, status: 'mmdc finds no browser', fix: browserFix }
46      : { feature: 'Mermaid as images', ok: false, status: 'mmdc not found', fix: install(f.platform, '`brew install mermaid-cli`', '`npm install -g @mermaid-js/mermaid-cli`') },
47    !f.magick
48      ? { feature: 'Big headings', ok: false, status: 'ImageMagick not found', fix: install(f.platform, '`brew install imagemagick`', '`sudo apt install imagemagick` or `sudo pacman -S imagemagick`') }
49      : !f.fcMatch
50        ? { feature: 'Big headings', ok: false, status: 'fc-match not found', fix: install(f.platform, '`brew install fontconfig`', '`sudo apt install fontconfig` or `sudo pacman -S fontconfig`') }
51        : { feature: 'Big headings', ok: !!f.font, status: f.font ? `${f.magick}, ${f.font}` : 'no bold monospace font found', ...(f.font ? {} : { fix: 'install a monospace font with a bold face' }) },
52    {
53      feature: 'Pasted images',
54      ok: true,
55      status: f.magick ? 'PNG, JPEG, GIF and WebP' : 'PNG only',
56      ...(f.magick ? {} : { fix: 'ImageMagick, as for big headings, shows other formats' }),
57    },
58    f.background
59      ? { feature: 'Click to zoom', ok: false, status: 'not in a background session' }
60      : f.kittyRemote === null
61      ? { feature: 'Click to zoom', ok: false, status: f.terminal === 'herdr' ? 'not inside herdr' : 'kitty only' }
62      : f.kittyRemote
63        ? { feature: 'Click to zoom', ok: true, status: 'kitty remote control' }
64        : { feature: 'Click to zoom', ok: false, status: 'kitty remote control is off', fix: 'add `allow_remote_control socket-only` and `listen_on unix:/tmp/kitty` to kitty.conf, then restart kitty' },
65    !f.latex
66      ? { feature: 'LaTeX math', ok: false, status: 'off', fix: f.ratex ? 'set `latex` to `true` in `/config`' : 'install RaTeX as `ratex-render`, then set `latex` to `true`' }
67      : f.ratex
68        ? { feature: 'LaTeX math', ok: true, status: 'ratex-render' }
69        : { feature: 'LaTeX math', ok: false, status: 'ratex-render not found', fix: 'install RaTeX as `ratex-render` on your PATH' },
70  ]
71}
72
73const cell = (text: string) => text.replace(/\|/g, '\\|')
74
75export const doctorText = (facts: Facts): string => {
76  const table = rows(facts).map(r => `| ${r.feature} | ${r.ok ? '✓' : '✗'} ${cell(r.status)} | ${cell(r.fix ?? '')} |`)
77  return [
78    '## monocle doctor',
79    '',
80    '| Feature | Status | To fix |',
81    '|---|---|---|',
82    ...table,
83    '',
84    'Anything not ready falls back to text, so nothing breaks. After installing, run `/reload`. Full guide: docs/SETUP.md',
85  ].join('\n')
86}
87
hooks/palette.ts 76 lines
1import type { Theme } from './theme'
2
3export type Palette = { colors: string[]; foreground: string; background: string }
4
5const TERMINAL_KEYS: Record<string, number> = { rainbow_red: 1, rainbow_green: 2, rainbow_yellow: 3, rainbow_indigo: 4, rainbow_violet: 5, rainbow_blue: 6, subtle: 8, inactive: 8 }
6const FILLED = ['strong', 'codeText', 'diagramText'] as const
7
8const hexOf = (value: string | undefined): string | null => {
9  const hex = /^#?([0-9a-f]{6})$/i.exec(value?.trim() ?? '')?.[1]
10  return hex ? `#${hex.toLowerCase()}` : null
11}
12
13const paletteOf = (colors: Map<number, string>, foreground: string | null, background: string | null): Palette | null => {
14  const sixteen = Array.from({ length: 16 }, (_, i) => colors.get(i) ?? '')
15  return foreground && background && sixteen.every(Boolean) ? { colors: sixteen, foreground, background } : null
16}
17
18export const kittyPalette = (stdout: string): Palette | null => {
19  const colors = new Map<number, string>()
20  let foreground: string | null = null
21  let background: string | null = null
22  for (const line of stdout.split('\n')) {
23    const [key, value] = line.trim().split(/\s+/)
24    const hex = hexOf(value)
25    if (!key || !hex) continue
26    const index = /^color(\d+)$/.exec(key)?.[1]
27    if (index !== undefined && Number(index) < 16) colors.set(Number(index), hex)
28    if (key === 'foreground') foreground = hex
29    if (key === 'background') background = hex
30  }
31  return paletteOf(colors, foreground, background)
32}
33
34export const ghosttyPalette = (stdout: string): Palette | null => {
35  const colors = new Map<number, string>()
36  let foreground: string | null = null
37  let background: string | null = null
38  for (const line of stdout.split('\n')) {
39    const match = /^\s*([a-z-]+)\s*=\s*(.+?)\s*$/.exec(line)
40    if (!match) continue
41    const [, key, value] = match
42    if (key === 'palette') {
43      const entry = /^(\d+)\s*=\s*(\S+)$/.exec(value!)
44      const hex = hexOf(entry?.[2])
45      if (entry && hex && Number(entry[1]) < 16) colors.set(Number(entry[1]), hex)
46    }
47    if (key === 'foreground') foreground = hexOf(value) ?? foreground
48    if (key === 'background') background = hexOf(value) ?? background
49  }
50  return paletteOf(colors, foreground, background)
51}
52
53const named = (color: string, palette: Palette): string | null => {
54  const index = TERMINAL_KEYS[color.trim()]
55  return index === undefined ? null : palette.colors[index]!
56}
57
58export const usesNames = (theme: Theme): boolean => Object.values(theme).some(color => typeof color === 'string' && color.trim() in TERMINAL_KEYS)
59
60const mix = (from: string, to: string, amount: number): string => {
61  const channel = (hex: string, i: number) => parseInt(hex.slice(1 + i * 2, 3 + i * 2), 16)
62  return `#${[0, 1, 2].map(i => Math.round(channel(from, i) + (channel(to, i) - channel(from, i)) * amount).toString(16).padStart(2, '0')).join('')}`
63}
64
65export const paintTheme = (theme: Theme, palette: Palette, fill: boolean): Theme => {
66  const painted: Theme = Object.fromEntries(Object.entries(theme).map(([token, color]) => [token, color === undefined ? color : (named(color, palette) ?? color)]))
67  if (!fill) return painted
68  for (const token of FILLED) painted[token] ??= palette.foreground
69  painted.rule = mix(palette.background, palette.foreground, 0.16)
70  painted.tableRule = mix(palette.background, palette.foreground, 0.4)
71  return painted
72}
73
74export const samePalette = (a: Palette | null, b: Palette | null): boolean =>
75  a === b || (!!a && !!b && a.foreground === b.foreground && a.background === b.background && a.colors.every((c, i) => c === b.colors[i]))
76
hooks/paste.ts 35 lines
1export type PastedSource = { mediaType: string; data: string }
2
3const IMAGE_REF = /\[Image #(\d+)\]/g
4const CELL_RATIO = 0.46
5const PX_PER_ROW = 22
6export const PREVIEW_ROWS = 12
7
8export const imageRefs = (text: string): number[] => [...text.matchAll(IMAGE_REF)].map(m => Number(m[1]))
9
10type Block = { type: string; [field: string]: unknown }
11type Message = { role: string; content: readonly Block[] }
12
13export const pastedImages = (messages: readonly Message[]): Map<number, PastedSource> => {
14  const found = new Map<number, PastedSource>()
15  for (const message of messages) {
16    if (message.role !== 'user') continue
17    const refs = imageRefs(message.content.flatMap(b => (b.type === 'text' && typeof b.text === 'string' ? [b.text] : [])).join('\n'))
18    const images = message.content.flatMap(b => {
19      const source = b.type === 'image' ? (b.source as { type?: string; media_type?: string; data?: string } | undefined) : undefined
20      return source?.type === 'base64' && source.data && source.media_type ? [{ mediaType: source.media_type, data: source.data }] : []
21    })
22    refs.slice(0, images.length).forEach((id, k) => found.set(id, images[k]!))
23  }
24  return found
25}
26
27export const fitPreview = (size: { width: number; height: number }, columns: number, maxRows = PREVIEW_ROWS): { columns: number; rows: number } | null => {
28  const room = Math.min(255, columns)
29  if (room < 4 || size.width < 1 || size.height < 1) return null
30  const aspect = size.width / size.height / CELL_RATIO
31  let rows = Math.max(1, Math.min(maxRows, Math.ceil(size.height / PX_PER_ROW)))
32  if (Math.round(aspect * rows) > room) rows = Math.max(1, Math.floor(room / aspect))
33  return { columns: Math.max(1, Math.min(room, Math.round(aspect * rows))), rows }
34}
35
hooks/latex.ts 99 lines
1import type { Block } from './markdown'
2
3type Picture = { png: string; width: number; height: number }
4
5export type Formula = (Picture & { padded?: Picture }) | { error: true }
6
7type Size = { width: number; height: number }
8type Fit = { columns: number; rows: number }
9
10export const LATEX_FONT_PX = 80
11export const LATEX_DPR = 1
12const PX_PER_ROW = LATEX_FONT_PX * LATEX_DPR
13const CELL_RATIO = 0.5
14const RATEX_MARGIN_PX = 10
15const MAX_IMAGE_BYTES = 2 * 1024 * 1024
16
17const ANSI16 = ['000000', 'cd0000', '00cd00', 'cdcd00', '0000ee', 'cd00cd', '00cdcd', 'e5e5e5', '7f7f7f', 'ff0000', '00ff00', 'ffff00', '5c5cff', 'ff00ff', '00ffff', 'ffffff']
18const ANSI_NAMES = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']
19
20const hex = (...rgb: number[]): string => `#${rgb.map(n => Math.max(0, Math.min(255, n)).toString(16).padStart(2, '0')).join('')}`
21
22const ansi256 = (n: number): string => {
23  if (n < 16) return `#${ANSI16[n]}`
24  const level = (v: number) => (v ? 55 + v * 40 : 0)
25  if (n < 232) return hex(level(Math.floor((n - 16) / 36)), level(Math.floor((n - 16) / 6) % 6), level((n - 16) % 6))
26  const gray = 8 + (n - 232) * 10
27  return hex(gray, gray, gray)
28}
29
30export const ratexColor = (color: string): string => {
31  const c = color.trim()
32  const rgb = /^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i.exec(c)
33  if (rgb) return hex(...rgb.slice(1).map(Number))
34  const ansi = /^ansi256\((\d+)\)$/i.exec(c)
35  if (ansi) return ansi256(Number(ansi[1]))
36  const name = /^(black|red|green|yellow|blue|magenta|cyan|white|gray|grey)(bright)?$/i.exec(c)
37  if (!name?.[2]) return c
38  const base = name[1]!.toLowerCase()
39  return `#${ANSI16[base === 'gray' || base === 'grey' ? 8 : ANSI_NAMES.indexOf(base) + 8]}`
40}
41
42const MAX_FORMULAS = 50
43
44export const keepFormulas = <T,>(store: Record<string, T>, fresh: readonly (readonly [string, T])[], wanted: ReadonlySet<string>, max = MAX_FORMULAS): Record<string, T> => {
45  const added = new Set(fresh.map(([key]) => key))
46  const entries = [...Object.entries(store).filter(([key]) => !added.has(key)), ...fresh]
47  const spare = entries.filter(([key]) => !wanted.has(key) && !added.has(key)).map(([key]) => key)
48  const dropped = new Set(spare.slice(0, Math.max(0, entries.length - max)))
49  return Object.fromEntries(entries.filter(([key]) => !dropped.has(key)))
50}
51
52export const fitsImage = (base64: string): boolean => (base64.length * 3) / 4 <= MAX_IMAGE_BYTES
53
54const naturalRows = (size: Size): number => size.height / PX_PER_ROW
55
56export const padFor = (size: Size): { rows: number; fontSize: number; dpr: number } | null => {
57  const natural = naturalRows(size)
58  const below = Math.floor(natural)
59  const above = Math.max(1, Math.ceil(natural))
60  if ((below >= 1 && below / natural >= 0.9) || above / natural <= 1.05) return null
61  const dpr = LATEX_DPR + (above * PX_PER_ROW - size.height) / (2 * RATEX_MARGIN_PX)
62  return { rows: above, fontSize: PX_PER_ROW / dpr, dpr }
63}
64
65export const mathOf = (block: Block): string | null => {
66  if (block.kind !== 'code' || block.lang.toLowerCase() !== 'math' || block.isOpen) return null
67  const tex = block.lines.map(line => line.replace(/(^|[^\\])%.*$/, '$1')).join(' ').replace(/\s+/g, ' ').trim()
68  return tex === '' || tex.startsWith('#') ? null : tex
69}
70
71export const pngSize = (base64: string): Size | null => {
72  const head = atob(base64.slice(0, 32))
73  if (!head.startsWith('\x89PNG')) return null
74  const word = (at: number) => [0, 1, 2, 3].reduce((n, i) => n * 256 + head.charCodeAt(at + i), 0)
75  return { width: word(16), height: word(20) }
76}
77
78const rowsOf = (size: Size): number => Math.min(255, Math.max(1, Math.round(naturalRows(size))))
79
80const fitFormula = (size: Size, columns: number): Fit | null => {
81  const aspect = size.width / size.height / CELL_RATIO
82  const room = Math.min(255, columns - 2)
83  let rows = rowsOf(size)
84  while (rows > 1 && Math.round(aspect * rows) > room) rows--
85  const width = Math.max(1, Math.round(aspect * rows))
86  return width <= room ? { columns: width, rows } : null
87}
88
89export const pickFormula = (formula: Exclude<Formula, { error: true }>, columns: number): { picture: Picture; fit: Fit } | null => {
90  if (formula.padded) {
91    const fit = fitFormula(formula.padded, columns)
92    if (fit && fit.rows === rowsOf(formula.padded)) return { picture: formula.padded, fit }
93  }
94  const fit = fitFormula(formula, columns)
95  return fit ? { picture: formula, fit } : null
96}
97
98export const renderedOf = (stdout: string): Set<number> => new Set([...stdout.matchAll(/^OK\s+(\d+)\s/gm)].map(m => Number(m[1])))
99
hooks/markdown.ts 416 lines
1export type Inline =
2  | { kind: 'text'; text: string }
3  | { kind: 'strong'; children: Inline[] }
4  | { kind: 'emphasis'; children: Inline[] }
5  | { kind: 'strike'; children: Inline[] }
6  | { kind: 'code'; text: string }
7  | { kind: 'math'; text: string }
8  | { kind: 'link'; text: string; href: string }
9  | { kind: 'number'; text: string }
10  | { kind: 'path'; text: string }
11  | { kind: 'dim'; text: string }
12  | { kind: 'footnote'; text: string }
13
14export type Block = { raw: string } & (
15  | { kind: 'heading'; level: number; inline: Inline[] }
16  | { kind: 'paragraph'; inline: Inline[] }
17  | { kind: 'list'; ordered: boolean; items: ListItem[] }
18  | { kind: 'code'; lang: string; lines: string[]; isOpen?: true }
19  | { kind: 'quote'; inline: Inline[] }
20  | { kind: 'alert'; level: AlertLevel; inline: Inline[] }
21  | { kind: 'rule' }
22  | { kind: 'table'; header: Inline[][]; align: ('left' | 'right' | 'center')[]; rows: Inline[][][] }
23  | { kind: 'notes'; notes: { mark: string; inline: Inline[] }[] }
24)
25
26export type ListItem = { marker: string; depth: number; task?: boolean; inline: Inline[]; blocks?: Block[] }
27
28type Draft = Block extends infer B ? (B extends unknown ? Omit<B, 'raw'> : never) : never
29
30export type AlertLevel = 'note' | 'tip' | 'important' | 'warning' | 'caution'
31const ALERT = /^\[!(note|tip|important|warning|caution)\]\s*(.*)$/i
32
33export type Highlight = { numbers: boolean; paths: boolean }
34
35const MAX_INLINE = 4000
36const TABLE_SEP = /^\s*\|?\s*:?-{2,}:?\s*(\|\s*:?-{2,}:?\s*)*\|?\s*$/
37const LIST_ITEM = /^(\s*)([-*+]|\d+[.)])\s+(.*)$/
38const FENCE = /^\s*(`{3,}|~{3,})\s*([\w+-]*)/
39const NOTE_DEF = /^\[\^([^\]\s]+)\]:\s*(.*)$/
40const SUPERSCRIPT = '⁰¹²³⁴⁵⁶⁷⁸⁹'
41
42const splitRow = (line: string): string[] => {
43  const trimmed = line.trim().replace(/^\|/, '').replace(/(?<!\\)\|$/, '')
44  const cells: string[] = []
45  let cell = ''
46  for (let i = 0; i < trimmed.length; i++) {
47    const ch = trimmed[i]
48    if (ch === '\\' && trimmed[i + 1] === '|') {
49      cell += '|'
50      i++
51    } else if (ch === '|') {
52      cells.push(cell.trim())
53      cell = ''
54    } else {
55      cell += ch
56    }
57  }
58  cells.push(cell.trim())
59  return cells
60}
61
62const INLINE = /(`+)(?!`)(.+?)(?<!`)\1(?!`)|\[([^\]]+)\]\(([^)\s]+)\)|\*\*([^*]+?)\*\*|(?<![\w_])__([^_]+?)__(?![\w_])|~~([^~]+?)~~|(?<![\w*])\*([^*\s][^*]*?)\*(?!\w)|(?<![\w_])_([^_\s][^_]*?)_(?!\w)|(https?:\/\/[^\s<>()]+[^\s<>().,;:!?'"])|(?<![\\$\w])\$(?=\S)((?:\\.|[^$\n\\])+?)(?<=\S)\$(?![\w$])|\[\^([^\]\s]+)\]/g
63const NUMBER = /(?<![\w.#/-])(v?\d+(?:[.,:]\d+)*(?:%|ms|s|m|h|d|Gi|Mi|GB|MB|KB|x)?)(?![\w/])/g
64const PATH = /(?<![\w/.:])((?:~|\.{1,2})?\/[\w.@+-]+(?:\/[\w.@+-]*)*)/g
65
66const decorate = (text: string, hl: Highlight): Inline[] => {
67  if (!hl.numbers && !hl.paths) return [{ kind: 'text', text }]
68  const marks: { start: number; end: number; kind: 'number' | 'path' }[] = []
69  if (hl.paths) for (const m of text.matchAll(PATH)) marks.push({ start: m.index, end: m.index + m[0].length, kind: 'path' })
70  if (hl.numbers) {
71    for (const m of text.matchAll(NUMBER)) {
72      const start = m.index
73      if (!marks.some(p => start < p.end && start + m[0].length > p.start)) {
74        marks.push({ start, end: start + m[0].length, kind: 'number' })
75      }
76    }
77  }
78  marks.sort((a, b) => a.start - b.start)
79  const out: Inline[] = []
80  let at = 0
81  for (const m of marks) {
82    if (m.start > at) out.push({ kind: 'text', text: text.slice(at, m.start) })
83    out.push({ kind: m.kind, text: text.slice(m.start, m.end) })
84    at = m.end
85  }
86  if (at < text.length) out.push({ kind: 'text', text: text.slice(at) })
87  return out
88}
89
90export const parseInline = (text: string, hl: Highlight): Inline[] => {
91  if (text.length > MAX_INLINE) return [{ kind: 'text', text }]
92  const out: Inline[] = []
93  let at = 0
94  for (const m of text.matchAll(INLINE)) {
95    if (m.index > at) out.push(...decorate(text.slice(at, m.index), hl))
96    if (m[2] !== undefined) out.push({ kind: 'code', text: m[2] })
97    else if (m[3] !== undefined) out.push({ kind: 'link', text: m[3], href: m[4] ?? "" })
98    else if (m[5] !== undefined || m[6] !== undefined) out.push({ kind: 'strong', children: parseInline(m[5] ?? m[6] ?? "", hl) })
99    else if (m[7] !== undefined) out.push({ kind: 'strike', children: parseInline(m[7], hl) })
100    else if (m[8] !== undefined || m[9] !== undefined) out.push({ kind: 'emphasis', children: parseInline(m[8] ?? m[9] ?? "", hl) })
101    else if (m[10] !== undefined) out.push({ kind: 'link', text: m[10], href: m[10] })
102    else if (m[11] !== undefined) out.push(...(isTex(m[11]) ? [{ kind: 'math' as const, text: texText(m[11]) }] : decorate(m[0], hl)))
103    else if (m[12] !== undefined) out.push({ kind: 'footnote', text: m[12] })
104    at = m.index + m[0].length
105  }
106  if (at < text.length) out.push(...decorate(text.slice(at), hl))
107  return out
108}
109
110const TEX: Record<string, string> = {
111  alpha: 'α', beta: 'β', gamma: 'γ', Gamma: 'Γ', delta: 'δ', Delta: 'Δ', epsilon: 'ε', varepsilon: 'ε', zeta: 'ζ', eta: 'η', theta: 'θ', Theta: 'Θ', iota: 'ι', kappa: 'κ', lambda: 'λ', Lambda: 'Λ', mu: 'μ', nu: 'ν', xi: 'ξ', Xi: 'Ξ', pi: 'π', Pi: 'Π', rho: 'ρ', sigma: 'σ', Sigma: 'Σ', tau: 'τ', upsilon: 'υ', phi: 'φ', varphi: 'φ', Phi: 'Φ', chi: 'χ', psi: 'ψ', Psi: 'Ψ', omega: 'ω', Omega: 'Ω',
112  cdot: '·', times: '×', div: '÷', pm: '±', mp: '∓', le: '≤', leq: '≤', ge: '≥', geq: '≥', neq: '≠', ne: '≠', approx: '≈', sim: '∼', equiv: '≡', propto: '∝', infty: '∞', to: '→', rightarrow: '→', leftarrow: '←', Rightarrow: '⇒', iff: '⇔', mapsto: '↦',
113  sum: 'Σ', prod: '∏', int: '∫', iint: '∬', oint: '∮', partial: '∂', nabla: '∇', in: '∈', notin: '∉', subset: '⊂', subseteq: '⊆', cup: '∪', cap: '∩', forall: '∀', exists: '∃', neg: '¬', land: '∧', lor: '∨', emptyset: '∅',
114  ldots: '…', cdots: '⋯', dots: '…', ell: 'ℓ', hbar: 'ℏ', lceil: '⌈', rceil: '⌉', lfloor: '⌊', rfloor: '⌋', langle: '⟨', rangle: '⟩', mid: '∣', quad: ' ', qquad: '  ',
115}
116const FUNCTIONS = new Set(['ln', 'log', 'exp', 'min', 'max', 'sin', 'cos', 'tan', 'lim', 'det', 'sup', 'inf', 'arg', 'deg', 'gcd', 'Pr'])
117const PLAIN = new Set(['text', 'mathrm', 'mathit', 'mathbf', 'textbf', 'texttt', 'mathsf', 'operatorname', 'boldsymbol'])
118const SPACES: Record<string, string> = { ',': ' ', ';': ' ', ':': ' ', ' ': ' ', '!': '', '\\': ' ', '{': '{', '}': '}', '%': '%', '$': '$', '&': '&', '#': '#', '_': '_' }
119const scriptMap = (from: string, to: string) => new Map(Array.from(from).map((c, i) => [c, Array.from(to)[i]!]))
120const SUP = scriptMap('0123456789+-−=()niabcdefghjklmoprstuvwxyzABDEGHIJKLMNOPRTUVW', '⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁻⁼⁽⁾ⁿⁱᵃᵇᶜᵈᵉᶠᵍʰʲᵏˡᵐᵒᵖʳˢᵗᵘᵛʷˣʸᶻᴬᴮᴰᴱᴳᴴᴵᴶᴷᴸᴹᴺᴼᴾᴿᵀᵁⱽᵂ')
121const SUB = scriptMap('0123456789+-−=()aehijklmnoprstuvx', '₀₁₂₃₄₅₆₇₈₉₊₋₋₌₍₎ₐₑₕᵢⱼₖₗₘₙₒₚᵣₛₜᵤᵥₓ')
122const LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
123const FONTS: Record<string, string> = { mathbb: '𝔸𝔹ℂ𝔻𝔼𝔽𝔾ℍ𝕀𝕁𝕂𝕃𝕄ℕ𝕆ℙℚℝ𝕊𝕋𝕌𝕍𝕎𝕏𝕐ℤ', mathcal: '𝒜ℬ𝒞𝒟ℰℱ𝒢ℋℐ𝒥𝒦ℒℳ𝒩𝒪𝒫𝒬ℛ𝒮𝒯𝒰𝒱𝒲𝒳𝒴𝒵' }
124const FN = '\u0000'
125
126const script = (text: string, map: Map<string, string>, mark: string) => {
127  const chars = Array.from(text)
128  const values = new Set(map.values())
129  if (chars.length > 0 && chars.every(c => map.has(c) || values.has(c))) return chars.map(c => map.get(c) ?? c).join('')
130  return mark + (chars.length === 1 ? text : `(${text})`)
131}
132const atomic = (text: string) => /^√?[\p{L}\p{N}.,]+$/u.test(text) || Array.from(text).length === 1
133
134const texUnicode = (tex: string): string => {
135  let i = 0
136  const name = () => {
137    const m = /^[A-Za-z]+/.exec(tex.slice(i))
138    if (m) return (i += m[0].length, m[0])
139    return tex[i++] ?? ''
140  }
141  const arg = (): string => {
142    while (tex[i] === ' ') i++
143    if (tex[i] === '{') {
144      let depth = 0
145      const start = ++i
146      for (; i < tex.length; i++) {
147        if (tex[i] === '\\') i++
148        else if (tex[i] === '{') depth++
149        else if (tex[i] === '}' && depth-- === 0) break
150      }
151      return tex.slice(start, i++)
152    }
153    if (tex[i] === '\\') return (i++, `\\${name()}`)
154    return tex[i++] ?? ''
155  }
156  const out: string[] = []
157  const prev = () => out.join('').slice(-1)
158  while (i < tex.length) {
159    const c = tex[i++]!
160    if (c === '\\') {
161      const cmd = name()
162      if (cmd in SPACES) out.push(SPACES[cmd]!)
163      else if (cmd === 'frac' || cmd === 'dfrac' || cmd === 'tfrac') {
164        const top = texUnicode(arg())
165        const bottom = texUnicode(arg())
166        const frac = `${atomic(top) ? top : `(${top})`}/${atomic(bottom) ? bottom : `(${bottom})`}`
167        const loose = /^$|[\s(=,[]$/.test(prev()) && /^(\s|$|\\right|[)\],])/.test(tex.slice(i))
168        out.push(loose ? frac : `(${frac})`)
169      } else if (cmd === 'sqrt') {
170        const inner = texUnicode(arg())
171        out.push(`√${atomic(inner) ? inner : `(${inner})`}`)
172      } else if (PLAIN.has(cmd)) out.push(cmd === 'text' ? arg() : texUnicode(arg()))
173      else if (cmd in FONTS) out.push(Array.from(arg()).map(l => (LETTERS.includes(l) ? Array.from(FONTS[cmd]!)[LETTERS.indexOf(l)]! : l)).join(''))
174      else if (cmd === 'left' || cmd === 'right' || cmd === 'big' || cmd === 'Big' || cmd === 'bigg') {
175        const d = tex[i] === '\\' ? (i++, name()) : tex[i++] ?? ''
176        out.push(d === '.' ? '' : d === '{' || d === '}' ? d : TEX[d] ?? d)
177      } else if (FUNCTIONS.has(cmd)) out.push(cmd + FN)
178      else out.push(TEX[cmd] ?? `\\${cmd}`)
179    } else if (c === '^' || c === '_') out.push(script(texUnicode(arg()), c === '^' ? SUP : SUB, c))
180    else if (c === '{') {
181      i--
182      out.push(texUnicode(arg()))
183    } else if (c === '}' ) continue
184    else if (c === '~' || c === '&') out.push(' ')
185    else out.push(c)
186  }
187  return out.join('')
188}
189
190const isTex = (tex: string) => /[\\^_={}]/.test(tex) || /^[A-Za-z]$/.test(tex)
191
192export const texText = (tex: string): string =>
193  texUnicode(tex)
194    .replace(new RegExp(`${FN}(?=[\\p{L}\\p{N}])`, 'gu'), ' ')
195    .replaceAll(FN, '')
196    .replace(/\)\(/g, ')·(')
197    .replace(/ {2,}/g, ' ')
198    .trim()
199
200export const inlineText = (inline: Inline[]): string =>
201  inline.map(n => ('children' in n ? inlineText(n.children) : n.text)).join('')
202
203const mathAt = (lines: string[], start: number): { lines: string[]; end: number } | null => {
204  const trimmed = (n: number) => (lines[n] ?? '').trim()
205  const first = trimmed(start)
206  if (!first.startsWith('$$')) return null
207  const open = first.slice(2)
208  let end = start
209  if (!(open.length >= 2 && open.endsWith('$$'))) {
210    end = start + 1
211    while (end < lines.length && !trimmed(end).endsWith('$$')) {
212      if (trimmed(end) === '') return null
213      end++
214    }
215    if (end === lines.length) return null
216  }
217  const body = end === start ? [open.slice(0, -2)] : [open, ...lines.slice(start + 1, end), trimmed(end).slice(0, -2)]
218  const tex = body.filter(line => line.trim() !== '').map((line, i, all) => (i === 0 || i === all.length - 1 ? line.trim() : line))
219  return tex.length ? { lines: tex, end } : null
220}
221
222export const displayText = (inline: Inline[]): string =>
223  inline.map(n => (n.kind === 'link' && n.text !== n.href ? `${n.text} (${n.href})` : 'children' in n ? displayText(n.children) : n.text)).join('')
224
225export const isWebLink = (href: string): boolean => /^(https?:|mailto:)/i.test(href)
226
227const markOf = (n: number) => [...String(n)].map(d => SUPERSCRIPT[Number(d)]).join('')
228
229const numberNotes = (blocks: Block[], defs: Map<string, string>, hl: Highlight): Block[] => {
230  const order = new Map<string, number>()
231  const seen = (nodes: Inline[]): void => nodes.forEach(n => {
232    if (n.kind === 'footnote' && defs.has(n.text) && !order.has(n.text)) order.set(n.text, order.size + 1)
233    else if ('children' in n) seen(n.children)
234  })
235  const fix = (nodes: Inline[]): Inline[] => nodes.map(n =>
236    n.kind === 'footnote' ? (order.has(n.text) ? { kind: 'footnote', text: markOf(order.get(n.text)!) } : { kind: 'text', text: `[^${n.text}]` })
237    : 'children' in n ? { ...n, children: fix(n.children) } : n)
238  const mapInlines = (b: Block, f: (nodes: Inline[]) => Inline[]): Block =>
239    'inline' in b ? { ...b, inline: f(b.inline) }
240    : b.kind === 'list' ? { ...b, items: b.items.map(it => ({ ...it, inline: f(it.inline), ...(it.blocks ? { blocks: it.blocks.map(c => mapInlines(c, f)) } : {}) })) }
241    : b.kind === 'table' ? { ...b, header: b.header.map(f), rows: b.rows.map(r => r.map(f)) }
242    : b
243  blocks.forEach(b => mapInlines(b, nodes => (seen(nodes), nodes)))
244  for (const label of defs.keys()) if (!order.has(label)) order.set(label, order.size + 1)
245  const out = blocks.map(b => mapInlines(b, fix))
246  const notes = [...defs].sort(([a], [b]) => order.get(a)! - order.get(b)!).map(([label, text]) => ({ mark: markOf(order.get(label)!), inline: fix(parseInline(text, hl)) }))
247  return defs.size ? [...out, { kind: 'notes', notes, raw: '' }] : out
248}
249
250const CONTROL = /\u001b\][^\u0007\u001b]*(?:\u0007|\u001b\\)?|\u001b\[[0-?]*[ -/]*[@-~]|\u001b[@-_]?|[\u0000-\u0008\u000b-\u001f\u007f]/g
251
252export const parse = (raw: string, hl: Highlight): Block[] => {
253  const source = raw.replace(CONTROL, '')
254  const blocks = parseBlocks(source, hl)
255  return source.includes('[^') ? numberNotes(blocks, notesOf(source), hl) : blocks
256}
257
258const notesOf = (source: string): Map<string, string> => {
259  const defs = new Map<string, string>()
260  const lines = source.replace(/\r\n?/g, '\n').split('\n')
261  let fenced = false
262  let last: string | undefined
263  for (const line of lines) {
264    if (FENCE.test(line)) fenced = !fenced
265    const def = fenced ? null : NOTE_DEF.exec(line)
266    if (def) {
267      last = def[1]!
268      if (!defs.has(last)) defs.set(last, def[2] ?? '')
269    } else if (last !== undefined && /^\s{2,}\S/.test(line)) defs.set(last, `${defs.get(last)} ${line.trim()}`)
270    else last = undefined
271  }
272  return defs
273}
274
275const indentOf = (line: string): number => (line.match(/^\s*/)?.[0] ?? '').replace(/\t/g, '  ').length
276
277const parseBlocks = (source: string, hl: Highlight): Block[] => {
278  const lines = source.replace(/\r\n?/g, '\n').split('\n')
279  const at = (n: number) => lines[n] ?? ''
280  const blocks: Block[] = []
281  const add = (block: Draft, from: number, to: number) => blocks.push({ ...block, raw: lines.slice(from, to).join('\n') } as Block)
282  let paraStart = 0
283  let para: string[] = []
284
285  const flush = (end: number) => {
286    if (para.length) add({ kind: 'paragraph', inline: parseInline(para.join('\n'), hl) }, paraStart, end)
287    para = []
288  }
289
290  for (let i = 0; i < lines.length; i++) {
291    const line = at(i)
292    const fence = FENCE.exec(line)
293    if (fence) {
294      flush(i)
295      const start = i
296      const run = fence[1] ?? '```'
297      const closer = new RegExp(`^\\s*\\${run[0]}{${run.length},}\\s*$`)
298      const body: string[] = []
299      i++
300      while (i < lines.length && !closer.test(at(i))) body.push(at(i++))
301      add({ kind: 'code', lang: fence[2] ?? '', lines: body, ...(i < lines.length ? {} : { isOpen: true as const }) }, start, i + 1)
302      continue
303    }
304    const math = mathAt(lines, i)
305    if (math) {
306      flush(i)
307      add({ kind: 'code', lang: 'math', lines: math.lines }, i, math.end + 1)
308      i = math.end
309      continue
310    }
311    if (line.trim() === '') {
312      flush(i)
313      continue
314    }
315    if (NOTE_DEF.test(line)) {
316      flush(i)
317      while (/^\s{2,}\S/.test(at(i + 1))) i++
318      continue
319    }
320    const heading = /^(#{1,6})\s+(.*?)\s*#*\s*$/.exec(line)
321    if (heading) {
322      flush(i)
323      add({ kind: 'heading', level: (heading[1] ?? '#').length, inline: parseInline(heading[2] ?? '', hl) }, i, i + 1)
324      continue
325    }
326    if (/^\s*([-*_])(\s*\1){2,}\s*$/.test(line)) {
327      flush(i)
328      add({ kind: 'rule' }, i, i + 1)
329      continue
330    }
331    if (line.includes('|') && i + 1 < lines.length && TABLE_SEP.test(at(i + 1))) {
332      flush(i)
333      const start = i
334      const header = splitRow(line)
335      const align = splitRow(at(i + 1)).map(c =>
336        c.startsWith(':') && c.endsWith(':') ? 'center' : c.endsWith(':') ? 'right' : 'left',
337      )
338      i += 2
339      const rows: Inline[][][] = []
340      while (i < lines.length && at(i).includes('|') && at(i).trim() !== '') {
341        const cells = splitRow(at(i++))
342        rows.push(header.map((_, c) => parseInline(cells[c] ?? '', hl)))
343      }
344      add({ kind: 'table', header: header.map(c => parseInline(c, { numbers: false, paths: false })), align, rows }, start, i)
345      i--
346      continue
347    }
348    if (/^\s*>/.test(line)) {
349      flush(i)
350      const start = i
351      const body: string[] = []
352      while (i < lines.length && /^\s*>/.test(at(i))) body.push(at(i++).replace(/^\s*>\s?/, ''))
353      const alert = ALERT.exec(body[0] ?? '')
354      if (alert) add({ kind: 'alert', level: alert[1]!.toLowerCase() as AlertLevel, inline: parseInline([alert[2]!, ...body.slice(1)].join(' ').trim(), hl) }, start, i)
355      else add({ kind: 'quote', inline: parseInline(body.join(' '), hl) }, start, i)
356      i--
357      continue
358    }
359    const item = LIST_ITEM.exec(line)
360    if (item) {
361      flush(i)
362      const start = i
363      const ordered = /\d/.test(item[2] ?? '')
364      const items: ListItem[] = []
365      const contentIndent: number[] = []
366      while (i < lines.length) {
367        const it = LIST_ITEM.exec(at(i))
368        if (it) {
369          contentIndent.push((it[1] ?? '').replace(/\t/g, '  ').length + (it[2] ?? '').length + 1)
370          const task = /^\[([ xX])\]\s+(.*)$/.exec(it[3] ?? '')
371          items.push({ marker: it[2] ?? '-', depth: Math.floor((it[1] ?? '').replace(/\t/g, '  ').length / 2), ...(task ? { task: task[1] !== ' ' } : {}), inline: parseInline(task ? task[2]! : it[3] ?? '', hl) })
372        } else if (/^\s{2,}\S/.test(at(i)) && items.length) {
373          const indent = indentOf(at(i))
374          let owner = items.length - 1
375          while (owner > 0 && contentIndent[owner]! > indent) owner--
376          const target = items[owner]!
377          const fence = FENCE.exec(at(i))
378          const last = target.blocks?.at(-1)
379          if (fence) {
380            const from = i
381            const run = fence[1] ?? '```'
382            const closer = new RegExp(`^\\s*\\${run[0]}{${run.length},}\\s*$`)
383            const strip = new RegExp(`^ {0,${indent}}`)
384            const body: string[] = []
385            i++
386            while (i < lines.length && !closer.test(at(i)) && (at(i).trim() === '' || indentOf(at(i)) >= indent)) body.push(at(i++).replace(strip, ''))
387            const isOpen = i === lines.length
388            if (!isOpen && !closer.test(at(i))) {
389              while (body.at(-1)?.trim() === '') body.pop()
390              i--
391            }
392            target.blocks = [...(target.blocks ?? []), { kind: 'code', lang: fence[2] ?? '', lines: body, ...(isOpen ? { isOpen: true as const } : {}), raw: lines.slice(from, i + 1).join('\n') }]
393          } else if (last?.kind === 'paragraph') {
394            last.inline = [...last.inline, { kind: 'text', text: ' ' }, ...parseInline(at(i).trim(), hl)]
395            last.raw += `\n${at(i)}`
396          } else if (last) {
397            target.blocks!.push({ kind: 'paragraph', inline: parseInline(at(i).trim(), hl), raw: at(i) })
398          } else {
399            target.inline = [...target.inline, { kind: 'text', text: ' ' }, ...parseInline(at(i).trim(), hl)]
400          }
401        } else {
402          break
403        }
404        i++
405      }
406      add({ kind: 'list', ordered, items }, start, i)
407      i--
408      continue
409    }
410    if (!para.length) paraStart = i
411    para.push(line.trim())
412  }
413  flush(lines.length)
414  return blocks
415}
416
hooks/clipboard.ts 31 lines
1export const MAC_TABLE_COPY = `
2ObjC.import('AppKit')
3var input = $.NSFileHandle.fileHandleWithStandardInput.readDataToEndOfFile
4var payload = JSON.parse(ObjC.unwrap($.NSString.alloc.initWithDataEncoding(input, $.NSUTF8StringEncoding)))
5var item = $.NSPasteboardItem.alloc.init
6if (!item.setStringForType(payload.html, $.NSPasteboardTypeHTML) ||
7    !item.setStringForType(payload.text, $.NSPasteboardTypeString)) throw new Error('Cannot encode table')
8var pasteboard = $.NSPasteboard.generalPasteboard
9if (!pasteboard || !pasteboard.writeObjects) throw new Error('macOS clipboard is unavailable to this session')
10pasteboard.clearContents
11if (!pasteboard.writeObjects($.NSArray.arrayWithObject(item))) throw new Error('Cannot write clipboard')
12if (ObjC.unwrap(pasteboard.stringForType($.NSPasteboardTypeHTML)) !== payload.html ||
13    ObjC.unwrap(pasteboard.stringForType($.NSPasteboardTypeString)) !== payload.text) throw new Error('Clipboard verification failed')
14`
15
16export const LINUX_TABLE_COPY = `
17var table = JSON.parse(str(arguments[1]))
18copy('text/html', table.html, 'text/plain', table.text)
19if (str(clipboard('text/html')) !== table.html ||
20    str(clipboard('text/plain')) !== table.text) throw new Error('Clipboard verification failed')
21`
22
23export type ClipboardBackend = 'macos' | 'linux'
24
25export const clipboardCommand = (backend: ClipboardBackend, html: string, text: string) => ({
26  stdin: JSON.stringify({ html, text }),
27  ...backend === 'macos'
28    ? { argv: ['/usr/bin/osascript', '-l', 'JavaScript', '-e', MAC_TABLE_COPY], failure: 'macOS clipboard helper failed' }
29    : { argv: ['copyq', 'eval', LINUX_TABLE_COPY, '-'], failure: 'CopyQ failed; install and start CopyQ in the graphical session' },
30})
31
hooks/mermaid.tsx 199 lines
1import type { ElementTable, RenderElement } from 'claude-code'
2
3import { cells, graphemes, remember, width } from './render'
4import type { Style, Theme } from './theme'
5import { renderMermaidAscii, setChartSize } from './vendor/mermaid-text.js'
6
7const MAX_LINES = 80
8const PAD = '\uFDD0'
9const textCache = new Map<string, string | null>()
10
11export const chartSize = (columns: number, source = '') => {
12  const labels = (/^\s*x-axis\b[^[\n]*\[([^\]\n]*)\]/m.exec(source)?.[1] ?? '').split(',').map(s => s.trim().replace(/^"|"$/g, ''))
13  const fit = labels.length * (Math.max(...labels.map(l => l.length)) + 2)
14  const width = Math.max(24, Math.min(60, Math.max(Math.floor(columns / 6), fit), columns - 12))
15  return { width, height: Math.max(8, Math.min(20, Math.round(width * 0.3))) }
16}
17
18const padWide = (source: string) => (/^[\x00-\x7F]*$/.test(source) ? source : graphemes(source).map(g => (g.length === 1 && width(g) === 2 ? g + PAD : g)).join(''))
19
20const unquoteCategories = (source: string) =>
21  source.replace(/^(\s*x-axis\b[^[\n]*\[)([^\]\n]*)\]/m, (_, head: string, items: string) => `${head}${items.replace(/"([^"]*)"/g, (_q, s: string) => s.replaceAll(',', ' '))}]`)
22
23const labelBars = (art: string, source: string): string => {
24  const series = source.match(/^\s*(bar|line)\b.*$/gm) ?? []
25  const values = /^\s*bar\b[^[\n]*\[([^\]\n]*)\]/m.exec(source)?.[1]?.split(',').map(v => v.trim())
26  if (series.length !== 1 || !values || /^\s*xychart(-beta)?\s+horizontal/m.test(source)) return art
27  const grid = art.split('\n').map(l => [...l])
28  const axis = grid.findLastIndex(row => row.includes('┬'))
29  const ticks = grid[axis]?.flatMap((ch, x) => (ch === '┬' ? [x] : [])) ?? []
30  if (ticks.length !== values.length) return art
31  ticks.forEach((x, k) => {
32    const top = grid.findIndex(row => row[x] === '█')
33    const y = top === -1 ? axis - 1 : top - 1
34    const text = [...values[k]!]
35    const from = x - Math.floor((text.length - 1) / 2)
36    const row = grid[y]
37    if (!row || y < 0) return
38    while (row.length < from + text.length) row.push(' ')
39    if (!text.every((_, i) => /[ ·]/.test(row[from + i] ?? ' '))) return
40    text.forEach((ch, i) => (row[from + i] = ch))
41  })
42  return grid.map(row => row.join('').trimEnd()).join('\n')
43}
44
45const SIDEWAYS = /^(\s*(?:flowchart|graph)[ \t]+)(LR|RL)\b/
46
47const SUPPORTED = /^(flowchart|graph|statediagram(-v2)?|xychart(-beta)?|sequencediagram|classdiagram|erdiagram)$/i
48
49export const unsupportedKind = (source: string): string | undefined => {
50  const kind = source.replace(/^(\s*%%[^\n]*\n)+/, '').trim().split(/\s/)[0] ?? ''
51  return kind && !SUPPORTED.test(kind) ? kind : undefined
52}
53
54const INLINE_TAG = /<\/?(?:b|i|u|s|em|strong|del|code|small|sub|sup|span|mark)\b[^<>\n]*>/gi
55
56type Mark = { italic?: boolean; bold?: boolean; underline?: boolean; strikethrough?: boolean }
57const TAG_MARK: Record<string, keyof Mark> = { i: 'italic', em: 'italic', b: 'bold', strong: 'bold', u: 'underline', s: 'strikethrough', del: 'strikethrough' }
58
59export const inlineMarks = (source: string): [string[], Mark][] =>
60  [...source.matchAll(/<(i|em|b|strong|u|s|del)\b[^<>\n]*>([^<\n]+)<\/\1\s*>/gi)]
61    .map(m => [cells(m[2]!.trim()), { [TAG_MARK[m[1]!.toLowerCase()]!]: true }] as [string[], Mark])
62
63export const mermaidText = (source: string, ascii: boolean, columns: number): string | null => {
64  source = source.replace(INLINE_TAG, '')
65  if (source.length > 8000 || source.split('\n').length > MAX_LINES) return null
66  const art = draw(source, ascii, columns)
67  const fits = (a: string) => a.split('\n').every(l => width(l) <= columns - 2)
68  if (art === null || fits(art) || !SIDEWAYS.test(source)) return art
69  return draw(source.replace(SIDEWAYS, '$1TD'), ascii, columns) ?? art
70}
71
72const evenTicks = (art: string) => {
73  const rows = art.split('\n').flatMap((line, i) => (/^\s*-?[\d.,]+[kMG%]?\s*[┤┼+|]/.test(line) ? [i] : []))
74  return rows.slice(2).every((row, i) => row - rows[i + 1]! === rows[1]! - rows[0]!)
75}
76
77const draw = (source: string, ascii: boolean, columns: number): string | null => {
78  const isChart = /^\s*xychart/.test(source)
79  const padded = padWide(source)
80  const size = chartSize(columns, isChart ? padded : '')
81  const key = `${ascii}:${isChart ? size.width : 0}:${source}`
82  return remember(textCache, key, () => {
83    try {
84      const render = () => renderMermaidAscii(unquoteCategories(padded.replace(/^(\s*%%[^\n]*\n)+/, '')).replace(/(-->|-\.->|==>|---|-\.-|===)[ \t]+\|/g, '$1|'), { useAscii: ascii, colorMode: 'none', paddingX: 3, paddingY: 1 }).replace(/[ \t]+$/gm, '').trimEnd().replace(/▶/g, '►').replace(/◀/g, '◄')
85      let art = ''
86      for (const height of isChart ? [0, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 7, -7, 8, -8].map(d => size.height + d).filter(h => h >= 6) : [0]) {
87        if (isChart) setChartSize(size.width, height)
88        art = render()
89        if (!isChart || evenTicks(art)) break
90      }
91      if (isChart && !evenTicks(art)) {
92        setChartSize(size.width, size.height)
93        art = render()
94      }
95      return (isChart ? labelBars(art, padded) : art.split('\n').filter(l => !/^[\s│|]*$/.test(l)).join('\n')).replaceAll(PAD, '')
96    } catch {
97      return null
98    }
99  })
100}
101
102const LINE = /[─-╿◇]/
103const ARROW = /[►◄▲▼]/
104
105const paint = (art: string, style: Style): (string | undefined)[][] => {
106  const t = style.theme
107  const grid = art.split('\n').map(cells)
108  const cell = (r: number, c: number) => grid[r]?.[c] ?? ''
109  const color: (string | undefined)[][] = grid.map(row => row.map(() => undefined))
110  const palette = [...new Set([t.link, t.number, t.heading, t.emphasis, t.path, t.codeFlag, t.accent])].filter((c): c is string => c !== undefined)
111  const next = (i: number) => palette[i % palette.length]
112  const labels = new Map<string, string | undefined>()
113
114  const rects: { r: number; c: number; r2: number; c2: number }[] = []
115  for (let r = 0; r < grid.length; r++) {
116    for (let c = 0; c < (grid[r]?.length ?? 0); c++) {
117      if (!/[┌╭(]/.test(cell(r, c))) continue
118      let c2 = c + 1
119      while (/[─┬┴┼▲▼]/.test(cell(r, c2))) c2++
120      if (!/[┐╮)]/.test(cell(r, c2)) || c2 === c + 1) continue
121      let r2 = r + 1
122      while (/[│├┤┼►◄]/.test(cell(r2, c))) r2++
123      if (!/[└╰(]/.test(cell(r2, c)) || !/[┘╯)]/.test(cell(r2, c2))) continue
124      rects.push({ r, c, r2, c2 })
125    }
126  }
127  const inside = (a: (typeof rects)[number], b: (typeof rects)[number]) => a !== b && b.r > a.r && b.r2 < a.r2 && b.c > a.c && b.c2 < a.c2
128  for (const box of rects.filter(a => !rects.some(b => inside(a, b)))) {
129    const { r, c, r2, c2 } = box
130    const label = grid.slice(r + 1, r2).map(row => row.slice(c + 1, c2).join('')).join(' ').trim()
131    if (!labels.has(label)) labels.set(label, next(labels.size))
132    const hue = labels.get(label)
133    for (let y = r; y <= r2; y++) for (let x = c; x <= c2; x++) if (cell(y, x).trim()) color[y]![x] = hue
134  }
135
136  const bars = [...new Set(grid.flatMap(row => row.flatMap((ch, c) => (ch === '█' && row[c - 1] !== '█' ? [c] : []))))].sort((a, b) => a - b)
137  const ticks = grid.findLast(row => row.includes('┬'))?.filter(ch => ch === '┬').length ?? 0
138  const tops = bars.map(x => grid.findIndex(row => row[x] === '█'))
139  const single = bars.length > 1 && bars.length <= ticks
140  const barColor = (i: number) => (single ? (tops[i] === Math.min(...tops) ? t.emphasis ?? t.accent : t.quote) : next(i))
141  grid.forEach((row, r) =>
142    row.forEach((ch, c) => {
143      if (color[r]![c] !== undefined) return
144      if (ch === '█') {
145        let start = c
146        while (row[start - 1] === '█') start--
147        color[r]![c] = barColor(bars.indexOf(start))
148      } else if (ch === '·') color[r]![c] = t.rule
149      else if (ARROW.test(ch)) color[r]![c] = t.accent
150      else if (LINE.test(ch)) color[r]![c] = t.diagram
151      else if (/^\d+[┤┼]/.test(row.slice(c).join(''))) color[r]![c] = t.number
152      else color[r]![c] = t.diagramText
153    }),
154  )
155  return color
156}
157
158const painted = new WeakMap<Theme, Map<string, (string | undefined)[][]>>()
159
160export const boxArt = ({ Box, Text }: ElementTable, style: Style, art: string, key: string, marks: [string[], Mark][] = []): RenderElement => {
161  const cache = painted.get(style.theme) ?? new Map<string, (string | undefined)[][]>()
162  painted.set(style.theme, cache)
163  const colors = remember(cache, art, () => paint(art, style))
164  return (
165    <Box key={key} flexDirection="column" paddingLeft={2}>
166      {art.split('\n').map((line, i) => {
167        const chars = cells(line)
168        const marked: (Mark | undefined)[] = chars.map(() => undefined)
169        for (const [phrase, mark] of marks)
170          for (let c = 0; c + phrase.length <= chars.length; c++)
171            if (phrase.every((ch, k) => chars[c + k] === ch)) for (let k = 0; k < phrase.length; k++) marked[c + k] = mark
172        const parts: RenderElement[] = []
173        let at = 0
174        while (at < chars.length) {
175          const hue = colors[i]?.[at]
176          const mark = marked[at]
177          let end = at + 1
178          while (end < chars.length && colors[i]?.[end] === hue && marked[end] === mark) end++
179          parts.push(<Text key={`t${parts.length}`} color={hue} {...mark}>{chars.slice(at, end).join('')}</Text>)
180          at = end
181        }
182        return <Text key={`${key}.${i}`}>{parts.length ? parts : ' '}</Text>
183      })}
184    </Box>
185  )
186}
187
188const EDGE_LABEL = /(-[-.=]+\s*)"([^"\n]+)"(\s*[-.=]+>)|\|([^|\n]+)\|/g
189
190const cap = (label: string, max: number) => {
191  const flat = label.replace(/<br\s*\/?>/gi, ' ').replace(/\s+/g, ' ').trim()
192  const chars = graphemes(flat)
193  return chars.length > max ? `${chars.slice(0, max - 1).join('').trimEnd()}…` : flat
194}
195
196export const shortenEdgeLabels = (source: string, max: number): string =>
197  source.replace(EDGE_LABEL, (m, open, quoted, close, piped) =>
198    quoted !== undefined ? `${open}"${cap(quoted, max)}"${close}` : `|${cap(piped, max)}|`)
199
hooks/render.tsx 1329 lines
1import type { ElementTable, RenderElement } from 'claude-code'
2
3import { tableHtml, tableText } from './html'
4import type { Block, Inline } from './markdown'
5import { displayText, inlineText } from './markdown'
6import { headingColor } from './heading'
7import { commentTail, commentVisual, flow, hasRtl, terminalLine } from './rtl'
8import type { Style, Theme } from './theme'
9import type { PrismToken } from './vendor/prism.js'
10import { languages, tokenize } from './vendor/prism.js'
11
12const WIDE = /[\u1100-\u115F\u2E80-\uA4CF\uAC00-\uD7A3\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFF60\uFFE0-\uFFE6]|\p{Extended_Pictographic}/u
13const segmenter = typeof Intl !== 'undefined' && 'Segmenter' in Intl ? new Intl.Segmenter() : undefined
14
15export const graphemes = (s: string): string[] => (segmenter ? [...segmenter.segment(s)].map(g => g.segment) : [...s])
16
17export const cells = (s: string): string[] => (/^[ -~]*$/.test(s) ? [...s] : graphemes(s).flatMap(g => (WIDE.test(g) ? [g, ''] : [g])))
18
19export const width = (s: string): number =>
20  /^[ -~]*$/.test(s) ? s.length : graphemes(s).reduce((w, g) => (/^\p{M}+$/u.test(g) ? w : w + (WIDE.test(g) ? 2 : 1)), 0)
21
22const wrapRanges = (text: string, w: number): [number, number][] => {
23  if (width(text) <= w) return [[0, text.length]]
24  const out: [number, number][] = []
25  let line: [number, number] | undefined
26  let used = 0
27  for (const word of text.matchAll(/\S+/g)) {
28    let start = word.index
29    const end = start + word[0].length
30    let size = width(word[0])
31    while (size > w) {
32      if (line) out.push(line)
33      line = undefined
34      let cut = start
35      let taken = 0
36      for (const g of graphemes(text.slice(start, end))) {
37        const step = width(g)
38        if (cut > start && taken + step > w) break
39        cut += g.length
40        taken += step
41      }
42      out.push([start, cut])
43      start = cut
44      size -= taken
45    }
46    if (start === end) continue
47    const joined = line ? used + width(text.slice(line[1], start)) + size : Infinity
48    if (line && joined <= w) {
49      line = [line[0], end]
50      used = joined
51    } else {
52      if (line) out.push(line)
53      line = [start, end]
54      used = size
55    }
56  }
57  if (line) out.push(line)
58  return out.length ? out : [[0, 0]]
59}
60
61const sliceInline = (nodes: Inline[], from: number, to: number): Inline[] => {
62  const out: Inline[] = []
63  let at = 0
64  for (const n of nodes) {
65    const length = inlineText([n]).length
66    const a = Math.max(from - at, 0)
67    const b = Math.min(to - at, length)
68    at += length
69    if (a >= b) continue
70    if ('children' in n) out.push({ ...n, children: sliceInline(n.children, a, b) })
71    else out.push({ ...n, text: n.text.slice(a, b) })
72  }
73  return out
74}
75
76const hasLink = (nodes: Inline[]): boolean => nodes.some(n => n.kind === 'link' || ('children' in n && hasLink(n.children)))
77
78const mostLines = (text: string, w: number): number => (text.match(/\S+/g)?.length ?? 0) + Math.ceil(width(text) / w)
79
80const wrapInline = (nodes: Inline[], w: number): Inline[][] => {
81  const text = inlineText(nodes)
82  return width(text) <= w ? [nodes] : wrapRanges(text, w).map(([a, b]) => sliceInline(nodes, a, b))
83}
84
85const flowOf = (style: Style, nodes: Inline[], columns: number) => (style.reorder ? flow(nodes, columns, width, style.shape) : null)
86
87const renderInline = (el: ElementTable, style: Style, nodes: Inline[], keyBase: string): RenderElement[] => {
88  const { Text } = el
89  const t = style.theme
90  return nodes.map((n, i) => {
91    const key = `${keyBase}.${i}`
92    switch (n.kind) {
93      case 'text':
94        return <Text key={key}>{n.text}</Text>
95      case 'strong':
96        return <Text key={key} bold color={t.strong}>{renderInline(el, style, n.children, key)}</Text>
97      case 'emphasis':
98        return <Text key={key} italic color={t.emphasis}>{renderInline(el, style, n.children, key)}</Text>
99      case 'strike':
100        return <Text key={key} strikethrough dimColor>{renderInline(el, style, n.children, key)}</Text>
101      case 'code':
102        return <Text key={key} color={t.inlineCode}>{n.text}</Text>
103      case 'math':
104        return <Text key={key} italic color={t.emphasis}>{n.text}</Text>
105      case 'link':
106        return /^(https:|http:\/\/localhost\b)/i.test(n.href)
107          ? n.text === n.href ? <el.Link key={key} href={n.href} /> : <el.Link key={key} href={n.href}><Text color={t.link} underline>{n.text}</Text></el.Link>
108          : <Text key={key} color={t.link} underline>{n.text}</Text>
109      case 'number':
110        return <Text key={key} color={t.number}>{n.text}</Text>
111      case 'path':
112        return <Text key={key} color={t.path}>{n.text}</Text>
113      case 'dim':
114        return <Text key={key} dimColor>{n.text}</Text>
115      case 'footnote':
116        return <Text key={key} color={t.accent}>{n.text}</Text>
117    }
118  })
119}
120
121const renderFlow = (el: ElementTable, style: Style, lines: Inline[][], key: string, props: { italic?: boolean; color?: string; bold?: boolean } = {}) => {
122  const { Text } = el
123  return lines.map((line, i) => <Text key={`${key}.${i}`} {...props}>{renderInline(el, style, line, `${key}.${i}`)}</Text>)
124}
125
126const PRISM_COLORS: Record<string, keyof Theme> = {
127  comment: 'codeComment', prolog: 'codeComment', doctype: 'codeComment', cdata: 'codeComment',
128  string: 'codeString', char: 'codeString', 'template-string': 'codeString', 'attr-value': 'codeString', url: 'codeString',
129  number: 'number', boolean: 'number', constant: 'number', symbol: 'number', inserted: 'number',
130  keyword: 'codeFlag', important: 'codeFlag', atrule: 'codeFlag', rule: 'codeFlag', deleted: 'codeFlag',
131  function: 'codeCommand', 'class-name': 'codeCommand', builtin: 'codeCommand', key: 'codeCommand', selector: 'codeCommand',
132  property: 'link', tag: 'link', 'attr-name': 'emphasis', variable: 'emphasis', regex: 'path',
133}
134
135type Segment = { text: string; color?: string; italic: boolean }
136
137const flatten = (tokens: PrismToken[], style: Style, color?: string, italic = false): Segment[] =>
138  tokens.flatMap(token => {
139    if (typeof token === 'string') return [{ text: token, color, italic }]
140    const names = [token.type, ...(Array.isArray(token.alias) ? token.alias : token.alias ? [token.alias] : [])]
141    const slot = names.map(n => PRISM_COLORS[n]).find(Boolean)
142    const inner = Array.isArray(token.content) ? token.content : [token.type === 'comment' && typeof token.content === 'string' && style.reorder ? commentVisual(token.content, style.shape) : token.content]
143    return flatten(inner, style, slot ? style.theme[slot] : color, italic || token.type === 'comment')
144  })
145
146export const remember = <T,>(cache: Map<string, T>, key: string, make: () => T, limit = 200): T => {
147  const hit = cache.get(key)
148  if (hit !== undefined) {
149    cache.delete(key)
150    cache.set(key, hit)
151    return hit
152  }
153  const value = make()
154  cache.set(key, value)
155  if (cache.size > limit) cache.delete(cache.keys().next().value!)
156  return value
157}
158
159const highlighted = new WeakMap<Theme, Map<string, Segment[][]>>()
160
161const grammarFor = (lang: string) => {
162  const name = lang.toLowerCase()
163  const grammar = Object.hasOwn(languages, name) ? languages[name] : undefined
164  return grammar !== null && typeof grammar === 'object' ? grammar : undefined
165}
166
167const highlightRows = (style: Style, lines: string[], lang: string): Segment[][] | null => {
168  const grammar = grammarFor(lang)
169  if (!grammar) return null
170  const code = lines.join('\n')
171  const cache = highlighted.get(style.theme) ?? new Map<string, Segment[][]>()
172  highlighted.set(style.theme, cache)
173  return remember(cache, `${style.reorder ? style.shape : ''}\0${lang}\0${code}`, () => {
174    const out: Segment[][] = [[]]
175    for (const seg of flatten(tokenize(code, grammar), style)) {
176      seg.text.split('\n').forEach((piece, i) => {
177        if (i > 0) out.push([])
178        if (piece) out[out.length - 1]!.push({ ...seg, text: piece })
179      })
180    }
181    return out
182  })
183}
184
185export const highlightBlock = ({ Text }: ElementTable, style: Style, lines: string[], lang: string, key: string): RenderElement[] | null => {
186  const rows = highlightRows(style, lines, lang)
187  if (!rows) return null
188  return rows.map((row, r) => (
189    <Text key={`${key}.${r}`} color={style.theme.codeText}>
190      {row.length ? row.map((s, i) => <Text key={`${key}.${r}.${i}`} color={s.color} italic={s.italic}>{s.text}</Text>) : ' '}
191    </Text>
192  ))
193}
194
195const isShellLang = (lang: string) => lang === '' || /^(sh|bash|zsh|shell|console|fish|powershell|ps1)$/i.test(lang)
196
197export const codeLine = (el: ElementTable, style: Style, line: string, lang: string, key: string): RenderElement => {
198  const { Text } = el
199  const t = style.theme
200  const isShell = isShellLang(lang)
201  if (/[\u2500-\u257F]/.test(line)) return <Text key={key} color={t.codeText}>{line}</Text>
202  const comment = style.reorder ? commentTail(line, style.shape) : null
203  if (comment) {
204    return (
205      <Text key={key} color={t.codeText}>
206        {comment.head ? codeLine(el, style, comment.head, lang, `${key}.h`) : null}
207        <Text color={t.codeComment}>{comment.marker + comment.tail}</Text>
208      </Text>
209    )
210  }
211  if (!isShell) return <Text key={key} color={t.codeText}>{line || ' '}</Text>
212  if (/^\s*#/.test(line)) return <Text key={key} color={t.codeComment}>{line}</Text>
213  const parts = line.split(/("[^"]*"|'[^']*'|\s+)/).filter(p => p !== '')
214  let seenCommand = false
215  return (
216    <Text key={key} color={t.codeText}>
217      {parts.map((p, i) => {
218        if (/^\s+$/.test(p)) return <Text key={`${key}.${i}`}>{p}</Text>
219        if (/^["']/.test(p)) return <Text key={`${key}.${i}`} color={t.codeString}>{p}</Text>
220        if (/^--?[\w-]/.test(p)) return <Text key={`${key}.${i}`} color={t.codeFlag}>{p}</Text>
221        if (!seenCommand && !/^[$>|&;]+$/.test(p)) {
222          seenCommand = true
223          return <Text key={`${key}.${i}`} color={t.codeCommand}>{p}</Text>
224        }
225        if (/^(\||&&|;|\|\|)$/.test(p)) seenCommand = false
226        return <Text key={`${key}.${i}`}>{p}</Text>
227      })}
228    </Text>
229  )
230}
231
232const longestWord = (text: string): number => Math.max(0, ...text.split(/\s+/).map(width))
233
234export const columnWidths = (natural: number[], available: number, gap: number, words: number[] = []): number[] => {
235  const room = Math.max(natural.length, available - gap * (natural.length - 1))
236  const total = natural.reduce((a, b) => a + b, 0)
237  if (total <= room) return natural
238  const minimum = natural.map((w, c) => Math.min(w, Math.max(1, words[c] ?? 1)))
239  const widths = natural.map(() => 0)
240  let open = natural.map((_, c) => c)
241  let left = room
242  for (let fixed = [-1]; fixed.length; ) {
243    const share = Math.floor(left / open.length)
244    const claim = (c: number) => (natural[c]! <= share ? natural[c]! : minimum[c]!)
245    const minimumsFit = open.reduce((a, c) => a + minimum[c]!, 0) <= left
246    const needed = open.reduce((a, c) => a + claim(c), 0)
247    fixed = open.filter(c => natural[c]! <= share)
248    if (minimumsFit && (!fixed.length || needed > left)) fixed = open.filter(c => minimum[c]! > share)
249    fixed.forEach(c => (widths[c] = claim(c), left -= widths[c]!))
250    open = open.filter(c => !fixed.includes(c))
251  }
252  open.forEach((c, i) => (widths[c] = Math.max(1, Math.floor(left / open.length) + (i < left % open.length ? 1 : 0))))
253  while (widths.reduce((a, b) => a + b, 0) > room) {
254    const widest = widths.indexOf(Math.max(...widths))
255    if (widths[widest]! <= 1) break
256    widths[widest]!--
257  }
258  return widths
259}
260
261const isRtlTable = (style: Style, block: Extract<Block, { kind: 'table' }>): boolean => {
262  const cells = [...block.header, ...block.rows.flat()].filter(cell => displayText(cell).trim() !== '')
263  return cells.filter(cell => flowOf(style, cell, Infinity)?.base === 'R').length * 2 > cells.length
264}
265
266const ART_WIDTH = 100
267
268export const tableArt = (block: Extract<Block, { kind: 'table' }>): string => {
269  const cells = [block.header, ...block.rows].map(r => block.header.map((_, c) => displayText(r[c] ?? [])))
270  const widths = columnWidths(
271    block.header.map((_, c) => Math.max(...cells.map(r => width(r[c]!)))),
272    ART_WIDTH - 4,
273    3,
274    block.header.map((_, c) => Math.max(...cells.map(r => longestWord(r[c]!)))),
275  )
276  const pad = (text: string, c: number, align: 'left' | 'right' | 'center') => {
277    const room = widths[c]! - width(text)
278    const left = align === 'right' ? room : align === 'center' ? Math.floor(room / 2) : 0
279    return ' '.repeat(left) + text + ' '.repeat(room - left)
280  }
281  const line = (l: string, m: string, r: string) => l + widths.map(w => '─'.repeat(w + 2)).join(m) + r
282  const row = (r: string[], header: boolean) => {
283    const lines = r.map((text, c) => wrapRanges(text, widths[c]!).map(([a, b]) => text.slice(a, b)))
284    return Array.from({ length: Math.max(...lines.map(l => l.length)) }, (_, i) =>
285      `│ ${lines.map((l, c) => pad(l[i] ?? '', c, header ? 'center' : block.align[c] ?? 'left')).join(' │ ')} │`)
286  }
287  const art = [line('┌', '┬', '┐'), ...row(cells[0]!, true), ...cells.slice(1).flatMap(r => [line('├', '┼', '┤'), ...row(r, false)]), line('└', '┴', '┘')]
288  return ['```', ...art, '```'].join('\n')
289}
290
291const renderTable = (el: ElementTable, style: Style, block: Extract<Block, { kind: 'table' }>, columns: number, key: string, rtl: boolean) => {
292  const { Box, Text } = el
293  const t = style.theme
294  const box = style.tableStyle === 'box'
295  const gap = box ? 0 : style.tableStyle === 'grid' ? 3 : 2
296  const natural = block.header.map((h, c) =>
297    Math.max(width(displayText(h)), ...block.rows.map(r => width(displayText(r[c] ?? [])))),
298  )
299  const words = block.header.map((h, c) =>
300    Math.max(longestWord(displayText(h)), ...block.rows.map(r => longestWord(displayText(r[c] ?? [])))),
301  )
302  const widths = box ? columnWidths(natural, columns - 4, 3, words) : columnWidths(natural, columns, gap, words)
303  const order = natural.map((_, c) => c)
304  if (rtl) order.reverse()
305  const ruleChar = style.tableStyle === 'grid' ? '━' : '─'
306  const justify = (c: number) =>
307    block.align[c] === 'right' ? 'flex-end' : block.align[c] === 'center' ? 'center' : 'flex-start'
308
309  const rule = (k: string, heavy: boolean) => (
310    <Box key={k} flexDirection="row" columnGap={gap}>
311      {order.map(c => (
312        <Text key={`${k}.${c}`} color={t.tableRule} dimColor={!heavy && !t.tableRule}>
313          {(heavy ? ruleChar : '─').repeat(widths[c]!)}
314        </Text>
315      ))}
316    </Box>
317  )
318
319  const bar = (k: string, text: string, lines = 1) => (
320    <Text key={k} color={t.tableRule} dimColor={!t.tableRule}>{Array.from({ length: lines }, () => text).join('\n')}</Text>
321  )
322  const clipped = (lines: number) => (k: string, text: string) => (
323    <Box key={k} minWidth={width(text)}>
324      <Box position="absolute" top={0} bottom={0} left={0} minWidth={width(text)} overflow="hidden">
325        {bar(`${k}.b`, text, lines)}
326      </Box>
327    </Box>
328  )
329  const edge = (k: string, [left, fill, mid, right]: string) =>
330    bar(k, left + order.map(c => fill!.repeat(widths[c]! + 2)).join(mid) + right)
331
332  const terminalRow = (cells: Inline[][], k: string, isHeader: boolean) => {
333    const parts: Inline[] = box ? [{ kind: 'dim', text: '│ ' }] : []
334    order.forEach((c, i) => {
335      if (i > 0) parts.push(box ? { kind: 'dim', text: ' │ ' } : { kind: 'text', text: ' '.repeat(gap) })
336      const cell = flowOf({ ...style, shape: 'visual' }, cells[c] ?? [], Infinity)
337      const content = cell ? cell.lines[0]! : (cells[c] ?? [])
338      const pad = Math.max(0, widths[c]! - width(inlineText(content)))
339      const side = cell?.base === 'R' && block.align[c] !== 'center' ? 'flex-end' : justify(c)
340      const before = side === 'flex-end' ? pad : side === 'center' ? Math.floor(pad / 2) : 0
341      parts.push({ kind: 'text', text: ' '.repeat(before) }, ...(isHeader ? [{ kind: 'strong' as const, children: content }] : content), { kind: 'text', text: ' '.repeat(pad - before) })
342    })
343    if (box) parts.push({ kind: 'dim', text: ' │' })
344    return <Text key={k}>{renderInline(el, style, terminalLine(parts), k)}</Text>
345  }
346
347  const row = (cells: Inline[][], k: string, isHeader: boolean, border: (k: string, text: string) => RenderElement = bar) => rtl && style.shape === 'inverse' ? terminalRow(cells, k, isHeader) : (
348    <Box key={k} flexDirection="row" columnGap={gap} alignItems="stretch">
349      {box && border(`${k}.l`, '│ ')}
350      {order.map((c, i) => {
351        const w = widths[c]!
352        const cell = flowOf(style, cells[c] ?? [], Infinity)
353        const content = cell ? cell.lines[0]! : (cells[c] ?? [])
354        const side = cell?.base === 'R' && block.align[c] !== 'center' ? 'flex-end' : justify(c)
355        const cellBox = (
356          <Box key={`${k}.${c}`} width={w} flexShrink={0} justifyContent={side}>
357            {isHeader
358              ? <Text bold color={t.tableHeader}>{inlineText(content)}</Text>
359              : <Text>{renderInline(el, style, content, `${k}.${c}`)}</Text>}
360          </Box>
361        )
362        return box && i > 0 ? [border(`${k}.${c}s`, ' │ '), cellBox] : cellBox
363      })}
364      {box && border(`${k}.r`, ' │')}
365    </Box>
366  )
367
368  const wrappedRow = (cells: Inline[][], k: string, isHeader: boolean) => {
369    if (!isHeader && cells.some(hasLink)) return [row(cells, k, isHeader, clipped(Math.max(1, ...widths.map((w, c) => mostLines(displayText(cells[c] ?? []), w)))))]
370    const wrapped = widths.map((w, c) => {
371      const content = cells[c] ?? []
372      if (natural[c]! <= w) return [content]
373      return wrapInline(isHeader ? [{ kind: 'text', text: inlineText(content) }] : content, w)
374    })
375    return Array.from({ length: Math.max(...wrapped.map(l => l.length)) }, (_, i) =>
376      row(wrapped.map(l => l[i] ?? []), `${k}.${i}`, isHeader))
377  }
378
379  if (box) {
380    const lines: RenderElement[] = [edge(`${key}.t`, '┌─┬┐'), ...wrappedRow(block.header, `${key}.h`, true), edge(`${key}.hr`, '╞═╪╡')]
381    block.rows.forEach((r, i) => {
382      if (i > 0) lines.push(edge(`${key}.r${i}r`, '├─┼┤'))
383      lines.push(...wrappedRow(r, `${key}.r${i}`, false))
384    })
385    lines.push(edge(`${key}.b`, '└─┴┘'))
386    return <Box key={key} flexDirection="column" {...(rtl ? { alignSelf: 'flex-end' as const } : {})}>{lines}</Box>
387  }
388
389  const body: RenderElement[] = [row(block.header, `${key}.h`, true), rule(`${key}.hr`, true)]
390  block.rows.forEach((r, i) => {
391    body.push(row(r, `${key}.r${i}`, false))
392    if (style.tableStyle !== 'minimal' && i < block.rows.length - 1) body.push(rule(`${key}.r${i}r`, false))
393  })
394  return <Box key={key} flexDirection="column" {...(rtl ? { alignSelf: 'flex-end' as const } : {})}>{body}</Box>
395}
396
397const renderHeading = (el: ElementTable, style: Style, block: Extract<Block, { kind: 'heading' }>, key: string) => {
398  const rtl = flowOf(style, block.inline, Infinity)
399  if (!rtl) return drawHeading(el, style, block, block.inline, key)
400  const heading = drawHeading(el, style, block, rtl.lines[0]!, key)
401  return rtl.base === 'R' ? <el.Box key={key} alignSelf="flex-end">{heading}</el.Box> : heading
402}
403
404const drawHeading = (el: ElementTable, style: Style, block: Extract<Block, { kind: 'heading' }>, inline: Inline[], key: string) => {
405  const { Box, Text } = el
406  const t = style.theme
407  const color = headingColor(t, block.level)
408  const label = inlineText(inline)
409  switch (style.headingStyle) {
410    case 'uppercase':
411      return <Text key={key} bold color={color}>{block.level === 1 ? label.toUpperCase() : label}</Text>
412    case 'underline':
413      return <Text key={key} bold underline={block.level <= 2} color={color}>{label}</Text>
414    case 'banner':
415      if (block.level === 1) return <Box key={key} alignSelf="flex-start" borderStyle="bold" borderColor={color} paddingX={1}><Text bold color={color}>{renderInline(el, style, inline, key)}</Text></Box>
416      return block.level === 2
417        ? <Box key={key} flexDirection="column" alignSelf="flex-start"><Text bold color={color}>{renderInline(el, style, inline, key)}</Text><Text color={color}>{'━'.repeat(width(label))}</Text></Box>
418        : <Text key={key} bold color={color}>{renderInline(el, style, inline, key)}</Text>
419    default:
420      return <Text key={key} bold color={color}>{renderInline(el, style, inline, key)}</Text>
421  }
422}
423
424const ALERT_COLOR = { note: 'blue', tip: 'green', important: 'magenta', warning: 'yellow', caution: 'red' } as const
425
426const splitLines = (nodes: Inline[]): Inline[][] => {
427  const text = inlineText(nodes)
428  const lines: Inline[][] = []
429  let from = 0
430  for (let at = text.indexOf('\n'); at !== -1; at = text.indexOf('\n', from)) {
431    lines.push(sliceInline(nodes, from, at))
432    from = at + 1
433  }
434  return [...lines, sliceInline(nodes, from, text.length)]
435}
436
437const renderParagraph = (el: ElementTable, style: Style, block: Extract<Block, { kind: 'paragraph' }>, columns: number, key: string): RenderElement => {
438  const { Box, Text } = el
439  const lines = splitLines(block.inline)
440  if (lines.length > 1) return <Box key={key} flexDirection="column">{lines.map((inline, i) => renderParagraph(el, style, { ...block, inline }, columns, `${key}.${i}`))}</Box>
441  const rtl = flowOf(style, block.inline, columns)
442  if (rtl?.base === 'R') return <Box key={key} flexDirection="column" alignItems="flex-end">{renderFlow(el, style, rtl.lines, key)}</Box>
443  return <Text key={key} bold={style.narration}>{renderInline(el, style, rtl ? rtl.lines[0]! : block.inline, key)}</Text>
444}
445
446const renderQuote = (el: ElementTable, style: Style, block: Extract<Block, { kind: 'quote' }>, columns: number, key: string) => {
447  const { Box, Text } = el
448  const t = style.theme
449  const rtl = flowOf(style, block.inline, columns - 2)
450  if (rtl?.base === 'R') {
451    return (
452      <Box key={key} flexDirection="row" justifyContent="flex-end">
453        <Box flexDirection="column" alignItems="flex-end">{renderFlow(el, style, rtl.lines, key, { italic: true, color: t.quote })}</Box>
454        <Text color={t.accent}> │</Text>
455      </Box>
456    )
457  }
458  return (
459    <Box key={key} flexDirection="row">
460      <Text color={t.accent}>│ </Text>
461      <Text italic color={t.quote}>{renderInline(el, style, rtl ? rtl.lines[0]! : block.inline, key)}</Text>
462    </Box>
463  )
464}
465
466const renderAlert = (el: ElementTable, style: Style, block: Extract<Block, { kind: 'alert' }>, columns: number, key: string) => {
467  const { Box, Text } = el
468  const color = ALERT_COLOR[block.level]
469  const title = <Text bold color={color}>{block.level[0]!.toUpperCase() + block.level.slice(1)}</Text>
470  const rtl = flowOf(style, block.inline, columns - 4)
471  if (rtl?.base === 'R') {
472    return (
473      <Box key={key} flexDirection="column" alignSelf="flex-end" alignItems="flex-end" borderStyle="round" borderColor={color} paddingX={1}>
474        {title}
475        {renderFlow(el, style, rtl.lines, key)}
476      </Box>
477    )
478  }
479  const inline = rtl ? rtl.lines[0]! : block.inline
480  return (
481    <Box key={key} flexDirection="column" alignSelf="flex-start" borderStyle="round" borderColor={color} paddingX={1}>
482      {title}
483      {inline.length ? <Text>{renderInline(el, style, inline, key)}</Text> : null}
484    </Box>
485  )
486}
487
488const TASK_GLYPHS = { checks: ['[ ]', '[✓]'], ticks: ['○', '✓'], box: ['□', '✓'], progress: ['○', '✓'] } as const
489
490const renderList = (el: ElementTable, style: Style, block: Extract<Block, { kind: 'list' }>, columns: number, key: string, copy?: CopyButton, fold?: Fold) => {
491  const { Box, Text } = el
492  const t = style.theme
493  const tasks = block.items.filter(item => item.task !== undefined)
494  const done = tasks.filter(item => item.task).length
495  const filled = tasks.length ? Math.round((done / tasks.length) * 20) : 0
496  const strike = style.taskStyle === 'checks' || style.taskStyle === 'box'
497  const tick = style.taskStyle === 'ticks' || style.taskStyle === 'progress'
498  return (
499    <Box key={key} flexDirection="column">
500      {style.taskStyle === 'progress' && tasks.length > 0 && (
501        <Text>
502          <Text color={t.accent}>{'━'.repeat(filled)}</Text>
503          <Text color={t.bullet} dimColor>{'─'.repeat(20 - filled)}</Text>
504          <Text color={t.bullet}>{` ${done}/${tasks.length} done`}</Text>
505        </Text>
506      )}
507      {block.items.map((item, i) => {
508        const k = `${key}.${i}`
509        const glyph = item.task !== undefined ? TASK_GLYPHS[style.taskStyle][item.task ? 1 : 0] : /\d/.test(item.marker) ? item.marker : item.depth ? '◦' : '•'
510        const glyphColor = item.task && tick ? t.accent : t.bullet
511        const pad = item.depth * 2 + glyph.length + 1
512        const rtl = flowOf(style, item.inline, columns - pad)
513        const row = rtl?.base === 'R' ? (
514          <Box key={k} flexDirection="row" justifyContent="flex-end" paddingRight={item.depth * 2}>
515            <Box flexDirection="column" alignItems="flex-end">{renderFlow(el, style, rtl.lines, k)}</Box>
516            <Text color={glyphColor}>{` ${glyph}`}</Text>
517          </Box>
518        ) : (
519          <Box key={k} flexDirection="row" paddingLeft={item.depth * 2}>
520            <Text color={glyphColor}>{`${glyph} `}</Text>
521            <Text dimColor={item.task === true} strikethrough={item.task === true && strike}>{renderInline(el, style, rtl ? rtl.lines[0]! : item.inline, k)}</Text>
522          </Box>
523        )
524        if (!item.blocks) return row
525        const scoped: CopyButton | undefined = copy && ((text, id, label, html) => copy(text, `${k}.${id}`, label, html))
526        const scopedFold: Fold | undefined = fold && ((id, hidden, key) => fold(id, hidden, `${k}.${key}`))
527        return (
528          <Box key={k} flexDirection="column">
529            {row}
530            <Box flexDirection="column" paddingLeft={pad}>{renderBlocks(el, style, item.blocks, columns - pad, new Map(), scoped, scopedFold)}</Box>
531          </Box>
532        )
533      })}
534    </Box>
535  )
536}
537
538export type CopyButton = (text: string | (() => string), key: string, label?: string, html?: () => string) => RenderElement | null
539export type Drawn = Map<number, { element?: RenderElement; art?: string; note?: string; button?: RenderElement }>
540export type Fold = (id: string, hidden: number, key: string) => { folded: boolean; element: RenderElement } | null
541
542const NUMBER_AT = 10
543const NUMBER_MAX = 400
544const FOLD_AT = 30
545const FOLD_SHOW = 20
546
547const codeId = (lines: string[]): string => {
548  let h = 5381
549  for (const line of lines) for (let i = 0; i < line.length; i++) h = (h * 33 + line.charCodeAt(i)) | 0
550  return `${lines.length}:${(h >>> 0).toString(36)}`
551}
552
553const copySource = (block: Block): string | undefined =>
554  block.kind === 'code' ? block.lines.join('\n') : block.kind === 'table' || block.kind === 'list' ? block.raw : block.kind === 'quote' || block.kind === 'alert' ? block.raw.split('\n').map(line => line.replace(/^\s*>\s?/, '')).join('\n') : undefined
555
556export const renderBlocks = (el: ElementTable, style: Style, blocks: Block[], columns: number, drawn: Drawn = new Map(), copy?: CopyButton, fold?: Fold): RenderElement[] => {
557  const { Box, Text } = el
558  const t = style.theme
559  const rtlTables = blocks.map(block => style.reorder && block.kind === 'table' && isRtlTable(style, block))
560  const rendered = blocks.map((block, b) => {
561    const key = `b${b}`
562    switch (block.kind) {
563      case 'heading':
564        return drawn.get(b)?.element ?? renderHeading(el, style, block, key)
565      case 'paragraph':
566        return renderParagraph(el, style, block, columns, key)
567      case 'quote':
568        return renderQuote(el, style, block, columns, key)
569      case 'alert':
570        return renderAlert(el, style, block, columns, key)
571      case 'rule':
572        return <Text key={key} color={t.rule} dimColor={!t.rule}>{'─'.repeat(Math.max(8, Math.min(columns, 80)))}</Text>
573      case 'code': {
574        const done = drawn.get(b)?.element
575        if (done) return done
576        const note = drawn.get(b)?.note
577        const lines = block.lines
578        const toggle = lines.length > FOLD_AT ? fold?.(codeId(lines), lines.length - FOLD_SHOW, `fold${b}`) : null
579        const shown = toggle?.folded ? FOLD_SHOW : lines.length
580        const rows = (isShellLang(block.lang) ? null : highlightBlock(el, style, lines, block.lang, key))?.slice(0, shown) ?? lines.slice(0, shown).map((line, i) => codeLine(el, style, line, block.lang, `${key}.${i}`))
581        const gutter = String(lines.length).length
582        return (
583          <Box key={key} flexDirection="column" alignSelf="flex-start">
584            {note ? <Text color={t.codeComment}>{note}</Text> : null}
585            <Box flexDirection="row" justifyContent="space-between" columnGap={4}>
586              <Text color={t.codeComment}>{`── ${block.lang || 'code'}`}</Text>
587              {copy?.(lines.join('\n'), `copy${b}`) ?? null}
588            </Box>
589            <Box flexDirection="column" paddingLeft={2}>
590              {lines.length < NUMBER_AT || lines.length > NUMBER_MAX ? rows : rows.map((row, i) => row.type === 'Text' ? <Box key={`n${i}`} flexDirection="row"><Text color={t.codeComment} dimColor>{`${String(i + 1).padStart(gutter)}  `}</Text>{row}</Box> : row)}
591              {toggle?.element ?? null}
592            </Box>
593          </Box>
594        )
595      }
596      case 'list':
597        return renderList(el, style, block, columns, key, copy, fold)
598      case 'table':
599        return renderTable(el, style, block, columns, key, rtlTables[b]!)
600      case 'notes':
601        return (
602          <Box key={key} flexDirection="column">
603            <Text dimColor>{'─'.repeat(Math.min(columns, 12))}</Text>
604            {block.notes.map((note, i) => <Text key={`${key}.${i}`} dimColor>{note.mark} {renderInline(el, style, note.inline, `${key}.${i}`)}</Text>)}
605          </Box>
606        )
607    }
608  })
609  const copied = rendered.map((element, b) => {
610    const block = blocks[b]
611    const text = block ? copySource(block) : undefined
612    const isPlainCode = block?.kind === 'code' && !drawn.get(b)?.element
613    const art = drawn.get(b)?.art ?? (block?.kind === 'table' ? () => tableArt(block) : undefined)
614    const first = text === undefined || isPlainCode ? null : copy?.(text, `copy${b}`, block?.kind === 'table' ? '⧉ md' : art === undefined ? undefined : '⧉ source')
615    const second = first && art !== undefined ? copy?.(art, `art${b}`, '⧉ art') : null
616    const html = first && block?.kind === 'table' ? copy?.(() => tableText(block), `html${b}`, '⧉ html', () => tableHtml(block)) : null
617    const extra = first ? drawn.get(b)?.button ?? null : null
618    const button = second || extra ? (
619      <el.Box key={`copies${b}`} flexDirection="row" columnGap={1}>
620        {first}
621        {second}
622        {html}
623        {extra}
624      </el.Box>
625    ) : first
626    if (!button) return element
627    const { Box } = el
628    const rtl = style.reorder && block !== undefined && hasRtl(block.raw)
629    return block?.kind === 'quote' || block?.kind === 'alert' ? (
630      <Box key={`c${b}`} flexDirection="row" columnGap={2} {...(rtl ? { justifyContent: 'flex-end' as const } : {})}>
631        {element}
632        {button}
633      </Box>
634    ) : (
635      <Box key={`c${b}`} flexDirection="column" {...(rtl && (block?.kind === 'list' || rtlTables[b]) ? {} : { alignSelf: 'flex-start' as const })}>
636        <Box justifyContent="flex-end">{button}</Box>
637        {element}
638      </Box>
639    )
640  })
641  const isFigure = (b: number) => blocks[b]?.kind === 'table' || drawn.get(b)?.art !== undefined
642  const out: RenderElement[] = []
643  for (let b = 0; b < rendered.length; b++) {
644    if (!isFigure(b) || !isFigure(b + 1)) {
645      out.push(copied[b]!)
646      continue
647    }
648    const start = b
649    while (isFigure(b + 1)) b++
650    out.push(
651      <Box key={`row${start}`} flexDirection="row" flexWrap="wrap" columnGap={4} rowGap={1}>
652        {copied.slice(start, b + 1).map((figure, i) => <Box key={`f${start + i}`} flexShrink={0}>{figure}</Box>)}
653      </Box>,
654    )
655  }
656  return out
657}
658
659export type ToolRow = { tool: string; input: unknown; isRunning: boolean; isErrored: boolean; isInterrupted: boolean }
660
661const VERBS: Record<string, string> = {
662  Bash: 'Ran', PowerShell: 'Ran', Read: 'Read', Write: 'Wrote', Edit: 'Edited', MultiEdit: 'Edited', NotebookEdit: 'Edited',
663  Grep: 'Searched', Glob: 'Listed', WebFetch: 'Fetched', WebSearch: 'Searched the web for', Agent: 'Delegated', Task: 'Delegated',
664}
665
666const field = (input: unknown, ...keys: string[]): string | undefined => {
667  if (input === null || typeof input !== 'object') return undefined
668  for (const k of keys) {
669    const v = (input as Record<string, unknown>)[k]
670    if (typeof v === 'string' && v.trim() !== '') return v
671  }
672  return undefined
673}
674
675const TARGET_FIELDS = ['file_path', 'notebook_path', 'path', 'pattern', 'url', 'query', 'description']
676
677const statusColor = (t: Theme, row: ToolRow) =>
678  row.isErrored ? t.codeFlag : row.isInterrupted ? t.codeComment : row.isRunning ? t.accent : t.number
679
680const toolDim = (style: Style) => style.toolStyle !== "classic"
681
682const toolGutter = ({ Box, Text }: ElementTable, style: Style, color: string | undefined, running: boolean) =>
683  style.toolStyle.startsWith("tree")
684    ? <Box width={4} flexShrink={0}><Text color={color}>{'  ⎿ '}</Text></Box>
685    : <Box width={2} flexShrink={0}><Text color={color}>{running ? '◌' : '●'}</Text></Box>
686
687const toolLayout = (el: ElementTable, style: Style, columns: number, label: string, color: string | undefined, running: boolean, text: RenderElement) => {
688  const { Box, Text } = el
689  if (style.toolStyle !== "chat") return <Box flexDirection="row">{toolGutter(el, style, color, running)}{text}</Box>
690  const w = Math.min(width(label) + 2, Math.max(20, Math.floor(columns * 0.6)))
691  return (
692    <Box flexDirection="row" justifyContent="flex-end" width="100%">
693      <Box width={w} flexDirection="row">{text}<Text color={color}>{running ? " ◌" : " ●"}</Text></Box>
694    </Box>
695  )
696}
697
698type Places = Pick<Style, 'cwd' | 'home'>
699
700const slashes = (path: string): string => path.replace(/\\/g, '/').replace(/\/+$/, '')
701
702const folded = (path: string): string => (/^[a-z]:(\/|$)/i.test(path) ? path.toLowerCase() : path)
703
704export const shortPath = (style: Places, path: string): string => {
705  const target = folded(slashes(path))
706  for (const [root, home] of [[style.cwd, false], [style.home, true]] as const) {
707    const base = folded(slashes(root))
708    if (base !== '' && target.startsWith(`${base}/`)) return home ? `~${path.slice(base.length)}` : path.slice(base.length + 1)
709  }
710  return path
711}
712
713const compactPath = (path: string): string => {
714  const parts = path.split('/')
715  if (!path.startsWith('/') || parts.length <= 6) return path
716  return [...parts.slice(0, 3), '…', ...parts.slice(-2)].join('/')
717}
718
719const sameDir = (a: string, b: string): boolean => a !== '' && folded(slashes(a)) === folded(slashes(b))
720
721export const shortTarget = (places: Places, path: string): string => (sameDir(places.cwd, path) ? '.' : sameDir(places.home, path) ? '~' : compactPath(shortPath(places, path)))
722
723
724const PATH_FIELDS = ['file_path', 'notebook_path', 'path']
725
726const targetOf = (style: Style, input: unknown, fields: string[]): { target?: string; isPathField: boolean } => {
727  const target = field(input, ...fields)
728  const isPathField = target !== undefined && PATH_FIELDS.some(k => field(input, k) === target)
729  return { target: isPathField ? shortPath(style, target!) : target, isPathField }
730}
731
732const RESULT_LINES = 3
733
734export const renderShellResult = (el: ElementTable, style: Style, output: unknown): RenderElement => {
735  const { Box, Text } = el
736  const out = output !== null && typeof output === 'object' ? (output as Record<string, unknown>) : {}
737  const shown = [...lines(out.stdout), ...lines(out.stderr)]
738  const head = shown.slice(0, RESULT_LINES).map(l => shortenPaths(l, style))
739  return (
740    <Box flexDirection="row">
741      <Text dimColor>{'  ⎿  '}</Text>
742      <Box flexDirection="column">
743        {head.length === 0 ? <Text dimColor>(No output)</Text> : head.map((l, i) => <Text key={`l${i}`} wrap="truncate-end">{l === '' ? ' ' : l}</Text>)}
744        {shown.length > head.length ? <Text dimColor>{`… +${shown.length - head.length} lines`}</Text> : null}
745      </Box>
746    </Box>
747  )
748}
749
750const DIFF_LINES = 20
751const DIFF_MAX = 400
752
753type DiffLine = { mark: '+' | '-' | ' '; num: number; text: string }
754
755const DIFF_LANGS: Record<string, string> = {
756  ts: 'ts', mts: 'ts', cts: 'ts', tsx: 'tsx', js: 'js', mjs: 'js', cjs: 'js', jsx: 'jsx', py: 'py', go: 'go', rs: 'rust',
757  java: 'java', kt: 'kt', kts: 'kts', swift: 'swift', c: 'c', h: 'c', cpp: 'cpp', cc: 'cpp', hpp: 'cpp', cs: 'cs', rb: 'rb',
758  json: 'json', yaml: 'yaml', yml: 'yml', toml: 'toml', sql: 'sql', html: 'html', htm: 'html', xml: 'xml', svg: 'svg',
759  css: 'css', tf: 'hcl', hcl: 'hcl', diff: 'diff', patch: 'diff',
760}
761
762export const diffLanguage = (path: unknown): string | undefined => {
763  if (typeof path !== 'string') return undefined
764  const name = path.replace(/\\/g, '/').split('/').pop() ?? ''
765  if (/^dockerfile/i.test(name)) return 'docker'
766  const ext = /\.([a-z0-9]+)$/i.exec(name)?.[1]?.toLowerCase()
767  return ext ? DIFF_LANGS[ext] : undefined
768}
769
770const HEX = /^#[0-9a-f]{6}$/i
771
772export const tint = (base: string | undefined, color: string | undefined, share = 0.2): string | undefined => {
773  if (!base || !color || !HEX.test(base) || !HEX.test(color)) return undefined
774  const channel = (hex: string, i: number) => parseInt(hex.slice(i, i + 2), 16)
775  return `#${[1, 3, 5].map(i => Math.round(channel(base, i) * (1 - share) + channel(color, i) * share).toString(16).padStart(2, '0')).join('')}`
776}
777
778export const diffLines = (output: unknown): DiffLine[] | undefined => {
779  if (output === null || typeof output !== 'object') return undefined
780  const out = output as { type?: unknown; content?: unknown; structuredPatch?: unknown }
781  if (out.type === 'create' && typeof out.content === 'string') return lines(out.content).map((text, i) => ({ mark: '+', num: i + 1, text }))
782  if (!Array.isArray(out.structuredPatch) || out.structuredPatch.length === 0) return undefined
783  const rows: DiffLine[] = []
784  for (const hunk of out.structuredPatch as { oldStart?: unknown; newStart?: unknown; lines?: unknown }[]) {
785    if (typeof hunk?.oldStart !== 'number' || typeof hunk.newStart !== 'number' || !Array.isArray(hunk.lines)) return undefined
786    let [o, n] = [hunk.oldStart, hunk.newStart]
787    for (const l of hunk.lines) {
788      if (typeof l !== 'string' || l.startsWith('\\')) continue
789      const mark = l[0] === '+' || l[0] === '-' ? l[0] : ' '
790      let num: number
791      if (mark === '-') num = o++
792      else {
793        if (mark === ' ') o++
794        num = n++
795      }
796      rows.push({ mark, num, text: l.slice(1) })
797    }
798  }
799  return rows
800}
801
802export const renderDiffCard = (el: ElementTable, style: Style, output: unknown, fold: Fold, id: string): RenderElement | undefined => {
803  const rows = diffLines(output)
804  if (rows === undefined) return undefined
805  const { Box, Text } = el
806  const t = style.theme
807  const added = rows.filter(r => r.mark === '+').length
808  const removed = rows.filter(r => r.mark === '-').length
809  const toggle = rows.length > DIFF_LINES ? fold?.(id, rows.length - DIFF_LINES, 'fold') : null
810  const shown = rows.slice(0, toggle?.folded === false ? DIFF_MAX : DIFF_LINES)
811  const gutter = String(Math.max(0, ...shown.map(r => r.num))).length
812  const color = (mark: DiffLine['mark']) => (mark === '+' ? t.number : mark === '-' ? t.codeFlag : undefined)
813  const painted = style.followsTerminal && style.paletteRead ? style.imageTheme : undefined
814  const tints = {
815    '+': tint(t.rule, t.number) ?? tint(painted?.rule, painted?.number),
816    '-': tint(t.rule, t.codeFlag) ?? tint(painted?.rule, painted?.codeFlag),
817    ' ': undefined,
818  }
819  const lang = style.diffHighlight ? diffLanguage((output as { filePath?: unknown }).filePath) : undefined
820  const syntax = lang ? highlightRows(style, shown.map(r => r.text), lang) : null
821  return (
822    <Box flexDirection="row">
823      <Text dimColor>{'  ⎿  '}</Text>
824      <Box flexDirection="column" borderStyle="round" borderColor={t.rule} paddingX={1} flexShrink={1}>
825        <Text>
826          <Text color={t.number} dimColor={added === 0}>{`+${added}`}</Text>
827          <Text> </Text>
828          <Text color={t.codeFlag} dimColor={removed === 0}>{`−${removed}`}</Text>
829        </Text>
830        {shown.map((r, i) => {
831          const bg = tints[r.mark]
832          return (
833            <Box key={`d${i}`} flexDirection="row" {...(bg ? { backgroundColor: bg } : {})}>
834              <Text dimColor>{`${String(r.num).padStart(gutter)} `}</Text>
835              <Text color={color(r.mark)} dimColor={r.mark === ' '}>{`${r.mark} `}</Text>
836              {syntax?.[i]?.length ? (
837                <Text color={t.codeText} dimColor={r.mark === ' '}>
838                  {syntax[i]!.map((s, j) => <Text key={`d${i}.${j}`} color={s.color} italic={s.italic}>{s.text}</Text>)}
839                </Text>
840              ) : (
841                <Text color={bg || syntax ? t.codeText : color(r.mark)} dimColor={r.mark === ' '}>{r.text === '' ? ' ' : r.text}</Text>
842              )}
843            </Box>
844          )
845        })}
846        {rows.length > shown.length && toggle?.folded !== true ? <Text dimColor>{`… +${rows.length - shown.length} lines`}</Text> : null}
847        {toggle?.element ?? null}
848      </Box>
849    </Box>
850  )
851}
852
853const NOISE_LINES = /^((Error:\s*)?Exit code \d+|at\s+(\S+\s+\()?\S+:\d+(:\d+)?\)?|Node\.js v\d.*|exit status \d+|\.\.\. \d+ more)$/i
854
855const REFUSAL = /^(Error:\s*)?(The user doesn't want to proceed|Permission (for|to) .*\bdenied|The server-side auto mode classifier|\S+ hook error:)/i
856
857export const refusalReason = (output: unknown): string | undefined => {
858  if (typeof output !== 'string' || /^(Error:\s*)?Exit code \d+/m.test(output) || !REFUSAL.test(output)) return undefined
859  return output.replace(/\s+/g, ' ').trim()
860}
861
862export const CALLS_REMEMBERED = 2000
863
864export const rememberCall = (calls: Set<string>, id: string, limit = CALLS_REMEMBERED): void => {
865  calls.delete(id)
866  calls.add(id)
867  if (calls.size > limit) calls.delete(calls.values().next().value!)
868}
869
870export const errorReason = (output: unknown): string | undefined => {
871  const out = output !== null && typeof output === 'object' ? (output as Record<string, unknown>) : {}
872  const text = typeof output === 'string' ? output : typeof out.stderr === 'string' && out.stderr.trim() ? out.stderr : out.stdout
873  if (typeof text !== 'string') return undefined
874  return text.split('\n').map(l => l.trim()).filter(l => l !== '' && !NOISE_LINES.test(l)).at(-1)
875}
876
877export const renderFailure = (el: ElementTable, style: Style, output: unknown, key?: string): RenderElement | undefined => {
878  const refusal = refusalReason(output)
879  const reason = refusal ?? errorReason(output)
880  if (reason === undefined) return undefined
881  const chat = style.toolStyle === 'chat'
882  return (
883    <el.Box key={key} flexDirection="row" width="100%" justifyContent={chat ? 'flex-end' : 'flex-start'}>
884      <el.Text color={style.theme.codeFlag} wrap={refusal === undefined ? 'truncate-middle' : 'wrap'}>{`${chat ? '' : '  ⎿ '}${refusal ?? shortenPaths(reason, style)}`}</el.Text>
885    </el.Box>
886  )
887}
888
889const callLabel = (call: ToolRow, style: Style): string => {
890  const isShell = call.tool === 'Bash' || call.tool === 'PowerShell'
891  if (isShell) {
892    const command = field(call.input, 'command') ?? ''
893    const plain = call.tool === 'Bash' ? describeShell(command, style) : undefined
894    if (plain) return plain
895    const description = field(call.input, 'description')
896    if (description === undefined) return `Ran ${shortenPaths(command.split('\n')[0]!, style)}`
897    const programs = call.tool === 'Bash' ? programsOf(command) : []
898    return programs.length ? `${description} · ${programs.join(', ')}` : description
899  }
900  if (call.tool === 'Grep') {
901    const pattern = field(call.input, 'pattern')
902    const path = field(call.input, 'path')
903    if (pattern !== undefined) return `Searched ${path === undefined ? '.' : shortTarget(style, path)} for "${pattern}"`
904  }
905  const verb = call.tool === 'Skill' ? 'Loaded skill' : VERBS[call.tool] ?? call.tool.replace(/^mcp__([^_]+)__/, '$1 ')
906  const target = field(call.input, 'skill', ...TARGET_FIELDS)
907  const args = call.tool === 'Skill' ? field(call.input, 'args') : undefined
908  return target === undefined ? verb : `${verb} ${shortTarget(style, target)}${args ? ` ${args}` : ''}`
909}
910
911const quietToolRow = (el: ElementTable, style: Style, row: ToolRow, columns: number): RenderElement => {
912  const t = style.theme
913  const dot = statusColor(t, row)
914  const what = callLabel(row, style)
915  const label = `${what}${row.isInterrupted ? ' interrupted' : row.isErrored ? ' failed' : ''}`
916  return toolLayout(el, style, columns, label, dot, row.isRunning, (
917    <el.Text wrap="truncate-end" dimColor={toolDim(style)}>
918      {what}
919      {row.isInterrupted ? <el.Text dimColor> interrupted</el.Text> : row.isErrored ? <el.Text color={t.codeFlag}> failed</el.Text> : null}
920    </el.Text>
921  ))
922}
923
924export const renderToolRow = (el: ElementTable, style: Style, row: ToolRow, columns = 100): RenderElement => {
925  if (style.toolOutput === 'quiet') return quietToolRow(el, style, row, columns)
926  const { Box, Text } = el
927  const t = style.theme
928  const isShell = row.tool === 'Bash' || row.tool === 'PowerShell'
929  const verb = VERBS[row.tool] ?? row.tool.replace(/^mcp__([^_]+)__/, '$1 ')
930  const found = isShell ? { target: field(row.input, 'command')?.split('\n')[0], isPathField: false } : targetOf(style, row.input, TARGET_FIELDS)
931  const target = found.target
932  const dot = statusColor(t, row)
933  const isPath = target !== undefined && (found.isPathField || /^(~|\.{0,2}\/|[A-Za-z]:\\)/.test(target))
934
935  const label = `${verb}${target === undefined ? "" : ` ${target}`}${row.isInterrupted ? " interrupted" : row.isErrored ? " failed" : ""}`
936  return toolLayout(el, style, columns, label, dot, row.isRunning, (
937      <Text wrap="truncate-end" dimColor={toolDim(style)}>
938        <Text bold={!toolDim(style)} dimColor={toolDim(style)}>{verb}</Text>
939        {target === undefined ? null : <Text> </Text>}
940        {target === undefined ? null : isShell ? codeLine(el, style, target, 'bash', 'cmd') : <Text color={isPath ? t.path : t.inlineCode} dimColor={toolDim(style)}>{target}</Text>}
941        {row.isInterrupted ? <Text dimColor> interrupted</Text> : row.isErrored ? <Text color={t.codeFlag}> failed</Text> : null}
942      </Text>
943  ))
944}
945
946const READ_TOOLS = new Set(['Read', 'Grep', 'Glob', 'LS', 'NotebookRead', 'WebFetch', 'WebSearch', 'Skill'])
947
948const READ_COMMANDS = new Set([
949  'awk', 'basename', 'cat', 'cd', 'column', 'cut', 'diff', 'dirname', 'du', 'echo', 'file', 'find', 'grep', 'head', 'jq', 'less',
950  'ls', 'nl', 'pdftotext', 'printf', 'pwd', 'readlink', 'realpath', 'rg', 'sed', 'sort', 'stat', 'tail', 'test', 'tr', 'tree',
951  'true', 'uniq', 'wc', 'which',
952])
953
954const READ_SUBCOMMANDS: Record<string, RegExp> = {
955  git: /^(log|show|diff|status|blame|grep|ls-files|rev-parse|branch --show-current)\b/,
956  gh: /^((pr|issue|run|release) (view|list|diff|checks)|search)\b/,
957}
958
959const flagValue = (args: string[], i: number, flag: string): string => {
960  const arg = args[i]!
961  return arg === flag ? (args[i + 1] ?? '') : arg.slice(flag.length).replace(/^=/, '')
962}
963
964const ghApiReadsOnly = (rest: string[]): boolean =>
965  !rest.some((a, i) => {
966    if (/^(-[fF]|--field|--raw-field|--input)/.test(a)) return true
967    if (!/^(-X|--method)/.test(a)) return false
968    return !/^GET$/i.test(flagValue(rest, i, a.startsWith('--') ? '--method' : '-X'))
969  })
970
971type ShellPart = { words: string[]; piped: boolean }
972
973const HEREDOC = /<<(?!<)-?\s*(['"]?)([A-Za-z_][\w-]*)\1/g
974
975const dropHeredocBodies = (command: string): string => {
976  const lines = command.split('\n')
977  const out: string[] = []
978  for (let i = 0; i < lines.length; i++) {
979    const line = lines[i]!
980    out.push(line)
981    for (const [, , end] of line.matchAll(HEREDOC)) {
982      while (i + 1 < lines.length && lines[i + 1]!.trim() !== end) i++
983      i++
984    }
985  }
986  return out.join('\n')
987}
988
989const parseShell = (source: string): { segments: ShellPart[]; redirects: boolean } => {
990  const command = dropHeredocBodies(source)
991  const segments: ShellPart[] = [{ words: [], piped: false }]
992  let word = ''
993  let quote = ''
994  let started = false
995  let redirects = false
996  const current = () => segments[segments.length - 1]!
997  const endWord = () => {
998    if (started) current().words.push(word)
999    word = ''
1000    started = false
1001  }
1002  const endSegment = (piped: boolean) => {
1003    endWord()
1004    if (current().words.length) segments.push({ words: [], piped })
1005    else current().piped = piped
1006  }
1007  for (let i = 0; i < command.length; i++) {
1008    const ch = command[i]!
1009    if (quote) {
1010      if (ch === quote) quote = ''
1011      else if (ch === '\\' && quote === '"' && /[$`"\\\n]/.test(command[i + 1] ?? '')) word += command[++i]
1012      else word += ch
1013    } else if (ch === '"' || ch === "'") {
1014      quote = ch
1015      started = true
1016    } else if (ch === '\\' && command[i + 1] === '\n') {
1017      i++
1018    } else if (ch === '\\' && i + 1 < command.length) {
1019      word += command[++i]
1020      started = true
1021    } else if (ch === '\n' || ch === ';') {
1022      endSegment(false)
1023    } else if (ch === '&' && command[i + 1] === '&') {
1024      i++
1025      endSegment(false)
1026    } else if (ch === '&' && command[i + 1] !== '>' && command[i - 1] !== '|' && command[i - 1] !== '>') {
1027      endSegment(false)
1028    } else if (ch === '|') {
1029      const or = command[i + 1] === '|'
1030      if (or) i++
1031      endSegment(!or)
1032    } else if (ch === '>' || ch === '<') {
1033      const harmless = ch === '>' && /^(&\d|\s*\/dev\/null)/.exec(command.slice(i + 1))
1034      if (harmless) i += harmless[0].length
1035      else redirects = true
1036      if (/^(\d|&)$/.test(word)) {
1037        word = ''
1038        started = false
1039      }
1040      endWord()
1041    } else if (/\s/.test(ch)) {
1042      endWord()
1043    } else {
1044      word += ch
1045      started = true
1046    }
1047  }
1048  endWord()
1049  return { segments: segments.filter(s => s.words.length), redirects }
1050}
1051
1052const isAssignment = (word: string) => /^[A-Za-z_][A-Za-z0-9_]*=/.test(word)
1053
1054const LEADING_KEYWORDS = new Set(['do', 'then', 'else', 'elif', 'if', 'while', 'until', '!', '{', '(', 'time'])
1055const HEADER_KEYWORDS = new Set(['for', 'select', 'done', 'fi', 'esac', '}', ')'])
1056
1057const commandWords = (words: string[]) => {
1058  let i = 0
1059  while (i < words.length && (isAssignment(words[i]!) || LEADING_KEYWORDS.has(words[i]!))) i++
1060  return HEADER_KEYWORDS.has(words[i] ?? '') ? [] : words.slice(i)
1061}
1062
1063const readsOnly = ({ words }: ShellPart): boolean => {
1064  const [name, ...rest] = commandWords(words)
1065  if (name === undefined) return true
1066  const args = rest.join(' ')
1067  if (name === 'gh' && rest[0] === 'api') return ghApiReadsOnly(rest.slice(1))
1068  if (name in READ_SUBCOMMANDS) return READ_SUBCOMMANDS[name]!.test(args) && !(name === 'git' && rest.some(a => /^--output\b/.test(a)))
1069  if (!READ_COMMANDS.has(name)) return false
1070  if (name === 'sed') {
1071    const [script, ...files] = rest.filter(a => a !== '-n')
1072    return script !== undefined && /^\d+(,\d+)?p$/.test(script) && files.every(f => !f.startsWith('-'))
1073  }
1074  if (name === 'find') return !rest.some(a => /^-(delete|exec|execdir|ok|okdir|fprint|fprint0|fprintf|fls)$/.test(a))
1075  if (name === 'awk') return !rest.some(a => /^(-f|--file)/.test(a)) && !/system\s*\(|getline|[|>]/.test(args)
1076  if (name === 'sort') return !rest.some(a => /^(-[A-Za-z]*o|--output)/.test(a))
1077  if (name === 'uniq') return operandsOf(name, rest).length < 2
1078  if (name === 'tree') return !rest.some(a => /^(-o|--output)/.test(a))
1079  if (name === 'rg') return !rest.some(a => /^--pre/.test(a))
1080  if (name === 'pdftotext') return rest.at(-1) === '-'
1081  return true
1082}
1083
1084export const isReadOnlyCall = (tool: string, input: unknown): boolean => {
1085  if (READ_TOOLS.has(tool)) return true
1086  if (tool !== 'Bash' && tool !== 'PowerShell') return false
1087  const command = field(input, 'command')
1088  if (command === undefined) return false
1089  if (/\$\(|`|<<|\btee\b/.test(command)) return false
1090  const shell = parseShell(command)
1091  return !shell.redirects && shell.segments.every(readsOnly)
1092}
1093
1094const TRIVIAL_PROGRAMS = new Set(['cd', 'echo', 'export', 'printf', 'pwd', 'set', 'test', 'true'])
1095
1096export const programsOf = (command: string, limit = 3): string[] => {
1097  const names: string[] = []
1098  for (const part of parseShell(command).segments) {
1099    const name = commandWords(part.words)[0]?.split('/').pop()
1100    if (name && !TRIVIAL_PROGRAMS.has(name) && !names.includes(name)) names.push(name)
1101  }
1102  return names.length > limit ? [...names.slice(0, limit), '…'] : names
1103}
1104
1105export const shortenPaths = (text: string, places: Places): string => {
1106  const prefix = (path: string) => new RegExp(`(?<![\\w.~:/-])${path.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/`, 'g')
1107  let out = text
1108  if (places.cwd) out = out.replace(prefix(places.cwd), '')
1109  if (places.home) out = out.replace(prefix(places.home), '~/')
1110  return out.replace(/(?<![\w.~:/-])\/[^\s'"`;|&<>()]+/g, compactPath)
1111}
1112
1113const VALUE_FLAGS: Record<string, Set<string>> = {
1114  grep: new Set(['-A', '-B', '-C', '-m', '-e', '-f', '--max-count', '--include', '--exclude', '--exclude-dir']),
1115  rg: new Set(['-A', '-B', '-C', '-m', '-e', '-f', '-g', '-t', '--type', '--glob', '--max-count', '--max-depth']),
1116  tree: new Set(['-L', '-P', '-I']),
1117  uniq: new Set(['-f', '-s', '-w']),
1118}
1119
1120const operandsOf = (name: string, args: string[]): string[] => {
1121  const valued = VALUE_FLAGS[name]
1122  const out: string[] = []
1123  for (let i = 0; i < args.length; i++) {
1124    const a = args[i]!
1125    if (valued?.has(a)) i++
1126    else if (!a.startsWith('-')) out.push(a)
1127  }
1128  return out
1129}
1130
1131const describeCommand = (name: string, args: string[], places: Places): string | undefined => {
1132  const files = (list: string[]) => list.map(f => shortTarget(places, f)).join(', ')
1133  const ops = operandsOf(name, args)
1134  if (TRIVIAL_PROGRAMS.has(name)) return ''
1135  if (name === 'sed') {
1136    const range = args.find(a => /^\d+(,\d+)?p$/.test(a))
1137    const targets = ops.filter(a => a !== range)
1138    if (!range || !targets.length) return undefined
1139    const [from, to] = range.slice(0, -1).split(',')
1140    return `Read ${files(targets)} lines ${from}${to ? `–${to}` : ''}`
1141  }
1142  if (name === 'cat' || name === 'less' || name === 'nl') return ops.length ? `Read ${files(ops)}` : undefined
1143  if (name === 'pdftotext') return ops.length ? `Read ${files(ops.slice(0, 1))}` : undefined
1144  if (name === 'head' || name === 'tail') {
1145    const targets = ops.filter(a => !/^\d+$/.test(a))
1146    return targets.length ? `Read ${name === 'head' ? 'start' : 'end'} of ${files(targets)}` : undefined
1147  }
1148  if (name === 'grep' || name === 'rg') {
1149    const e = args.indexOf('-e')
1150    const pattern = e >= 0 ? args[e + 1] : ops[0]
1151    const paths = e >= 0 ? ops : ops.slice(1)
1152    return pattern === undefined ? undefined : `Searched ${paths.length ? files(paths) : '.'} for "${pattern}"`
1153  }
1154  if (name === 'ls' || name === 'tree') return `Listed ${ops.length ? files(ops) : '.'}`
1155  if (name === 'find') return `Listed ${args[0] && !args[0].startsWith('-') ? files([args[0]]) : '.'}`
1156  if (name === 'wc') return ops.length ? `Counted ${files(ops)}` : undefined
1157  return undefined
1158}
1159
1160export const describeShell = (command: string, places: Places = { cwd: '', home: '' }): string | undefined => {
1161  if (!isReadOnlyCall('Bash', { command })) return undefined
1162  const vars = new Map<string, string>()
1163  const expand = (w: string) => w.replace(/\$\{?([A-Za-z_][A-Za-z0-9_]*)\}?/g, (m, v: string) => vars.get(v) ?? m)
1164  const parts: string[] = []
1165  for (const segment of parseShell(command).segments) {
1166    const words = commandWords(segment.words)
1167    if (!words.length) {
1168      for (const w of segment.words.filter(isAssignment)) vars.set(w.slice(0, w.indexOf('=')), expand(w.slice(w.indexOf('=') + 1)))
1169      continue
1170    }
1171    if (segment.piped) continue
1172    const [name, ...args] = words.map(expand)
1173    if (args.some(a => /\$\{?[A-Za-z_]/.test(a))) return undefined
1174    const text = describeCommand(name!, args, places)
1175    if (text === undefined) return undefined
1176    if (text) parts.push(text)
1177  }
1178  return parts.length ? parts.join(' · ') : undefined
1179}
1180
1181const OUTPUT_LINES = 120
1182
1183const lines = (value: unknown): string[] => (typeof value === 'string' && value !== '' ? value.replace(/\n$/, '').split('\n') : [])
1184
1185export const renderExpandedShell = (el: ElementTable, style: Style, row: ToolRow & { output?: unknown }): RenderElement => {
1186  const { Box, Text } = el
1187  const t = style.theme
1188  const command = (field(row.input, 'command') ?? '').split('\n')
1189  const out = row.output !== null && typeof row.output === 'object' ? (row.output as Record<string, unknown>) : {}
1190  const stdout = lines(out.stdout)
1191  const stderr = lines(out.stderr)
1192  const shown = [...stdout.map(text => ({ text, color: undefined as string | undefined })), ...stderr.map(text => ({ text, color: t.codeFlag as string | undefined }))]
1193  const visible = shown.slice(0, OUTPUT_LINES)
1194  const dot = statusColor(t, row)
1195  return (
1196    <Box flexDirection="column">
1197      <Box flexDirection="row">
1198        <Box width={2} flexShrink={0}>
1199          <Text color={dot}>{row.isRunning ? '◌' : '●'}</Text>
1200        </Box>