SLOPSHOPPER

crab-progress

Progress bar with Token Optimizer marks, subagent panel and file tree with a mascot that reacts to what Claude is doing, and waves when it needs you. Merges…

panebandguardcommandtoastprompt
★ 1v0.7.1MITupdated 2026-10-23RyanWeera/crab-progress
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · crab-progress
│ ┃ Files: app ✕ › fix the failing auth test and add an audit log call │ ┃ app▣ client module ./ft-button.tsx ▣ client │ ┃  no git repo · git status starts after git ⏺ Read(src/auth.ts) │ ┃ init ⎿ Read 6 lines │ ┃ / : search ⏎ jump ⏺ Update(src/auth.ts) │ ┃ ▣ client module ./ft-rows.tsx ⎿ Added 2 lines, removed 1 line │ ┃ 󰕒 pushed ⏺ Bash(bun test) │ ┃ ⎿ 3 pass, 1 fail │ ┃ │ ┃ ● Done. refresh now rejects expired claims and logs an audit event. │ ┃ │ ┃ ✻ Worked for 42s · done 4:20 PM │ ┃ │ ┃ › /filetree │ ┃ ⎿ crab-progress: File tree on /work/app (follows the cwd). │ ┃ │ ┃ │ ┃ │ ┃ │ ┃ │ ┃ │ ┃ │ ┃ │ ┃ │ ┃ │ ┃ █████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 21% done ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Band
█████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 21% done
Pane · Files: app
app▣ client module ./ft-button.tsx ▣ client module ./ft-butt  no git repo · git status starts after git init / : search ⏎ jump ▣ client module ./ft-rows.tsx 󰕒 pushed
Pane · crab-agents
≈$0.00, 0 tokens, 0:00 Collapse No subagents yet.
README

<img src="docs/logo.svg" alt="crab-progress logo" width="128">

crab-progress

A mascot for your Claude Code session. A progress bar he rides, Token Optimizer's quality, context, cache and usage marks in the same band, a panel for your subagents, a file tree he digs through, and costumes for everything Claude does. He waves a question bubble when Claude needs you.

License: MIT Windows macOS Linux

<img src="docs/preview.png" alt="The mascot in his costumes, the six characters, and the file-tree dig scene" width="560">

Unofficial fan project. Not affiliated with or endorsed by Anthropic, Mojang or Microsoft.

What you get

A progress bar with a passenger

  • A bar above the prompt that moves on its own during every turn and hits 100% when the turn ends. Use the progress tool or TodoWrite to set exact done/total instead.
  • The mascot rides the head of the fill. When the bar jumps 12% or more he sprints, and when it stops he pants. A leap of 30% or more is too far to run, so he gets in a car and drives it. After the car he parks and climbs out; he never pants. If it stalls he idles instead of freezing.
  • At 100% he celebrates, and how depends on the turn: out of the car after a drive, a medal after a sprint, a green-checked terminal after coding, a firework after fixing, a shut book after reading, a ticked page after writing. Every character has its own flourish (the frog hops, the creeper swells, the slime squishes, the robot shakes, the cat sways).
  • If you also run the cache-tax / keepwarm status line and the prompt cache has gone cold, he is frozen in a cube of ice, shivering, until the next turn warms it up.
  • His costume follows the task, picked by a tiny keyword classifier that runs locally (no model call, no network): wizard hat and wand for fixing, sunglasses and a terminal for coding, a book for reading, a chef's toque and a pan for long jobs, a nightcap after a while idle, a crown after a long task.
  • Between costumes he digs the next one out of a belly pocket and puts it on where it belongs.
  • With nothing to do he roams the bar: he walks, flies with a propeller cap, reads, waves, dances and sunbathes under an umbrella.
  • When Claude needs you (a question, a plan to approve, a permission prompt) he stops, waves a claw and a gold question bubble bobs over his head, and the bar turns gold, until you answer.

Token Optimizer in the same band

Merged from token-optimizer's desktop bar and drawn in the crab's style, under the progress row:

  • One sentence about what matters now, with at most one button.
  • Five marks with hover cards: quality grade, context, cache countdown, 5-hour and weekly usage.
  • An unfold arrow for session time, tool calls, checkpoint and tokens saved, plus Clean up, Start fresh, Keep warm and Full dashboard.
  • It needs Token Optimizer installed (it reads its data). Turn the rows off with CRAB_OPTIMIZER_BAR=0 and start slim with CRAB_OPTIMIZER_BAR_SIZE=slim in your settings env; set TOKEN_OPTIMIZER_STATUS_BAR=0 so Token Optimizer's own bar does not draw a second band.
  • Idle sessions stand still and redraw rarely, and session switches do not wait on its reads.

A subagents panel (/agents-info)

  • Cost, tokens and time up top, then one row per subagent with model, effort, context use, cost, elapsed time and its own progress bar. Each row's mascot dresses for what that agent is doing and cheers, then idles, when it finishes.

A file tree (/filetree)

  • An IDE-style tree with git status that shimmers where Claude reads and writes. The desktop layout is roomier (two-line rows) and styled to match the rest of the mod.
  • While files are being touched the crab works inside the real rows: a burrow strip opens under three of them, he dives into the file that was touched, throws things out until he finds a gem, pops up, jumps into another file and pokes his head out of a third. It is drawn in the tree's own grid, so it lines up with the rows and scrolls with them.

Characters (/mascot)

/mascot claude, frog, creeper, slime, robot or cat (or /frog, /creeper, ...). Every costume and animation works on every character, and your pick is remembered.

Install

You need Claude Code with plugin support (built and tested on 2.1.295) and git. The steps are the same on every system.

From inside Claude Code (all systems)

/plugin marketplace add RyanWeera/crab-progress
/plugin install crab-progress@crab-progress

Then start a new session. To update later:

/plugin marketplace update crab-progress

From a terminal

# Homebrew users: brew install git   (Xcode command line tools also include git)
claude plugin marketplace add RyanWeera/crab-progress
claude plugin install crab-progress@crab-progress

Run it without installing, straight from a clone:

git clone https://github.com/RyanWeera/crab-progress.git ~/crab-progress
claude --plugin-dir ~/crab-progress

The file tree uses git, find and du, which macOS already has. Quick Look preview uses qlmanage.

# Debian/Ubuntu: sudo apt install git     Fedora: sudo dnf install git     Arch: sudo pacman -S git
claude plugin marketplace add RyanWeera/crab-progress
claude plugin install crab-progress@crab-progress

Or from a clone:

git clone https://github.com/RyanWeera/crab-progress.git ~/crab-progress
claude --plugin-dir ~/crab-progress

The file tree uses git, find and du. Opening files uses your desktop's default handler.

# winget install --id Git.Git   (Git for Windows supplies find, du and sh, which the file tree uses)
claude plugin marketplace add RyanWeera/crab-progress
claude plugin install crab-progress@crab-progress

Or from a clone:

git clone https://github.com/RyanWeera/crab-progress.git $HOME\crab-progress
claude --plugin-dir $HOME\crab-progress

If you already use the originals

This mod includes the filetree and savvy-progress mods and registers the same /filetree and /agents-info commands. Remove the originals first so they don't fight:

claude plugin uninstall filetree
claude plugin uninstall savvy-progress

Uninstall

claude plugin uninstall crab-progress@crab-progress

Commands

CommandWhat it does
/mascot <name>Switch character: claude, frog, creeper, slime, robot, cat. /mascot list shows them.
/frog /creeper /slime /robot /catShortcuts for /mascot <name>.
/agents-infoShow or hide the subagents panel.
/filetree [path]Show the file tree (no path: the current directory).

Settings

The file tree keeps its upstream options, set in Claude Code's plugin settings:

OptionValuesDefaultDoes
activityreads and writes, writes, reads, nonereads and writesWhich file activity shimmers in the tree
glyphsauto, nerd, plainautoIcon set (Nerd Font icons need a Nerd Font)
followon, offonTree scrolls to what Claude touches
starton, offonOpen the pane when a session starts
columndate, age, sizedateRight-hand column
hintson, offon"git status starts after git init" line
opentextemptyWhat double-click/Enter runs (preview shows it in the pane)

FAQ

Is anything sent over the network? No. The mod has no network access. Costume choice is a local keyword classifier, not a model call. The file tree runs git, find and du on your machine.

Does it cost tokens? The mod adds one small tool (progress) and one for subagents (step), both optional for the model to call. It never edits your prompts.

The mascot isn't moving. Some systems have animations turned off in the OS. The mod ignores that setting on purpose, but a restart of the session is needed after installing or updating.

I switched characters and nothing changed. The choice applies to the bar, the panel and the tree header immediately; the crab inside the tree rows keeps the same shape and only takes the character's colour.

How it works

The mascot is drawn as SVG rectangles on a 30-wide pixel grid with CSS animation. Animations start mid-cycle so the once-a-second redraw never restarts them. Inside the file tree he is drawn with half-block characters, because the tree is a text grid.

hooks/
  register.tsx   bar, panel hooks, commands, state
  crab.ts        sprites, characters, animation CSS
  classify.ts    what he is doing, and the idle-roam script
  agents.ts      subagents panel drawing
  dig.ts         the SVG digging scene (agents panel)
  ft-*.ts(x)     the file tree (merged from filetree), incl. ft-rows.tsx with the in-row crab
types/           state contract

Develop with the Claude Code CLI from the repo root:

claude plugin validate .
claude plugin test .

Credits and merged mods

This mod folds three existing mods into one so they share a look and one set of hooks; see NOTICE.md. Two are MIT licensed. token-optimizer is PolyForm Noncommercial, so hooks/to/ is for noncommercial use only.

Merged modAuthorLicenseLink
filetree: file tree pane with git status and Claude activityKurt Buhler / data-goblinMIThttps://github.com/data-goblin/claude-code-filetree
savvy-progress: progress bar and subagents paneljohnnyvizzMIThttps://github.com/JohnnyVizz/claude-kit (plugins/savvy-progress)
token-optimizer: the desktop status bar (quality, context, cache, usage marks and buttons)Alex GreenshpunPolyForm Noncommercial 1.0.0https://github.com/alexgreensh/token-optimizer

Used alongside it, not merged:

ModAuthorLink
cache-tax: keep-warm and the cold-cache line the crab freezes forKaran Bansal (karanb192)https://github.com/karanb192/cache-tax
claude-reflect: learned rules from your correctionsBayram Annakovhttps://github.com/BayramAnnakov/claude-reflect

Reference art and ideas:

Contributing

Issues and pull requests are welcome. See CONTRIBUTING.md.

License

MIT for this repository's own code (LICENSE). The merged file tree keeps its upstream MIT license (LICENSE-filetree); see NOTICE.md for savvy-progress. The merged Token Optimizer code in hooks/to/ is PolyForm Noncommercial 1.0.0 (LICENSE-token-optimizer, small-business permission): noncommercial use only.

Source 25 files
hooks/register.tsx 691 lines
1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register } from 'claude-code'
3
4import type { AgentRun, Bar, Character, Outfit, Panel, Run, Sess } from '../types'
5import { DIG_H, DIG_W, digSvg } from './dig'
6import { register as registerTree } from './ft-register'
7import { registerOptimizer } from './to/hooks/register'
8import type { Dest } from './crab'
9import { BAR_H, BAR_Y, CHARACTERS, CRAB_H, CRAB_W, ITEM_COLOR, ITEM_DEST, PX, ROW_H, WIN, codeWindow, crabCss, crabSvg } from './crab'
10import { CHANGE_MS, MOVING, autoRatio, isIcedNow, isLong, pickOutfit, roamAt, winPose } from './classify'
11import {
12  HEADER_H,
13  LINE_H,
14  ROW_H as AGENT_ROW_H,
15  agentLine,
16  agentSvg,
17  compactSvg,
18  costOf,
19  taskOutfit,
20  ctxBar,
21  gridSvg,
22  gridTiles,
23  headerSvg,
24  modelName,
25  sameTask,
26  summaryOf,
27  totals,
28  windowOf,
29} from './agents'
30
31const BLANK: Sess = { busy: false, startedAt: 0, lastActive: 0, doneAt: 0, tools: 0, edits: 0, prompt: '', lastTool: '', end: '' }
32const FLAT: Bar = { from: 0, to: 0, at: 0, ref: 0, movedAt: 0, bigAt: 0 }
33
34const run = atom({ plugin: 'crab-progress', key: 'run' } as const, null)
35const sess = atom({ plugin: 'crab-progress', key: 'sess' } as const, BLANK)
36const look = atom({ plugin: 'crab-progress', key: 'look' } as const, { outfit: 'idle', since: 0 })
37const now = atom({ plugin: 'crab-progress', key: 'now' } as const, 0)
38const bar = atom({ plugin: 'crab-progress', key: 'bar' } as const, FLAT)
39const char = atom({ plugin: 'crab-progress', key: 'char' } as const, 'claude')
40const agents = atom({ plugin: 'crab-progress', key: 'agents' } as const, [])
41const panel = atom({ plugin: 'crab-progress', key: 'panel' } as const, { isCompact: false, isDoneCollapsed: false })
42const files = atom({ plugin: 'crab-progress', key: 'files' } as const, { at: 0, names: [], paths: [] })
43
44const TOOL = 'mcp__crab-progress__progress'
45const STEP_TOOL = 'mcp__crab-progress__step'
46const PANE = 'crab-agents'
47const MAX_AGENTS = 200
48const FILE_TOOLS = new Set(['Read', 'Edit', 'Write', 'MultiEdit', 'NotebookEdit', 'Glob', 'Grep'])
49const DIG_ACTIVE_MS = 8_000
50const EDIT_TOOLS = new Set(['Edit', 'Write', 'MultiEdit', 'NotebookEdit'])
51const TASK_OUTFITS = new Set<Outfit>(['coding', 'fixing', 'chef', 'working', 'reading', 'writing'])
52// Tools that stop and wait for the person, and how long a pending request waits before he reacts
53// (an auto-approved permission check is over in a blink and should not flash a question).
54const AWAIT_TOOLS = new Set(['AskUserQuestion', 'ExitPlanMode'])
55const ASK_MS = 800
56// Shell tools: while one is in flight the crab sits at a console of its own.
57const CMD_TOOLS: Record<string, 'shell' | 'pwsh'> = { Bash: 'shell', PowerShell: 'pwsh' }
58const HOLD_MS = 15_000
59const EASE_MS = 1_200
60// A rise of this much at once sends him sprinting; when it stops he pants.
61const BIG_JUMP = 0.12
62// An even bigger rise and he takes a car instead.
63const CAR_JUMP = 0.3
64const DRIVE_MS = 3_000
65const EXIT_MS = 1_500
66const RUN_MS = 1_800
67const PANT_MS = 3_500
68// The beat between the run (or the bar reaching its end) and the victory pose.
69const TRANS_MS = 1_400
70const SETTLE_MS = 400
71const STALL_MS = 5_000
72const MAX = 200
73const CLAY = '#D97757'
74const DONE = '#5fbf8f'
75const GOLD_BAR = '#D9A23A'
76const FONT = "-apple-system,BlinkMacSystemFont,'SF Pro Text','Segoe UI',sans-serif"
77// Room around the bar: padding above the crab, a thicker bar, space below, and a wider gap before the percent.
78const PAD = 10
79const THICK = 14
80const H = ROW_H + 19
81
82const WORD: Record<Outfit, string> = {
83  idle: 'idle',
84  changing: 'changing',
85  run: 'running',
86  drive: 'driving',
87  exitCar: 'done',
88  medal: 'done',
89  winCode: 'done',
90  winFix: 'done',
91  winRead: 'done',
92  winWrite: 'done',
93  frozen: 'frozen',
94  pant: 'panting',
95  park: 'parking',
96  brace: 'finishing',
97  stroll: 'idle',
98  coding: 'coding',
99  fixing: 'fixing',
100  chef: 'cooking',
101  reading: 'reading',
102  writing: 'writing',
103  sleeping: 'zzz',
104  working: 'working',
105  done: 'done',
106  crown: 'done',
107  error: 'error',
108  greeting: 'hi',
109  walkCool: 'idle',
110  walkWizard: 'idle',
111  walkChef: 'idle',
112  walkCode: 'idle',
113  walkCmd: 'idle',
114  fly: 'idle',
115  sunbathe: 'chilling',
116  propeller: 'idle',
117  cool: 'idle',
118  dance: 'idle',
119  ask: 'waiting for you',
120  shell: 'running a command',
121  pwsh: 'running PowerShell',
122}
123
124const clean = (n: unknown): number => (Number.isFinite(n) ? Math.min(MAX, Math.max(0, Math.round(n as number))) : 0)
125
126const targetOf = (r: Run | null, s: Sess, at: number): number =>
127  r && r.total > 0 ? Math.min(1, r.done / r.total) : autoRatio(s, at)
128
129// Where the eased bar is at time `at`.
130const shownAt = (b: Bar, at: number): number => b.from + (b.to - b.from) * Math.min(1, Math.max(0, (at - b.at) / EASE_MS))
131
132const isStalled = (s: Sess, b: Bar, at: number): boolean => s.busy && at - b.movedAt > STALL_MS
133
134// One step of the world: new clock, outfit, and the bar's next ease segment.
135async function refresh($: EngineInterface): Promise<void> {
136  const at = await $.clock.now()
137  const s = await read($, sess)
138  const target = targetOf(await read($, run), s, at)
139  await update($, now, () => at)
140  const b = await update($, bar, prev => {
141    // A drop (new prompt, done window over) snaps; a rise eases.
142    if (target < prev.to - 0.05) return { from: target, to: target, at, ref: target, movedAt: at, bigAt: 0 }
143    const isMoved = Math.abs(target - prev.ref) >= 0.02
144    const isChanged = Math.abs(target - prev.to) > 0.0005
145    return {
146      from: isChanged ? shownAt(prev, at) : prev.from,
147      to: isChanged ? target : prev.to,
148      at: isChanged ? at : prev.at,
149      ref: isMoved ? target : prev.ref,
150      movedAt: isMoved ? at : prev.movedAt,
151      bigAt: isChanged && target - prev.to >= BIG_JUMP ? at : prev.bigAt,
152      // A smaller jump right behind a big one keeps the big one's gear (no car cut short into a pant).
153      bigBy: isChanged && target - prev.to >= BIG_JUMP ? Math.max(target - prev.to, at - prev.bigAt < DRIVE_MS + PANT_MS ? prev.bigBy ?? 0 : 0) : prev.bigBy,
154    }
155  })
156  // A bar that has stopped moving falls back to the idle animations.
157  const want: Outfit = isStalled(s, b ?? FLAT, at) ? 'idle' : pickOutfit(s, at)
158  await update($, look, prev => {
159    if (prev.outfit === want) return prev
160    const isSticky = TASK_OUTFITS.has(prev.outfit) && TASK_OUTFITS.has(want) && at - prev.since < HOLD_MS
161    return isSticky ? prev : { outfit: want, since: at }
162  })
163}
164
165// The once-a-second world tick. A session nobody has touched for QUIET_MS stops strolling (the
166// crab stands still) and is redrawn every SLOW_MS: a mod that redraws by the second all day
167// slows the whole app.
168const QUIET_MS = 120_000
169const SLOW_MS = 15_000
170let worldTimer: { cancel: () => void } | null = null
171let worldAt = 0
172
173const isQuiet = (s: Sess, at: number): boolean => !s.busy && at - Math.max(s.lastActive, s.doneAt) > QUIET_MS
174
175async function worldTick($: EngineInterface): Promise<void> {
176  const at = await $.clock.now()
177  if (at - worldAt < SLOW_MS && isQuiet(await read($, sess), at)) return
178  worldAt = at
179  await refresh($)
180}
181
182// A question or permission prompt is over (or at least something else happened): he is back at work.
183async function clearAsk($: EngineInterface): Promise<void> {
184  await update($, sess, s => (s.ask ? { ...s, ask: 0 } : s))
185}
186
187// Opens the agents panel, or closes it when it is up; true when it ends up open.
188async function togglePane($: EngineInterface): Promise<boolean> {
189  const isOpen = (await $.ui.panes()).some(p => p.id === PANE)
190  if (isOpen) {
191    await $.ui.close({ id: PANE })
192    return false
193  }
194  await $.ui.open({ id: PANE, title: 'Agents' })
195  return true
196}
197
198async function switchTo($: EngineInterface, name: Character): Promise<{ text: string }> {
199  await update($, char, () => name)
200  await $.store.set('mascot', name)
201  return { text: name === 'claude' ? 'Mascot is back to the crab.' : `Mascot is now the ${name}.` }
202}
203
204// `roam` is set while nothing is happening: he strolls about the empty bar on his own.
205// `change` is set during a costume change: he digs the next one out of a pocket.
206const rowSvg = (b: Bar, at: number, outfit: Outfit, W: number, isWalking: boolean, roam: { from: number; to: number } | null, character: Character, change?: { item: string; age: number; dest?: Dest }, settle?: number): string => {
207  const barW = Math.max(60, W - 66)
208  const px = (r: number): number => {
209    const w = Math.round(barW * r)
210    return w ? Math.max(w, THICK) : 0
211  }
212  const crabAt = (r: number): number => Math.max(0, Math.min(barW - CRAB_W, Math.round(barW * r) - CRAB_W / 2))
213  const cur = shownAt(b, at)
214  const rem = roam ? 1000 : Math.max(0, b.at + EASE_MS - at)
215  const travel = (x: number): number => Math.round((barW - CRAB_W) * x)
216  const fromW = px(cur)
217  const toW = px(b.to)
218  const crabX = roam ? travel(roam.to) : crabAt(b.to)
219  const dx = (roam ? travel(roam.from) : crabAt(cur)) - crabX
220  const color = outfit === 'ask' ? GOLD_BAR : WIN.has(outfit) && outfit !== 'crown' ? DONE : CLAY
221  const winW = Math.round(40 * PX)
222  const codeX = crabX + CRAB_W + winW + 10 > barW ? crabX - winW - 8 : crabX + CRAB_W + 8
223  const slide = rem > 0 ? `@keyframes gw{from{width:${fromW}px}to{width:${toW}px}}@keyframes cm{from{transform:translateX(${dx}px)}to{transform:translateX(0)}}.fill{animation:gw ${rem}ms linear forwards}.cm{animation:cm ${rem}ms linear forwards}` : ''
224  return `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}" viewBox="0 0 ${W} ${H}">${crabCss(at)}<style>${slide}</style><g transform="translate(0 ${PAD})">
225<rect class="k" x="0" y="${BAR_Y}" width="${barW}" height="${THICK}" rx="${THICK / 2}"/>
226<rect class="fill" x="0" y="${BAR_Y}" width="${toW}" height="${THICK}" rx="${THICK / 2}" fill="${color}"/>
227${outfit === 'idle' ? codeWindow(Math.round(codeX), BAR_Y - Math.round(19 * PX)) : ''}
228<g class="cm">${crabSvg(outfit, crabX, BAR_Y - CRAB_H, isWalking, character, change, settle)}</g>
229<text class="m" x="${W}" y="${BAR_Y + THICK}" text-anchor="end" font-family="${FONT}" font-size="16" font-variant-numeric="tabular-nums">${Math.round(cur * 100)}%</text>
230</g>
231</svg>`
232}
233
234export const register: Register = (on, options) => {
235  // The file tree (merged from the filetree mod) registers its own hooks beside ours.
236  registerTree(on, options)
237
238  on('session.start', async ($, e, next) => {
239    const started = await next(e)
240    const at = await $.clock.now()
241    await update($, sess, s => ({ ...s, lastActive: at }))
242    await update($, now, () => at)
243    // The registrations are independent: made together, they do not hold a session switch one by one.
244    const [saved] = await Promise.all([
245      $.store.get('mascot'),
246      $.tool.register({
247        name: 'progress',
248        description:
249          "Optional: report the main task's progress to the bar above the prompt. The bar already moves by itself; call with total (planned steps) and done (finished steps) only to show exact step progress.",
250        inputSchema: {
251          type: 'object',
252          properties: {
253            done: { type: 'integer', minimum: 0, description: 'Steps finished so far.' },
254            total: { type: 'integer', minimum: 1, description: 'Steps planned.' },
255            title: { type: 'string', description: 'Few-word name of the task.' },
256          },
257          required: ['done'],
258        },
259        isDeferred: false,
260      }),
261      $.tool.register({
262        name: 'step',
263        description:
264          'For subagents: report progress on your own task to the agents panel. Right after reading the brief, call it with `total` (your plan in 3-8 steps) and `done: 0`; call it again as each step finishes. Cheap and silent: it only draws a bar.',
265        inputSchema: {
266          type: 'object',
267          properties: {
268            done: { type: 'integer', minimum: 0, description: 'Steps finished so far.' },
269            total: { type: 'integer', minimum: 1, description: 'Steps planned; may change if the plan changes.' },
270            note: { type: 'string', description: 'The step in progress, a few words.' },
271          },
272          required: ['done'],
273        },
274        isDeferred: false,
275      }),
276      $.command.register({
277        name: 'mascot',
278        description: `Change the mascot: /mascot ${CHARACTERS.join(' | ')}, or /mascot list`,
279      }),
280      $.command.register({
281        name: 'agents-info',
282        description: 'Show or hide the panel of subagents: running and finished, with model, context, cost and time',
283      }),
284      ...CHARACTERS.filter(name => name !== 'claude').map(name => $.command.register({ name, description: `Turn the mascot into the ${name}` })),
285    ])
286    if (typeof saved === 'string' && (CHARACTERS as string[]).includes(saved)) await update($, char, () => saved as Character)
287    void $.ui.open({ id: PANE, title: 'Agents' })
288    // One timer however many sessions start: a second one would double every redraw for good.
289    worldTimer?.cancel()
290    worldTimer = $.clock.every(1000, () => void worldTick($))
291    return started
292  })
293
294  on('prompt.submit', async ($, e, next) => {
295    const at = await $.clock.now()
296    const text = String((e as unknown as { text?: unknown }).text ?? '')
297    await update($, sess, () => ({ busy: true, startedAt: at, lastActive: at, doneAt: 0, tools: 0, edits: 0, prompt: text.slice(0, 300), lastTool: '', end: '' }))
298    await update($, run, () => null)
299    await update($, bar, () => ({ from: 0, to: 0, at, ref: 0, movedAt: at, bigAt: 0 }))
300    await refresh($)
301    return next(e)
302  })
303
304  on('tool.call', { tool: TOOL }, async ($, e) => {
305    if (e.agentId) return { result: 'ignored: only the main session reports' }
306    const input = e as unknown as { done?: number; total?: number; title?: string }
307    const next = await update($, run, prev => {
308      const total = clean(input.total ?? prev?.total)
309      return { title: input.title?.trim().slice(0, 120) || prev?.title || 'progress', total, done: Math.min(total || MAX, clean(input.done)) }
310    })
311    await refresh($)
312    return { result: `ok: ${next && next.total ? Math.round((next.done / next.total) * 100) : 0}%` }
313  })
314
315  on('tool.call', async ($, e, next) => {
316    const at = await $.clock.now()
317    if (FILE_TOOLS.has(e.tool)) {
318      const input = e as unknown as { file_path?: string; notebook_path?: string; path?: string; pattern?: string }
319      const target = input.file_path ?? input.notebook_path ?? input.path ?? input.pattern
320      if (typeof target === 'string' && target) {
321        const name = target.split(/[\\/]/).filter(Boolean).pop() ?? target
322        await update($, files, f => ({ at, names: [name, ...f.names.filter(n => n !== name)].slice(0, 3), paths: [target, ...f.paths.filter(p => p !== target)].slice(0, 3) }))
323      }
324    }    // A subagent's tool call changes its own row (and costume), not the main session's state.
325    if (e.agentId) {
326      await update($, sess, s => (s.ask ? { ...s, ask: 0 } : s))
327      const agentId = e.agentId
328      await update($, agents, list =>
329        list.map((a): AgentRun => {
330          if (a.agentId !== agentId) return a
331          const grown = { ...a, tools: (a.tools ?? 0) + 1, lastTool: e.tool, edits: (a.edits ?? 0) + (EDIT_TOOLS.has(e.tool) ? 1 : 0) }
332          const want = taskOutfit(grown, at)
333          const held = a.outfit && a.outfitAt !== undefined && at - a.outfitAt < HOLD_MS && TASK_OUTFITS.has(a.outfit) && TASK_OUTFITS.has(want)
334          return held ? grown : { ...grown, outfit: want, outfitAt: at }
335        }),
336      )
337      return next(e)
338    }    if (e.tool === 'TodoWrite' && !e.agentId) {
339      const todos = (e as unknown as { todos?: { status?: string }[] }).todos
340      if (Array.isArray(todos) && todos.length > 0) {
341        const list = todos.slice(0, MAX)
342        await update($, run, () => ({ title: 'todo', total: list.length, done: list.filter(t => t.status === 'completed').length }))
343      }
344    }
345    await update($, sess, s => ({
346      ...s,
347      busy: true,
348      startedAt: s.busy ? s.startedAt : at,
349      lastActive: at,
350      doneAt: 0,
351      tools: s.tools + 1,
352      lastTool: e.tool,
353      edits: s.edits + (EDIT_TOOLS.has(e.tool) ? 1 : 0),
354      ask: AWAIT_TOOLS.has(e.tool) ? at : 0,
355      cmd: CMD_TOOLS[e.tool],
356    }))
357    await refresh($)
358    if (!AWAIT_TOOLS.has(e.tool) && !CMD_TOOLS[e.tool]) return next(e)
359    // The call returns when the person answers a question, or when the command finishes: the
360    // bubble, or the console, lasts exactly that long.
361    try {
362      return await next(e)
363    } finally {
364      await update($, sess, s => (s.ask || s.cmd ? { ...s, ask: 0, cmd: undefined } : s))
365      await refresh($)
366    }
367  })
368
369  // A permission dialog the person has to answer. Only a real dialog fires this: a command that
370  // the rules or the mode allow never does, so running commands do not make him ask. He reacts
371  // once it has waited a moment; the next sign of life (a tool call or result, the end of the turn)
372  // puts him back to work.
373  on('classic.PermissionRequest', async ($, e, next) => {
374    const at = await $.clock.now()
375    await update($, sess, s => (s.busy ? { ...s, ask: at } : s))
376    return next(e)
377  })
378  on('classic.PostToolUse', async ($, e, next) => {
379    await clearAsk($)
380    return next(e)
381  })
382  on('classic.PostToolUseFailure', async ($, e, next) => {
383    await clearAsk($)
384    return next(e)
385  })
386  on('classic.PermissionDenied', async ($, e, next) => {
387    await clearAsk($)
388    return next(e)
389  })
390
391  on('turn.complete', async ($, e, next) => {
392    if (!e.agentId) {
393      const at = await $.clock.now()
394      const reason = (e as unknown as { reason?: string }).reason
395      await update($, sess, s => ({ ...s, ask: 0, cmd: undefined, busy: false, lastActive: at, doneAt: at, end: reason !== 'answer' ? 'fail' : isLong(s, at) ? 'long' : 'ok' }))
396      await update($, run, () => null)
397      await refresh($)
398    } else {
399      const at = await $.clock.now()
400      const agentId = e.agentId
401      const reason = (e as unknown as { reason?: string }).reason
402      const usage = (e as unknown as { usage?: { model?: string; input_tokens: number; output_tokens: number; cache_read_input_tokens: number; cache_creation_input_tokens: number } }).usage
403      await update($, agents, list =>
404        list.map((a): AgentRun => {
405          if (a.agentId !== agentId) return a
406          // A run whose steps went unseen still gets the turn's own sum.
407          const fallback = a.steps === 0 && usage
408          return {
409            ...a,
410            status: reason === 'answer' ? 'done' : 'failed',
411            endedAt: at,
412            ...(fallback && usage
413              ? {
414                  model: usage.model || a.model,
415                  tokens: usage.input_tokens + usage.output_tokens + usage.cache_read_input_tokens + usage.cache_creation_input_tokens,
416                  costUsd: costOf(usage.model || a.model, usage, false),
417                }
418              : {}),
419          }
420        }),
421      )
422    }
423    return next(e)
424  })
425
426  // A worker's own progress: the call runs in the worker's loop, so agentId names it.
427  on('tool.call', { tool: STEP_TOOL }, async ($, e) => {
428    const input = e as unknown as { done?: number; total?: number; note?: string }
429    const agentId = e.agentId
430    if (!agentId) return { result: 'ignored: only subagents report steps' }
431    let matched = false
432    await update($, agents, list =>
433      list.map(a => {
434        if (a.agentId !== agentId) return a
435        matched = true
436        const total = Math.max(0, Math.round(input.total ?? a.stepTotal ?? 0))
437        const done = Math.max(0, Math.round(input.done ?? a.stepDone ?? 0))
438        return { ...a, stepTotal: total, stepDone: total ? Math.min(total, done) : done, stepNote: input.note?.trim() || undefined }
439      }),
440    )
441    return { result: matched ? 'ok' : `ignored: no panel row for agent ${agentId}` }
442  })
443
444  on('agent.spawn', async ($, e, next) => {
445    const started = await next(e)
446    if (started.deny !== undefined) return started
447    const at = await $.clock.now()
448    await update($, agents, list => {
449      const round = 1 + list.filter(a => e.description && sameTask(a.description, e.description)).length
450      const run: AgentRun = {
451        id: started.agentId ?? e.tool_use_id,
452        agentId: started.agentId,
453        type: e.subagentType,
454        description: e.description,
455        model: started.model,
456        status: 'running',
457        startedAt: at,
458        contextTokens: 0,
459        contextMax: windowOf(started.model),
460        tokens: 0,
461        costUsd: 0,
462        steps: 0,
463        round,
464      }
465      return [...list.filter(a => a.id !== run.id), run].slice(-MAX_AGENTS)
466    })
467    return started
468  })
469
470  // Each model request of a subagent: live context, tokens and cost.
471  on('turn.step', async function* ($, e, next) {
472    const result = yield* next(e)
473    const agentId = e.agentId
474    const usage = result.usage
475    if (!agentId || !usage) return result
476    const model = usage.model || e.model
477    await update($, agents, list =>
478      list.map((a): AgentRun => {
479        if (a.agentId !== agentId) return a
480        const prompt = (usage.input_tokens || 0) + (usage.cache_read_input_tokens || 0) + (usage.cache_creation_input_tokens || 0)
481        return {
482          ...a,
483          model,
484          effort: typeof e.effort === 'string' ? e.effort : a.effort,
485          status: 'running',
486          endedAt: undefined,
487          contextTokens: prompt + (usage.output_tokens || 0),
488          contextMax: windowOf(model),
489          tokens: a.tokens + prompt + (usage.output_tokens || 0),
490          costUsd: a.costUsd + costOf(model, usage, true),
491          steps: a.steps + 1,
492        }
493      }),
494    )
495    return result
496  })
497
498  on('command.run', { command: 'agents-info' }, async $ => {
499    const isOpen = await togglePane($)
500    return { text: isOpen ? 'Agents panel opened.' : 'Agents panel closed.' }
501  })
502
503  on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
504    const ui = $.ui.resolve(e)
505    const { Box, Text, Button } = ui
506    const list = await read($, agents)
507    const p: Panel = await read($, panel)
508    await read($, now) // redraw on every tick
509    const at = await $.clock.now()
510    const character = await read($, char)
511    const touched = await read($, files)
512    const isDigging = touched.at > 0 && at - touched.at <= DIG_ACTIVE_MS
513    const running = list.filter(a => a.status === 'running').reverse()
514    const finished = list.filter(a => a.status !== 'running').reverse()
515    const t = totals(list, at)
516
517    const toggleCompact = (
518      <Button key="compact" label={p.isCompact ? 'Expand' : 'Collapse'} plain onPress={() => update($, panel, prev => ({ ...prev, isCompact: !prev.isCompact }))} />
519    )
520    const toggleDone = (
521      <Button
522        key="done"
523        label={`${p.isDoneCollapsed ? '\u25b8' : '\u25be'} Finished \u00b7 ${finished.length}`}
524        plain
525        onPress={() => update($, panel, prev => ({ ...prev, isDoneCollapsed: !prev.isDoneCollapsed }))}
526      />
527    )
528
529    if (e.surface !== 'terminal' && 'Svg' in ui) {
530      const { Svg } = ui
531      const W = Math.max(240, Math.min(900, (e.props.bodyColumns || 40) * 8 - 8))
532      if (list.length > 0 && p.isCompact) {
533        return (
534          <Box flexDirection="column" gap={1}>
535            {isDigging && <Svg source={digSvg(at, touched.names, character)} alt="Claude digging through files" width={DIG_W} height={DIG_H} />}
536            <Svg source={compactSvg(W, list, at, t, character)} alt={`${list.length} agents, ${summaryOf(t)}`} width={W} height={40} />
537            {toggleCompact}
538          </Box>
539        )
540      }
541      const shownDone = p.isDoneCollapsed ? 0 : finished.length
542      const lines = 1 + (list.length === 0 ? 1 : 0) + (running.length > 0 ? 1 : 0) + (finished.length > 0 ? 1 : 0)
543      const gridH = (isDigging ? DIG_H : 0) + HEADER_H + lines * LINE_H + (running.length + shownDone) * AGENT_ROW_H
544      return (
545        <Box flexDirection="column">
546          {gridTiles(gridH).map((tileH, i) => (
547            <Svg key={`grid-${i}`} source={gridSvg(W, tileH)} alt="dotted grid" width={W} height={tileH} />
548          ))}
549          <Box flexDirection="column" position="absolute" top={0} left={0} right={0}>
550            {isDigging && <Svg source={digSvg(at, touched.names, character)} alt="Claude digging through files" width={DIG_W} height={DIG_H} />}
551            <Svg source={headerSvg(W, at, t)} alt={summaryOf(t)} width={W} height={HEADER_H} />
552            {toggleCompact}
553            {list.length === 0 && <Text dimColor>No subagents yet.</Text>}
554            {running.length > 0 && <Text dimColor>{`Running \u00b7 ${running.length}`}</Text>}
555            {running.map(a => (
556              <Svg key={a.id} source={agentSvg(W, a, at, character)} alt={`${a.description}: ${modelName(a.model)}, running`} width={W} height={AGENT_ROW_H} />
557            ))}
558            {finished.length > 0 && toggleDone}
559            {!p.isDoneCollapsed &&
560              finished.map(a => (
561                <Svg key={a.id} source={agentSvg(W, a, at, character)} alt={`${a.description}: ${modelName(a.model)}, finished`} width={W} height={AGENT_ROW_H} />
562              ))}
563          </Box>
564        </Box>
565      )
566    }
567
568    // Terminal: the same rows in text.
569    const cols = Math.max(24, e.props.bodyColumns || 40)
570    const barW = Math.max(6, Math.min(20, cols - 34))
571    const row = (a: AgentRun) => {
572      const l = agentLine(a, at)
573      return (
574        <Box key={a.id} flexDirection="column" marginBottom={1}>
575          <Text bold wrap="truncate-end">{l.title}</Text>
576          <Text dimColor wrap="truncate-end">{l.meta}</Text>
577          <Text wrap="truncate-end">
578            <Text color="#D97757">{ctxBar(l.progress === null ? l.ctx : l.progress * 100, barW)}</Text>
579            <Text dimColor>{` ${l.stats}`}</Text>
580          </Text>
581        </Box>
582      )
583    }
584    return (
585      <Box flexDirection="column">
586        <Box flexDirection="row" justifyContent="space-between">
587          <Text dimColor>{summaryOf(t)}</Text>
588          {toggleCompact}
589        </Box>
590        {list.length === 0 && <Text dimColor>No subagents yet.</Text>}
591        {!p.isCompact && running.map(row)}
592        {finished.length > 0 && toggleDone}
593        {!p.isCompact && !p.isDoneCollapsed && finished.map(row)}
594      </Box>
595    )
596  })
597
598  on('command.run', { command: 'mascot' }, async ($, e) => {
599    const arg = (e.args ?? '').trim().toLowerCase()
600    const list = `Mascots: ${CHARACTERS.join(', ')}. Use /mascot <name>.`
601    if (!arg || arg === 'list') return { text: list }
602    const name = CHARACTERS.find(c => c === arg)
603    return name ? switchTo($, name) : { text: `Unknown mascot "${arg}". ${list}` }
604  })
605
606  for (const name of CHARACTERS) {
607    if (name !== 'claude') on('command.run', { command: name }, async $ => switchTo($, name))
608  }
609
610  on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
611    if (e.props.hasSurvey) return next(e)
612    const s = await read($, sess)
613    const b = await read($, bar)
614    await read($, now) // redraw on every tick; the drawing itself uses the real clock
615    const at = await $.clock.now()
616    const lk = await read($, look)
617    const outfit = lk.outfit
618    const character = await read($, char)
619    const cur = shownAt(b, at)
620    const pct = `${Math.round(cur * 100)}%`
621    const drawn = await Promise.resolve(next(e)).catch(() => null)
622    const below = drawn && typeof drawn === 'object' && (drawn as { type?: string }).type !== 'engine' ? drawn : null
623    // keepwarm's cache-tax line says the prompt cache is cold: he is frozen in a cube of ice.
624    const isIced = !s.busy && isIcedNow(below, s.doneAt, at)
625    // Claude is waiting on you (a question, a plan, a permission prompt): a bubble and a gold bar.
626    const isAsk = !isIced && s.busy && Boolean(s.ask) && at - (s.ask ?? 0) > ASK_MS
627    // A Bash or PowerShell command is running: his own console, unless he is waiting on you.
628    const isCmd = !isIced && !isAsk && s.busy && Boolean(s.cmd)
629    const isRoaming = !s.busy && outfit === 'idle' && !isIced && !isQuiet(s, at)
630    const roam = isRoaming ? { now: roamAt(at), next: roamAt(at + 1000) } : null
631    // An explicit progress that hits 100% while the turn still runs is a win too: no panting.
632    const isFull = !isIced && b.to >= 0.999 && outfit !== 'error'
633    const isWin = WIN.has(outfit) || isFull
634    const jumped = b.bigAt > 0 && b.bigAt >= s.startedAt
635    // Victory depends on how the turn went: out of the car, a medal, or the task's own pose.
636    const pose: Outfit = roam ? roam.now.pose : outfit === 'done' || (isFull && !WIN.has(outfit)) ? winPose(s, b.bigBy ?? 0, jumped) : outfit
637    const jumpAge = b.bigAt > 0 ? at - b.bigAt : Infinity
638    const isCar = (b.bigBy ?? 0) >= CAR_JUMP
639    const sprintMs = isCar ? DRIVE_MS : RUN_MS
640    const isRun = !roam && !isIced && jumpAge < sprintMs
641    // Between the run (or the bar reaching its end) and the victory pose: a short beat of its own.
642    const arriveAt = b.to >= 0.999 ? b.at + EASE_MS : lk.since
643    const transEnd = Math.max(arriveAt, jumped ? b.bigAt + sprintMs : 0) + TRANS_MS
644    const isTrans = !roam && !isIced && !isRun && isWin && at < transEnd
645    const age = roam ? roam.now.age : isWin ? at - transEnd : at - lk.since
646    const item = ITEM_COLOR[pose]
647    // After a sprint he pants; after the car he just climbs out. Neither once the turn is won.
648    const isPant = !roam && !isIced && !isRun && !isCar && !isWin && jumpAge < sprintMs + PANT_MS
649    const isExit = !roam && !isIced && !isRun && isCar && !isWin && jumpAge < sprintMs + EXIT_MS
650    const isChanging = !isIced && !isRun && !isTrans && !isPant && !isExit && item !== undefined && age < CHANGE_MS
651    const transPose: Outfit = pose === 'exitCar' ? 'park' : pose === 'medal' ? 'pant' : 'brace'
652    const shown: Outfit = isIced ? 'frozen' : isAsk ? 'ask' : isCmd ? s.cmd ?? 'shell' : isRun ? (isCar ? 'drive' : 'run') : isTrans ? transPose : isPant ? 'pant' : isExit ? 'exitCar' : isChanging ? 'changing' : pose
653    // Just landed in a new look: a quick squash-and-stretch settle.
654    const settle = isIced || isAsk || isCmd || isRun || isTrans || isPant || isExit || isChanging ? undefined : age - (item === undefined ? 0 : CHANGE_MS)
655    const isWalking = isIced || isAsk || isCmd || isRun || isTrans || isPant || isExit || isWin ? false : roam ? MOVING.has(pose) && pose !== 'fly' : s.busy && !isStalled(s, b, at) && outfit !== 'idle'
656    const word = isIced ? 'frozen' : WORD[shown]
657
658    const ui = $.ui.resolve(e)
659    const { Box, Text } = ui
660
661    if (e.surface !== 'terminal' && 'Svg' in ui) {
662      const { Svg } = ui
663      const width = Math.max(180, Math.min(1600, (e.props.bodyColumns || 100) * 8 - 40))
664      // `below` holds the Token Optimizer rows (its hook sits beneath ours), so the gap sets them apart.
665      return (
666        <Box flexDirection="column" rowGap={1}>
667          <Svg source={rowSvg(b, at, shown, width, isWalking, roam ? { from: roam.now.x, to: roam.next.x } : null, character, isChanging ? { item, age, dest: ITEM_DEST[pose] } : undefined, settle)} alt={`${word}: ${pct}`} width={width} height={H} />
668          {below}
669        </Box>
670      )
671    }
672
673    const width = Math.max(6, Math.min(60, (e.props.bodyColumns || 80) - 24))
674    const filled = Math.round(width * cur)
675    return (
676      <Box flexDirection="column">
677        <Box flexDirection="row" gap={1}>
678          <Text color={outfit === 'done' ? DONE : CLAY}>{'█'.repeat(filled) + '░'.repeat(width - filled)}</Text>
679          <Text dimColor>{pct}</Text>
680          <Text dimColor>{isIced ? 'frozen' : WORD[outfit]}</Text>
681        </Box>
682        {below}
683      </Box>
684    )
685  })
686
687  // Last, so its AbovePrompt band sits beneath the crab's row (see `below`). Token Optimizer's
688  // bar, merged from its desktop mod, feeds on the same turn events.
689  registerOptimizer(on, options)
690}
691
hooks/dig.ts 90 lines
1import type { Character } from '../types'
2import { crabCss, crabSvg } from './crab'
3
4// The side-panel scene: while files are being touched, a crab dives into a file, throws things
5// out until he finds something, pops up with it, jumps into another file and peeks out of a
6// third. One 10 s loop of CSS, started mid-cycle so each redraw carries on where it was.
7export const DIG_W = 236
8export const DIG_H = 100
9export const DIG_LOOP = 10
10const G = 60 // ground line
11const FILE_W = 54
12const XS = [8, 90, 172] as const // file left edges: A (dive in), B (jump in), C (peek out)
13const HIDE = 34 // fully behind the file front (its feet stay above the panel's bottom edge)
14const PEEK = 12 // eyes and the top of the head show above the file front
15
16const xml = (s: string): string => s.replace(/[&<>"]/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' })[c] ?? c)
17const base = (p: string): string => {
18  const name = p.split(/[\\/]/).filter(Boolean).pop() ?? p
19  return name.length > 8 ? `${name.slice(0, 7)}…` : name
20}
21
22const ITEMS = ['#EEEDE8', '#8E929A', '#D9A23A', '#7FC4F2', '#C0705A', '#4FA37A', '#6A50AA', '#F5C542']
23
24// Deterministic spread so each item flies its own way.
25const spread = (i: number): { dx: number; h: number } => ({
26  dx: ((i * 37) % 90) - 45,
27  h: 26 + ((i * 53) % 30),
28})
29
30export const digSvg = (at: number, names: string[], character: Character): string => {
31  const t = (at / 1000) % DIG_LOOP
32  const delay = `-${t.toFixed(3)}s`
33  const cx = (i: number): number => (XS[i] ?? 0) + (FILE_W - 30) / 2
34  const [a, b, c] = [cx(0), cx(1), cx(2)]
35  const dur = `${DIG_LOOP}s`
36
37  const items = ITEMS.map((color, i) => {
38    const s = 17 + i * 5 // percent of the loop each throw starts at
39    const { dx, h } = spread(i)
40    const name = `th${i}`
41    const kf = `@keyframes ${name}{0%,${s}%{opacity:0;transform:translate(0,0)}${s + 0.5}%{opacity:1}${s + 2}%{transform:translate(${dx / 2}px,${-h}px)}${s + 4.5}%{opacity:1;transform:translate(${dx}px,-6px)}${s + 5.5}%,100%{opacity:0;transform:translate(${dx}px,6px)}}`
42    const rect = `<g style="animation:${name} ${dur} linear infinite ${delay}"><rect x="${(XS[0] ?? 0) + FILE_W / 2 - 3}" y="${G - 12}" width="${i % 2 ? 7 : 6}" height="${i % 3 ? 6 : 8}" fill="${color}"/></g>`
43    return { kf, rect }
44  })
45
46  // x/y of the crab over the loop: walk to A, dive, hidden while throwing, pop up, walk to B,
47  // dive, travel unseen to C, peek, climb out, and back to the start.
48  const path = `@keyframes path{
490%{transform:translate(${c}px,0)}
5010%{transform:translate(${a}px,0)}
5114%{transform:translate(${a}px,${HIDE}px)}
5260%{transform:translate(${a}px,${HIDE}px)}
5366%{transform:translate(${a}px,-6px)}
5470%{transform:translate(${a}px,0)}
5578%{transform:translate(${b}px,0)}
5682%{transform:translate(${b}px,${HIDE}px)}
5786%{transform:translate(${c}px,${HIDE}px)}
5890%{transform:translate(${c}px,${PEEK}px)}
5996%{transform:translate(${c}px,${PEEK}px)}
60100%{transform:translate(${c}px,0)}}`
61  const gem = `@keyframes gem{0%,61%{opacity:0}62%,71%{opacity:1}72%,100%{opacity:0}}`
62
63  const files = XS.map((x, i) => {
64    const label = xml(base(names[i] ?? ['index', 'src', 'README'][i] ?? 'file'))
65    return {
66      back: `<rect class="fb" x="${x}" y="${G - 16}" width="${FILE_W}" height="46" rx="4"/>`,
67      front: `<rect class="ff" x="${x}" y="${G + 2}" width="${FILE_W}" height="${DIG_H - (G + 2)}" rx="4"/><text class="ft" x="${x + FILE_W / 2}" y="${G + 20}" text-anchor="middle" font-size="9" font-family="-apple-system,'Segoe UI',sans-serif">${label}</text>`,
68    }
69  })
70
71  return `<svg xmlns="http://www.w3.org/2000/svg" width="${DIG_W}" height="${DIG_H}" viewBox="0 0 ${DIG_W} ${DIG_H}">${crabCss(at)}
72<style>
73.bg{fill:rgba(255,255,255,.78)}.fb{fill:#cfccc4}.ff{fill:#e4e1da}.ft{fill:#4a4a47}
74@media (prefers-color-scheme: dark){.bg{fill:rgba(24,24,23,.82)}.fb{fill:#3a3a38}.ff{fill:#2c2c2b}.ft{fill:#b8b8b4}}
75g{transform-box:fill-box}
76${path}
77${gem}
78${items.map(i => i.kf).join('\n')}
79</style>
80<rect class="bg" x="0" y="0" width="${DIG_W}" height="${DIG_H}" rx="12"/>
81${files.map(f => f.back).join('')}
82${items.map(i => i.rect).join('')}
83<g style="animation:path ${dur} linear infinite ${delay}">
84${crabSvg('working', 0, G - 27, true, character)}
85<rect x="24" y="${G - 36}" width="8" height="8" fill="#F5C542" style="animation:gem ${dur} linear infinite ${delay}"/>
86</g>
87${files.map(f => f.front).join('')}
88</svg>`
89}
90
hooks/ft-register.tsx 1642 lines
1import { type BuiltinToolResults, type EngineInterface, type Register, type Timer, update } from 'claude-code'
2import { LINUX_LAUNCH, openCommand, openWithCommand } from './ft-open'
3import type { ButtonProps } from './ft-button'
4
5import type { Activity, Character, FileNode, FileTree, Files, Outfit, Theme } from '../types'
6import { crabCss, crabSvg } from './crab'
7import { BRANCH_ICON, chainOf, type GitAction, gitActions, readOnly, readTargets, resolve, TONES } from './ft-git'
8import type { DigSpec, RowSpec, RowsProps, Seg } from './ft-rows'
9import { CHEVRON_CLOSED, CHEVRON_OPEN, fileIcon, GIT_COLOR } from './ft-icons'
10import {
11  age,
12  ancestorsOf,
13  type Change,
14  DEFAULT_THEME,
15  dirname,
16  emptyTree,
17  formatSize,
18  inside,
19  isAbsolute,
20  join,
21  mapper,
22  parseGit,
23  parseNumstat,
24  parseTheme,
25  posix,
26  relative,
27  dropBelow,
28  useDrives,
29  rollCounts,
30  replaceChildren,
31  rollUp,
32  stamp,
33  toNodes,
34  underAny,
35  visibleRows,
36} from './ft-tree'
37
38const TREE = { plugin: 'crab-progress', key: 'tree' } as const
39const THEME = { plugin: 'crab-progress', key: 'theme' } as const
40const ACTIVITY = { plugin: 'crab-progress', key: 'activity' } as const
41const PANE = 'filetree'
42const LOOK = { plugin: 'crab-progress', key: 'look' } as const
43const CHAR = { plugin: 'crab-progress', key: 'char' } as const
44const FILES = { plugin: 'crab-progress', key: 'files' } as const
45// The digging scene shows while a file was touched this recently.
46const DIG_ACTIVE_MS = 8_000
47const CHAR_COLOR: Record<string, string> = { claude: '#D97757', frog: '#C66B3E', creeper: '#5DAA4E', slime: '#6CC25F', robot: '#9AA4B0', cat: '#E39A4A' }
48
49// The three rows the crab works in: the touched file (or the first file shown), then two others.
50function pickDig(specs: RowSpec[], paths: string[]): [number, number, number] | undefined {
51  const files = specs.flatMap((s, i) => (s.kind === 'file' ? [i] : []))
52  const first = files[0]
53  if (first === undefined) return undefined
54  const base = (p: string): string => p.split('/').pop() ?? p
55  const want = paths.map(p => p.replace(/\\/g, '/').toLowerCase())
56  const hit = files.find(i => {
57    const id = (specs[i]?.id ?? '').replace(/\\/g, '/').toLowerCase()
58    return want.some(w => w === id || w.endsWith(id) || id.endsWith(w) || base(w) === base(id))
59  })
60  const a = hit ?? first
61  const rest = files.filter(i => i !== a)
62  const b = rest.find(i => i > a + 2) ?? rest[0] ?? a
63  const c = [...rest].reverse().find(i => i !== b) ?? b
64  return [a, b, c]
65}
66const SHIMMER = Object.fromEntries(Object.entries(TONES).map(([k, v]) => [k, { bright: v.bright, dim: v.dim }]))
67const BRANCH_ROW = '#branch'
68const FLASH_MS = 2700
69const RUNNING_MAX_MS = 600_000
70const NO_REPO_DEPTH = 4
71const DOUBLE_MS = 450
72const FIND_LIMIT = 200
73const ROWS_BUDGET = 80_000
74const READ_REVEAL_LIMIT = 12
75const SEARCH_REVEAL_LIMIT = 60
76const ACTIVITY_TTL_MS = 45_000
77const ADD_COLOR = '#98c379'
78const DEL_COLOR = '#e06c75'
79const THEME_FILE = '.local/state/omarchy/current/theme/colors.toml'
80const THEME_POLL_MS = 10_000
81const FONT_SCRIPT =
82  'if command -v fc-list >/dev/null 2>&1; then f=$(fc-list ":charset=$1" file | head -n1 | cut -d: -f1); ' +
83  'else f=$(ls "$HOME"/Library/Fonts/*Nerd* /Library/Fonts/*Nerd* 2>/dev/null | head -n1); fi; ' +
84  '[ -n "$f" ] || { echo missing; exit 0; }; m=$(stat -c %Z "$f" 2>/dev/null || stat -f %c "$f"); p=$PPID; ' +
85  'while [ -n "$p" ] && [ "$p" -gt 1 ]; do c=$(ps -o comm= -p "$p" 2>/dev/null); c=$(basename "$c" 2>/dev/null | tr -d " "); case "$c" in ' +
86  'ghostty|kitty|alacritty|Alacritty|foot|footclient|wezterm-gui|konsole|gnome-terminal-|xterm|urxvt|st|Terminal|iTerm2) ' +
87  'e=$(ps -o etime= -p "$p" 2>/dev/null | awk -F\'[-:]\' \'{n=NF; s=$n+60*$(n-1); if (n>2) s+=3600*$(n-2); if (n>3) s+=86400*$(n-3); print s}\'); [ -n "$e" ] && [ $(( $(date +%s) - e )) -lt "$m" ] && echo stale || echo ok; exit 0;; esac; ' +
88  'p=$(ps -o ppid= -p "$p" 2>/dev/null | tr -d " "); done; echo ok'
89const PRUNE = ['.git', 'node_modules', 'target', '.venv', '__pycache__', 'dist', '.next']
90const HOME_PRUNE = ['Library', 'AppData', '.Trash']
91const SIZE_TIMEOUT_MS = 15_000
92const SIZE_JOBS = 2
93const SIZE_WALK_LIMIT = 50_000
94const AGE_TICK_MS = 60_000
95
96let blink: Timer | null = null
97let themePoll: Timer | null = null
98let ageTick: Timer | null = null
99let themeMtime: number | null = null
100let generation = 0
101let lastPress = { key: '', at: 0 }
102let noNerd = false
103let glyphSetting = 'auto'
104let openWithSetting = ''
105let previewed: { path: string; text: string; note: string } | null = null
106let previewRows = 20
107let follow = true
108let followClaude = true
109let scanning: Promise<void> | null = null
110let scanJobs: Job[] = []
111let gitRun: Promise<void> | null = null
112let gitNext: Promise<void> | null = null
113let queuedReads: string[] = []
114let showReads = true
115let showWrites = true
116let searchIndex: { root: string; paths: Promise<string[]> } | null = null
117let activityId = 0
118let pointer = true
119let hovered = ''
120let view = { from: 0, max: 0 }
121let nativeScroll = false
122let lastSync = 0
123let noDock = false
124let home = ''
125let platform: Promise<'linux' | 'darwin' | 'win32'> | null = null
126let dirty: { root: string; files: Record<string, Change> } = { root: '', files: {} }
127let markId = 0
128let listSeq = 0
129let lastRoot = ''
130let sizeDefault = false
131let showAge = false
132let showHints = true
133let sizeEpoch = 0
134let sizeActive = 0
135let sizeQueue: string[] = []
136const sizing = new Set<string>()
137const listLatest = new Map<string, number>()
138const unreadable = new Set<string>()
139const background = new Map<string, Background>()
140
141function osName($: EngineInterface): Promise<'linux' | 'darwin' | 'win32'> {
142  platform ??= (async () => {
143    if ((await $.env.get('OS')) === 'Windows_NT') return 'win32'
144    try {
145      return (await $.process.run(['uname', '-s'], { timeoutMs: 3_000 })).stdout.trim() === 'Darwin' ? 'darwin' : 'linux'
146    } catch {
147      return 'linux'
148    }
149  })()
150  return platform
151}
152
153async function cwdOf($: EngineInterface): Promise<string> {
154  return posix(await $.session.cwd())
155}
156
157function clean(segs: Seg[]): Seg[] {
158  for (const seg of segs) for (const k of Object.keys(seg) as (keyof Seg)[]) if (seg[k] === undefined) delete seg[k]
159  return segs
160}
161
162function faint(hex: string): string {
163  const v = parseInt(hex.slice(1), 16)
164  const ch = (shift: number) => Math.round(0x26 + (((v >> shift) & 255) - 0x26) * 0.3)
165  return `#${[16, 8, 0].map(x => ch(x).toString(16).padStart(2, '0')).join('')}`
166}
167
168async function get($: EngineInterface): Promise<FileTree> {
169  return { ...emptyTree(''), ...(await $.state.get(TREE)).value }
170}
171
172async function put($: EngineInterface, fn: (t: FileTree) => FileTree): Promise<void> {
173  await update($, TREE, cur => fn({ ...emptyTree(''), ...cur }))
174}
175
176function patch($: EngineInterface, fn: (t: FileTree) => Partial<FileTree>) {
177  return put($, t => ({ ...t, ...fn(t) }))
178}
179
180async function activities($: EngineInterface): Promise<Activity[]> {
181  return (await $.state.get(ACTIVITY)).value ?? []
182}
183
184async function setActivities($: EngineInterface, fn: (list: Activity[]) => Activity[]): Promise<void> {
185  await update($, ACTIVITY, cur => fn(cur ?? []).slice(-6))
186}
187
188async function list($: EngineInterface, dir: string): Promise<FileNode[] | null> {
189  try {
190    const entries = await $.fs.list(dir)
191    const resolved = await Promise.all(
192      entries.map(async e => {
193        if (!e.isLink) return { name: e.name, kind: e.kind, mtimeMs: e.mtimeMs, size: e.size, isLink: false }
194        try {
195          const target = await $.fs.stat(join(dir, e.name))
196          return target.kind === 'dir'
197            ? { name: e.name, kind: 'dir' as const, mtimeMs: target.mtimeMs, size: 0, isLink: false }
198            : { name: e.name, kind: 'file' as const, mtimeMs: target.mtimeMs, size: target.size, isLink: true }
199        } catch {
200          return { name: e.name, kind: 'other' as const, mtimeMs: 0, size: 0, isLink: true }
201        }
202      }),
203    )
204    unreadable.delete(dir)
205    return toNodes(dir, resolved)
206  } catch (err) {
207    if (!(await exists($, dir))) return []
208    if (!unreadable.has(dir)) {
209      unreadable.add(dir)
210      $.ui.toast(`could not list ${shortPath(dir)}: ${err instanceof Error ? err.message : String(err)}`)
211    }
212    return null
213  }
214}
215
216async function loadDirs($: EngineInterface, dirs: string[]): Promise<Map<string, FileNode[]>> {
217  const root = (await get($)).root
218  const seq = ++listSeq
219  for (const dir of dirs) listLatest.set(dir, seq)
220  const results = await Promise.all(dirs.map(async dir => [dir, await list($, dir)] as const))
221  const listed = new Map<string, FileNode[]>()
222  for (const [dir, kids] of results) {
223    if (listLatest.get(dir) !== seq) continue
224    listLatest.delete(dir)
225    if (kids) listed.set(dir, kids)
226  }
227  await patch($, t => (t.root === root ? { nodes: replaceChildren(t.nodes, listed) } : {}))
228  return listed
229}
230
231async function git($: EngineInterface, cwd: string, args: string[], timeoutMs = 20_000) {
232  return $.process.run(['git', '--no-optional-locks', '-C', cwd, ...args], { timeoutMs, env: { GIT_OPTIONAL_LOCKS: '0' } })
233}
234
235async function detectRepo($: EngineInterface): Promise<void> {
236  const t = await get($)
237  if (!t.root) return
238  let top = ''
239  let prefix = ''
240  try {
241    const run = await git($, t.root, ['rev-parse', '--show-prefix', '--show-toplevel'], 5_000)
242    const [p = '', tl = ''] = run.exitCode === 0 ? run.stdout.split('\n') : []
243    prefix = p.trim()
244    top = tl.trim()
245  } catch {
246    top = ''
247  }
248  await patch($, cur => (cur.root === t.root ? { top, prefix } : {}))
249}
250
251async function readGit($: EngineInterface): Promise<void> {
252  const t = await get($)
253  if (!t.root || !t.top) return
254  const root = t.root
255  try {
256    const status = await git($, root, ['status', '--porcelain=v1', '-b', '-z', '--ignored=traditional', '--untracked-files=normal', '--', '.'])
257    if (status.exitCode !== 0) return
258    const parsed = parseGit(status.stdout, root, t.prefix)
259    const diff: Record<string, [number, number]> = {}
260    const head = await git($, root, ['diff', 'HEAD', '--numstat', '-z', '--', '.'])
261    if (head.exitCode === 0) parseNumstat(head.stdout, root, t.prefix, diff)
262    const files = { ...parsed.files }
263    if (parsed.untrackedDirs.length) {
264      const others = await git($, root, ['ls-files', '-o', '--exclude-standard', '-z', '--', ...parsed.untrackedDirs.map(d => d.slice(root.length + 1) || '.')])
265      if (others.exitCode === 0) for (const rel of others.stdout.split('\0').filter(Boolean)) files[join(root, rel)] = 'new'
266    }
267    for (const path of Object.keys(diff)) if (files[path] === 'new') delete diff[path]
268    dirty = { root, files }
269    await patch($, cur =>
270      cur.root === root
271        ? {
272            git: parsed.git,
273            ignored: parsed.ignored,
274            untrackedDirs: parsed.untrackedDirs,
275            branch: parsed.branch,
276            counts: rollCounts(files, root),
277            diff: rollUp(diff, root),
278          }
279        : {},
280    )
281  } catch {
282    return
283  }
284}
285
286function refreshGit($: EngineInterface): Promise<void> {
287  if (!gitRun) {
288    gitRun = readGit($).finally(() => {
289      gitRun = null
290    })
291    return gitRun
292  }
293  const rerun = () => {
294    gitNext = null
295    return refreshGit($)
296  }
297  gitNext ??= gitRun.then(rerun, rerun)
298  return gitNext
299}
300
301async function reset($: EngineInterface, root: string, focus = false): Promise<void> {
302  const prev = await get($)
303  generation += 1
304  blink?.cancel()
305  blink = null
306  lastRoot = root
307  searchIndex = null
308  sizeEpoch += 1
309  sizeQueue = []
310  await put($, () => ({ ...emptyTree(root), showHidden: prev.showHidden, showSize: prev.root ? prev.showSize : sizeDefault }))
311  const title = `Files: ${root.split('/').pop() || root}`
312  if (focus) await $.ui.open({ id: PANE, title, focus: true })
313  else if (!noDock) await $.ui.open({ id: PANE, title })
314  await loadDirs($, [root])
315  await detectRepo($)
316  await refreshGit($)
317}
318
319async function revealPaths($: EngineInterface, paths: string[]): Promise<void> {
320  const tried = new Set<string>()
321  for (let pass = 0; pass < 32; pass++) {
322    const t = await get($)
323    const loaded = new Set(t.nodes.filter(n => n.loaded).map(n => n.id))
324    if (t.nodes.some(n => n.parent === t.root)) loaded.add(t.root)
325    const need = new Set<string>()
326    for (const p of paths) {
327      for (const dir of [t.root, ...ancestorsOf(p, t.root).reverse()]) {
328        if (!loaded.has(dir)) {
329          if (!tried.has(dir)) need.add(dir)
330          break
331        }
332      }
333    }
334    if (need.size === 0) return
335    for (const dir of need) tried.add(dir)
336    await loadDirs($, [...need])
337  }
338}
339
340function pruneArgs(root: string, ignored: string[] = []): string[] {
341  const names = broad(root) ? [...PRUNE, ...HOME_PRUNE] : PRUNE
342  return ['(', ...names.flatMap((name, i) => (i === 0 ? ['-name', name] : ['-o', '-name', name])), ...ignored.flatMap(p => ['-o', '-path', p]), ')', '-prune', '-o']
343}
344
345async function newer($: EngineInterface, paths: string[], sinceMs: number): Promise<string[]> {
346  const stats = await Promise.all(
347    paths.map(async p => {
348      try {
349        return (await $.fs.stat(p)).mtimeMs > sinceMs ? p : ''
350      } catch {
351        return ''
352      }
353    }),
354  )
355  return stats.filter(Boolean)
356}
357
358function broad(root: string): boolean {
359  return root === home || root === '/' || /^[A-Za-z]:\/$/.test(root)
360}
361
362function rootOfDisk(root: string): boolean {
363  return root === '/' || /^[A-Za-z]:\/$/.test(root)
364}
365
366async function changedSince($: EngineInterface, root: string, since: Since, depth: number, ignored: string[] = []): Promise<string[]> {
367  if (rootOfDisk(root)) return []
368  const test = since.mark ? ['-newer', since.mark] : ['-newermt', `@${(since.ms / 1000).toFixed(3)}`]
369  try {
370    const run = await $.process.run(['find', '-H', root, '-xdev', ...(depth ? ['-maxdepth', String(depth)] : []), ...pruneArgs(root, ignored.filter(p => inside(root, p)).slice(0, 40)), ...test, '-print0'], { timeoutMs: 8_000 })
371    return run.stdout.split('\0').filter(p => p && p !== root)
372  } catch {
373    return []
374  }
375}
376
377async function changedInRepo($: EngineInterface, root: string, since: Since, before: Record<string, Change>, ignored: string[]): Promise<{ hits: string[]; gone: string[] }> {
378  if (since.os !== 'win32') return { hits: await changedSince($, root, since, 0, ignored), gone: [] }
379  if (dirty.root !== root) return { hits: [], gone: [] }
380  const entries = Object.entries(dirty.files)
381  const gone = entries.filter(([p, c]) => c === 'del' && before[p] !== 'del').map(([p]) => p)
382  const live = entries.filter(([, c]) => c !== 'del').map(([p]) => p)
383  return { hits: await newer($, live.slice(0, 4_000), since.ms), gone }
384}
385
386type Since = { ms: number; mark: string; os: 'linux' | 'darwin' | 'win32' }
387
388async function sinceNow($: EngineInterface, writes: boolean): Promise<Since> {
389  const ms = await $.clock.now()
390  const os = await osName($)
391  if (os !== 'darwin' || !writes) return { ms, mark: '', os }
392  const mark = `${((await $.env.get('TMPDIR')) || '/tmp').replace(/\/+$/, '')}/filetree-${await $.session.id()}-${++markId}.mark`
393  try {
394    await $.process.run(['touch', mark], { timeoutMs: 3_000 })
395    return { ms, mark, os }
396  } catch {
397    return { ms, mark: '', os }
398  }
399}
400
401async function walkSize($: EngineInterface, dir: string): Promise<number> {
402  let total = 0
403  let seen = 0
404  let level = [dir]
405  while (level.length) {
406    const next: string[] = []
407    for (const d of level) {
408      let entries
409      try {
410        entries = await $.fs.list(d)
411      } catch {
412        continue
413      }
414      for (const e of entries) {
415        if (++seen > SIZE_WALK_LIMIT) return -1
416        if (e.kind === 'dir' && !e.isLink) next.push(join(d, e.name))
417        else if (e.kind === 'file') total += e.size
418      }
419    }
420    level = next
421  }
422  return total
423}
424
425async function dirSize($: EngineInterface, dir: string): Promise<number> {
426  if (rootOfDisk(dir)) return -1
427  if ((await osName($)) === 'win32') return walkSize($, dir)
428  try {
429    const run = await $.process.run(['du', '-skxH', dir], { timeoutMs: SIZE_TIMEOUT_MS })
430    const kb = Number(run.stdout.trim().split(/\s/)[0] || NaN)
431    return Number.isFinite(kb) ? kb * 1024 : -1
432  } catch {
433    return -1
434  }
435}
436
437function pumpSizes($: EngineInterface): void {
438  while (sizeActive < SIZE_JOBS && sizeQueue.length) {
439    const dir = sizeQueue.shift() ?? ''
440    const epoch = sizeEpoch
441    sizing.add(dir)
442    sizeActive += 1
443    void dirSize($, dir)
444      .then(async bytes => {
445        if (epoch === sizeEpoch) return patch($, cur => (inside(cur.root, dir) ? { dirSizes: { ...cur.dirSizes, [dir]: bytes } } : {}))
446        const cur = await get($)
447        if (cur.showSize && inside(cur.root, dir) && !(dir in cur.dirSizes) && !sizeQueue.includes(dir)) sizeQueue.push(dir)
448      })
449      .catch(() => undefined)
450      .finally(() => {
451        sizing.delete(dir)
452        sizeActive -= 1
453        pumpSizes($)
454      })
455  }
456}
457
458function wantSizes($: EngineInterface, dirs: string[]): void {
459  for (const dir of dirs) if (!sizing.has(dir) && !sizeQueue.includes(dir)) sizeQueue.push(dir)
460  pumpSizes($)
461}
462
463async function staleSizes($: EngineInterface, paths?: string[]): Promise<void> {
464  sizeEpoch += 1
465  sizeQueue = []
466  await patch($, cur => {
467    if (!paths) return { dirSizes: {} }
468    const keep: Record<string, number> = {}
469    for (const [dir, bytes] of Object.entries(cur.dirSizes)) if (!paths.some(p => inside(dir, p))) keep[dir] = bytes
470    return { dirSizes: keep }
471  })
472}
473
474function openDirs(t: FileTree): string[] {
475  const open = new Set(t.expanded)
476  return t.nodes.filter(n => n.kind === 'dir' && n.loaded && open.has(n.id)).map(n => n.id)
477}
478
479function keepTop(cur: FileTree, expanded: string[]): Partial<FileTree> {
480  if (followClaude) return { scroll: null }
481  const top = visibleRows(cur)[cur.scroll ?? view.from]?.node.id
482  const at = top ? visibleRows({ ...cur, expanded }).findIndex(r => r.node.id === top) : -1
483  return at < 0 ? {} : { scroll: at }
484}
485
486async function flash($: EngineInterface, tones: Record<string, string>): Promise<void> {
487  const unique = Object.keys(tones)
488  if (unique.length === 0) return
489  const mine = ++generation
490  const root = (await get($)).root
491  blink?.cancel()
492  blink = null
493  await patch($, cur => {
494    if (cur.root !== root) return {}
495    const open = new Set(cur.expanded)
496    const bright = new Set([...(cur.flashOn ? cur.flash.filter(id => !unique.includes(id)) : []), ...unique])
497    const dim = new Set<string>()
498    const all: Record<string, string> = cur.flashOn ? { ...cur.flashTones } : {}
499    if (cur.flashOn) {
500      for (const id of cur.flashDim) dim.add(id)
501    }
502    for (const id of unique) {
503      const tone = tones[id] ?? 'orange'
504      all[id] = tone
505      if (id === BRANCH_ROW) continue
506      const chain = ancestorsOf(id, cur.root)
507      if (!chain.some(a => !open.has(a))) continue
508      for (const a of chain) {
509        if (!open.has(a)) {
510          dim.add(a)
511          all[a] = all[a] ?? tone
512        }
513        open.add(a)
514      }
515    }
516    for (const id of bright) dim.delete(id)
517    return {
518      flash: [...bright],
519      flashDim: [...dim],
520      flashOn: true,
521      flashTones: all,
522      expanded: [...open],
523      ...keepTop(cur, [...open]),
524    }
525  })
526  if (generation !== mine) return
527  blink = $.clock.after(FLASH_MS, () => {
528    if (generation !== mine) return
529    blink = null
530    void patch($, cur => (generation === mine ? { flash: [], flashDim: [], flashOn: false, flashTones: {} } : {}))
531  })
532}
533
534async function followCwd($: EngineInterface): Promise<boolean> {
535  if (!follow) return false
536  const cwd = await cwdOf($)
537  const t = await get($)
538  if (t.root === cwd) return false
539  await reset($, cwd)
540  return true
541}
542
543type Pending = { actions: GitAction[]; ids: number[]; command: string; head: string }
544type Job = { actions: GitAction[]; since: Since; initRepo: boolean; readOnly: boolean }
545type Background = { p: Pending | null; job: Job; reads: string[] }
546type GitOperation = NonNullable<BuiltinToolResults['Bash']['gitOperation']>
547type Outcome = { state: 'done' | 'failed'; label: string; detail: string }
548
549const PROOF: Record<string, (op: GitOperation) => boolean> = {
550  push: op => Boolean(op.push),
551  merge: op => op.branch?.action === 'merged',
552  rebase: op => op.branch?.action === 'rebased',
553  'pr create': op => op.pr?.action === 'created',
554  'pr merge': op => op.pr?.action === 'merged',
555  'pr comment': op => op.pr?.action === 'commented',
556}
557
558async function startGit($: EngineInterface, actions: GitAction[]): Promise<number[]> {
559  const now = await $.clock.now()
560  const ids = actions.map(() => ++activityId)
561  await setActivities($, cur => [
562    ...cur.filter(a => a.state === 'running' || now - a.at < ACTIVITY_TTL_MS),
563    ...actions.map((a, i) => ({
564      id: ids[i] ?? 0,
565      kind: a.kind,
566      label: a.running,
567      state: 'running' as const,
568      detail: '',
569      at: now,
570      tone: a.tone,
571      nerd: a.icon.nerd,
572      plain: a.icon.plain,
573    })),
574  ])
575  return ids
576}
577
578async function headOf($: EngineInterface, cwd: string): Promise<string> {
579  try {
580    const run = await git($, cwd, ['rev-parse', 'HEAD'], 5_000)
581    return run.exitCode === 0 ? run.stdout.trim() : ''
582  } catch {
583    return ''
584  }
585}
586
587async function outcomes($: EngineInterface, p: Pending, ok: boolean, op: GitOperation | undefined): Promise<Outcome[]> {
588  const chain = chainOf(p.command)
589  const after = p.actions.some(a => a.verb === 'commit') && !op?.commit ? await headOf($, (await get($)).root || (await cwdOf($))) : ''
590  return p.actions.map(a => {
591    if (a.verb === 'commit') {
592      const sha = op?.commit?.sha || (after && after !== p.head ? after : '')
593      return sha ? { state: 'done', label: a.done, detail: sha.slice(0, 7) } : { state: 'failed', label: `${a.verb} failed`, detail: '' }
594    }
595    if ((op && PROOF[a.verb]?.(op)) || (ok && chain.and)) return { state: 'done', label: a.done, detail: '' }
596    if (!ok && chain.and && chain.size === 1) return { state: 'failed', label: `${a.verb} failed`, detail: '' }
597    return { state: 'done', label: `ran ${a.kind}`, detail: '' }
598  })
599}
600
601async function finishGit($: EngineInterface, p: Pending, results: Outcome[]): Promise<void> {
602  const now = await $.clock.now()
603  await setActivities($, cur =>
604    cur.map(a => {
605      const i = p.ids.indexOf(a.id)
606      const result = i < 0 ? undefined : results[i]
607      return result ? { ...a, ...result, at: now } : a
608    }),
609  )
610  $.clock.after(ACTIVITY_TTL_MS + 500, () => void setActivities($, cur => cur.filter(a => a.state === 'running' || a.at > now)))
611}
612
613function failAll(p: Pending): Outcome[] {
614  return p.actions.map(a => ({ state: 'failed', label: `${a.verb} failed`, detail: '' }))
615}
616
617function proven(p: Pending | null, results: Outcome[]): GitAction[] {
618  return p ? p.actions.filter((_, i) => results[i]?.state !== 'failed') : []
619}
620
621function taskEnds(text: string): [string, string][] {
622  return [...text.matchAll(/<task-notification>([\s\S]*?)<\/task-notification>/g)].flatMap(m => {
623    const id = /<task-id>([^<]*)<\/task-id>/.exec(m[1] ?? '')?.[1]?.trim()
624    const status = /<status>([^<]*)<\/status>/.exec(m[1] ?? '')?.[1]?.trim()
625    return id && status ? [[id, status] as [string, string]] : []
626  })
627}
628
629async function settleBackground($: EngineInterface, text: string): Promise<void> {
630  if (background.size === 0 || !text.includes('<task-notification>')) return
631  for (const [id, status] of taskEnds(text)) {
632    const task = background.get(id)
633    if (!task) continue
634    background.delete(id)
635    const ok = status === 'completed'
636    const results = task.p ? await outcomes($, task.p, ok, undefined) : []
637    if (task.p) await finishGit($, task.p, results)
638    if (showReads) queuedReads.push(...task.reads)
639    scheduleScan($, { ...task.job, actions: proven(task.p, results) })
640  }
641}
642
643async function gitPaths($: EngineInterface, root: string, prefix: string, committed: boolean): Promise<string[]> {
644  const map = mapper(root, prefix)
645  try {
646    const run = committed
647      ? await git($, root, ['diff-tree', '--root', '--no-commit-id', '--name-only', '-r', '-z', 'HEAD'], 5_000)
648      : await git($, root, ['diff', '--cached', '--name-only', '-z'], 5_000)
649    return run.exitCode === 0 ? run.stdout.split('\0').filter(Boolean).flatMap(rel => map(rel) ?? []) : []
650  } catch {
651    return []
652  }
653}
654
655async function gitDir($: EngineInterface, cwd: string): Promise<string> {
656  for (let dir = cwd, i = 0; i < 64; i++) {
657    try {
658      const dot = join(dir, '.git')
659      const st = await $.fs.stat(dot)
660      if (st.kind === 'dir') return dot
661      const ref = /^gitdir:\s*(.+)$/m.exec(String(await $.fs.read(dot)))?.[1]?.trim()
662      if (ref) return resolve(dir, ref, home)
663    } catch {
664      const up = dirname(dir)
665      if (up === dir) return ''
666      dir = up
667      continue
668    }
669    return ''
670  }
671  return ''
672}
673
674async function sync($: EngineInterface, force = false): Promise<void> {
675  const now = await $.clock.now()
676  if (!force && now - lastSync < 2_000) return
677  lastSync = now
678  searchIndex = null
679  const t = await get($)
680  if (!t.root) return
681  await loadDirs($, [t.root, ...openDirs(t)])
682  if (t.top) await refreshGit($)
683}
684
685async function copyPath($: EngineInterface, id: string, absolute: boolean, surface?: string): Promise<void> {
686  const t = await get($)
687  const text = !absolute && id !== t.root && inside(t.root, id) ? id.slice(t.root.endsWith('/') ? t.root.length : t.root.length + 1) : id
688  const done = await $.ui.copy({ text, ...(surface ? { surface: surface as 'terminal' } : {}) })
689  $.ui.toast(done.isCopied ? `Copied ${text}` : 'Could not copy the path')
690}
691
692async function exists($: EngineInterface, path: string): Promise<boolean> {
693  try {
694    await $.fs.stat(path)
695    return true
696  } catch {
697    return false
698  }
699}
700
701async function afterBash($: EngineInterface, jobs: Job[]): Promise<void> {
702  const reads = queuedReads
703  queuedReads = []
704  if (await followCwd($)) return
705  const t = await get($)
706  if (!t.root) return
707  const actions = jobs.flatMap(j => j.actions)
708  const writers = jobs.filter(j => !j.readOnly)
709  const since = (writers.length ? writers : jobs).reduce((a, j) => (j.since.ms < a.ms ? j.since : a), (writers[0] ?? jobs[0])?.since ?? { ms: 0, mark: '', os: 'linux' as const })
710  const writes = !jobs.every(j => j.readOnly)
711  const before = dirty.root === t.root ? dirty.files : {}
712  if (jobs.some(j => j.initRepo)) await detectRepo($)
713  else if (!t.top && (await exists($, join(t.root, '.git')))) await detectRepo($)
714  const probed = await get($)
715  if (probed.top && (writes || probed.top !== t.top)) await refreshGit($)
716  if (writes) {
717    searchIndex = null
718    await staleSizes($)
719  }
720  const fresh = await get($)
721  const ignored = new Set(fresh.ignored)
722  const tones: Record<string, string> = {}
723  if (writes) {
724    const found = fresh.top
725      ? await changedInRepo($, t.root, since, before, fresh.ignored)
726      : { hits: since.os === 'win32' ? [] : await changedSince($, t.root, since, NO_REPO_DEPTH), gone: [] }
727    const hits = found.hits.filter(x => inside(t.root, x) && !underAny(x, ignored, t.root)).slice(0, FIND_LIMIT)
728    await revealPaths($, hits)
729    const loaded = await get($)
730    const dirs = [...new Set([loaded.root, ...openDirs(loaded), ...hits.map(dirname), ...found.gone.map(dirname)])].filter(d => inside(loaded.root, d))
731    const listed = await loadDirs($, dirs)
732    if (!fresh.top && since.os === 'win32')
733      for (const kids of listed.values()) for (const n of kids) if (n.mtime > since.ms && hits.length < FIND_LIMIT) hits.push(n.id)
734    await patch($, cur => {
735      const ids = new Set(cur.nodes.map(n => n.id))
736      return { expanded: cur.expanded.filter(id => ids.has(id)) }
737    })
738    const present = new Set((await get($)).nodes.map(n => n.id))
739    const touchTone = actions.find(a => !['commit', 'push', 'add'].includes(a.verb))?.tone ?? 'orange'
740    if (showWrites) for (const id of hits) if (present.has(id)) tones[id] = touchTone
741  }
742  if (showReads) {
743    const found: string[] = []
744    for (const r of reads) {
745      if (found.length >= READ_REVEAL_LIMIT) break
746      if (r !== t.root && inside(t.root, r) && !underAny(r, ignored, t.root) && (await exists($, r))) found.push(r)
747    }
748    if (found.length) {
749      await revealPaths($, found)
750      const shown = new Set((await get($)).nodes.map(n => n.id))
751      for (const r of found) if (shown.has(r) && !tones[r]) tones[r] = 'purple'
752    }
753  }
754  if (showWrites && actions.length) {
755    const final = await get($)
756    const committed = actions.some(a => a.verb === 'commit')
757    if ((committed || actions.some(a => a.verb === 'add')) && final.top) {
758      const paths = (await gitPaths($, final.root, final.prefix, committed)).filter(x => !underAny(x, ignored, final.root)).slice(0, READ_REVEAL_LIMIT)
759      await revealPaths($, paths)
760      const shown = new Set((await get($)).nodes.map(n => n.id))
761      for (const x of paths) if (shown.has(x)) tones[x] = 'green'
762    }
763    const pushLike = actions.find(a => ['push', 'pull', 'fetch', 'checkout', 'switch', 'branch', 'merge', 'rebase', 'tag'].includes(a.verb) || a.kind.startsWith('gh '))
764    if (pushLike) tones[BRANCH_ROW] = pushLike.tone
765    else if (committed) tones[BRANCH_ROW] = 'green'
766  }
767  await flash($, tones)
768}
769
770function scheduleScan($: EngineInterface, job: Job): void {
771  scanJobs.push(job)
772  if (scanning) return
773  scanning = (async () => {
774    while (scanJobs.length) {
775      const jobs = scanJobs
776      scanJobs = []
777      try {
778        await afterBash($, jobs)
779      } finally {
780        const marks = jobs.map(j => j.since.mark).filter(Boolean)
781        if (marks.length) await $.process.run(['rm', '-f', ...marks], { timeoutMs: 3_000 }).catch(() => undefined)
782      }
783    }
784  })().finally(() => {
785    scanning = null
786  })
787}
788
789async function touched($: EngineInterface, paths: string[], tone: string, show: boolean): Promise<void> {
790  if (tone !== 'purple' && previewed && paths.map(posix).includes(previewed.path)) previewed = null
791  if (await followCwd($)) return
792  const t = await get($)
793  if (!t.root) return
794  const within = paths.map(posix).filter(p => inside(t.root, p))
795  if (within.length === 0) return
796  if (tone !== 'purple') {
797    searchIndex = null
798    await staleSizes($, within)
799    await revealPaths($, within.map(dirname))
800    await loadDirs($, [...new Set(within.map(dirname))].filter(d => inside(t.root, d)))
801    await refreshGit($)
802  } else await revealPaths($, within)
803  if (!show) return
804  const tones: Record<string, string> = {}
805  for (const p of within) tones[p] = tone
806  await flash($, tones)
807}
808
809async function reveal($: EngineInterface, paths: string[]): Promise<void> {
810  if (await followCwd($)) return
811  await revealPaths($, paths)
812  await patch($, cur => {
813    const open = new Set(cur.expanded)
814    for (const p of paths) for (const a of ancestorsOf(p, cur.root)) open.add(a)
815    return { expanded: [...open], cursor: paths[paths.length - 1] ?? cur.cursor, scroll: null }
816  })
817}
818
819async function walk($: EngineInterface, root: string, depth: number, limit: number): Promise<string[]> {
820  const out: string[] = []
821  let level = [root]
822  for (let d = 0; d < depth && level.length && out.length < limit; d++) {
823    const next: string[] = []
824    for (const dir of level) {
825      for (const n of (await list($, dir)) ?? []) {
826        if (n.kind === 'dir') {
827          if (!PRUNE.includes(n.name)) next.push(n.id)
828        } else out.push(n.id)
829      }
830      if (out.length >= limit) break
831    }
832    level = next
833  }
834  return out
835}
836
837async function listAll($: EngineInterface, t: FileTree): Promise<string[] | null> {
838  try {
839    if (!t.top && (await osName($)) === 'win32') return await walk($, t.root, rootOfDisk(t.root) ? 3 : 6, 20_000)
840    const run = t.top
841      ? await git($, t.root, ['ls-files', '-co', '--exclude-standard', '-z'], 10_000)
842      : await $.process.run(['find', '-H', t.root, '-xdev', '-maxdepth', rootOfDisk(t.root) ? '3' : '6', ...pruneArgs(t.root), '-type', 'f', '-print0'], { timeoutMs: 10_000 })
843    if (t.top && run.exitCode !== 0) return null
844    const paths = run.stdout.split('\0').filter(Boolean)
845    if (run.isStdoutTruncated) paths.pop()
846    return paths.map(p => (isAbsolute(p) ? p : join(t.root, p)))
847  } catch {
848    return null
849  }
850}
851
852function indexPaths($: EngineInterface, t: FileTree): Promise<string[]> {
853  if (searchIndex?.root !== t.root) {
854    const index: { root: string; paths: Promise<string[]> } = {
855      root: t.root,
856      paths: listAll($, t).then(paths => {
857        if (paths === null && searchIndex === index) searchIndex = null
858        return paths ?? []
859      }),
860    }
861    searchIndex = index
862  }
863  return searchIndex.paths
864}
865
866async function search($: EngineInterface, query: string): Promise<string[]> {
867  if (!(await get($)).query.trim()) searchIndex = null
868  await patch($, () => ({ query }))
869  const q = query.trim().toLowerCase()
870  if (!q) return []
871  const t = await get($)
872  const hits = (await indexPaths($, t)).filter(p => relative(t.root, p).toLowerCase().includes(q)).slice(0, SEARCH_REVEAL_LIMIT)
873  if ((await get($)).query !== query) return []
874  await revealPaths($, hits)
875  const shown = await get($)
876  const ids = new Set(shown.nodes.map(n => n.id))
877  const stale = [...new Set(hits.filter(p => !ids.has(p)).map(dirname))].filter(d => inside(shown.root, d))
878  if (stale.length) await loadDirs($, stale)
879  return hits
880}
881
882async function jump($: EngineInterface, query: string): Promise<void> {
883  const hits = (await search($, query)).slice(0, 10)
884  await patch($, cur => {
885    const open = new Set(cur.expanded)
886    for (const p of hits) for (const a of ancestorsOf(p, cur.root)) open.add(a)
887    return { query: '', expanded: [...open], cursor: hits[0] ?? cur.cursor, scroll: null }
888  })
889}
890
891async function fontState($: EngineInterface, charset: string): Promise<'ok' | 'stale' | 'missing'> {
892  try {
893    const out = (await $.process.run(['sh', '-c', FONT_SCRIPT, 'sh', charset], { timeoutMs: 5_000 })).stdout.trim()
894    return out === 'stale' || out === 'missing' ? out : 'ok'
895  } catch {
896    return 'missing'
897  }
898}
899
900async function finePointerOk($: EngineInterface): Promise<boolean> {
901  if (!(await $.env.get('HERDR_ENV'))) return true
902  try {
903    const out = (await $.process.run([(await $.env.get('HERDR_BIN_PATH')) || 'herdr', '--version'], { timeoutMs: 3_000 })).stdout
904    const [major = 0, minor = 0, fix = 0] = (/(\d+)\.(\d+)\.(\d+)/.exec(out) ?? []).slice(1).map(Number)
905    return major * 1e6 + minor * 1e3 + fix >= 9_001
906  } catch {
907    return false
908  }
909}
910
911const PREVIEW_CHARS = 90_000
912const PREVIEW_LINE = 2_000
913
914function fitSerialized(lines: string[], max: number): string[] {
915  let size = 0
916  for (let i = 0; i < lines.length; i++) {
917    size += JSON.stringify(lines[i]).length
918    if (size > max) return lines.slice(0, i)
919  }
920  return lines
921}
922
923async function loadPreview($: EngineInterface, path: string): Promise<void> {
924  let text = ''
925  let note = ''
926  try {
927    const stat = await $.fs.stat(path)
928    if (stat.size > 4_000_000) note = `${formatSize(stat.size)}, too large to preview`
929    else {
930      text = await $.fs.read(path)
931      if (text.slice(0, 8000).includes('\0')) {
932        text = ''
933        note = `binary file, ${formatSize(stat.size)}`
934      }
935    }
936  } catch (err) {
937    note = `could not read: ${String((err as Error)?.message ?? err).split('\n')[0]}`
938  }
939  const total = text.length
940  let lines = text.slice(0, PREVIEW_CHARS).replace(/\r\n?/g, '\n').replace(/[\u0000-\u0008\u000b-\u001f\u007f]/g, '').split('\n')
941  if (total > PREVIEW_CHARS && lines.length > 1) lines.pop()
942  lines = lines.map(l => (l.length > PREVIEW_LINE ? `${l.slice(0, PREVIEW_LINE)}…` : l))
943  const fit = fitSerialized(lines, PREVIEW_CHARS)
944  if (total > PREVIEW_CHARS || fit.length < lines.length) note = `first ${fit.length.toLocaleString('en')} lines of ${total.toLocaleString('en')} characters`
945  previewed = { path, text: fit.join('\n'), note }
946}
947
948async function showPreview($: EngineInterface, path: string): Promise<void> {
949  await loadPreview($, path)
950  await patch($, () => ({ preview: path, previewTop: 0, selected: path }))
951}
952
953async function closePreview($: EngineInterface): Promise<void> {
954  await patch($, () => ({ preview: '', previewTop: 0 }))
955}
956
957async function scrollPreview($: EngineInterface, by: number): Promise<void> {
958  const t = await get($)
959  const last = Math.max(0, (previewed?.text.split('\n').length ?? 1) - previewRows)
960  const top = Math.max(0, Math.min(last, t.previewTop + by))
961  if (top !== t.previewTop) await patch($, () => ({ previewTop: top }))
962}
963
964function previewLines(text: string, top: number, rows: number, markdown: boolean): { text: string; top: number } {
965  const lines = text.split('\n')
966  const from = Math.max(0, Math.min(top, lines.length - 1))
967  let shown = lines.slice(from, from + rows)
968  if (markdown) {
969    // Reopen a code fence the window starts inside, so the rest is not read as prose.
970    let fence = ''
971    let opener = ''
972    for (const line of lines.slice(0, from)) {
973      const m = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line)
974      if (!m) continue
975      const mark = m[1] ?? ''
976      if (!fence) {
977        fence = mark
978        opener = line.trimStart()
979      } else if (mark[0] === fence[0] && mark.length >= fence.length && !(m[2] ?? '').trim()) fence = ''
980    }
981    if (fence) shown = [opener, ...shown]
982  }
983  return { text: shown.join('\n'), top: from }
984}
985
986async function openFile($: EngineInterface, path: string, external = false): Promise<void> {
987  if (!external && openWithSetting.trim() === 'preview') return showPreview($, path)
988  const os = await osName($)
989  const native = (p: string) => (os === 'win32' ? p.replace(/\//g, '\\') : p)
990  const custom = external ? null : openWithCommand(openWithSetting, native(path), native(dirname(path)))
991  const { argv, init } = custom ? { argv: os === 'linux' ? ['sh', '-c', LINUX_LAUNCH, 'sh', ...custom] : custom, init: { timeoutMs: 10_000 } } : openCommand(os, path)
992  try {
993    const run = await $.process.run(argv, init)
994    if (run.exitCode !== 0) $.ui.toast(`could not open ${path} with ${custom?.[0] ?? argv[0]}: ${run.stderr.trim().split('\n')[0] || `exit ${run.exitCode}`}`)
995  } catch {
996    $.ui.toast(`could not open ${path} with ${custom?.[0] ?? argv[0]}`)
997  }
998}
999
1000async function quickLook($: EngineInterface, path: string): Promise<void> {
1001  try {
1002    await $.process.run(['sh', '-c', '(nohup qlmanage -p "$1" >/dev/null 2>&1 &); sleep 0.4; osascript -e \'tell application "System Events" to set frontmost of first process whose name is "qlmanage" to true\' >/dev/null 2>&1 || true', 'sh', path], { timeoutMs: 10_000 })
1003  } catch {
1004    $.ui.toast(`could not preview ${path} with Quick Look`)
1005  }
1006}
1007
1008async function toggle($: EngineInterface, n: FileNode): Promise<void> {
1009  if (n.kind === 'dir' && !(await get($)).expanded.includes(n.id)) await loadDirs($, [n.id])
1010  await patch($, t => {
1011    const open = new Set(t.expanded)
1012    if (n.kind === 'dir') open.has(n.id) ? open.delete(n.id) : open.add(n.id)
1013    return { expanded: [...open], cursor: n.id, selected: n.kind === 'dir' ? t.selected : n.id }
1014  })
1015}
1016
1017async function press($: EngineInterface, n: FileNode): Promise<void> {
1018  const now = await $.clock.now()
1019  const isDouble = lastPress.key === n.id && now - lastPress.at < DOUBLE_MS
1020  lastPress = { key: isDouble ? '' : n.id, at: now }
1021  if (!isDouble) {
1022    await toggle($, n)
1023    return
1024  }
1025  await openNode($, n)
1026}
1027
1028async function openNode($: EngineInterface, n: FileNode): Promise<void> {
1029  if (n.kind === 'dir') {
1030    follow = false
1031    await reset($, n.id)
1032  } else await openFile($, n.id)
1033}
1034
1035async function loadTheme($: EngineInterface): Promise<void> {
1036  const path = `${(await $.env.get('HOME')) ?? ''}/${THEME_FILE}`
1037  try {
1038    const stat = await $.fs.stat(path)
1039    if (stat.kind !== 'file') throw new Error('no theme file')
1040    if (stat.mtimeMs === themeMtime) return
1041    await $.state.set(THEME, parseTheme(String(await $.fs.read(path))))
1042    themeMtime = stat.mtimeMs
1043  } catch {
1044    if (themeMtime === 0) return
1045    await $.state.set(THEME, DEFAULT_THEME)
1046    themeMtime = 0
1047  }
1048}
1049
1050async function pressHeader($: EngineInterface, id: string): Promise<void> {
1051  const t = await get($)
1052  if (id === 'up') {
1053    follow = false
1054    await reset($, dirname(t.root))
1055  } else if (id === 'cwd') {
1056    follow = true
1057    await reset($, await cwdOf($))
1058  } else if (id === 'refresh') {
1059    searchIndex = null
1060    await staleSizes($)
1061    await loadDirs($, [t.root, ...t.expanded])
1062    await detectRepo($)
1063    await refreshGit($)
1064    if (t.query.trim()) await search($, t.query)
1065  } else if (id === 'hidden') await patch($, cur => ({ showHidden: !cur.showHidden }))
1066  else if (id === 'size') await patch($, cur => ({ showSize: !cur.showSize }))
1067  else if (id === 'collapse') await patch($, cur => ({ expanded: [], nodes: dropBelow(cur.nodes, cur.nodes.filter(x => x.parent === cur.root && x.kind === 'dir').map(x => x.id)) }))
1068  else if (id === 'unselect') await patch($, () => ({ selected: '' }))
1069  else if (id === 'external' && t.preview) await openFile($, t.preview, true)
1070}
1071
1072function shortPath(path: string): string {
1073  return home && inside(home, path) ? `~${path.slice(home.length)}` : path
1074}
1075
1076export const register: Register = (on, options) => {
1077  glyphSetting = typeof options?.glyphs === 'string' ? options.glyphs : 'auto'
1078  const activity = typeof options?.activity === 'string' ? options.activity : 'reads and writes'
1079  showReads = activity.includes('reads')
1080  showWrites = activity.includes('writes')
1081  followClaude = options?.follow !== 'off'
1082  sizeDefault = options?.column === 'size'
1083  showAge = options?.column === 'age'
1084  showHints = options?.hints !== 'off'
1085  openWithSetting = typeof options?.open === 'string' ? options.open : ''
1086  noDock = options?.start === 'off'
1087  // Both values of `isInteractive` (the desktop app is not a REPL), so it sits beside the crab's own hook.
1088  for (const isInteractive of [true, false])
1089    on('session.start', { isInteractive }, async ($, e, next) => {
1090    await $.command.register({ name: 'filetree', description: 'Show the file tree; args: [path] (no path = cwd)' })
1091    const windows = (await $.env.get('OS')) === 'Windows_NT'
1092    useDrives(windows)
1093    home = posix(((await $.env.get('HOME')) || (await $.env.get('USERPROFILE')) || '').replace(/[\\/]+$/, ''))
1094    activityId = Math.max(activityId, ...(await activities($)).map(a => a.id))
1095    void (async () => {
1096      pointer = await finePointerOk($)
1097      try {
1098        const remote = Boolean((await $.env.get('SSH_CONNECTION')) || (await $.env.get('SSH_TTY')))
1099        noNerd = !remote && (windows || (await fontState($, 'f04eb')) !== 'ok')
1100      } catch {
1101        noNerd = true
1102      }
1103      await loadTheme($)
1104      themePoll?.cancel()
1105      themePoll = themeMtime ? $.clock.every(THEME_POLL_MS, () => void loadTheme($)) : null
1106      ageTick?.cancel()
1107      ageTick = showAge ? $.clock.every(AGE_TICK_MS, () => $.ui.invalidate('ui.render')) : null
1108      const t = await get($)
1109      if (t.flashOn) await patch($, () => ({ flash: [], flashDim: [], flashOn: false, flashTones: {} }))
1110      if (t.preview) await patch($, () => ({ preview: '', previewTop: 0 }))
1111      await setActivities($, cur => cur.map(a => (a.state === 'running' ? { ...a, state: 'failed', label: `${a.kind} interrupted` } : a)))
1112      const cwd = await cwdOf($)
1113      if (!t.root || t.nodes.length === 0 || (follow && t.root !== cwd)) await reset($, cwd)
1114      else if (!noDock) await $.ui.open({ id: PANE, title: `Files: ${t.root.split('/').pop() || t.root}` })
1115    })()
1116    return next(e)
1117  })
1118
1119  on('command.run', { command: 'filetree' }, async ($, e) => {
1120    // The layout checks are the terminal's: the desktop app docks the pane itself and
1121    // reports no fullscreen layout, so a session no terminal draws skips them.
1122    const terminal = await $.session.surfaces().then(s => s.includes('terminal'), () => true)
1123    if (terminal && !e.presentation.isFullscreen) return { text: 'filetree shows in the sidebar, which needs the fullscreen layout. Run /tui fullscreen, then /filetree.' }
1124    if (terminal && e.presentation.columns < 110) return { text: 'filetree shows in the sidebar, which needs a terminal at least 110 columns wide. Widen it, then run /filetree.' }
1125    noDock = false
1126    const arg = (e.args ?? '').trim()
1127    const cwd = await cwdOf($)
1128    follow = !arg
1129    const root = arg ? resolve(cwd, arg, home) : cwd
1130    await reset($, root, true)
1131    return { text: `File tree on ${shortPath(root)}${follow ? ' (follows the cwd)' : ''}.` }
1132  })
1133
1134  // One hook per tool it cares about, so it sits beside the crab's own all-tools hook.
1135  for (const tool of ['Edit', 'Write', 'NotebookEdit', 'Bash', 'PowerShell'] as const)
1136    on('tool.call', { tool }, async ($, e, next) => {
1137    const shell = e.tool === 'Bash' || e.tool === 'PowerShell'
1138    if (e.tool !== 'Edit' && e.tool !== 'Write' && e.tool !== 'NotebookEdit' && !shell) return next(e)
1139    const command = e.tool === 'Bash' || e.tool === 'PowerShell' ? e.command : ''
1140    const cwd = shell ? await cwdOf($) : ''
1141    const actions = gitActions(command)
1142    const quiet = !actions.length && readOnly(command)
1143    const since = await sinceNow($, shell && !quiet)
1144    const before = actions.some(a => a.verb === 'commit') ? await get($) : null
1145    const head = before?.top ? await headOf($, before.root) : ''
1146    const pending: Pending | null = actions.length ? { actions, ids: await startGit($, actions), command, head } : null
1147    let result: Awaited<ReturnType<typeof next>>
1148    try {
1149      result = await next(e)
1150    } catch (err) {
1151      if (pending) await finishGit($, pending, failAll(pending))
1152      if (since.mark) void $.process.run(['rm', '-f', since.mark], { timeoutMs: 3_000 }).catch(() => undefined)
1153      throw err
1154    }
1155    if (pending && result.deny) {
1156      const ids = pending.ids
1157      void setActivities($, cur => cur.filter(a => !ids.includes(a.id)))
1158    }
1159    if (result.deny || (result.isError && !shell)) {
1160      if (since.mark) void $.process.run(['rm', '-f', since.mark], { timeoutMs: 3_000 }).catch(() => undefined)
1161      return result
1162    }
1163    if (shell) {
1164      const out: Partial<BuiltinToolResults['Bash']> = !result.isError && result.result && typeof result.result === 'object' ? (result.result as Partial<BuiltinToolResults['Bash']>) : {}
1165      const reads = showReads ? readTargets(command, cwd, typeof out.stdout === 'string' ? out.stdout : '', home) : []
1166      const job: Job = { actions: [], since, initRepo: actions.some(a => a.init), readOnly: quiet || result.isReadOnly === true }
1167      if (out.backgroundTaskId) background.set(out.backgroundTaskId, { p: pending, job, reads })
1168      else void (async () => {
1169        const results = pending ? await outcomes($, pending, !result.isError, out.gitOperation) : []
1170        if (pending) await finishGit($, pending, results)
1171        queuedReads.push(...reads)
1172        scheduleScan($, { ...job, actions: proven(pending, results) })
1173      })()
1174      if (follow && /(^|[;&|\s])(cd|pushd|popd)(\s|$)/.test(command)) $.clock.after(400, () => void followCwd($))
1175    } else {
1176      const file =
1177        'file_path' in e && typeof e.file_path === 'string'
1178          ? e.file_path
1179          : 'notebook_path' in e && typeof e.notebook_path === 'string'
1180            ? e.notebook_path
1181            : ''
1182      if (file) void touched($, [file], 'orange', showWrites)
1183    }
1184    return result
1185  })
1186
1187  on('tool.call', { tool: 'Read' }, async ($, e, next) => {
1188    const result = await next(e)
1189    if (result.deny || result.isError || e.tool !== 'Read' || !showReads) return result
1190    void touched($, [posix(e.file_path)], 'purple', true)
1191    return result
1192  })
1193
1194  for (const tool of ['Grep', 'Glob'] as const)
1195    on('tool.call', { tool }, async ($, e, next) => {
1196      const result = await next(e)
1197      if (result.deny || result.isError || !showReads) return result
1198      const names = (result.result as Partial<BuiltinToolResults['Grep']> | undefined)?.filenames
1199      if (!Array.isArray(names) || names.length === 0) return result
1200      const cwd = await cwdOf($)
hooks/to/hooks/register.tsx 1539 lines
1// The Token Optimizer half of the crab band (merged from token-optimizer's desktop mod).
2//
3// `registerOptimizer` adds the hooks that feed it; `optimizerBand` draws it under the crab's
4// progress row, in the crab's art style. Every decision lives in ../src (pure) and ./data.ts
5// (the data gatherer over a port). The engine allows `$` only into functions declared at the
6// top of a module, so each `$` helper below is one, and closures made inside hooks only ever
7// hand `$` on to them.
8//
9// State the drawing reads lives in `$.state` atoms (../../../types, keys `to-*`) so a hot
10// reload keeps it. Clawd is gone (the crab replaces him), and so is his pose machinery.
11import { atom, read, update } from 'claude-code'
12import type { Elements, EngineInterface, Register } from 'claude-code'
13
14import type { TokenOptimizerDesktopSession } from '../../../types'
15import {
16  BUSY_TIMEOUT_MS,
17  CAPTURE_TIMEOUT_MS,
18  DASHBOARD_ARGS,
19  DASHBOARD_FAILED,
20  DASHBOARD_TIMEOUT_MS,
21  HANDOFF_KEY,
22  HANDOFF_TTL_MS,
23  RESUME_TIMEOUT_MS,
24  WARM_PROMPT,
25  attachesHandoff,
26  busyNow,
27  busyTimeoutMs,
28  canOpenDashboard,
29  cleanUpOutcome,
30  dashboardFailed,
31  dashboardStatus,
32  handoffFate,
33  initialUi,
34  isArmed,
35  lifetimeFromUsage,
36  liveTokens,
37  noteNow,
38  prepareHandoff,
39  requestContextTokens,
40  stripCrossSessionPointer,
41  warmToast,
42  withBusy,
43  withNote,
44  type Busy,
45  type Handoff,
46  type HandoffPort,
47  type UiState,
48} from '../src/actions.ts'
49import { canKeepWarm, initialClock, reduceClock, view, type ClockEvent, type ClockState } from '../src/clock.ts'
50import { LIGHT, type Palette } from '../src/palette.ts'
51import type { Snapshot } from '../src/contracts.ts'
52import { ICONS, ICON_ALT, iconSvg, type IconName } from '../src/icons.ts'
53import { COMPACT_HEAVY, QUALITY_FLOOR, sentence, type ActionId, type Run } from '../src/ladder.ts'
54import { cards, marks, row, type Card, type Mark, type MarkTone } from '../src/marks.ts'
55import { envSize, slimSide, storedSize, type BandSize } from '../src/size.ts'
56import {
57  QUALITY_REFRESH_MS,
58  STATUS_AFTER_TURN_MS,
59  TICK_IDLE_MS,
60  TICK_WARNING_MS,
61  cleanId,
62  findTokenOptimizerRoot,
63  gather,
64  mergeStored,
65  readHome,
66  runMeasure,
67  type DataIo,
68  type GatherOptions,
69} from './data.ts'
70
71const sessionAtom = atom({ plugin: 'crab-progress', key: 'to-session' } as const, null)
72const handoffAtom = atom({ plugin: 'crab-progress', key: 'to-handoff' } as const, null)
73const clockAtom = atom({ plugin: 'crab-progress', key: 'to-clock' } as const, null)
74const engineCallAtom = atom({ plugin: 'crab-progress', key: 'to-engineCall' } as const, null)
75const uiAtom = atom({ plugin: 'crab-progress', key: 'to-ui' } as const, null)
76const frameAtom = atom({ plugin: 'crab-progress', key: 'to-frame' } as const, 0)
77
78type Desktop = Elements['desktop']
79type Tones = { good: string; caution: string; bad: string; cold: string; ink: string; track: string; card: string; line: string }
80
81/** The crab's colours: its win green and gold (light; each picture follows dark mode itself, see DARK_STYLE). */
82function tonesFor(p: Palette): Tones {
83  return { good: '#5fbf8f', caution: '#D9A23A', bad: p.bad, cold: p.cold, ink: p.ink, track: '#d9d6cd', card: p.card, line: '#e2dfd6' }
84}
85
86// ---- module state: plain variables, rebuilt from the atoms after a reload ----
87
88let enabled = true
89let active = false
90/** Whether the CRAB_OPTIMIZER_BAR switches have been read in this environment. */
91let switchesRead = false
92let timers: { cancel: () => void }[] = []
93let statusTimer: { cancel: () => void } | null = null
94let frameText = ''
95/** The transcript Claude Code named for one session: never handed to another session's read. */
96let transcript: { sessionId: string; path: string } | null = null
97
98function noteTranscript(sessionId: unknown, path: unknown): void {
99  if (typeof path === 'string' && path && typeof sessionId === 'string' && sessionId) transcript = { sessionId: cleanId(sessionId), path }
100}
101
102function transcriptFor(sid: string): string | undefined {
103  return transcript !== null && sid !== '' && transcript.sessionId === sid ? transcript.path : undefined
104}
105let warmInFlight = false
106/**
107 * Which session the band is serving: bumped when one starts, is cleared or is
108 * swapped in. Work begun under one generation drops its result in the next.
109 */
110let sessionGen = 0
111/**
112 * A press is being claimed: a second press meanwhile is ignored (no double
113 * compaction). A claim older than the busy timeout is stale: a stuck press
114 * never holds the buttons past it.
115 */
116let claim: { at: number } | null = null
117/**
118 * The one engine call the buttons may have running: our `$.session.compact()`
119 * or our `$.command.run({ command: 'clear' })`. Set before the call's first
120 * await, cleared only when it settles, whatever the busy timeout says: a
121 * second compaction or clear never overlaps it. `engineCallAtom` records it
122 * too, so a reload mid-call still refuses (see runningCall).
123 */
124let engineCall: 'compact' | 'clear' | null = null
125/** When engineCall was claimed: a call that never settles stops blocking after HANDOFF_TTL_MS. */
126let engineCallAt = 0
127/** The live session a render last asked to re-read after finding another session's figures. */
128let resyncFor = ''
129/** Compactions that landed in this process, so Clean up can tell one happened during its /compact. */
130let compactionsLanded = 0
131
132/** The store key for the user's band size: the user's choice, so it survives sessions and restarts. */
133const SIZE_KEY = 'status-bar-size'
134/** The env default (CRAB_OPTIMIZER_BAR_SIZE), read once in readSwitches. */
135let sizeEnvDefault: BandSize = 'full'
136/** The stored size, once read or pressed: null while only the env default applies. */
137let sizeClicked: BandSize | null = null
138/** Whether $.store was consulted for the size successfully (a failed read retries next render). */
139let sizeRead = false
140
141const attempt = async <T,>(work: () => Promise<T>, fallback: T): Promise<T> => {
142  try {
143    return await work()
144  } catch {
145    return fallback
146  }
147}
148
149/** Session ids become file names; keep what Token Optimizer's sanitizer keeps (as data.ts does). */
150
151// ---- data ----
152
153/** The gatherer's port over `$` (data.ts never sees `$`). */
154function dataIo($: EngineInterface): DataIo {
155  return {
156    now: () => $.clock.now(),
157    sessionId: () => $.session.id(),
158    cwd: () => $.session.cwd(),
159    envHome: () => $.env.get('HOME'),
160    envUserProfile: () => $.env.get('USERPROFILE'),
161    envConfigDir: () => $.env.get('CLAUDE_CONFIG_DIR'),
162    envAutoCompactWindow: () => $.env.get('CLAUDE_CODE_AUTO_COMPACT_WINDOW'),
163    usage: () => $.session.usage(),
164    list: path => $.fs.list(path),
165    stat: path => $.fs.stat(path),
166    read: async path => {
167      const text = await $.fs.read(path)
168      return typeof text === 'string' ? text : ''
169    },
170    run: (argv, init) => $.process.run(argv, init),
171    pluginRoot: () => $.plugin.root,
172    log: async text => {
173      await $.ui.log(text, { to: 'debug' })
174    },
175  }
176}
177
178/** Gather and store the session's figures, then let the cache clock learn from them. */
179async function refresh($: EngineInterface, options: GatherOptions = {}): Promise<void> {
180  refreshedAt = Date.now()
181  let gen = sessionGen
182  const current = await attempt(() => read($, sessionAtom), null)
183  const liveSid = cleanId(options.sessionId ?? (await attempt(() => $.session.id(), '')))
184  const path = options.transcript ?? transcriptFor(liveSid)
185  const fresh = await gather(dataIo($), current, path ? { ...options, transcript: path } : options)
186  // Begun before a clear or a session change: its figures belong to the old session.
187  if (gen !== sessionGen) return
188  if (!options.reset && current !== null && fresh.sessionId !== '' && current.sessionId !== fresh.sessionId) {
189    // Another session (a new one, or a resume): nothing of the last one carries over.
190    sessionGen += 1
191    gen = sessionGen
192    toolsPending = 0
193    await feedClock($, { type: 'clear' })
194    await setUi($, () => initialUi())
195  }
196  const latest = await attempt(() => read($, sessionAtom), null)
197  if (gen !== sessionGen) return
198  const merged = mergeStored(latest, fresh, options.reset, options.savings === true)
199  // Nothing shown changed (gatheredAt always does): no write, so no redraw.
200  if (latest !== null && sameShown(latest, merged)) return void (await syncClock($, fresh, gen))
201  await update($, sessionAtom, cur => gen === sessionGen ? mergeStored(cur, fresh, options.reset, options.savings === true) : cur)
202  if (gen !== sessionGen) return
203  await syncClock($, fresh, gen)
204}
205
206function sameShown(a: TokenOptimizerDesktopSession, b: TokenOptimizerDesktopSession): boolean {
207  return JSON.stringify({ ...a, gatheredAt: 0 }) === JSON.stringify({ ...b, gatheredAt: 0 })
208}
209
210/**
211 * Runs work off the current event (the status command can take seconds). Where no
212 * clock can schedule it, it runs now rather than not at all.
213 */
214async function later($: EngineInterface, work: () => Promise<void>): Promise<void> {
215  // Work for the session as it is now: a clear or session change before it runs drops it.
216  const gen = sessionGen
217  const guarded = async () => {
218    if (gen === sessionGen) await work()
219  }
220  try {
221    $.clock.after(0, () => void attempt(guarded, undefined))
222  } catch {
223    await attempt(guarded, undefined)
224  }
225}
226
227/** A prompt is taking the hand-off: a second prompt in the same moment does not. */
228let takingHandoff = false
229
230/** Tool calls the band counted since the last write: written alongside a redraw that happens anyway. */
231let toolsPending = 0
232
233async function flushTools($: EngineInterface): Promise<void> {
234  if (toolsPending === 0) return
235  const n = toolsPending
236  toolsPending = 0
237  await attempt(() => update($, sessionAtom, cur => (cur ? { ...cur, toolCallsSeen: (cur.toolCallsSeen ?? 0) + n } : cur)), undefined)
238}
239
240/** The status command's anchor and measured lifetime feed the clock when they are newer than what it holds. */
241async function syncClock($: EngineInterface, s: TokenOptimizerDesktopSession, gen?: number): Promise<void> {
242  if (gen !== undefined && gen !== sessionGen) return
243  await attempt(
244    () =>
245      update($, clockAtom, cur => {
246        if (gen !== undefined && gen !== sessionGen) return cur
247        let c: ClockState = cur ?? initialClock()
248        if (s.cacheLifetime && c.lifetime !== s.cacheLifetime) c = reduceClock(c, { type: 'lifetime-measured', lifetime: s.cacheLifetime })
249        if (s.lastRequestEpoch !== null) {
250          const at = s.lastRequestEpoch * 1000
251          if (c.anchor === null || at > c.anchor) {
252            c = reduceClock(c, { type: 'request-done', at, lifetime: s.cacheLifetime, contextTokens: s.contextTokens ?? c.contextTokens ?? 0 })
253          }
254        }
255        if (c.contextTokens === null && s.contextTokens !== null) c = { ...c, contextTokens: s.contextTokens }
256        return c
257      }),
258    undefined,
259  )
260}
261
262async function feedClock($: EngineInterface, event: ClockEvent, now?: number): Promise<void> {
263  await attempt(() => update($, clockAtom, cur => reduceClock(cur ?? initialClock(), event, now)), undefined)
264}
265
266function planDefault(s: TokenOptimizerDesktopSession | null): 3600 | 300 {
267  // Rate limits mean a Claude plan: an hour of cache; the API keeps five minutes.
268  // A limit seen once this session keeps it a plan, even if usage briefly stops reporting one.
269  return s && (s.fiveHour || s.week || s.sawLimits) ? 3600 : 300
270}
271
272/**
273 * Redraws when what the band shows changes: by the second near a deadline, otherwise every 5 s,
274 * with at most one redraw a minute while nothing moves.
275 */
276/** Last tick that did its full work: when nothing moves by the second, every 5 s is enough. */
277let fullTickAt = 0
278const QUIET_TICK_MS = 5000
279/** Something on screen changes by the second (the last cache minutes, a note, a pending press). */
280let nearDeadline = false
281
282async function tick($: EngineInterface): Promise<void> {
283  try {
284    const now = await $.clock.now()
285    // By the second only while a countdown is near its end or a press is pending; otherwise
286    // every 5 s (each tick is several engine calls).
287    if (now - fullTickAt < QUIET_TICK_MS && !nearDeadline) return
288    fullTickAt = now
289    const clock = (await read($, clockAtom)) ?? initialClock()
290    const session = await read($, sessionAtom)
291    const ui = (await read($, uiAtom)) ?? initialUi()
292    const v = view(clock, now, planDefault(session))
293    // Only a change the band shows redraws it, at most once a minute.
294    // One minute clock: the cache countdown's when it runs, else the wall clock's (for "27m ago").
295    const minute = v.secondsLeft != null ? `c${Math.ceil(v.secondsLeft / 60)}` : `w${Math.floor(now / TICK_IDLE_MS)}`
296    const text = [v.state, minute, busyNow(ui, now), noteNow(ui, now), isArmed(ui, now)].join('|')
297    nearDeadline = v.state === 'warning' || v.state === 'warming' || busyNow(ui, now) !== null || noteNow(ui, now) !== null || isArmed(ui, now)
298    if (text !== frameText) await flushTools($)
299    if (text !== frameText) {
300      frameText = text
301      await update($, frameAtom, n => (n ?? 0) + 1)
302    }
303  } catch {
304    // The next tick tries again.
305  }
306}
307
308/** When the session last did something (a tool call or a turn): an idle session needs fewer reads. */
309let activityAt = Date.now()
310/** When the last full read finished. */
311let refreshedAt = 0
312const IDLE_AFTER_MS = 10 * 60_000
313
314/** The once-a-minute read; a session idle for ten minutes is read every ten minutes instead. */
315async function periodic($: EngineInterface): Promise<void> {
316  const now = Date.now()
317  if (now - activityAt > IDLE_AFTER_MS && now - refreshedAt < IDLE_AFTER_MS) return
318  await attempt(() => refresh($), undefined)
319}
320
321function startCadence($: EngineInterface): void {
322  for (const t of timers) t.cancel()
323  timers = []
324  try {
325    timers.push($.clock.every(TICK_WARNING_MS, () => void tick($)))
326    timers.push($.clock.every(QUALITY_REFRESH_MS, () => void periodic($)))
327  } catch {
328    // No timers: the band still redraws on events.
329  }
330}
331
332/** Everything a desktop session needs once: a pending hand-off from disk, figures, the cadence. */
333async function start($: EngineInterface): Promise<void> {
334  sessionGen += 1
335  // A warm-up marked running with no fork in flight here was cut off by a reload.
336  const clock = await attempt(() => read($, clockAtom), null)
337  if (clock?.warming && !warmInFlight) await feedClock($, { type: 'warm-failed' })
338  const sizing = attempt(() => readSize($), undefined)
339  const held = await heldHandoff($)
340  if (held) await handoffThatFits($, held)
341  await sizing
342  // The quick reads now, so the band fills at once; the status command (seconds, at worst)
343  // runs off the start event, so the session never waits on it.
344  await attempt(() => refresh($), undefined)
345  await later($, () => refresh($, { savings: true }))
346  startCadence($)
347}
348
349function isHandoff(v: unknown): v is Handoff {
350  if (!v || typeof v !== 'object') return false
351  const h = v as Record<string, unknown>
352  return typeof h.fromSessionId === 'string' && typeof h.cwd === 'string' && typeof h.text === 'string' && h.text !== '' && typeof h.checkpointPath === 'string' && typeof h.createdAt === 'number'
353}
354
355/**
356 * The pending hand-off. `$.store` is the truth (it survives restarts and is
357 * deleted when one session takes it); the atom only mirrors it for drawing. A
358 * store that cannot be read holds nothing: no stale mirror is ever attached.
359 */
360async function heldHandoff($: EngineInterface): Promise<Handoff | null> {
361  let stored: unknown
362  try {
363    stored = await $.store.get(HANDOFF_KEY)
364  } catch {
365    return null
366  }
367  const held = isHandoff(stored) ? stored : null
368  if (stored != null && !held) await attempt(() => $.store.delete(HANDOFF_KEY), undefined)
369  const mirror = await attempt(() => read($, handoffAtom), null)
370  if (JSON.stringify(mirror) !== JSON.stringify(held)) await attempt(() => update($, handoffAtom, () => held), undefined)
371  return held
372}
373
374/** When the live session began (`$.clock.now()` ms), or null when the engine cannot say. */
375async function sessionStartedAt($: EngineInterface): Promise<number | null> {
376  const usage = await attempt(() => $.session.usage(), null)
377  return typeof usage?.startedAt === 'number' && Number.isFinite(usage.startedAt) ? usage.startedAt : null
378}
379
380/**
381 * The held hand-off when it joins this session: another session than
382 * the one that saved it, in its project, started since the save, within 10
383 * minutes of it. An expired one in this project is dropped with a one-line
384 * note; another project's is left alone. `as` names the session when the
385 * caller knows its id better than the engine does yet (a clear's own start event).
386 */
387async function handoffThatFits($: EngineInterface, h: Handoff, as?: { sessionId: string; startedAt: number | null }): Promise<Handoff | null> {
388  const sessionId = as?.sessionId ?? cleanId(await attempt(() => $.session.id(), ''))
389  const cwd = await attempt(() => $.session.cwd(), '')
390  const now = await $.clock.now()
391  const startedAt = as ? as.startedAt : await sessionStartedAt($)
392  const fate = handoffFate(h, { sessionId, cwd, startedAt, now })
393  if (fate === 'attach') return h
394  if (fate === 'skip') return null
395  await dropHandoff($)
396  const line = `Start fresh's saved hand-off was discarded: ${fate.drop}.`
397  await setUi($, u => withNote(u, line, now))
398  toast($, line)
399  return null
400}
401
402/** The refusal for a press while our compaction or clear still runs. */
403function stillRunning(kind: 'compact' | 'clear'): string {
404  return kind === 'compact' ? 'Still finishing the last clean-up.' : 'Still clearing.'
405}
406
407/**
408 * Our compaction or clear still running: this module's own, or one recorded
409 * before a reload that is under 10 minutes old (an older record is cleared).
410 */
411async function runningCall($: EngineInterface): Promise<'compact' | 'clear' | null> {
412  if (engineCall) {
413    const at = await attempt(() => $.clock.now(), null)
414    if (at === null || at - engineCallAt < HANDOFF_TTL_MS) return engineCall
415    // Never settled: the record's own expiry below decides, as after a reload.
416    engineCall = null
417  }
418  const held = await attempt(() => read($, engineCallAtom), null)
419  if (engineCall) return engineCall
420  if (!held) return null
421  // A clock that cannot be read keeps the record: refuse rather than overlap.
422  const now = await attempt(() => $.clock.now(), null)
423  if (now === null || now - held.startedAt < HANDOFF_TTL_MS) return held.kind
424  await attempt(() => update($, engineCallAtom, cur => (cur && cur.startedAt === held.startedAt && cur.kind === held.kind ? null : cur)), undefined)
425  return null
426}
427
428/**
429 * Claims the one engine call (the caller checked runningCall with no await
430 * since) and records it for a reload. False when it cannot be recorded: the
431 * call is not made.
432 */
433async function claimCall($: EngineInterface, kind: 'compact' | 'clear'): Promise<{ kind: 'compact' | 'clear'; startedAt: number } | null> {
434  // Never over a call already in flight, whatever the caller checked before.
435  if (engineCall !== null) return null
436  engineCall = kind
437  try {
438    const mine = { kind, startedAt: await $.clock.now() }
439    engineCallAt = mine.startedAt
440    await update($, engineCallAtom, () => mine)
441    return mine
442  } catch {
443    engineCall = null
444    return null
445  }
446}
447
448/** The call settled: release it here and in the record (only our own record). */
449async function releaseCall($: EngineInterface, mine: { kind: 'compact' | 'clear'; startedAt: number }): Promise<void> {
450  engineCall = null
451  await attempt(() => update($, engineCallAtom, cur => (cur && cur.kind === mine.kind && cur.startedAt === mine.startedAt ? null : cur)), undefined)
452}
453
454// ---- buttons ----
455
456function toast($: EngineInterface, text: string): void {
457  try {
458    $.ui.toast(text)
459  } catch {
460    // A toast that cannot show is not worth failing a press over.
461  }
462}
463
464async function setUi($: EngineInterface, fn: (ui: UiState) => UiState): Promise<void> {
465  await attempt(() => update($, uiAtom, cur => fn(cur ?? initialUi())), undefined)
466}
467
468async function disarm($: EngineInterface): Promise<void> {
469  const ui = await attempt(() => read($, uiAtom), null)
470  if (ui?.freshArmedAt != null) await setUi($, u => ({ ...u, freshArmedAt: null }))
471}
472
473const BUSY_WORDS: Record<Exclude<Busy, null>, string> = {
474  clean: 'Clean up',
475  'fresh-capture': 'Start fresh',
476  'fresh-clear': 'Start fresh',
477  dashboard: 'Opening the dashboard',
478}
479
480/** A busy state ends by itself: a step label never outlives its work. */
481function armBusyTimeout($: EngineInterface, busy: Exclude<Busy, null>, since: number): void {
482  try {
483    $.clock.after(busyTimeoutMs(busy), () => void expireBusy($, busy, since))
484  } catch {
485    // busyNow() still treats it as over after the timeout.
486  }
487}
488
489async function expireBusy($: EngineInterface, busy: Exclude<Busy, null>, since: number): Promise<void> {
490  const ui = await attempt(() => read($, uiAtom), null)
491  if (!ui || ui.busy !== busy || ui.busySince !== since) return
492  const now = await $.clock.now()
493  // The clear waits for the turn to end and can still land: the hand-off stays
494  // for the conversation it creates, within 10 minutes of the save.
495  const line =
496    busy === 'fresh-clear' ? 'Start fresh clears when the current turn ends.' : busy === 'dashboard' ? DASHBOARD_FAILED : `${BUSY_WORDS[busy]} timed out.`
497  await setUi($, u => withNote(withBusy(u, null, now), line, now))
498  // The dashboard link fails by its note alone.
499  if (busy !== 'dashboard') toast($, line)
500}
501
502/** Deletes the held hand-off, store first; true only when the delete succeeded (the mirror follows it). */
503async function dropHandoff($: EngineInterface): Promise<boolean> {
504  try {
505    await $.store.delete(HANDOFF_KEY)
506  } catch {
507    return false
508  }
509  await attempt(() => update($, handoffAtom, () => null), undefined)
510  return true
511}
512
513async function isTurnRunning($: EngineInterface): Promise<boolean> {
514  const clock = await attempt(() => read($, clockAtom), null)
515  return Boolean(clock?.working)
516}
517
518/** Clean up: compaction with Token Optimizer's own PreCompact guidance, run outside the press. */
519async function cleanUp($: EngineInterface): Promise<void> {
520  const now = await $.clock.now()
521  const ui = (await attempt(() => read($, uiAtom), null)) ?? initialUi()
522  if (busyNow(ui, now) !== null) return
523  await disarm($)
524  const running = await runningCall($)
525  if (running) {
526    toast($, stillRunning(running))
527    return
528  }
529  if (await isTurnRunning($)) {
530    toast($, 'Clean up waits until the turn finishes.')
531    return
532  }
533  await setUi($, u => withBusy(u, 'clean', now))
534  armBusyTimeout($, 'clean', now)
535  $.clock.after(0, () => void attempt(() => runCompact($, now), undefined))
536}
537
538async function runCompact($: EngineInterface, since: number): Promise<void> {
539  // A turn that started since the press would be cut short.
540  if (await isTurnRunning($)) {
541    const ui = await attempt(() => read($, uiAtom), null)
542    if (!ui || ui.busy !== 'clean' || ui.busySince !== since) return
543    const now = await $.clock.now()
544    await setUi($, u => withBusy(u, null, now))
545    toast($, 'Clean up waits until the turn finishes.')
546    return
547  }
548  const running = await runningCall($)
549  if (running || engineCall) {
550    const ui = await attempt(() => read($, uiAtom), null)
551    if (!ui || ui.busy !== 'clean' || ui.busySince !== since) return
552    const now = await $.clock.now()
553    await setUi($, u => withBusy(u, null, now))
554    toast($, stillRunning(running ?? engineCall ?? 'compact'))
555    return
556  }
557  /** The note when no compaction can be confirmed; null after a confirmed one. */
558  let skip: string | null = null
559  const skipped = (reason: string) => `Clean up skipped: ${reason.replace(/\.$/, '')}.`
560  // Claimed with no await between the check and the claim; recorded before the call.
561  const mine = await claimCall($, 'compact')
562  if (!mine) {
563    skip = skipped('it could not be recorded')
564  } else {
565    try {
566      // The command, as if typed: $.session.compact() is refused in a headless
567      // session, and the desktop app runs its sessions headless.
568      // Success is decided from evidence, never from the reply's words (#207):
569      // Claude Code answers a refused /compact ("Not enough messages to compact.",
570      // a hook's veto) without throwing, and a reply may be empty or not English.
571      const landedBefore = compactionsLanded
572      const tokensBefore = liveTokens(await attempt(() => $.session.usage(), null))
573      const answer = await $.command.run({ command: 'compact' })
574      const tokensAfter = liveTokens(await attempt(() => $.session.usage(), null))
575      const outcome = cleanUpOutcome({
576        landed: compactionsLanded > landedBefore,
577        tokensBefore,
578        tokensAfter,
579        said: answer?.text ?? '',
580      })
581      if (!outcome.ok) skip = outcome.line
582    } catch (error) {
583      skip = skipped(error instanceof Error && error.message ? error.message.split('\n')[0] ?? 'it failed' : 'it failed')
584    } finally {
585      await releaseCall($, mine)
586    }
587  }
588  const ui = await attempt(() => read($, uiAtom), null)
589  // Timed out meanwhile, or another step took over: nothing to report here.
590  if (!ui || ui.busy !== 'clean' || ui.busySince !== since) return
591  const now = await $.clock.now()
592  if (skip !== null) {
593    const line = skip
594    await setUi($, u => withNote(withBusy(u, null, now), line, now))
595    toast($, line)
596  } else {
597    await setUi($, u => withNote(withBusy(u, null, now), 'Cleaned up.', now))
598  }
599  $.clock.after(0, () => void attempt(() => refresh($), undefined))
600}
601
602/** Keep warm: guarded at press time, one fork, never on a timer. */
603async function keepWarm($: EngineInterface): Promise<void> {
604  const now = await $.clock.now()
605  await disarm($)
606  const clock = (await attempt(() => read($, clockAtom), null)) ?? initialClock()
607  if (warmInFlight || clock.warming) {
608    toast($, 'A warm-up is already running.')
609    return
610  }
611  if (!canKeepWarm(clock, now)) {
612    toast($, clock.working || (await isTurnRunning($)) ? 'Keep warm waits until the turn finishes.' : 'The cache is too close to dropping to keep it warm.')
613    return
614  }
615  warmInFlight = true
616  const gen = sessionGen
617  await feedClock($, { type: 'warm-start' }, now)
618  $.clock.after(0, () => void attempt(() => runWarm($, clock.contextTokens, gen), undefined))
619}
620
621type ForkReply = Awaited<ReturnType<EngineInterface['model']['fork']>>
622
623/** The warm-up fork, or null once `ms` pass without an answer; a late answer is then ignored. */
624function forkWithin($: EngineInterface, ms: number): Promise<ForkReply | null> {
625  return new Promise<ForkReply | null>((resolve, reject) => {
626    let timer: { cancel: () => void } | null = null
627    try {
628      timer = $.clock.after(ms, () => resolve(null))
629    } catch {
630      // No timer: the fork alone decides.
631    }
632    $.model.fork({ prompt: WARM_PROMPT }).then(
633      reply => {
634        timer?.cancel()
635        resolve(reply)
636      },
637      error => {
638        timer?.cancel()
639        reject(error)
640      },
641    )
642  })
643}
644
645async function runWarm($: EngineInterface, known: number | null, gen: number): Promise<void> {
646  try {
647    const session = await attempt(() => read($, sessionAtom), null)
648    // A recorded 0 is no size at all: fall back to the session's own.
649    const contextTokens = known || session?.contextTokens || 0
650    const reply = await forkWithin($, BUSY_TIMEOUT_MS)
651    // Cleared meanwhile: this warm-up says nothing about the new session.
652    if (gen !== sessionGen) return
653    const at = await $.clock.now()
654    if (reply === null) {
655      await feedClock($, { type: 'warm-failed' })
656      toast($, warmToast({ ok: false, reason: 'the request failed' }))
657    } else if (reply.isAnswered) {
658      await feedClock($, { type: 'warm-done', at, cacheReadTokens: reply.usage.cache_read_input_tokens, contextTokens })
659      const after = await attempt(() => read($, clockAtom), null)
660      const line = warmToast({
661        ok: true,
662        lapsed: Boolean(after?.lapsed),
663        contextTokens,
664        cacheRead: reply.usage.cache_read_input_tokens,
665        lifetimeS: view(after ?? initialClock(), at, planDefault(session)).lifetime,
666      })
667      toast($, line)
668      // Also in the sentence for a few seconds, where the eye already is.
669      await setUi($, u => withNote(u, line, at))
670    } else {
671      await feedClock($, { type: 'warm-failed' })
672      toast($, warmToast({ ok: false, reason: reply.reason }))
673    }
674  } catch {
675    if (gen !== sessionGen) return
676    await feedClock($, { type: 'warm-failed' })
677    toast($, warmToast({ ok: false, reason: 'the request failed' }))
678  } finally {
679    warmInFlight = false
680  }
681}
682
683/**
684 * "Full dashboard": the dashboard command (it regenerates the page and opens it in the
685 * browser, whatever the OS opener is) through the runner Start fresh uses. A click while
686 * anything is busy is ignored; a failure is a note for NOTE_MS and nothing else.
687 */
688async function openDashboard($: EngineInterface): Promise<void> {
689  const now = await $.clock.now()
690  const ui = (await attempt(() => read($, uiAtom), null)) ?? initialUi()
691  if (!canOpenDashboard(ui, now)) return
692  await setUi($, u => withBusy(u, 'dashboard', now))
693  armBusyTimeout($, 'dashboard', now)
694  $.clock.after(0, () => void attempt(() => runDashboard($, now), undefined))
695}
696
697async function runDashboard($: EngineInterface, since: number): Promise<void> {
698  const io = dataIo($)
699  const root = await attempt(async () => findTokenOptimizerRoot(io, await readHome(io)), null)
700  const result = root ? await attempt(() => runMeasure(io, root, DASHBOARD_ARGS, { timeoutMs: DASHBOARD_TIMEOUT_MS }), null) : null
701  const ui = await attempt(() => read($, uiAtom), null)
702  // Timed out meanwhile, or another step took over: the person was told already.
703  if (!ui || ui.busy !== 'dashboard' || ui.busySince !== since) return
704  const now = await $.clock.now()
705  const failed = dashboardFailed(result)
706  await setUi($, u => (failed ? withNote(withBusy(u, null, now), DASHBOARD_FAILED, now) : withBusy(u, null, now)))
707}
708
709/** Start fresh: first press arms, a second within 5 s runs it. */
710async function startFresh($: EngineInterface): Promise<void> {
711  const now = await $.clock.now()
712  const ui = (await attempt(() => read($, uiAtom), null)) ?? initialUi()
713  if (busyNow(ui, now) !== null) return
714  if (!isArmed(ui, now)) {
715    await setUi($, u => ({ ...u, freshArmedAt: now }))
716    return
717  }
718  const running = await runningCall($)
719  if (running) {
720    await disarm($)
721    toast($, stillRunning(running))
722    return
723  }
724  if (await isTurnRunning($)) {
725    await disarm($)
726    toast($, 'Start fresh waits until the turn finishes.')
727    return
728  }
729  const sid = cleanId(await attempt(() => $.session.id(), ''))
730  if (!sid) {
731    await disarm($)
732    toast($, 'Start fresh stopped: no session to save. Nothing was cleared.')
733    return
734  }
735  const gen = sessionGen
736  await setUi($, u => withBusy(u, 'fresh-capture', now))
737  armBusyTimeout($, 'fresh-capture', now)
738  $.clock.after(0, () => void attempt(() => runFresh($, sid, now, gen), undefined))
739}
740
741/** Still the session Start fresh was pressed in: a typed /clear meanwhile means stand down. */
742async function stillSession($: EngineInterface, sid: string, gen: number): Promise<boolean> {
743  return gen === sessionGen && cleanId(await attempt(() => $.session.id(), '')) === sid
744}
745
746async function runFresh($: EngineInterface, sid: string, since: number, gen: number): Promise<void> {
747  const io = dataIo($)
748  const stop = async (reason: string): Promise<void> => {
749    const ui = await attempt(() => read($, uiAtom), null)
750    if (!ui || ui.busySince !== since) return
751    const now = await $.clock.now()
752    const line = `Start fresh stopped: ${reason}. Nothing was cleared.`
753    await setUi($, u => withNote(withBusy(u, null, now), line, now))
754    toast($, line)
755  }
756
757  const root = await findTokenOptimizerRoot(io, await readHome(io))
758  if (!root) return stop('Token Optimizer was not found')
759  const port: HandoffPort = {
760    run: (args, stdin) =>
761      runMeasure(io, root, args, {
762        timeoutMs: args[0] === 'compact-capture' ? CAPTURE_TIMEOUT_MS : RESUME_TIMEOUT_MS,
763        ...(stdin === undefined ? {} : { stdin }),
764      }),
765    read: path => io.read(path),
766  }
767  const cwd = await attempt(() => $.session.cwd(), '')
768  // `now` is a placeholder: the hand-off is stamped once its save lands (below).
769  const result = await prepareHandoff(port, { sessionId: sid, transcriptPath: transcriptFor(cleanId(sid)) ?? null, cwd, now: since })
770  const standDown = async (): Promise<void> => {
771    const now = await $.clock.now()
772    await setUi($, u => (u.busySince === since ? withBusy(u, null, now) : u))
773  }
774  // The session changed under it (a typed /clear, a resume): clear nothing, and say so.
775  const changed = async (): Promise<void> => {
776    await standDown()
777    toast($, 'Start fresh stopped: the session changed.')
778  }
779  if (!(await stillSession($, sid, gen))) return changed()
780  const ui = await attempt(() => read($, uiAtom), null)
781  // Timed out meanwhile: the person was told; clear nothing.
782  if (!ui || ui.busy !== 'fresh-capture' || ui.busySince !== since) return
783  if (!result.ok) return stop(result.reason)
784  // Our compaction or clear still running: never overlap it.
785  const running = await runningCall($)
786  if (running) return stop(running === 'compact' ? 'the last clean-up is still running' : 'the last clear is still running')
787  // A turn started during the capture: a queued clear would wipe it.
788  if (await isTurnRunning($)) {
789    await standDown()
790    toast($, 'Start fresh waits until the turn finishes.')
791    return
792  }
793
794  // Saved, then stamped from the clock right after the save lands: the
795  // 10-minute window and "started since the save" both count from there.
796  let handoff: Handoff
797  try {
798    // First kept with a stamp in the far future, so no session can count as "started
799    // since the save" until the real stamp below lands.
800    await $.store.set(HANDOFF_KEY, { ...result.handoff, createdAt: Number.MAX_SAFE_INTEGER, pendingSince: since })
801  } catch {
802    return stop('the hand-off could not be kept on disk')
803  }
804  try {
805    handoff = { ...result.handoff, createdAt: await $.clock.now() }
806    await $.store.set(HANDOFF_KEY, handoff)
807  } catch {
808    await dropHandoff($)
809    return stop('the hand-off could not be kept on disk')
810  }
811  try {
812    await update($, handoffAtom, () => handoff)
813  } catch {
814    // The new session would never see it.
815    await dropHandoff($)
816    return stop('the hand-off could not be kept on disk')
817  }
818  if (!(await stillSession($, sid, gen))) {
819    await dropHandoff($)
820    return changed()
821  }
822  const now = await $.clock.now()
823  await setUi($, u => withBusy(u, 'fresh-clear', now))
824  armBusyTimeout($, 'fresh-clear', now)
825  $.clock.after(0, () => void attempt(() => runClear($, now, handoff, sid, gen), undefined))
826}
827
828async function runClear($: EngineInterface, since: number, handoff: Handoff, sid: string, gen: number): Promise<void> {
829  const ours = (h: Handoff | null) => h !== null && h.fromSessionId === handoff.fromSessionId && h.createdAt === handoff.createdAt
830  const fail = async (line: string): Promise<void> => {
831    const held = await attempt(() => read($, handoffAtom), null)
832    if (ours(held)) await dropHandoff($)
833    const now = await $.clock.now()
834    await setUi($, u => withNote(u.busy === 'fresh-clear' && u.busySince === since ? withBusy(u, null, now) : u, line, now))
835    toast($, line)
836  }
837  // The session changed while the clear waited its turn: clear nothing.
838  if (gen !== sessionGen || cleanId(await attempt(() => $.session.id(), '')) !== sid) {
839    const held = await attempt(() => read($, handoffAtom), null)
840    if (ours(held)) await dropHandoff($)
841    const now = await $.clock.now()
842    await setUi($, u => (u.busy === 'fresh-clear' && u.busySince === since ? withBusy(u, null, now) : u))
843    toast($, 'Start fresh stopped: the session changed.')
844    return
845  }
846  const running = await runningCall($)
847  if (running || engineCall) return fail(`Start fresh stopped: ${(running ?? engineCall) === 'compact' ? 'the last clean-up is still running' : 'the last clear is still running'}. Nothing was cleared.`)
848  // Claimed with no await between the check and the claim; recorded before the call.
849  const mine = await claimCall($, 'clear')
850  if (!mine) return fail('Start fresh could not clear. Your conversation is unchanged.')
851  let cleared = false
852  try {
853    await $.command.run({ command: 'clear' })
854    cleared = true
855  } catch {
856    // Reported below, once the call has settled.
857  } finally {
858    await releaseCall($, mine)
859  }
860  if (!cleared) return fail('Start fresh could not clear. Your conversation is unchanged.')
861  // The hand-off now waits for the first prompt of a conversation started since the save.
862  const now = await $.clock.now()
863  await setUi($, u => (u.busy === 'fresh-clear' && u.busySince === since ? withBusy(u, null, now) : u))
864}
865
866/**
867 * The user's band size: a stored click first, else the env default. The store
868 * is read off the render path (a store read can be slow) and kept in module
869 * state; a press made meanwhile always wins (it set sizeClicked itself).
870 */
871function bandSize(): BandSize {
872  return sizeClicked ?? sizeEnvDefault
873}
874
875/** One store read for the size, fired off the render path; a found size redraws once when it differs. */
876async function readSize($: EngineInterface): Promise<void> {
877  if (sizeRead || sizeClicked !== null) return
878  try {
879    const stored = await $.store.get(SIZE_KEY)
880    sizeRead = true
881    const found = storedSize(stored)
882    if (sizeClicked === null) {
883      sizeClicked = found
884      // A stored size that differs from what was drawn earns one redraw.
885      if (found !== null && found !== sizeEnvDefault) await attempt(() => update($, frameAtom, n => (n ?? 0) + 1), undefined)
886    }
887  } catch {
888    // Store unreadable for now: the env default serves; a later readSize call retries.
889  }
890}
891
892/** The size button: flips the band and remembers the choice. A refused write just does not persist. */
893async function toggleSize($: EngineInterface): Promise<void> {
894  await disarm($)
895  const next: BandSize = bandSize() === 'slim' ? 'full' : 'slim'
896  sizeClicked = next
897  await attempt(() => $.store.set(SIZE_KEY, next), undefined)
898  await attempt(() => update($, frameAtom, n => (n ?? 0) + 1), undefined)
899}
900
901async function toggleDetails($: EngineInterface): Promise<void> {
902  await disarm($)
903  await flushTools($)
904  const current = await attempt(() => read($, sessionAtom), null)
905  if (!current) return
906  const opening = !current.sheetOpen
907  await update($, sessionAtom, cur => (cur ? { ...cur, sheetOpen: !cur.sheetOpen } : cur))
908  // Savings read when the row opens.
909  if (opening) $.clock.after(0, () => void attempt(() => refresh($, { savings: true }), undefined))
910}
911
912async function act($: EngineInterface, id: ActionId | 'dashboard'): Promise<void> {
913  // One press at a time: the claim is taken before the first await, so a second press
914  // arriving while this one reads the clock sees it (an unread time counts as now).
915  const prior = claim
916  if (prior !== null && prior.at === Number.POSITIVE_INFINITY) return
917  const mine = { at: Number.POSITIVE_INFINITY }
918  claim = mine
919  const now = await attempt(() => $.clock.now(), 0)
920  if (prior !== null && now - prior.at < BUSY_TIMEOUT_MS) {
921    if (claim === mine) claim = prior
922    return
923  }
924  mine.at = now
925  try {
926    if (id === 'clean' || id === 'clean-first') await cleanUp($)
927    else if (id === 'dashboard') await openDashboard($)
928    else if (id === 'fresh') await startFresh($)
929    else await keepWarm($)
930  } catch {
931    toast($, 'That did not work. Try again in a moment.')
932  } finally {
933    if (claim === mine) claim = null
934  }
935}
936
937// ---- drawing ----
938
939/**
940 * The desktop gives the band no light/dark signal (the config's theme is the
941 * terminal's), so the marks and icons follow the app's own appearance through their
942 * colour-scheme query; the drawn colours are the light ones, the fallback.
943 * Classes: k = ink stroke, kf = ink fill, t = track stroke, tf = track fill.
944 */
945const DARK_STYLE = '<style>@media (prefers-color-scheme: dark){.k{stroke:#f3f1ea}.kf{fill:#f3f1ea}.t{stroke:#4b4a46}.tf{fill:#4b4a46}}</style>'
946
947function themed(svg: string, rootClass?: string): string {
948  const open = svg.indexOf('>') + 1
949  const head = rootClass ? svg.slice(0, open - 1).replace('<svg ', `<svg class="${rootClass}" `) + '>' : svg.slice(0, open)
950  return head + DARK_STYLE + svg.slice(open)
951}
952
953const esc = (v: string): string => v.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
954
955/**
956 * A mark as the crab draws a bar: a rounded track, filled in the tone's colour. A cold cache is
957 * a dashed outline instead of a fill; a grade is a rounded badge. Colour lives here, the text stays ink.
958 */
959const MARK_W = 18
960const MARK_H = 16
961const BADGE_W = 16
962const TRACK_STYLE = '<style>.tk{fill:#e4e4e2}@media (prefers-color-scheme: dark){.tk{fill:#2c2c2c}}</style>'
963
964const markWidth = (mark: Mark): number => (mark.badge !== undefined ? BADGE_W : MARK_W)
965
966function markSvg(mark: Mark, t: Tones): string {
967  const color = toneColor(mark.tone, t)
968  const w = markWidth(mark)
969  const p = Math.max(0, Math.min(100, mark.ringPercent ?? 0))
970  const fillW = p > 0 ? Math.max(8, Math.round((MARK_W * p) / 100)) : 0
971  const body =
972    mark.badge !== undefined
973      ? `<rect class="${mark.tone === 'none' ? 'tk' : ''}" x="0" y="0" width="${BADGE_W}" height="${MARK_H}" rx="4" fill="${mark.tone === 'none' ? t.track : color}"/>` +
974        `<text x="${BADGE_W / 2}" y="12" text-anchor="middle" font-family="system-ui, sans-serif" font-size="11" font-weight="700" fill="${t.card}">${esc(mark.badge)}</text>`
975      : mark.tone === 'cold'
976        ? `<rect x="1" y="4" width="${MARK_W - 2}" height="8" rx="4" fill="none" stroke="${color}" stroke-width="1.6" stroke-dasharray="3 3"/>`
977        : `<rect class="tk" x="0" y="3" width="${MARK_W}" height="10" rx="5"/>` +
978          (fillW > 0 ? `<rect x="0" y="3" width="${fillW}" height="10" rx="5" fill="${color}"/>` : '')
979  return `<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${MARK_H}" viewBox="0 0 ${w} ${MARK_H}" role="img" aria-label="${esc(mark.alt)}"><title>${esc(mark.alt)}</title>${TRACK_STYLE}${body}</svg>`
980}
981
982function toneColor(tone: MarkTone, t: Tones): string {
983  return tone === 'none' ? t.track : t[tone]
984}
985
986function runsOf(D: Desktop, runs: Run[], t: Tones) {
987  const { Text } = D
988  return runs.map(r => (r.lose ? <Text bold color={t.bad}>{r.text}</Text> : r.strong ? <Text bold>{r.text}</Text> : r.text))
989}
990
991function icon(D: Desktop, name: IconName, color: string, alt?: string) {
992  const { Svg } = D
993  const label = alt ?? ICON_ALT[name]
994  const svg = iconSvg(name, color, { alt: label })
995  return <Svg source={color === LIGHT.ink ? themed(svg, 'k') : svg} alt={label} width={16} height={16} />
996}
997
998type Model = {
999  snap: Snapshot
1000  tones: Tones
1001  sheetOpen: boolean
1002  savingsReason: string | null
1003  /** Keep warm can run now (the one guard): only then does the row offer it. */
1004  canWarm: boolean
1005}
1006
1007/** Below this, "saved this session" is noise and stays off the row. */
1008const SESSION_SAVED_MIN = 1000
1009
1010type Handlers = { act: (id: ActionId) => void; dashboard: () => void; details: () => void; size: () => void }
1011
1012
1013/** The five marks with their hover cards: one line, never wrapped, in both sizes. */
1014function markRow(D: Desktop, markList: Mark[], cardList: Card[], t: Tones, on: Handlers) {
1015  const { Box, Text, Svg } = D
1016  return (
1017    <Box flexDirection="row" flexWrap="wrap" columnGap={2} rowGap={1}>
1018      {markList.map((mark, i) => (
1019        <Box key={`mark-${mark.id}`} position="relative" flexDirection="row" alignItems="center" columnGap={1}>
1020          <Svg source={markSvg(mark, t)} alt={mark.alt} width={markWidth(mark)} height={MARK_H} />
1021          <Text bold>{mark.value}</Text>
1022          {/* Always labelled: without the word, nobody knows which number is which. */}
1023          <Text>{mark.label}</Text>
1024          {(() => {
1025            // Matched by id, not position: a missing limit never shifts a card under the wrong mark.
1026            const card = cardList.find(c => c.id === mark.id)
1027            return card ? cardBox(D, card, i, t, on) : ''
1028          })()}
1029        </Box>
1030      ))}
1031    </Box>
1032  )
1033}
1034
1035function cardBox(D: Desktop, card: Card, index: number, t: Tones, on: Handlers) {
1036  const { Box, Text, Button } = D
1037  const side = index < 2 ? { left: 0 } : { right: 0 }
1038  return (
1039    <Box
1040      position="absolute"
1041      bottom={1}
1042      {...side}
1043      display="none"
1044      hover={{ display: 'flex' }}
1045      flexDirection="column"
1046      width={34}
1047      paddingX={1}
1048      borderStyle="round"
1049      borderColor={t.line}
1050      backgroundColor={t.card}
1051    >
1052      <Text bold>{card.title}</Text>
1053      <Text wrap="wrap">{runsOf(D, card.body, t)}</Text>
1054      {card.actions.length > 0 ? (
1055        <Box flexDirection="row" columnGap={1} marginTop={1}>
1056          {card.actions.map(a => (
1057            <Button key={`card-${card.id}-${a.id}`} label={a.label} onPress={() => on.act(a.id)} />
1058          ))}
1059        </Box>
1060      ) : (
1061        ''
1062      )}
1063    </Box>
1064  )
1065}
1066
1067/** The unfolded row, shared by both sizes: the facts, any card action the moment calls for, the savings. */
1068function detailRow(D: Desktop, m: Model, on: Handlers, size: BandSize) {
1069  const { Box, Text, Button } = D
1070  const { snap, tones: t } = m
1071  const say = sentence(snap)
1072  const cardList = cards(snap)
1073  const detail = row(snap)
1074  // A card action joins the row only when the moment calls for it (quality sagging, the cache
1075  // about to drop) and the sentence's own button is not already offering it.
1076  const q = snap.quality
1077  const sagging = q !== null && (q.score < QUALITY_FLOOR || q.compactions >= COMPACT_HEAVY)
1078  const rowActions = [
1079    ...(sagging ? cardList.find(c => c.id === 'quality')?.actions ?? [] : []),
1080    ...(m.canWarm && snap.cache.state === 'warning' ? [{ id: 'warm' as const, label: 'Keep warm' }] : []),
1081  ].filter(a => a.id !== say.action?.id && !(a.id === 'clean' && say.action?.id === 'clean-first'))
1082
1083  // Said plainly: a bare "47M past 30 days" reads like tokens spent, and a 30-bar chart
1084  // ruled by one big day said nothing. The total is the dashboard's own.
1085  const sessionSaved = (detail.savings.sessionTokens ?? 0) >= SESSION_SAVED_MIN
1086  // No figure yet: say why (or that it is being measured), never "Saved -- tokens".
1087  const savingsBlock =
1088    detail.savings.last30Tokens == null ? (
1089      <Box flexDirection="row" alignItems="center" columnGap={1}>
1090        {icon(D, 'saved', t.ink)}
1091        <Text>{detail.savings.state === 'loading' ? 'Measuring savings…' : (m.savingsReason ?? detail.savings.reason ?? 'Savings appear once Token Optimizer has measured some.')}</Text>
1092      </Box>
1093    ) : (
1094      <Box flexDirection="row" alignItems="center" columnGap={1}>
1095        {icon(D, 'saved', t.good)}
1096        <Text>
1097          Saved <Text bold>{detail.savings.last30Text}</Text> tokens in 30 days
1098          {sessionSaved ? (
1099            <Text>
1100              {' '}(<Text bold>{detail.savings.sessionText}</Text> this session)
1101            </Text>
1102          ) : (
1103            ''
1104          )}
1105        </Text>
1106      </Box>
1107    )
1108
1109  // The end of the row: a plain text link (a Button drawn without chrome, the only thing the
1110  // desktop lets be pressed), its label its accessible name. The slim band has no sentence
1111  // line to carry "Opening the dashboard.", so there the status stands beside the link.
1112  const status = size === 'slim' ? dashboardStatus(snap.busy, snap.note) : null
1113  const dashboardLink = (
1114    <Box flexDirection="row" alignItems="center" columnGap={1}>
1115      {status !== null ? (
1116        <Box key="dashboard-status">
1117          <Text>{status}</Text>
1118        </Box>
1119      ) : (
1120        ''
1121      )}
1122      <Button key="dashboard" plain label="Full dashboard" onPress={() => on.dashboard()} />
1123    </Box>
1124  )
1125
1126  return (
1127    <Box key="row" flexDirection="row" flexWrap="wrap" alignItems="center" justifyContent="space-between" columnGap={2} rowGap={1}>
1128      <Box flexDirection="row" flexWrap="wrap" alignItems="center" columnGap={2} rowGap={1}>
1129        {detail.facts.map(f => (
1130          <Box flexDirection="row" alignItems="center" columnGap={1}>
1131            {icon(D, f.icon, t.ink)}
1132            <Text>{runsOf(D, f.runs, t)}</Text>
1133          </Box>
1134        ))}
1135        {rowActions.length > 0 ? (
1136          <Box flexDirection="row" alignItems="center" columnGap={1}>
1137            {rowActions.map(a => (
1138              <Button key={`row-${a.id}`} label={a.label} onPress={() => on.act(a.id)} />
1139            ))}
1140          </Box>
1141        ) : (
1142          ''
1143        )}
1144      </Box>
1145      <Box flexDirection="row" flexWrap="wrap" alignItems="center" columnGap={2} rowGap={1}>
1146        {savingsBlock}
1147        {dashboardLink}
1148      </Box>
1149    </Box>
1150  )
1151}
1152
1153/** The unfold arrow and the size button, together at the end of the marks line. */
1154function controls(D: Desktop, m: Model, on: Handlers, size: BandSize) {
1155  const { Box, Button } = D
1156  return (
1157    <Box flexDirection="row" columnGap={1} flexShrink={0}>
1158      {/* Only a Button can be pressed, and its label is text, so the arrow opens the detail row. */}
1159      <Button key="details" label={m.sheetOpen ? '▴' : '▾'} onPress={() => on.details()} />
1160      {/* One line instead of two: vertical space is at a premium. */}
1161      <Button key="size" label={size === 'slim' ? '+' : '–'} onPress={() => on.size()} />
1162    </Box>
1163  )
1164}
1165
1166/** The full band: what matters now (one sentence, at most one button), the five marks, the detail row when open. */
1167function drawBand(D: Desktop, m: Model, on: Handlers) {
1168  const { Box, Text, Button } = D
1169  const { snap, tones: t } = m
1170  const say = sentence(snap)
1171  return (
1172    // Keyed: the whole band is one hover zone (the desktop reveals within a keyed Box).
1173    <Box key="band" flexDirection="column" paddingX={1} rowGap={1}>
1174      <Box flexDirection="row" alignItems="center" justifyContent="space-between" columnGap={2}>
1175        <Box flexDirection="row" alignItems="center" columnGap={1} flexShrink={1}>
1176          <Text bold>Token Optimizer</Text>
1177          {icon(D, say.icon, toneColor(say.tone, t))}
1178          <Text wrap="wrap">{runsOf(D, say.runs, t)}</Text>
1179        </Box>
1180        <Box flexDirection="row" alignItems="center" columnGap={1} flexShrink={0}>
1181          {say.action ? <Button key="action" variant="primary" label={say.action.label} onPress={() => on.act(say.action!.id)} /> : ''}
1182          {controls(D, m, on, 'full')}
1183        </Box>
1184      </Box>
1185      {/* Its own line, with room: the marks wrap before they ever overlap. */}
1186      {markRow(D, marks(snap), cards(snap), t, on)}
1187      {m.sheetOpen ? detailRow(D, m, on, 'full') : ''}
1188    </Box>
1189  )
1190}
1191
1192/** The slim band: the marks on one line with the arrow and +, what the sentence earns on the right, the detail row when open. */
1193function drawSlim(D: Desktop, m: Model, on: Handlers) {
1194  const { Box, Text, Button } = D
1195  const { snap, tones: t } = m
1196  const side = slimSide(sentence(snap))
1197  return (
1198    <Box key="band" flexDirection="column" paddingX={1} rowGap={1}>
1199      <Box flexDirection="row" flexWrap="nowrap" alignItems="center" columnGap={2}>
1200        {controls(D, m, on, 'slim')}
hooks/crab.ts 692 lines
1import type { Character, Outfit } from '../types'
2
3// Pixel Clawd on a 30-wide grid, matching the Mascot Studio reference sheet: body 16x13,
4// claws 4x4, eyes 2x2, four 2x4 legs. Groups: `la`/`lb` leg pairs, everything else rides
5// inside `.body`; a class names a prop so CSS can move it on its own.
6export const CRAB_SCALE = 1
7export const CRAB_W = 30 * CRAB_SCALE
8// Feet sit at grid y=27; the tallest hat reaches y=-4, a flying crab lifts 5 more.
9export const CRAB_H = 27 * CRAB_SCALE
10export const BAR_H = 10
11export const BAR_Y = Math.round(31 * CRAB_SCALE + 6)
12export const ROW_H = BAR_Y + BAR_H + 3
13// Sizes drawn in pixels (the code window) follow the crab's scale.
14export const PX = CRAB_SCALE / 0.75
15
16const CLAY = '#D97757'
17const INK = '#1F1E1D'
18const GLASS = 'rgba(169,214,245,.55)'
19const GOLD = '#D9A23A'
20const PAPER = '#EEEDE8'
21
22type Fill = (x: number, y: number, w: number, h: number, c: string, cls?: string) => void
23
24const stamp = (f: Fill, x: number, y: number, rows: string[], map: Record<string, string>, cls?: string): void =>
25  rows.forEach((row, dy) => [...row].forEach((ch, dx) => map[ch] && f(x + dx, y + dy, 1, 1, map[ch] ?? '', cls)))
26
27// What differs between characters: the body, eyes, legs and arms. Outfits are drawn on top.
28type Parts = {
29  legs: (f: Fill) => void
30  torso: (f: Fill) => void
31  eyes: (f: Fill, y: number) => void
32  arm: (f: Fill, x: number, y: number, cls?: string) => void
33}
34
35const solid = (color: string) => (f: Fill, x: number, y: number, cls?: string): void => f(x, y, 4, 4, color, cls)
36// Arms are only drawn when a prop needs holding.
37const propArm = (color: string) => (f: Fill, x: number, y: number, cls?: string): void => {
38  if (cls) f(x, y, 4, 4, color, cls)
39}
40const fourLegs = (color: string) => (f: Fill): void => {
41  f(7, 23, 2, 4, color, 'la')
42  f(17, 23, 2, 4, color, 'la')
43  f(11, 23, 2, 4, color, 'lb')
44  f(21, 23, 2, 4, color, 'lb')
45}
46const pair = (color: string, w = 4) => (f: Fill): void => {
47  f(7, 23, w, 4, color, 'la')
48  f(23 - w, 23, w, 4, color, 'la')
49  f(7 + w + 1, 23, w, 4, color, 'lb')
50  f(22 - 2 * w, 23, w, 4, color, 'lb')
51}
52
53const FROG = '#C66B3E'
54const FROG_LIGHT = '#DA8E5E'
55const CAT = '#E39A4A'
56const CREEPER = '#5DAA4E'
57const SLIME = '#6CC25F'
58const STEEL_BLUE = '#9AA4B0'
59
60const CHARS: Record<Character, Parts> = {
61  // The default: Clawd, the crab.
62  claude: {
63    legs: fourLegs(CLAY),
64    torso: f => f(7, 10, 16, 13, CLAY),
65    eyes: (f, y) => {
66      f(9, y, 2, 2, INK, 'eye')
67      f(19, y, 2, 2, INK, 'eye')
68    },
69    arm: solid(CLAY),
70  },
71  // The Minecraft warm frog: squat, bulging eyes with wide black pupils, tan belly, big back legs.
72  frog: {
73    legs: f => {
74      f(2, 19, 6, 8, FROG, 'la'); f(22, 19, 6, 8, FROG, 'la'); f(0, 26, 3, 1, FROG_LIGHT, 'la'); f(27, 26, 3, 1, FROG_LIGHT, 'la')
75      f(10, 23, 4, 4, FROG, 'lb'); f(16, 23, 4, 4, FROG, 'lb'); f(9, 26, 6, 1, FROG_LIGHT, 'lb'); f(15, 26, 6, 1, FROG_LIGHT, 'lb')
76    },
77    torso: f => {
78      f(7, 12, 16, 11, FROG); f(7, 9, 6, 4, FROG); f(17, 9, 6, 4, FROG)
79      f(8, 9, 4, 1, FROG_LIGHT); f(18, 9, 4, 1, FROG_LIGHT)
80      f(11, 17, 8, 6, '#E2C285'); f(8, 16, 14, 1, '#8E4A2A')
81    },
82    eyes: (f, y) => {
83      f(8, y - 1, 4, 2, INK, 'eye'); f(18, y - 1, 4, 2, INK, 'eye')
84      f(8, y - 2, 4, 1, '#8A8D4B'); f(18, y - 2, 4, 1, '#8A8D4B')
85    },
86    arm: solid(FROG),
87  },
88  // The Minecraft creeper: green block, dark face, stub legs.
89  creeper: {
90    legs: pair('#3E7F36'),
91    torso: f => {
92      f(7, 10, 16, 13, CREEPER)
93      f(8, 11, 2, 2, '#3E7F36'); f(20, 12, 2, 2, '#3E7F36'); f(10, 20, 2, 2, '#3E7F36'); f(18, 19, 3, 2, '#3E7F36')
94      f(13, 15, 4, 2, INK); f(12, 17, 2, 3, INK); f(16, 17, 2, 3, INK)
95    },
96    eyes: (f, y) => {
97      f(9, y, 3, 3, INK, 'eye')
98      f(18, y, 3, 3, INK, 'eye')
99    },
100    arm: propArm(CREEPER),
101  },
102  // The Minecraft slime: a wobbly green cube with a darker core.
103  slime: {
104    legs: () => {},
105    torso: f => {
106      f(7, 11, 16, 12, SLIME); f(10, 14, 10, 7, '#4E9F47'); f(8, 12, 3, 1, '#B8F0A8'); f(9, 13, 1, 2, '#B8F0A8')
107      f(14, 19, 2, 1, INK)
108    },
109    eyes: (f, y) => {
110      f(11, y + 1, 2, 2, INK, 'eye')
111      f(17, y + 1, 2, 2, INK, 'eye')
112    },
113    arm: propArm(SLIME),
114  },
115  // A little robot: steel body, cyan eyes, antenna with a light.
116  robot: {
117    legs: pair('#6B7480', 3),
118    torso: f => {
119      f(7, 10, 16, 13, STEEL_BLUE); f(10, 16, 10, 5, '#5B6570'); f(11, 17, 2, 1, '#E0505A'); f(14, 17, 2, 1, '#5FBF8F'); f(17, 17, 2, 1, '#F5C542')
120      f(14, 6, 2, 4, '#6B7480'); f(13, 4, 4, 2, '#E0505A', 'ant')
121    },
122    eyes: (f, y) => {
123      f(9, y, 3, 2, '#7FD6FF', 'eye')
124      f(18, y, 3, 2, '#7FD6FF', 'eye')
125    },
126    arm: solid('#6B7480'),
127  },
128  // A tabby cat: ears, stripes, whiskers.
129  cat: {
130    legs: fourLegs(CAT),
131    torso: f => {
132      f(7, 10, 16, 13, CAT); f(7, 6, 4, 5, CAT); f(19, 6, 4, 5, CAT); f(8, 7, 2, 3, '#F2B6A0'); f(20, 7, 2, 3, '#F2B6A0')
133      f(10, 10, 2, 3, '#B8702E'); f(14, 10, 2, 2, '#B8702E'); f(18, 10, 2, 3, '#B8702E')
134      f(14, 15, 2, 1, '#E58AA0'); f(3, 15, 5, 1, '#F2F0EA'); f(22, 15, 5, 1, '#F2F0EA')
135      f(23, 20, 6, 2, CAT, 'tail'); f(27, 14, 2, 8, CAT, 'tail')
136    },
137    eyes: (f, y) => {
138      f(9, y, 2, 3, INK, 'eye')
139      f(19, y, 2, 3, INK, 'eye')
140    },
141    arm: solid(CAT),
142  },
143}
144
145export const CHARACTERS = Object.keys(CHARS) as Character[]
146
147// The colour of the thing he pulls out of his pocket when changing into each outfit.
148export const ITEM_COLOR: Partial<Record<Outfit, string>> = {
149  coding: '#2B2B2D',
150  walkCmd: '#2B2B2D',
151  cool: '#2B2B2D',
152  walkCool: '#2B2B2D',
153  fixing: '#6A50AA',
154  walkWizard: '#6A50AA',
155  chef: '#F4F3EE',
156  walkChef: '#F4F3EE',
157  reading: '#EEEDE8',
158  writing: '#EEEDE8',
159  walkCode: '#EEEDE8',
160  sleeping: '#3C79B8',
161  propeller: '#3FA796',
162  fly: '#3FA796',
163  idle: '#A9D6F5',
164  sunbathe: '#D8503F',
165}
166
167// Where the item ends up: on his head, in front of his eyes, in front of his belly, or held up.
168export type Dest = 'head' | 'face' | 'belly' | 'hand'
169export const ITEM_DEST: Partial<Record<Outfit, Dest>> = {
170  fixing: 'head', walkWizard: 'head', chef: 'head', walkChef: 'head', sleeping: 'head', propeller: 'head', fly: 'head', sunbathe: 'head',
171  coding: 'face', walkCmd: 'face', cool: 'face', walkCool: 'face',
172  reading: 'belly', writing: 'belly', walkCode: 'belly',
173  idle: 'hand',
174}
175const DEST_XY: Record<Dest, [number, number]> = { head: [-10, -15], face: [-9, -3], belly: [-12, 4], hand: [-2, -8] }
176
177// The item held up during a change; set by crabSvg.
178let ITEM = '#888888'
179
180// Set by crabSvg for the drawing in progress.
181let P: Parts = CHARS.claude
182const legs = (f: Fill): void => P.legs(f)
183const torso = (f: Fill): void => P.torso(f)
184const eyes = (f: Fill, y = 12): void => P.eyes(f, y)
185const happyEyes = (f: Fill): void => {
186  stamp(f, 9, 12, ['.x.', 'x.x'], { x: INK })
187  stamp(f, 19, 12, ['.x.', 'x.x'], { x: INK })
188}
189const shutEyes = (f: Fill): void => {
190  f(9, 13, 2, 1, INK)
191  f(19, 13, 2, 1, INK)
192}
193const lClaw = (f: Fill, y = 15, cls?: string): void => P.arm(f, 3, y, cls)
194const rClaw = (f: Fill, y = 15, cls?: string): void => P.arm(f, 23, y, cls)
195const shades = (f: Fill): void => {
196  f(8, 11, 5, 3, INK)
197  f(17, 11, 5, 3, INK)
198  f(13, 11, 4, 1, INK)
199  f(9, 12, 2, 1, '#6B6B6B')
200  f(19, 12, 2, 1, '#6B6B6B')
201}
202// An open book held over his belly; the right page turns.
203const book = (f: Fill): void => {
204  f(6, 17, 19, 9, INK)
205  f(7, 18, 8, 7, PAPER)
206  f(16, 18, 8, 7, PAPER, 'pg')
207  f(8, 19, 6, 1, CLAY)
208  f(8, 21, 6, 1, CLAY)
209  f(17, 19, 6, 1, CLAY, 'pg')
210  f(17, 21, 6, 1, CLAY, 'pg')
211}
212
213const WIZ = { p: '#6A50AA', l: '#A992E0', g: GOLD }
214const CAP = { b: '#3C79B8', w: '#F2F0EA' }
215
216const wizHat = (f: Fill): void => {
217  stamp(f, 5, -4, [
218      '..........pp........',
219      '..........pp........',
220      '.........ppp........',
221      '.........ppp........',
222      '.........ppp........',
223      '.......pppppp.......',
224      '.......ppgppp.......',
225      '.....pppggggppp.....',
226      '.....pppppgpppp.....',
227      '.....pppppppppp.....',
228      '...llllllllllllll...',
229      '...pppppppppppppp...',
230      'pppppppppppppppppppp',
231      'pppppppppppppppppppp',
232    ], WIZ)
233}
234const toque = (f: Fill): void => {
235  stamp(f, 6, 0, ['........lll.......', '.......lllll......', '.wwwwgwwwwwwgwwwww', 'wwwwwwwwwwwwwwwwww', 'wwwwwwwwwwwwwwwwww', 'wwwwwgwwwwwggwwwww', '.wwwwgwwwwwggwwwww', '.bbbbbbbbbbbbbbbb.', '.bbbbbbbbbbbbbbbb.', '.bbbbbbbbbbbbbbbb.'], { w: '#F4F3EE', l: '#F7F6F2', g: '#D2D1C8', b: '#E6E4DA' })
236}
237const propCap = (f: Fill): void => {
238  f(9, 8, 12, 2, '#3C79B8'); f(11, 6, 8, 2, '#B5432A'); f(13, 4, 4, 2, '#C9A24A'); f(14, 2, 2, 2, INK); f(9, 1, 12, 1, '#3FA796', 'prop')
239}
240// A small floating code panel he reads while walking.
241const codePanel = (f: Fill): void => {
242  f(18, 1, 12, 8, INK); f(19, 2, 10, 6, PAPER)
243  f(20, 3, 6, 1, '#7FC4F2', 'ln1'); f(21, 5, 8, 1, CLAY, 'ln2'); f(20, 7, 4, 1, '#4FA37A', 'ln3')
244}
245
246// A console window over his belly: title bar with three dots, a prompt chevron, three lines of
247// output that scroll up and fade (`sc1`..`sc3`), and a blinking cursor (`cur`).
248const consoleWindow = (f: Fill, screen: string, fg: string, fg2: string, bar: string): void => {
249  f(4, 15, 22, 11, INK)
250  f(5, 16, 20, 2, bar)
251  f(6, 16, 1, 1, '#FF5F57'); f(8, 16, 1, 1, '#FEBC2E'); f(10, 16, 1, 1, '#28C840')
252  f(5, 18, 20, 7, screen)
253  stamp(f, 6, 19, ['x.', '.x', 'x.'], { x: fg })
254  f(10, 19, 8, 1, fg2, 'sc1'); f(10, 21, 12, 1, fg, 'sc2'); f(7, 23, 10, 1, fg2, 'sc3')
255  f(19, 22, 2, 2, '#FFFFFF', 'cur')
256}
257
258// Victory poses; the character's own flourish (CSS `vc-<name>`) rides on these.
259export const WIN: ReadonlySet<Outfit> = new Set<Outfit>(['done', 'crown', 'exitCar', 'medal', 'winCode', 'winFix', 'winRead', 'winWrite'])
260
261// A little red convertible; `dx` parks it beside the crab. Drawn in front of his belly.
262const car = (f: Fill, dx = 0): void => {
263  f(dx, 20, 28, 5, '#D9453B', 'car'); f(dx, 20, 28, 1, '#B8352D', 'car'); f(dx + 4, 22, 20, 1, 'rgba(255,255,255,.35)', 'car')
264  f(dx + 27, 21, 3, 2, '#FFE9A8', 'car')
265  f(dx + 3, 24, 6, 4, INK, 'wh'); f(dx + 4, 25, 4, 2, '#8E929A', 'hub'); f(dx + 19, 24, 6, 4, INK, 'wh'); f(dx + 20, 25, 4, 2, '#8E929A', 'hub')
266}
267
268const DRAW: Record<Outfit, (f: Fill) => void> = {
269  // Idle: holds a magnifying glass and looks up at the code.
270  idle: f => {
271    legs(f); torso(f); lClaw(f); rClaw(f, 9, 'it'); eyes(f, 11)
272    stamp(f, 22, 0, ['.kkkk.', 'kllllk', 'klwllk', 'kllllk', 'kllllk', '.kkkk.'], { k: '#3A3A3C', l: GLASS, w: '#fff' }, 'it')
273    f(24, 6, 1, 3, '#7A4A26', 'it')
274  },
275  // Stroll: just walking about, arms swinging.
276  stroll: f => {
277    legs(f); torso(f); lClaw(f, 15, 'ta'); rClaw(f, 15, 'tb'); eyes(f)
278  },
279  // Coding: sunglasses and a terminal window held over his belly; claws at the sides.
280  coding: f => {
281    legs(f); torso(f); lClaw(f, 15, 'ta'); rClaw(f, 15, 'tb'); shades(f)
282    f(6, 18, 19, 7, INK); f(7, 19, 17, 5, PAPER)
283    stamp(f, 9, 20, ['x.', '.x', 'x.'], { x: INK })
284    f(13, 20, 5, 1, CLAY, 'ln1'); f(13, 22, 7, 1, '#4FA37A', 'ln2'); f(21, 20, 1, 1, '#8E929A', 'ln3')
285  },
286  // Fixing: wizard hat and a wand that waves and sparkles.
287  fixing: f => {
288    legs(f); torso(f); lClaw(f); eyes(f); rClaw(f, 9, 'it')
289    wizHat(f)
290    f(26, 8, 1, 1, '#7A4A26', 'it'); f(27, 7, 1, 1, '#7A4A26', 'it'); f(28, 6, 1, 1, '#7A4A26', 'it'); f(29, 5, 1, 1, '#7A4A26', 'it')
291    f(28, 3, 2, 2, '#F5C542', 'it'); f(26, 1, 1, 1, '#fff', 'sp'); f(29, 8, 1, 1, '#fff', 'st'); f(25, 4, 1, 1, '#F5C542', 'st')
292  },
293  // Chef: for the long jobs; tosses an omelette and now and then wipes his forehead.
294  chef: f => {
295    legs(f); torso(f); eyes(f)
296    toque(f)
297    lClaw(f, 15, 'wipe'); rClaw(f, 10, 'pan')
298    f(22, 8, 7, 2, '#4A4A48', 'pan'); f(26, 10, 1, 1, '#4A4A48', 'pan'); f(24, 7, 3, 1, '#F5B731', 'egg')
299    f(2, 7, 2, 3, '#7FC4F2', 'drop')
300  },
301  // Reading: nose in a book while he goes through files.
302  reading: f => {
303    legs(f); torso(f); lClaw(f, 16); rClaw(f, 16); eyes(f, 13); book(f)
304  },
305  // Writing: the same book with a pencil scribbling.
306  writing: f => {
307    legs(f); torso(f); lClaw(f, 16); rClaw(f, 16); eyes(f, 13); book(f)
308    f(24, 16, 1, 1, INK, 'wr'); f(25, 15, 1, 1, '#F5C542', 'wr'); f(26, 14, 1, 1, '#F5C542', 'wr'); f(27, 13, 1, 1, '#E8B04A', 'wr'); f(28, 12, 1, 1, '#C0705A', 'wr')
309  },
310  // Sleeping: nightcap, eyes shut, a pair of Zs floating up.
311  sleeping: f => {
312    legs(f); torso(f); lClaw(f); rClaw(f); shutEyes(f)
313    stamp(f, 7, 4, [
314      '...bbbbbb.......',
315      '.bbbbbbbbbb.....',
316      'bbbbbbbbbbbbbb..',
317      'bbbbbbbbbbbbbbbb',
318      'bbbbbbbbbbbbbbbb',
319      'wwwwwwwwwwwwwwww',
320    ], CAP)
321    f(22, 9, 3, 3, '#F2F0EA')
322    stamp(f, 24, 3, ['zzz', '..z', '.z.', 'z..', 'zzz'], { z: '#C0705A' }, 'z1')
323    stamp(f, 27, -2, ['zz', '.z', 'z.', 'zz'], { z: '#C0705A' }, 'z2')
324  },
325  // Working, no special task: thinking, three dots rising above his head.
326  working: f => {
327    legs(f); torso(f); lClaw(f); rClaw(f); eyes(f)
328    f(13, 5, 2, 2, '#C0705A', 'd1'); f(17, 3, 2, 2, '#C0705A', 'd2'); f(21, 5, 2, 2, '#C0705A', 'd3')
329  },
330  // Just finished: claws up, happy eyes.
331  done: f => {
332    legs(f); torso(f); happyEyes(f)
333    lClaw(f, 8, 'cheer'); rClaw(f, 8, 'cheer')
334    f(1, 3, 1, 1, '#F5C542', 'st'); f(28, 3, 1, 1, '#F5C542', 'st'); f(14, 4, 2, 2, '#F5C542', 'sp')
335  },
336  // Finished a long job: crown.
337  crown: f => {
338    legs(f); torso(f); lClaw(f); rClaw(f); happyEyes(f)
339    f(10, 4, 2, 3, GOLD); f(14, 3, 2, 4, GOLD); f(18, 4, 2, 3, GOLD); f(10, 7, 10, 3, GOLD); f(10, 9, 10, 1, '#B8841F')
340    f(14, 8, 2, 1, '#C0392B', 'gem')
341  },
342  // Something went wrong: a shaken crab with an exclamation mark.
343  error: f => {
344    legs(f); torso(f); lClaw(f); rClaw(f); shutEyes(f)
345    f(14, 0, 2, 6, '#C0392B', 'ex'); f(14, 7, 2, 2, '#C0392B', 'ex')
346  },
347  // Claude needs you: a gold question bubble bobs over his head while he waves a claw.
348  ask: f => {
349    legs(f); torso(f); lClaw(f); eyes(f); rClaw(f, 7, 'wv')
350    f(20, -6, 9, 10, GOLD, 'qb'); f(21, 4, 2, 2, GOLD, 'qb')
351    stamp(f, 22, -5, ['.xxx.', 'x...x', '....x', '...x.', '..x..', '.....', '..x..'], { x: INK }, 'qb')
352  },
353  // Running a Bash command: a dark console over his belly, output scrolling up, a blinking cursor.
354  shell: f => {
355    legs(f); torso(f); lClaw(f, 15, 'ta'); rClaw(f, 15, 'tb'); shades(f)
356    consoleWindow(f, '#101412', '#4FA37A', '#8FD694', '#3A3A3C')
357  },
358  // Running a PowerShell command: the same console in PowerShell blue.
359  pwsh: f => {
360    legs(f); torso(f); lClaw(f, 15, 'ta'); rClaw(f, 15, 'tb'); eyes(f)
361    consoleWindow(f, '#012456', '#EEEDE8', '#F9F1A5', '#2B5797')
362  },
363  // Greeting: waves with the right claw.
364  greeting: f => {
365    legs(f); torso(f); lClaw(f); happyEyes(f); rClaw(f, 7, 'wv')
366  },
367  // Propeller cap: the blades spin.
368  propeller: f => {
369    legs(f); torso(f); lClaw(f); rClaw(f); eyes(f)
370    propCap(f)
371  },
372  // Cool: just the sunglasses.
373  cool: f => {
374    legs(f); torso(f); lClaw(f); rClaw(f); shades(f)
375  },
376  // Sunbathe: stretched out on a deck chair under a striped umbrella, sunglasses on.
377  sunbathe: f => {
378    f(14, 0, 2, 11, '#CDBB93', 'um')
379    const span = (y: number, x0: number, x1: number): void => {
380      for (let x = x0; x <= x1; x++) f(x, y, 1, 1, Math.floor((x - 5) / 3) % 2 === 0 ? '#D8503F' : '#F4F1EA', 'um')
381    }
382    span(-4, 13, 21); span(-3, 10, 24); span(-2, 7, 27); span(-1, 5, 29)
383    f(0, -3, 4, 4, '#F5C542', 'sun'); f(5, -3, 1, 1, '#F5C542', 'sun')
384    legs(f); torso(f); lClaw(f, 16); rClaw(f, 16); shades(f)
385    f(3, 22, 24, 2, '#4FA3C7'); f(5, 22, 2, 2, '#F4F1EA'); f(13, 22, 2, 2, '#F4F1EA'); f(21, 22, 2, 2, '#F4F1EA')
386    f(4, 24, 2, 3, '#8E929A'); f(24, 24, 2, 3, '#8E929A')
387  },
388  // On the move: a plain stroll with sunglasses on.
389  walkCool: f => {
390    legs(f); torso(f); lClaw(f, 15, 'ta'); rClaw(f, 15, 'tb'); shades(f)
391  },
392  // On the move in a wizard hat.
393  walkWizard: f => {
394    legs(f); torso(f); lClaw(f, 15, 'ta'); rClaw(f, 15, 'tb'); eyes(f); wizHat(f)
395  },
396  // On the move in a chef's toque.
397  walkChef: f => {
398    legs(f); torso(f); lClaw(f, 15, 'ta'); rClaw(f, 15, 'tb'); eyes(f); toque(f)
399  },
400  // Flying: the propeller cap lifts him off the bar, claws flapping, legs dangling.
401  fly: f => {
402    legs(f); torso(f); lClaw(f, 12, 'fa'); rClaw(f, 12, 'fb'); eyes(f); propCap(f)
403  },
404  // Looking up at a code panel while he walks.
405  walkCode: f => {
406    legs(f); torso(f); lClaw(f, 15, 'ta'); rClaw(f, 15, 'tb')
407    f(10, 11, 2, 2, INK, 'eye'); f(20, 11, 2, 2, INK, 'eye')
408    codePanel(f)
409  },
410  // Running commands on a terminal while he walks.
411  walkCmd: f => {
412    legs(f); torso(f); lClaw(f, 15, 'ta'); rClaw(f, 15, 'tb'); shades(f)
413    f(6, 18, 19, 7, INK); f(7, 19, 17, 5, PAPER)
414    stamp(f, 9, 20, ['x.', '.x', 'x.'], { x: INK })
415    f(13, 20, 5, 1, CLAY, 'ln1'); f(13, 22, 7, 1, '#4FA37A', 'ln2'); f(21, 20, 1, 1, '#8E929A', 'ln3')
416  },
417  // The bar just jumped: sprinting, leaning forward, speed lines streaming behind.
418  run: f => {
419    legs(f); torso(f); lClaw(f, 14, 'ra'); rClaw(f, 14, 'rb'); eyes(f, 11)
420    f(-4, 13, 7, 1, 'rgba(140,140,140,.65)', 'sl'); f(-6, 17, 8, 1, 'rgba(140,140,140,.5)', 'sl'); f(-3, 21, 6, 1, 'rgba(140,140,140,.65)', 'sl')
421    f(3, 26, 3, 2, '#C9C5BC', 'dust'); f(0, 25, 2, 2, '#D8D4CB', 'dust')
422  },
423  // An even bigger leap: behind the wheel of a little red convertible, shades on, wheels spinning.
424  drive: f => {
425    torso(f); lClaw(f, 14, 'ra'); rClaw(f, 14, 'rb'); shades(f)
426    car(f)
427    f(-9, 14, 9, 1, 'rgba(140,140,140,.65)', 'sl'); f(-12, 19, 11, 1, 'rgba(140,140,140,.5)', 'sl'); f(-8, 23, 8, 1, 'rgba(140,140,140,.65)', 'sl')
428    f(-3, 22, 3, 3, '#C9C5BC', 'dust'); f(-6, 25, 3, 3, '#D8D4CB', 'dust')
429  },
430  // ...and then stopped: bent over, gasping, sweating, little clouds of breath.
431  pant: f => {
432    legs(f); torso(f); lClaw(f, 17); rClaw(f, 17); shutEyes(f)
433    f(13, 16, 4, 3, INK, 'mouth')
434    f(5, 9, 2, 3, '#7FC4F2', 'sw'); f(24, 8, 2, 3, '#7FC4F2', 'sw')
435    f(26, 15, 3, 2, '#FFFFFF', 'hf'); f(28, 18, 2, 2, '#FFFFFF', 'hf'); f(1, 15, 3, 2, '#FFFFFF', 'hf')
436  },
437  // Between the drive and the victory: the car brakes, wheels still, smoke and brake lights.
438  park: f => {
439    torso(f); lClaw(f, 14); rClaw(f, 14); eyes(f, 11)
440    car(f)
441    f(0, 21, 2, 2, '#FF3B30', 'bl')
442    f(-3, 25, 3, 3, '#C9C5BC', 'dust'); f(-7, 23, 3, 3, '#D8D4CB', 'dust'); f(-10, 28, 10, 1, '#8E929A')
443    f(24, 14, 2, 3, '#7FC4F2', 'sw')
444  },
445  // Between a finished task and its victory: a crouch, claws coming up, sparks drawn in.
446  brace: f => {
447    legs(f); torso(f); lClaw(f, 17, 'wd'); rClaw(f, 17, 'wd'); eyes(f, 11)
448    f(1, 5, 2, 2, GOLD, 'g1'); f(27, 5, 2, 2, GOLD, 'g2'); f(14, 0, 2, 2, '#fff', 'g3')
449  },
450  // Victory after the car: he has climbed out, the car is parked beside him, claws up.
451  exitCar: f => {
452    legs(f); torso(f); happyEyes(f); lClaw(f, 8, 'cheer'); rClaw(f, 8, 'cheer')
453    car(f, -26)
454    f(1, 3, 1, 1, '#F5C542', 'st'); f(28, 3, 1, 1, '#F5C542', 'st'); f(14, 4, 2, 2, '#F5C542', 'sp')
455  },
456  // Victory after the sprint: a gold medal on a red and blue ribbon.
457  medal: f => {
458    legs(f); torso(f); happyEyes(f); lClaw(f, 8, 'cheer'); rClaw(f, 8, 'cheer')
459    f(12, 14, 2, 2, '#D9453B'); f(14, 16, 2, 2, '#D9453B'); f(18, 14, 2, 2, '#3B6FD9'); f(16, 16, 2, 2, '#3B6FD9')
460    f(13, 18, 6, 5, GOLD); f(14, 19, 4, 3, '#F2C14E'); f(15, 20, 2, 1, '#FFF3C4', 'gem')
461    f(1, 3, 1, 1, '#F5C542', 'st'); f(28, 3, 1, 1, '#F5C542', 'st'); f(14, 4, 2, 2, '#F5C542', 'sp')
462  },
463  // Victory after coding: shades on, the terminal shows a green check.
464  winCode: f => {
465    legs(f); torso(f); shades(f); lClaw(f, 8, 'cheer'); rClaw(f, 8, 'cheer')
466    f(6, 18, 19, 7, INK); f(7, 19, 17, 5, PAPER)
467    stamp(f, 12, 19, ['......x', '.....x.', 'x...x..', '.x.x...', '..x....'], { x: '#4FA37A' }, 'gem')
468    f(1, 3, 1, 1, '#F5C542', 'st'); f(28, 3, 1, 1, '#F5C542', 'sp')
469  },
470  // Victory after fixing: the wizard throws a firework from the wand.
471  winFix: f => {
472    legs(f); torso(f); lClaw(f, 15); happyEyes(f); wizHat(f); rClaw(f, 8, 'cheer')
473    f(26, 5, 2, 2, GOLD, 'sp'); f(29, 8, 1, 1, '#fff', 'st'); f(23, 3, 1, 1, '#F5C542', 'st'); f(1, 4, 2, 2, '#A992E0', 'sp'); f(2, 10, 1, 1, '#fff', 'st')
474  },
475  // Victory after reading: the book is shut and held over his head.
476  winRead: f => {
477    legs(f); torso(f); happyEyes(f); lClaw(f, 8, 'cheer'); rClaw(f, 8, 'cheer')
478    f(9, 1, 12, 8, '#3C79B8'); f(9, 1, 2, 8, '#2B5C93'); f(13, 3, 5, 1, PAPER); f(13, 5, 3, 1, PAPER); f(22, 1, 1, 1, '#F5C542', 'sp')
479  },
480  // Victory after writing: the finished page, ticked off, held over his head.
481  winWrite: f => {
482    legs(f); torso(f); happyEyes(f); lClaw(f, 8, 'cheer'); rClaw(f, 8, 'cheer')
483    f(10, 0, 10, 10, INK); f(11, 1, 8, 8, PAPER); f(12, 2, 5, 1, CLAY); f(12, 4, 6, 1, CLAY)
484    stamp(f, 14, 6, ['..x', 'x.x', '.x.'], { x: '#4FA37A' }, 'gem')
485  },
486  // The prompt cache went cold: shivering inside a cube of ice.
487  frozen: f => {
488    legs(f); torso(f); lClaw(f); rClaw(f); eyes(f, 11)
489    f(1, 2, 28, 27, 'rgba(150,215,245,.3)', 'ice'); f(3, 0, 28, 2, 'rgba(215,242,253,.9)', 'ice'); f(29, 2, 3, 27, 'rgba(100,170,212,.75)', 'ice')
490    f(1, 2, 28, 1, '#D9F2FD', 'ice'); f(1, 2, 1, 27, '#D9F2FD', 'ice'); f(1, 28, 28, 1, '#6FB6DB', 'ice')
491    f(3, 4, 5, 1, '#FFFFFF', 'ice'); f(3, 5, 1, 4, '#FFFFFF', 'ice')
492    f(22, 24, 2, 2, '#FFFFFF', 'gl'); f(9, 8, 1, 1, '#FFFFFF', 'gl'); f(25, 10, 1, 1, '#FFFFFF', 'gl')
493  },
494  // Changing costume: digs the next one out of a belly pocket, holds it over his head, puffs.
495  changing: f => {
496    legs(f); torso(f); lClaw(f); eyes(f)
497    rClaw(f, 15, 'pk')
498    f(24, 8, 8, 5, ITEM, 'itm'); f(25, 9, 3, 1, 'rgba(255,255,255,.65)', 'itm')
499    f(11, 4, 2, 2, '#fff', 'puff'); f(18, 3, 2, 2, '#fff', 'puff'); f(14, 0, 2, 2, '#fff', 'puff'); f(8, 7, 2, 2, '#fff', 'puff'); f(21, 7, 2, 2, '#fff', 'puff')
500  },
501  // Dance: claws pumping, hopping.
502  dance: f => {
503    legs(f); torso(f); lClaw(f, 15, 'da'); rClaw(f, 15, 'db'); happyEyes(f)
504  },
505}
506
507export const crabSvg = (
508  outfit: Outfit,
509  x: number,
510  y: number,
511  isWalking = false,
512  character: Character = 'claude',
513  change?: { item: string; age: number; dest?: Dest },
514  settle?: number,
515): string => {
516  P = CHARS[character] ?? CHARS.claude
517  ITEM = change?.item ?? '#888888'
518  const groups = new Map<string, string[]>()
519  const f: Fill = (cx, cy, w, h, c, cls = 'bd') => {
520    let g = groups.get(cls)
521    if (!g) groups.set(cls, (g = []))
522    g.push(`<rect x="${cx}" y="${cy}" width="${w}" height="${h}" fill="${c}"/>`)
523  }
524  DRAW[outfit](f)
525  const [tx, ty] = DEST_XY[change?.dest ?? 'head']
526  const phase = outfit === 'changing' && change ? `style="animation-delay:-${Math.round(change.age)}ms;--tx:${tx}px;--ty:${ty}px"` : ''
527  const pop = settle !== undefined && settle >= 0 && settle < 400
528  const grp = (k: string): string => `<g class="${k}" ${phase}>${groups.get(k)?.join('') ?? ''}</g>`
529  // Legs go behind the body; the car goes in front of it and stays put while he bounces.
530  const BEHIND = ['la', 'lb']
531  const FRONT = ['car', 'wh', 'hub']
532  const legPairs = BEHIND.filter(k => groups.has(k)).map(grp).join('')
533  const front = FRONT.filter(k => groups.has(k)).map(grp).join('')
534  const rest = [...groups.keys()].filter(k => !BEHIND.includes(k) && !FRONT.includes(k)).map(grp).join('')
535  const cls = `c-${outfit}${WIN.has(outfit) ? ` win vc-${character}` : ''}${isWalking ? ' walk' : ''}${outfit === 'stroll' || outfit.startsWith('walk') ? ' amble' : ''}`
536  return `<g transform="translate(${x},${y}) scale(${CRAB_SCALE})" shape-rendering="crispEdges">${pop ? `<g class="pop" style="animation-delay:-${Math.round(settle ?? 0)}ms">` : ''}<g class="${cls}">${legPairs}<g class="body">${rest}</g>${front}</g>${pop ? '</g>' : ''}</g>`
537}
538
539// The code the idle crab studies, beside him.
540export const codeWindow = (x: number, y: number): string => `<g class="win" transform="translate(${x},${y}) scale(${PX})">
541<rect class="k" x="0" y="0" width="40" height="18" rx="2"/>
542<rect class="ln1" x="4" y="3" width="14" height="2" fill="#7FC4F2"/>
543<rect class="ln2" x="8" y="7" width="22" height="2" fill="${CLAY}"/>
544<rect class="ln3" x="4" y="11" width="10" height="2" fill="#8FD694"/>
545</g>`
546
547// Every loop is started `at` into its cycle, so the drawing that replaces this one each
548// second carries on from where the last left off instead of restarting.
549export const crabCss = (at: number): string => {
550  const s = at / 1000
551  const d = (period: number, offset = 0): string => `-${((s + offset) % period).toFixed(3)}s`
552  return `<style>
553g{transform-box:fill-box}
554.k{fill:#e4e4e2}.m{fill:#8a8a8a}
555@media (prefers-color-scheme: dark){.k{fill:#2c2c2c}.m{fill:#9a9a9a}}
556.body{animation:bob 1s ease-in-out infinite ${d(1)}}
557@keyframes bob{50%{transform:translateY(1px)}}
558.eye{animation:eb 5s steps(1) infinite ${d(5)}}
559@keyframes eb{0%,94%,100%{transform:scaleY(1)}96%{transform:scaleY(.1)}}
560.walk .la{animation:st .5s steps(1) infinite ${d(0.5)}}.walk .lb{animation:st .5s steps(1) infinite ${d(0.5, 0.25)}}
561.amble.walk .la{animation:st .8s steps(1) infinite ${d(0.8)}}.amble.walk .lb{animation:st .8s steps(1) infinite ${d(0.8, 0.4)}}
562.walk .ta{animation:sw .5s steps(1) infinite ${d(0.5, 0.25)}}.walk .tb{animation:sw .5s steps(1) infinite ${d(0.5)}}
563@keyframes st{50%{transform:translateY(-3px)}}
564@keyframes sw{50%{transform:translateY(2px)}}
565.c-working .d1,.c-working .d2,.c-working .d3{animation:dot 1.5s steps(1) infinite ${d(1.5)}}
566.c-working .d2{animation-delay:${d(1.5, 1)}}.c-working .d3{animation-delay:${d(1.5, 0.5)}}
567@keyframes dot{0%{opacity:0}33%{opacity:1}100%{opacity:1}}
568.c-idle .it{transform-origin:50% 100%;animation:sweep 3s ease-in-out infinite ${d(3)}}
569@keyframes sweep{0%,100%{transform:rotate(-8deg) translateX(-1px)}50%{transform:rotate(10deg) translateX(1px)}}
570.c-idle .eye{animation:lookup 4s steps(1) infinite ${d(4)}}
571@keyframes lookup{0%,100%{transform:translate(0,0)}30%{transform:translate(1px,-1px)}60%{transform:translate(0,-1px)}}
572.ln1{animation:cur 1.4s steps(1) infinite ${d(1.4)}}.ln2{animation:cur 1.4s steps(1) infinite ${d(1.4, 0.5)}}.ln3{animation:cur 1.4s steps(1) infinite ${d(1.4, 0.9)}}
573@keyframes cur{50%{opacity:.35}}
574.c-coding .ta,.c-shell .ta,.c-pwsh .ta{animation:tap .3s steps(1) infinite ${d(0.3)}}.c-coding .tb,.c-shell .tb,.c-pwsh .tb{animation:tap .3s steps(1) infinite ${d(0.3, 0.15)}}
575.c-shell .sc1,.c-pwsh .sc1{animation:outp 1.2s linear infinite ${d(1.2)}}.c-shell .sc2,.c-pwsh .sc2{animation:outp 1.2s linear infinite ${d(1.2, 0.4)}}.c-shell .sc3,.c-pwsh .sc3{animation:outp 1.2s linear infinite ${d(1.2, 0.8)}}
576@keyframes outp{0%{opacity:0;transform:translateY(2px)}25%{opacity:1}100%{opacity:.15;transform:translateY(-2px)}}
577.c-shell .cur,.c-pwsh .cur{animation:blink .6s steps(1) infinite ${d(0.6)}}
578@keyframes tap{50%{transform:translateY(2px)}}
579.c-fixing .it{transform-origin:0 100%;animation:wave 1.2s ease-in-out infinite ${d(1.2)}}
580@keyframes wave{50%{transform:rotate(-25deg)}}
581.c-fixing .sp,.c-done .sp{animation:blink .6s steps(1) infinite ${d(0.6)}}.c-fixing .st,.c-done .st{animation:blink .6s steps(1) infinite ${d(0.6, 0.3)}}
582@keyframes blink{50%{opacity:.1}}
583.c-chef .pan{transform-origin:0 50%;animation:tilt 1s ease-in-out infinite ${d(1)}}
584.c-chef .egg{animation:flip 1s ease-in-out infinite ${d(1)}}
585@keyframes tilt{20%,40%{transform:rotate(-12deg)}}
586@keyframes flip{30%{transform:translateY(-5px) scaleY(-1)}60%{transform:translateY(0)}}
587.c-chef .wipe{animation:wipe 11.3s ease-in-out infinite ${d(11.3)}}
588@keyframes wipe{0%,72%{transform:translate(0,0)}78%,84%{transform:translate(2px,-7px)}88%{transform:translate(0,-7px)}92%{transform:translate(2px,-7px)}100%{transform:translate(0,0)}}
589.c-chef .drop{animation:drop 11.3s linear infinite ${d(11.3)}}
590@keyframes drop{0%,76%{opacity:0;transform:translateY(0)}80%{opacity:1}96%{opacity:0;transform:translateY(5px)}100%{opacity:0}}
591.c-reading .pg,.c-writing .pg{transform-origin:0 50%;animation:page 4s ease-in-out infinite ${d(4)}}
592@keyframes page{0%,80%,100%{transform:scaleX(1)}88%{transform:scaleX(.1)}}
593.c-writing .wr{animation:scribble .4s steps(1) infinite ${d(0.4)}}
594@keyframes scribble{50%{transform:translate(-1px,1px)}}
595.c-greeting .wv,.c-ask .wv{transform-origin:0 100%;animation:wave2 .6s ease-in-out infinite ${d(0.6)}}
596.c-ask .qb{animation:qbob 1s ease-in-out infinite ${d(1)}}
597@keyframes qbob{50%{transform:translateY(-2px)}}
598.c-ask .body{animation:hop .6s ease-in-out infinite ${d(0.6)}}
599@keyframes wave2{50%{transform:rotate(-22deg)}}
600.c-propeller .prop{animation:spin .25s steps(1) infinite ${d(0.25)}}
601@keyframes spin{50%{transform:scaleX(.3)}}
602.c-fly{animation:fly 1.6s ease-in-out infinite ${d(1.6)}}
603@keyframes fly{0%,100%{transform:translateY(-3px)}50%{transform:translateY(-5px)}}
604.c-fly .prop{animation:spin .15s steps(1) infinite ${d(0.15)}}
605.c-fly .fa{animation:flap .3s steps(1) infinite ${d(0.3)}}.c-fly .fb{animation:flap .3s steps(1) infinite ${d(0.3, 0.15)}}
606@keyframes flap{50%{transform:translateY(-3px)}}
607.c-fly .la{animation:dangle 1.6s ease-in-out infinite ${d(1.6)}}.c-fly .lb{animation:dangle 1.6s ease-in-out infinite ${d(1.6, 0.8)}}
608@keyframes dangle{50%{transform:translateY(1px)}}
609.c-propeller .body{animation:jump 1.2s ease-out infinite ${d(1.2)}}
610@keyframes jump{0%,60%,100%{transform:translateY(0)}15%{transform:translateY(-7px)}30%{transform:translateY(0)}45%{transform:translateY(-4px)}}
611.c-sunbathe .um{transform-origin:50% 100%;animation:sway 4s ease-in-out infinite ${d(4)}}
612@keyframes sway{50%{transform:rotate(1.5deg)}}
613.c-sunbathe .sun{animation:blink 2s steps(1) infinite ${d(2)}}
614.c-sunbathe .body{animation:breathe 4s ease-in-out infinite ${d(4)}}
615.c-error .body{animation:shake .15s steps(1) infinite ${d(0.15)}}
616@keyframes shake{50%{transform:translateX(1px)}}
617.c-error .ex{animation:blink .5s steps(1) infinite ${d(0.5)}}
618.c-changing .pk,.c-changing .itm{animation:pkmove 1.4s ease-in-out forwards}
619.c-changing .itm{animation:pkmove 1.4s ease-in-out forwards,show 1.4s steps(1) forwards}
620@keyframes pkmove{0%{transform:translate(0,0)}20%,45%{transform:translate(-12px,4px)}80%,100%{transform:translate(var(--tx,-10px),var(--ty,-15px))}}
621.pop{transform-origin:50% 100%;animation:pop .4s ease-out both}
622@keyframes pop{0%{transform:scale(.82,1.18)}35%{transform:scale(1.12,.88)}65%{transform:scale(.97,1.04)}100%{transform:scale(1,1)}}
623.c-run .la{animation:sprint .16s steps(1) infinite ${d(0.16)}}.c-run .lb{animation:sprint .16s steps(1) infinite ${d(0.16, 0.08)}}
624@keyframes sprint{50%{transform:translateY(-5px)}}
625.c-run .body{transform-origin:50% 100%;animation:lean .32s ease-in-out infinite ${d(0.32)}}
626@keyframes lean{0%,100%{transform:rotate(7deg) translateY(0)}50%{transform:rotate(7deg) translateY(-2px)}}
627.c-run .ra{animation:pumpa .16s steps(1) infinite ${d(0.16)}}.c-run .rb{animation:pumpa .16s steps(1) infinite ${d(0.16, 0.08)}}
628@keyframes pumpa{50%{transform:translate(2px,-5px)}}
629.c-drive .body{animation:rumble .2s steps(1) infinite ${d(0.2)}}
630@keyframes rumble{50%{transform:translateY(1px)}}
631.c-drive .hub{animation:roll .12s steps(1) infinite ${d(0.12)}}
632@keyframes roll{50%{transform:translate(1px,1px)}}
633.c-drive .ra{animation:pumpa .3s steps(1) infinite ${d(0.3)}}.c-drive .rb{animation:pumpa .3s steps(1) infinite ${d(0.3, 0.15)}}
634.c-run .sl,.c-drive .sl{animation:rush .24s linear infinite ${d(0.24)}}
635@keyframes rush{0%{opacity:.9;transform:translateX(0)}100%{opacity:0;transform:translateX(-8px)}}
636.c-run .dust,.c-drive .dust{animation:dust .32s linear infinite ${d(0.32)}}
637@keyframes dust{0%{opacity:.9;transform:translate(0,0) scale(.6)}100%{opacity:0;transform:translate(-6px,-4px) scale(1.4)}}
638.c-pant .body{transform-origin:50% 100%;animation:pant .75s ease-in-out infinite ${d(0.75)}}
639@keyframes pant{0%,100%{transform:scale(1,1)}50%{transform:scale(1.06,.91)}}
640.c-pant .mouth{transform-origin:50% 0;animation:gasp .75s ease-in-out infinite ${d(0.75)}}
641@keyframes gasp{50%{transform:scaleY(1.7)}}
642.c-pant .sw{animation:drip 1.5s linear infinite ${d(1.5)}}
643@keyframes drip{0%{opacity:0;transform:translateY(0)}20%{opacity:1}100%{opacity:0;transform:translateY(7px)}}
644.c-pant .hf{animation:huff .75s ease-out infinite ${d(0.75)}}
645@keyframes huff{0%{opacity:.95;transform:translate(0,0) scale(.6)}100%{opacity:0;transform:translate(6px,-4px) scale(1.5)}}
646.c-park .body{animation:lurch .5s ease-out infinite ${d(0.5)}}
647@keyframes lurch{0%{transform:translateX(3px)}100%{transform:translateX(0)}}
648.c-park .bl{animation:blink .4s steps(1) infinite ${d(0.4)}}
649.c-park .dust{animation:dust .5s linear infinite ${d(0.5)}}
650.c-park .sw{animation:drip 1.5s linear infinite ${d(1.5)}}
651.c-brace .body{transform-origin:50% 100%;animation:crouch .7s ease-in infinite ${d(0.7)}}
652@keyframes crouch{0%{transform:scale(1,1)}80%,100%{transform:scale(1.1,.84)}}
653.c-brace .wd{animation:raise .7s ease-in infinite ${d(0.7)}}
654@keyframes raise{0%{transform:translateY(0)}100%{transform:translateY(-6px)}}
655.c-brace .g1,.c-brace .g2,.c-brace .g3{animation:gather .7s ease-in infinite ${d(0.7)}}
656.c-brace .g1{--gx:11px;--gy:9px}.c-brace .g2{--gx:-11px;--gy:9px}.c-brace .g3{--gx:0px;--gy:11px}
657@keyframes gather{0%{opacity:0;transform:translate(0,0)}25%{opacity:1}100%{opacity:0;transform:translate(var(--gx),var(--gy))}}
658@keyframes show{0%,44%{opacity:0}45%,100%{opacity:1}}
659.c-changing .puff{animation:puff 1.4s ease-out forwards}
660@keyframes puff{0%,80%{opacity:0;transform:scale(.3)}90%{opacity:1;transform:scale(1.3)}100%{opacity:0;transform:scale(1.6)}}
661.c-dance .da{animation:up .5s steps(1) infinite ${d(0.5)}}.c-dance .db{animation:up .5s steps(1) infinite ${d(0.5, 0.25)}}
662@keyframes up{50%{transform:translateY(-6px)}}
663.c-dance .body{animation:hop .5s ease-in-out infinite ${d(0.5)}}
664@keyframes hop{50%{transform:translateY(-2px)}}
665.c-crown .gem,.win .gem{animation:blink 1s steps(1) infinite ${d(1)}}
666.win .sp,.win .st{animation:blink .6s steps(1) infinite ${d(0.6)}}
667.win .cheer{animation:cheer .4s steps(1) infinite ${d(0.4)}}
668.win.vc-claude .body{transform-origin:50% 100%;animation:vclaude .5s ease-out infinite ${d(0.5)}}
669@keyframes vclaude{50%{transform:translateY(-3px)}}
670.win.vc-frog .body{transform-origin:50% 100%;animation:vfrog .6s ease-out infinite ${d(0.6)}}
671@keyframes vfrog{0%,100%{transform:translateY(0) scale(1.06,.9)}50%{transform:translateY(-9px) scale(.95,1.1)}}
672.win.vc-creeper .body{transform-origin:50% 100%;animation:vcreep .5s ease-in-out infinite ${d(0.5)}}
673@keyframes vcreep{0%,100%{transform:scale(1,1)}50%{transform:scale(1.14,1.14)}}
674.win.vc-slime .body{transform-origin:50% 100%;animation:vslime .5s ease-in-out infinite ${d(0.5)}}
675@keyframes vslime{0%,100%{transform:scale(1.12,.86)}50%{transform:translateY(-4px) scale(.9,1.14)}}
676.win.vc-robot .body{transform-origin:50% 100%;animation:vrobot .3s steps(1) infinite ${d(0.3)}}
677@keyframes vrobot{0%{transform:rotate(-4deg)}50%{transform:rotate(4deg)}}
678.win.vc-cat .body{transform-origin:50% 100%;animation:vcat 1s ease-in-out infinite ${d(1)}}
679@keyframes vcat{0%,100%{transform:rotate(-5deg)}50%{transform:rotate(5deg) translateY(-2px)}}
680.c-frozen .body,.c-frozen .eye{animation:none}
681.c-frozen .bd{animation:shiver .14s steps(1) infinite ${d(0.14)}}
682@keyframes shiver{50%{transform:translateX(1px)}}
683.c-frozen .gl{animation:blink 1.6s steps(1) infinite ${d(1.6)}}
684.c-sleeping .body{animation:breathe 3s ease-in-out infinite ${d(3)}}
685@keyframes breathe{50%{transform:translateY(1px)}}
686.c-sleeping .z1{animation:zf 2.4s ease-in infinite ${d(2.4)}}.c-sleeping .z2{animation:zf 2.4s ease-in infinite ${d(2.4, 1.2)}}
687@keyframes zf{0%{opacity:0;transform:translate(0,3px)}30%{opacity:1}100%{opacity:0;transform:translate(2px,-5px)}}
688.c-done .cheer{animation:cheer .4s steps(1) infinite ${d(0.4)}}
689@keyframes cheer{50%{transform:translateY(-2px)}}
690</style>`
691}
692
hooks/classify.ts 129 lines
1import type { Outfit, Sess } from '../types'
2
3export const SLEEP_MS = 5 * 60_000
4export const DONE_MS = 9_000
5export const LONG_MS = 2 * 60_000
6export const LONG_TOOLS = 25
7
8// Tiny on-device classifier: keyword weights over the prompt plus the tools used.
9// No network, no model call, no tokens.
10const FIX = /\b(fix|fixes|fixed|bug|bugs|broken|error|errors|fail|fails|failed|failing|crash|crashes|debug|issue|wrong|repair|patch|regression|not working|isn'?t working|doesn'?t work)\b/i
11const CODE = /\b(build|implement|add|create|write|refactor|code|function|component|feature|script|develop|make|mod|plugin)\b/i
12const DOCS = /\b(readme|docs?|documentation|essay|email|notes?|summary|summarize|blog|post|article|letter|draft|report|proposal)\b/i
13const CODEY = /\b(function|component|script|feature|plugin|code|api|class|refactor|implement)\b/i
14const REVIEW = /\b(review|audit|verify|check|test|lint|inspect|critique)\b/i
15const PLAN = /\b(plan|design|architect|outline|strategy|roadmap|brainstorm)\b/i
16const READING = new Set(['Read', 'Grep', 'Glob', 'LS', 'WebFetch', 'WebSearch'])
17
18export const isLong = (s: Sess, at: number): boolean => at - s.startedAt > LONG_MS || s.tools > LONG_TOOLS
19
20export function pickOutfit(s: Sess, at: number): Outfit {
21  if (!s.busy) {
22    if (s.doneAt > 0 && at - s.doneAt < DONE_MS) return s.end === 'fail' ? 'error' : s.end === 'long' ? 'crown' : 'done'
23    return at - s.lastActive > SLEEP_MS ? 'sleeping' : 'idle'
24  }
25  if (isLong(s, at)) return 'chef'
26  if (FIX.test(s.prompt)) return 'fixing'
27  if (s.edits === 0 && READING.has(s.lastTool)) return 'reading'
28  if (s.edits === 0 && REVIEW.test(s.prompt)) return 'cool'
29  if (s.edits === 0 && PLAN.test(s.prompt)) return 'idle'
30  if (DOCS.test(s.prompt) && !CODEY.test(s.prompt)) return 'writing'
31  if (s.edits > 0 || CODE.test(s.prompt)) return 'coding'
32  return 'working'
33}
34
35// Is the cache-tax / keepwarm status line, drawn below the bar by another plugin, saying "cold"?
36// It renders ' cache-tax · ' followed by a bold state word, so read the element's text in order.
37// The prompt cache lives 60 minutes after the last request.
38export const CACHE_TTL_MS = 60 * 60 * 1000
39
40// The state word of cache-tax's band ('cold', 'warm', ...), or null when the band is not in the tree.
41export function cacheState(node: unknown): string | null {
42  const out: string[] = []
43  const walk = (n: unknown, depth: number): void => {
44    if (depth > 12 || n === null || n === undefined) return
45    if (typeof n === 'string') out.push(n)
46    else if (Array.isArray(n)) n.forEach(c => walk(c, depth + 1))
47    else if (typeof n === 'object') {
48      const o = n as { props?: { children?: unknown }; children?: unknown }
49      walk(o.props?.children ?? o.children, depth + 1)
50    }
51  }
52  walk(node, 0)
53  const i = out.findIndex(t => t.includes('cache-tax'))
54  return i >= 0 ? (out[i + 1] ?? null) : null
55}
56
57export function isCacheCold(node: unknown): boolean {
58  return cacheState(node) === 'cold'
59}
60
61// Cold as the band would say it, or, when the band is out of sight (hook order), by our own clock.
62export function isIcedNow(node: unknown, doneAt: number, at: number): boolean {
63  const st = cacheState(node)
64  return st !== null ? st === 'cold' : doneAt > 0 && at - doneAt >= CACHE_TTL_MS
65}
66
67// What he was doing when the turn ended, and the victory pose that goes with it.
68export function winPose(s: Sess, jumpBy: number, jumpedThisTurn: boolean): Outfit {
69  if (jumpedThisTurn && jumpBy >= 0.3) return 'exitCar'
70  if (jumpedThisTurn && jumpBy >= 0.12) return 'medal'
71  const task = pickOutfit({ ...s, busy: true }, s.doneAt || s.lastActive)
72  return task === 'coding' ? 'winCode' : task === 'fixing' ? 'winFix' : task === 'reading' ? 'winRead' : task === 'writing' ? 'winWrite' : 'done'
73}
74
75// Bar value for a turn with no explicit progress: creeps toward 95% with tools and time.
76export function autoRatio(s: Sess, at: number): number {
77  if (s.busy) return Math.min(0.95, 1 - Math.exp(-(s.tools * 0.12 + (at - s.startedAt) / 90_000)))
78  return s.doneAt > 0 && at - s.doneAt < DONE_MS ? 1 : 0
79}
80
81// What he does when nothing is happening: a loop of steps. A stroll walks to the given
82// spot (0..1 of the bar) slowly; every other step stands still where the last stroll ended.
83// Poses that travel along the bar (the others stand still).
84export const MOVING = new Set<Outfit>(['stroll', 'walkCool', 'walkWizard', 'walkChef', 'walkCode', 'walkCmd', 'fly'])
85export const POSE_MS = 8_000
86export const STROLL_MS = 18_000
87export const IDLE_SCRIPT: readonly [Outfit, number | null][] = [
88  ['stroll', 0.55],
89  ['idle', null],
90  ['walkCool', 0.15],
91  ['reading', null],
92  ['fly', 0.8],
93  ['greeting', null],
94  ['walkWizard', 0.35],
95  ['propeller', null],
96  ['walkCode', 0.65],
97  ['cool', null],
98  ['walkChef', 0.1],
99  ['dance', null],
100  ['walkCmd', 0.5],
101  ['sunbathe', null],
102]
103
104const LENGTH: number[] = IDLE_SCRIPT.map(([, target]) => (target === null ? POSE_MS : STROLL_MS))
105export const IDLE_LOOP_MS = LENGTH.reduce((a, b) => a + b, 0)
106
107// Where each step starts: the end of the nearest stroll before it (the script wraps).
108const START: number[] = IDLE_SCRIPT.map((_, k) => {
109  for (let j = 1; j <= IDLE_SCRIPT.length; j++) {
110    const x = IDLE_SCRIPT[(k - j + IDLE_SCRIPT.length * 2) % IDLE_SCRIPT.length]?.[1]
111    if (x !== null && x !== undefined) return x
112  }
113  return 0
114})
115
116// A costume change (pulling the next one from a pocket) takes this long.
117export const CHANGE_MS = 1_400
118
119export function roamAt(at: number): { pose: Outfit; x: number; age: number } {
120  let t = ((at % IDLE_LOOP_MS) + IDLE_LOOP_MS) % IDLE_LOOP_MS
121  let k = 0
122  while (k < LENGTH.length - 1 && t >= (LENGTH[k] ?? 0)) t -= LENGTH[k++] ?? 0
123  const [pose, target] = IDLE_SCRIPT[k] ?? ['idle', null]
124  const from = START[k] ?? 0
125  if (target === null) return { pose, x: from, age: t }
126  const f = t / (LENGTH[k] ?? STROLL_MS)
127  const eased = f < 0.5 ? 2 * f * f : 1 - 2 * (1 - f) * (1 - f)
128  return { pose, x: from + (target - from) * eased, age: t }
129}
hooks/agents.ts 272 lines
1import type { AgentRun, Character, Outfit } from '../types'
2import { ITEM_COLOR, ITEM_DEST, crabCss, crabSvg } from './crab'
3import { CHANGE_MS, pickOutfit } from './classify'
4
5// Subagent panel drawing, ported from savvy-progress (MIT, JohnnyVizz/claude-kit) into this
6// mod. Pure functions only: the hooks that feed it live in register.tsx.
7
8const FONT = "-apple-system,BlinkMacSystemFont,'SF Pro Text','Segoe UI',sans-serif"
9export const ROW_H = 66
10export const LINE_H = 24
11const GRID_STEP = 16
12const GRID_TILE = 4096
13const DOT = '·'
14const ELLIPSIS = '…'
15const APPROX = '≈'
16
17// --- model facts -----------------------------------------------------------
18
19type Price = [input: number, output: number, cacheRead: number, cacheWrite: number]
20
21// USD per million tokens; the first row that matches the model id wins. Cache writes are
22// priced at the 1-hour rate (2x input), which is what Claude Code writes on a subscription.
23const PRICES: [RegExp, Price][] = [
24  [/(fable|mythos)-5-1/, [10, 50, 0.25, 20]],
25  [/fable|mythos/, [10, 50, 1, 20]],
26  [/opus-5-5/, [4, 20, 0.2, 8]],
27  [/opus/, [5, 25, 0.5, 10]],
28  [/sonnet-5-5/, [2, 10, 0.1, 4]],
29  [/sonnet-5/, [2, 10, 0.2, 4]],
30  [/sonnet/, [3, 15, 0.3, 6]],
31  [/haiku-5-5/, [0.1, 0.5, 0.01, 0.2]],
32  [/haiku/, [1, 5, 0.1, 2]],
33]
34const HAIKU_LONG_PROMPT = 100_000
35const HAIKU_LONG: Price = [0.5, 2.5, 0.05, 1]
36
37export type Usage = {
38  input_tokens: number
39  output_tokens: number
40  cache_read_input_tokens: number
41  cache_creation_input_tokens: number
42}
43
44const priceOf = (model: string, prompt: number): Price => {
45  const id = model.toLowerCase()
46  if (prompt > HAIKU_LONG_PROMPT && /haiku-5-5/.test(id)) return HAIKU_LONG
47  return PRICES.find(([re]) => re.test(id))?.[1] ?? [4, 20, 0.2, 8]
48}
49
50// `isRequest` says the usage is one model request, which is what a prompt-length price is
51// set by; a turn's sum is priced at the model's ordinary prices.
52export const costOf = (model: string, u: Usage, isRequest: boolean): number => {
53  const prompt = isRequest ? (u.input_tokens || 0) + (u.cache_read_input_tokens || 0) + (u.cache_creation_input_tokens || 0) : 0
54  const [i, o, r, w] = priceOf(model, prompt)
55  return (
56    ((u.input_tokens || 0) * i + (u.output_tokens || 0) * o + (u.cache_read_input_tokens || 0) * r + (u.cache_creation_input_tokens || 0) * w) /
57    1e6
58  )
59}
60
61export const windowOf = (model: string): number => (/haiku-4|claude-3/i.test(model) ? 200_000 : 1_000_000)
62
63export const modelName = (id: string): string => {
64  const m = /(fable|mythos|opus|sonnet|haiku)-(\d+)(?:-(\d{1,2})(?!\d))?/i.exec(id)
65  const [, family = '', major = '', minor] = m ?? []
66  if (!family) return id.replace(/^claude-/, '').replace(/\[.*\]$/, '') || '—'
67  return `${family.charAt(0).toUpperCase()}${family.slice(1).toLowerCase()} ${major}${minor ? '.' + minor : ''}`
68}
69
70const FAMILY_COLOR: Record<string, string> = { fable: '#7F77DD', mythos: '#7F77DD', opus: '#D85A30', sonnet: '#378ADD', haiku: '#1D9E75' }
71const familyOf = (model: string): string => /(fable|mythos|opus|sonnet|haiku)/i.exec(model)?.[1]?.toLowerCase() ?? 'other'
72const colorOf = (a: AgentRun): string => FAMILY_COLOR[familyOf(a.model)] ?? '#888780'
73
74// What a running agent looks like follows the task: its brief and the tools it is using.
75export const taskOutfit = (a: AgentRun, at: number): Outfit =>
76  pickOutfit(
77    {
78      busy: true,
79      startedAt: a.startedAt,
80      lastActive: at,
81      doneAt: 0,
82      tools: a.tools ?? 0,
83      edits: a.edits ?? 0,
84      prompt: `${a.description} ${a.type}`,
85      lastTool: a.lastTool ?? (/explore/i.test(a.type) ? 'Read' : ''),
86      end: '',
87    },
88    at,
89  )
90
91const ACTIVITY: Partial<Record<Outfit, string>> = {
92  coding: 'coding',
93  fixing: 'fixing',
94  reading: 'reading',
95  writing: 'writing',
96  cool: 'reviewing',
97  idle: 'planning',
98  chef: 'cooking',
99  working: 'thinking',
100  error: 'error',
101}
102
103// What a finished agent does while it waits, a new pose every 8 s (offset per agent).
104const IDLE_POSES: Outfit[] = ['idle', 'greeting', 'cool', 'dance', 'propeller', 'sunbathe', 'reading']
105const hash = (s: string): number => [...s].reduce((h, ch) => (h * 31 + (ch.codePointAt(0) ?? 0)) % 100003, 7)
106
107// The costume a working agent wears follows its model; the character under it follows /mascot.
108// A finished agent cheers for a few seconds, then idles through a loop of small animations.
109export const outfitFor = (a: AgentRun, at: number): Outfit => {
110  if (a.status === 'done') {
111    if (at - (a.endedAt ?? at) < 6_000) return 'done'
112    return IDLE_POSES[(hash(a.id) + Math.floor(at / 8_000)) % IDLE_POSES.length] ?? 'idle'
113  }  if (a.status === 'failed') return 'error'
114  if (a.outfit) return a.outfit
115  return taskOutfit(a, at)
116}
117
118// --- formatting ------------------------------------------------------------
119
120const norm = (s: string): string => s.toLowerCase().replace(/[^\p{L}\p{N}]+/gu, ' ').trim()
121export const sameTask = (a: string, b: string): boolean => norm(a) === norm(b)
122
123const fmtTokens = (n: number): string => (n >= 1e6 ? `${(n / 1e6).toFixed(1)}M` : n >= 1e3 ? `${Math.round(n / 1e3)}k` : `${Math.round(n)}`)
124const fmtCost = (usd: number): string => `$${usd < 10 ? usd.toFixed(2) : usd.toFixed(1)}`
125export const fmtTime = (ms: number): string => {
126  const s = Math.max(0, Math.round(ms / 1000))
127  const h = Math.floor(s / 3600)
128  const m = Math.floor((s % 3600) / 60)
129  const ss = String(s % 60).padStart(2, '0')
130  return h ? `${h}:${String(m).padStart(2, '0')}:${ss}` : `${m}:${ss}`
131}
132
133const xml = (s: string): string => s.replace(/[&<>"]/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' })[c] ?? c)
134
135// CJK and fullwidth text advances about 1em; the rest is a rough proportional estimate.
136const charEm = (ch: string): number =>
137  (ch.codePointAt(0) ?? 0) >= 0x2e80 ? 1 : /[\s.,:;'|!il1()[\]]/.test(ch) ? 0.3 : /[A-Z@%mw]/.test(ch) ? 0.72 : 0.56
138const textWidth = (s: string, size: number): number => [...s].reduce((w, ch) => w + charEm(ch) * size, 0)
139const fitText = (s: string, size: number, maxW: number): string => {
140  if (textWidth(s, size) <= maxW) return s
141  let out = ''
142  for (const ch of s) {
143    if (textWidth(out + ch + ELLIPSIS, size) > maxW) break
144    out += ch
145  }
146  return out + ELLIPSIS
147}
148
149// --- numbers ---------------------------------------------------------------
150
151export const elapsed = (a: AgentRun, at: number): number => (a.endedAt ?? Math.max(at, a.startedAt)) - a.startedAt
152
153export const totals = (list: AgentRun[], at: number) => {
154  const cost = list.reduce((s, a) => s + a.costUsd, 0)
155  const tokens = list.reduce((s, a) => s + a.tokens, 0)
156  const start = Math.min(...list.map(a => a.startedAt))
157  const end = Math.max(...list.map(a => a.endedAt ?? Math.max(at, a.startedAt)))
158  return { cost, tokens, time: list.length ? end - start : 0 }
159}
160export type Totals = ReturnType<typeof totals>
161
162const progressOf = (a: AgentRun): number | null => {
163  if (a.status === 'done') return 1
164  if (a.stepTotal) return Math.min(1, (a.stepDone ?? 0) / a.stepTotal)
165  return null
166}
167const ctxOf = (a: AgentRun): number => (a.contextMax ? Math.min(100, Math.round((a.contextTokens / a.contextMax) * 100)) : 0)
168
169export const summaryOf = (t: Totals): string => `${APPROX}${fmtCost(t.cost)}, ${fmtTokens(t.tokens)} tokens, ${fmtTime(t.time)}`
170
171// --- drawings --------------------------------------------------------------
172
173const PANE_CSS = `<style>
174.t{fill:#1f1f1f}.s{fill:#6b6b68}.m{fill:#9a9a96}.k{fill:#ecebe8}.ln{stroke:#e4e4e1}.tile{fill:#f4f3f0}
175@media (prefers-color-scheme: dark){.t{fill:#ececec}.s{fill:#a8a8a4}.m{fill:#7d7d79}.k{fill:#2c2c2b}.ln{stroke:#333331}.tile{fill:#262625}}
176.live{animation:p 1.6s ease-in-out infinite}@keyframes p{50%{opacity:.3}}
177</style>`
178
179const svg = (W: number, H: number, at: number, body: string): string =>
180  `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}" viewBox="0 0 ${W} ${H}">${PANE_CSS}${crabCss(at)}${body}</svg>`
181
182// The pane's backdrop: a dotted grid as tall as the content laid over it.
183export const gridTiles = (H: number): number[] => Array.from({ length: Math.ceil(H / GRID_TILE) }, (_, i) => Math.min(GRID_TILE, H - i * GRID_TILE))
184export const gridSvg = (W: number, H: number): string => `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}" viewBox="0 0 ${W} ${H}">
185<style>.d{fill:#d9d8d4}@media (prefers-color-scheme: dark){.d{fill:#3a3a38}}</style>
186<defs><pattern id="g" width="${GRID_STEP}" height="${GRID_STEP}" patternUnits="userSpaceOnUse"><circle class="d" cx="${GRID_STEP / 2}" cy="${GRID_STEP / 2}" r="1"/></pattern></defs>
187<rect width="${W}" height="${H}" fill="url(#g)"/>
188</svg>`
189
190export const HEADER_H = 44
191
192export const headerSvg = (W: number, at: number, t: Totals): string => {
193  const gap = 6
194  const tw = (W - gap * 2) / 3
195  const tile = (i: number, k: string, v: string): string =>
196    `<rect class="tile" x="${i * (tw + gap)}" y="0" width="${tw}" height="40" rx="8"/>
197<text class="s" x="${i * (tw + gap) + 9}" y="16" font-family="${FONT}" font-size="11">${k}</text>
198<text class="t" x="${i * (tw + gap) + 9}" y="33" font-family="${FONT}" font-size="15" font-weight="600" font-variant-numeric="tabular-nums">${v}</text>`
199  return svg(W, HEADER_H, at, `${tile(0, 'Cost', APPROX + fmtCost(t.cost))}${tile(1, 'Tokens', fmtTokens(t.tokens))}${tile(2, 'Time', fmtTime(t.time))}`)
200}
201
202export const agentSvg = (W: number, a: AgentRun, at: number, character: Character): string => {
203  const color = colorOf(a)
204  const ctx = ctxOf(a)
205  const textW = W - 42 - 22
206  const meta = [a.effort ? `${modelName(a.model)} ${DOT} ${a.effort}` : modelName(a.model)]
207  if (a.round > 1) meta.push(`round ${a.round}`)
208  if (a.status === 'failed') meta.push('error')
209  const doing = a.status === 'running' ? ACTIVITY[outfitFor(a, at)] : undefined
210  if (doing) meta.push(doing)
211  const progress = progressOf(a)
212  const o = outfitFor(a, at)
213  const item = ITEM_COLOR[o]
214  const age = a.outfitAt === undefined ? Infinity : at - a.outfitAt
215  const isChanging = a.status === 'running' && item !== undefined && age < CHANGE_MS
216  const shown: Outfit = isChanging ? 'changing' : o
217  const change = isChanging ? { item, age, dest: ITEM_DEST[o] } : undefined
218  const settle = a.status === 'running' && !isChanging ? age - (item === undefined ? 0 : CHANGE_MS) : undefined
219  const stats = `ctx ${ctx}% ${DOT} ${fmtTokens(a.contextTokens)}  ${APPROX}${fmtCost(a.costUsd)}  ${fmtTime(elapsed(a, at))}`
220  const steps = a.stepTotal ? `${a.stepDone ?? 0}/${a.stepTotal}${a.stepNote ? ` ${DOT} ${a.stepNote}` : ''}` : ''
221  const stepsW = Math.max(0, textW - textWidth(stats, 11) - 12)
222  const fillW = Math.round(textW * (progress ?? ctx / 100))
223  const mark =
224    a.status === 'running'
225      ? `<circle class="live" cx="${W - 8}" cy="16" r="3.5" fill="${color}"/>`
226      : a.status === 'done'
227        ? `<path d="M${W - 13} 16l3.5 3.5 6.5-7" fill="none" stroke="#3B9C5F" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>`
228        : `<path d="M${W - 12} 12l8 8M${W - 4} 12l-8 8" stroke="#D0453F" stroke-width="1.8" stroke-linecap="round"/>`
229  return svg(
230    W,
231    ROW_H,
232    at,
233    `${crabSvg(shown, 0, 22, a.status === 'running', character, change, settle)}
234<text class="t" x="42" y="18" font-family="${FONT}" font-size="13" font-weight="600">${xml(fitText(a.description || a.type, 13, textW))}</text>
235<text x="42" y="34" font-family="${FONT}" font-size="11"><tspan fill="${color}">${xml(a.type)}</tspan><tspan class="s">  ${xml(meta.join(`  ${DOT}  `))}</tspan></text>
236${steps && stepsW > 30 ? `<text class="t" x="42" y="49" font-family="${FONT}" font-size="11" font-variant-numeric="tabular-nums">${xml(fitText(steps, 11, stepsW))}</text>` : ''}
237<text class="s" x="${42 + textW}" y="49" text-anchor="end" font-family="${FONT}" font-size="11" font-variant-numeric="tabular-nums">${stats}</text>
238<rect class="k" x="42" y="55" width="${textW}" height="4" rx="2"/><rect${progress === null ? ' class="m"' : ''} x="42" y="55" width="${fillW}" height="4" rx="2"${progress === null ? '' : ` fill="${color}"`}/>
239${mark}
240<line class="ln" x1="0" y1="65.5" x2="${W}" y2="65.5"/>`,
241  )
242}
243
244export const compactSvg = (W: number, list: AgentRun[], at: number, t: Totals, character: Character): string => {
245  const ordered = [...list.filter(a => a.status === 'running'), ...list.filter(a => a.status !== 'running')]
246  const fit = Math.max(1, Math.floor((W - 150) / 40))
247  const shown = ordered.slice(0, fit)
248  const more = ordered.length - shown.length
249  const body = shown.map((a, i) => crabSvg(outfitFor(a, at), i * 40, 6, a.status === 'running', character)).join('')
250  return svg(
251    W,
252    40,
253    at,
254    `${body}${more ? `<text class="s" x="${shown.length * 40 + 4}" y="26" font-family="${FONT}" font-size="12">+${more}</text>` : ''}
255<text class="s" x="${W}" y="26" text-anchor="end" font-family="${FONT}" font-size="12" font-variant-numeric="tabular-nums">${APPROX}${fmtCost(t.cost)} ${DOT} ${fmtTokens(t.tokens)} ${DOT} ${fmtTime(t.time)}</text>`,
256  )
257}
258
259// --- terminal rows ---------------------------------------------------------
260
261export const ctxBar = (pct: number, width: number): string => {
262  const filled = Math.round((width * pct) / 100)
263  return '█'.repeat(filled) + '░'.repeat(Math.max(0, width - filled))
264}
265export const agentLine = (a: AgentRun, at: number): { title: string; meta: string; stats: string; progress: number | null; ctx: number } => ({
266  title: a.description || a.type,
267  meta: `${a.type} ${DOT} ${a.effort ? `${modelName(a.model)} ${DOT} ${a.effort}` : modelName(a.model)}`,
268  stats: `${a.stepTotal ? `${a.stepDone ?? 0}/${a.stepTotal} ${DOT} ` : ''}ctx ${ctxOf(a)}% ${DOT} ${fmtTokens(a.contextTokens)} ${APPROX}${fmtCost(a.costUsd)} ${fmtTime(elapsed(a, at))}`,
269  progress: progressOf(a),
270  ctx: ctxOf(a),
271})
272
hooks/ft-open.ts 68 lines
1export const LINUX_LAUNCH = 'setsid -f -w "$@" </dev/null >/dev/null 2>&1 & p=$!; sleep 1; kill -0 "$p" 2>/dev/null && exit 0; wait "$p"'
2
3export const LINUX_OPEN = [
4  'set -f',
5  'f=$1',
6  'if command -v gio >/dev/null; then',
7  '  if [ -f "$f" ] && command -v file >/dev/null && encoding=$(file -b --mime-encoding -- "$f") && [ -n "$encoding" ] && [ "$encoding" != binary ]; then',
8  '    case $(gio info -a standard::content-type -- "$f" | sed -n \'s/.*standard::content-type: //p\') in',
9  '      video/* | audio/*)',
10  '        app=$(xdg-mime query default text/plain)',
11  '        IFS=:',
12  '        for dir in "${XDG_DATA_HOME:-$HOME/.local/share}" ${XDG_DATA_DIRS:-/usr/local/share:/usr/share}; do',
13  '          [ -n "$app" ] && [ -f "$dir/applications/$app" ] && exec gio launch "$dir/applications/$app" "$f"',
14  '        done',
15  '        echo "No text/plain desktop application found for $f" >&2',
16  '        exit 1',
17  '        ;;',
18  '    esac',
19  '  fi',
20  '  if (exec gio open "$f"); then exit 0; fi',
21  'fi',
22  'exec xdg-open "$f"',
23].join('\n')
24
25const WIN_OPEN = "$ErrorActionPreference = 'Stop'; [Diagnostics.Process]::Start([Diagnostics.ProcessStartInfo]@{ FileName = $env:PANE_OPEN_TARGET; UseShellExecute = $true }) | Out-Null"
26
27export function openCommand(os: 'linux' | 'darwin' | 'win32', target: string) {
28  const argv = os === 'linux'
29    ? ['sh', '-c', LINUX_LAUNCH, 'sh', 'sh', '-c', LINUX_OPEN, 'sh', target]
30    : os === 'darwin'
31      ? ['open', '--', target]
32      : ['powershell', '-NoProfile', '-NonInteractive', '-Command', WIN_OPEN]
33  return {
34    argv,
35    init: {
36      timeoutMs: 10_000,
37      ...(os === 'win32' ? { env: { PANE_OPEN_TARGET: /^[a-z][a-z0-9+.-]*:\/\//i.test(target) ? target : target.replace(/\//g, '\\') } } : {}),
38    },
39  }
40}
41
42function words(command: string): string[] {
43  const out: string[] = []
44  let word: string | null = null
45  let quote = ''
46  for (const ch of command) {
47    if (quote) {
48      if (ch === quote) quote = ''
49      else word = (word ?? '') + ch
50    } else if (ch === '"' || ch === "'") {
51      quote = ch
52      word ??= ''
53    } else if (/\s/.test(ch)) {
54      if (word !== null) out.push(word)
55      word = null
56    } else word = (word ?? '') + ch
57  }
58  if (word !== null) out.push(word)
59  return out
60}
61
62export function openWithCommand(command: string, path: string, dir: string): string[] | null {
63  const argv = words(command)
64  if (!argv.length) return null
65  const filled = argv.map(w => w.replace(/\{(path|dir)\}/g, (_, key) => (key === 'path' ? path : dir)))
66  return argv.some(w => w.includes('{path}')) ? filled : [...filled, path]
67}
68
hooks/ft-button.tsx 19 lines
1import type { ClientModule } from 'claude-code'
2
3export type ButtonProps = { id: string; label: string; dim: boolean }
4
5// A header button that also tells the hooks module when the pointer is over it, which a Button
6// alone doesn't, so the header can say what the button does.
7const HeaderButton: ClientModule<ButtonProps, { over: boolean }> = (props, surface) => {
8  const { Button } = surface.elements
9  surface.onPointer(e => {
10    const over = e.type !== 'leave'
11    if (over === Boolean(surface.state?.over)) return
12    surface.setState({ over })
13    surface.post({ over })
14  })
15  return <Button key={props.id} plain dimColor={props.dim} label={props.label} onPress={() => surface.post({ press: true })} />
16}
17
18export default HeaderButton
19
hooks/ft-git.ts 262 lines
1import { drivesOn, isAbsolute, posix } from './ft-tree'
2
3export type GitAction = {
4  kind: string
5  verb: string
6  running: string
7  done: string
8  tone: string
9  icon: { nerd: string; plain: string }
10  init?: boolean
11}
12
13const ICON = {
14  commit: { nerd: '\u{f417}', plain: '●' },
15  push: { nerd: '\u{f0552}', plain: '↑' },
16  pull: { nerd: '\u{f0553}', plain: '↓' },
17  branch: { nerd: '\u{f418}', plain: '⑂' },
18  merge: { nerd: '\u{f419}', plain: '⑃' },
19  pr: { nerd: '\u{f407}', plain: '⇄' },
20  github: { nerd: '\u{f408}', plain: '◎' },
21  stash: { nerd: '\u{f01bc}', plain: '≡' },
22  undo: { nerd: '\u{f054c}', plain: '↶' },
23  tag: { nerd: '\u{f412}', plain: '⌖' },
24  git: { nerd: '\u{e702}', plain: '±' },
25}
26
27export const TONES: Record<string, { bright: string[]; dim: string[]; solid: string }> = {
28  orange: { bright: ['#D97757', '#E48B6D', '#EDA487', '#FAD9C9'], dim: ['#7d3f2a', '#92503a', '#aa6a52', '#bf8872'], solid: '#E48B6D' },
29  green: { bright: ['#22c55e', '#4ade80', '#86efac', '#dcfce7'], dim: ['#14532d', '#166534', '#2f7a47', '#4f9a66'], solid: '#4ade80' },
30  teal: { bright: ['#14b8a6', '#2dd4bf', '#5eead4', '#ccfbf1'], dim: ['#0f5e57', '#16786f', '#2a9488', '#4fb3a8'], solid: '#2dd4bf' },
31  blue: { bright: ['#3b82f6', '#60a5fa', '#93c5fd', '#dbeafe'], dim: ['#1e3a8a', '#1d4ed8', '#3b6fd1', '#6b93dc'], solid: '#60a5fa' },
32  purple: { bright: ['#a855f7', '#c084fc', '#d8b4fe', '#f3e8ff'], dim: ['#581c87', '#6b21a8', '#8b47c4', '#a874d6'], solid: '#c084fc' },
33  cyan: { bright: ['#06b6d4', '#22d3ee', '#67e8f9', '#cffafe'], dim: ['#155e75', '#0e7490', '#2b8ea3', '#5aa9b8'], solid: '#22d3ee' },
34  red: { bright: ['#ef4444', '#f87171', '#fca5a5', '#fee2e2'], dim: ['#7f1d1d', '#991b1b', '#b54040', '#c96a6a'], solid: '#f87171' },
35}
36
37const GIT_VERBS: Record<string, Omit<GitAction, 'kind'>> = {
38  commit: { verb: 'commit', running: 'committing', done: 'committed', tone: 'green', icon: ICON.commit },
39  push: { verb: 'push', running: 'pushing', done: 'pushed', tone: 'teal', icon: ICON.push },
40  pull: { verb: 'pull', running: 'pulling', done: 'pulled', tone: 'blue', icon: ICON.pull },
41  fetch: { verb: 'fetch', running: 'fetching', done: 'fetched', tone: 'blue', icon: ICON.pull },
42  checkout: { verb: 'checkout', running: 'checking out', done: 'checked out', tone: 'blue', icon: ICON.branch },
43  switch: { verb: 'switch', running: 'switching', done: 'switched', tone: 'blue', icon: ICON.branch },
44  branch: { verb: 'branch', running: 'branching', done: 'branched', tone: 'blue', icon: ICON.branch },
45  merge: { verb: 'merge', running: 'merging', done: 'merged', tone: 'purple', icon: ICON.merge },
46  rebase: { verb: 'rebase', running: 'rebasing', done: 'rebased', tone: 'purple', icon: ICON.merge },
47  'cherry-pick': { verb: 'cherry-pick', running: 'cherry-picking', done: 'cherry-picked', tone: 'purple', icon: ICON.merge },
48  stash: { verb: 'stash', running: 'stashing', done: 'stashed', tone: 'orange', icon: ICON.stash },
49  reset: { verb: 'reset', running: 'resetting', done: 'reset', tone: 'red', icon: ICON.undo },
50  restore: { verb: 'restore', running: 'restoring', done: 'restored', tone: 'red', icon: ICON.undo },
51  revert: { verb: 'revert', running: 'reverting', done: 'reverted', tone: 'red', icon: ICON.undo },
52  add: { verb: 'add', running: 'staging', done: 'staged', tone: 'green', icon: ICON.git },
53  rm: { verb: 'rm', running: 'removing', done: 'removed', tone: 'red', icon: ICON.git },
54  mv: { verb: 'mv', running: 'moving', done: 'moved', tone: 'orange', icon: ICON.git },
55  tag: { verb: 'tag', running: 'tagging', done: 'tagged', tone: 'purple', icon: ICON.tag },
56  init: { verb: 'init', running: 'initialising', done: 'initialised', tone: 'green', icon: ICON.git, init: true },
57  clone: { verb: 'clone', running: 'cloning', done: 'cloned', tone: 'blue', icon: ICON.pull, init: true },
58}
59
60const GH_VERBS: Record<string, Omit<GitAction, 'kind'>> = {
61  'pr create': { verb: 'pr create', running: 'opening PR', done: 'opened PR', tone: 'purple', icon: ICON.pr },
62  'pr merge': { verb: 'pr merge', running: 'merging PR', done: 'merged PR', tone: 'purple', icon: ICON.merge },
63  'pr checkout': { verb: 'pr checkout', running: 'checking out PR', done: 'checked out PR', tone: 'blue', icon: ICON.pr },
64  'pr comment': { verb: 'pr comment', running: 'commenting on PR', done: 'commented on PR', tone: 'purple', icon: ICON.pr },
65  'pr review': { verb: 'pr review', running: 'reviewing PR', done: 'reviewed PR', tone: 'purple', icon: ICON.pr },
66  'repo clone': { verb: 'repo clone', running: 'cloning', done: 'cloned', tone: 'blue', icon: ICON.github, init: true },
67  'release create': { verb: 'release create', running: 'releasing', done: 'released', tone: 'purple', icon: ICON.tag },
68  'issue create': { verb: 'issue create', running: 'opening issue', done: 'opened issue', tone: 'purple', icon: ICON.github },
69}
70
71const READ_ONLY_GIT = new Set(['status', 'log', 'diff', 'show', 'blame', 'rev-parse', 'ls-files', 'grep', 'describe', 'config', 'remote', 'reflog', 'shortlog', 'help', 'version', 'check-ignore'])
72
73function segments(command: string, seps: string[] = []): string[][] {
74  const out: string[][] = []
75  let cur: string[] = []
76  let tok = ''
77  let quote = ''
78  let has = false
79  const endTok = () => {
80    if (has) cur.push(tok)
81    tok = ''
82    has = false
83  }
84  const endSeg = () => {
85    endTok()
86    if (cur.length) out.push(cur)
87    cur = []
88  }
89  for (let i = 0; i < command.length; i++) {
90    const ch = command[i] ?? ''
91    if (quote) {
92      if (ch === quote) quote = ''
93      else if (ch === '\\' && quote === '"' && i + 1 < command.length) tok += command[++i]
94      else tok += ch
95    } else if (ch === '"' || ch === "'") {
96      quote = ch
97      has = true
98    } else if (ch === '\\' && i + 1 < command.length) {
99      tok += command[++i]
100      has = true
101    } else if (ch === '#' && !has) {
102      while (i + 1 < command.length && command[i + 1] !== '\n') i++
103    } else if (ch === ' ' || ch === '\t') endTok()
104    else if (ch === '\n' || ch === ';') {
105      seps.push(';')
106      endSeg()
107    } else if (ch === '&' && (command[i - 1] === '>' || command[i + 1] === '>')) {
108      tok += ch
109      has = true
110    } else if (ch === '&' || ch === '|') {
111      const twice = command[i + 1] === ch
112      if (twice) i++
113      seps.push(twice ? ch + ch : ch)
114      endSeg()
115    } else {
116      tok += ch
117      has = true
118    }
119  }
120  endSeg()
121  if (!quote) return out
122  seps.push(';')
123  return command
124    .split(/&&|\|\||;|\||\n/)
125    .map(s => s.trim().split(/\s+/).filter(Boolean))
126    .filter(s => s.length > 0)
127}
128
129function stripGlobals(tokens: string[]): string[] {
130  const out = [...tokens]
131  if (out[0] === 'env') out.shift()
132  while (out.length && /^[A-Z_][A-Z0-9_]*=/.test(out[0] ?? '')) out.shift()
133  return out
134}
135
136function gitVerb(tokens: string[]): number {
137  let i = 1
138  while (i < tokens.length && (tokens[i] ?? '').startsWith('-')) {
139    if (tokens[i] === '-C' || tokens[i] === '-c') i++
140    i++
141  }
142  return i
143}
144
145function gitReadOnly(verb: string, rest: string[]): boolean {
146  if (!verb || READ_ONLY_GIT.has(verb)) return true
147  if (verb === 'stash') return ['list', 'show'].includes(rest[0] ?? '')
148  if (verb === 'branch') return rest.every(t => t.startsWith('-'))
149  if (verb === 'tag') return rest.length === 0 || rest.some(t => /^(-l|--list|-n\d*|--contains|--points-at|--merged|--no-merged|-v|--verify)$/.test(t))
150  return false
151}
152
153export function chainOf(command: string): { size: number; and: boolean } {
154  const seps: string[] = []
155  const size = segments(command, seps).length
156  return { size, and: seps.every(s => s === '&&') }
157}
158
159export function gitActions(command: string): GitAction[] {
160  const out: GitAction[] = []
161  for (const raw of segments(command)) {
162    const tokens = stripGlobals(raw)
163    const head = tokens[0]?.split('/').pop()
164    if (head === 'git') {
165      const i = gitVerb(tokens)
166      const verb = tokens[i] ?? ''
167      const rest = tokens.slice(i + 1)
168      if (gitReadOnly(verb, rest)) continue
169      const spec = verb === 'checkout' && rest.includes('--') ? GIT_VERBS.restore : GIT_VERBS[verb]
170      if (spec) out.push({ kind: `git ${verb}`, ...spec })
171    } else if (head === 'gh') {
172      const pair = `${tokens[1] ?? ''} ${tokens[2] ?? ''}`
173      const spec = GH_VERBS[pair]
174      if (spec) out.push({ kind: `gh ${pair}`, ...spec })
175    }
176  }
177  return out
178}
179
180export const BRANCH_ICON = ICON.branch
181
182const READERS = new Set(['rg', 'grep', 'egrep', 'fgrep', 'find', 'fd', 'fdfind', 'cat', 'head', 'tail', 'bat', 'less', 'more', 'ls', 'eza', 'tree', 'wc', 'sed', 'awk', 'jq'])
183const PATTERN_FIRST = new Set(['rg', 'grep', 'egrep', 'fgrep', 'sed', 'awk', 'jq'])
184const OUTPUT_PATHS = new Set(['rg', 'grep', 'egrep', 'fgrep', 'find', 'fd', 'fdfind'])
185
186function tidy(path: string): string {
187  const s = path.replace(/\/+$/, '')
188  return s === '' ? '/' : /^[A-Za-z]:$/.test(s) && isAbsolute(`${s}/`) ? `${s}/` : s
189}
190
191export function resolve(cwd: string, p: string, home = ''): string {
192  let path = posix(p)
193  if (home && (path === '~' || path.startsWith('~/'))) path = home + path.slice(1)
194  const drive = isAbsolute(path) && !path.startsWith('/') ? path.slice(0, 2) : ''
195  const parts = isAbsolute(path) ? [drive] : posix(cwd).replace(/\/+$/, '').split('/')
196  for (const seg of path.slice(drive.length).split('/')) {
197    if (!seg || seg === '.') continue
198    if (seg === '..') {
199      if (parts.length > 1) parts.pop()
200    } else parts.push(seg)
201  }
202  return tidy(parts.join('/'))
203}
204
205const WRITE_FLAGS = /^-(delete|exec|execdir|ok|okdir|fprint\w*|fls|x|X|-exec|-exec-batch)$/
206
207export function readOnly(command: string): boolean {
208  const bare = ` ${command}`.replace(/\d*>&\d/g, ' ').replace(/(\d*|&)>>?\s*\/dev\/null/g, ' ')
209  if (/[^>]>[^>&]|>>/.test(bare)) return false
210  return segments(command).every(raw => {
211    const tokens = stripGlobals(raw)
212    const head = tokens[0]?.split('/').pop() ?? ''
213    if (head === 'cd' || head === 'echo' || head === 'printf' || head === 'true' || head === 'pwd') return true
214    if (head === 'git') {
215      const i = gitVerb(tokens)
216      return gitReadOnly(tokens[i] ?? '', tokens.slice(i + 1))
217    }
218    if (head === 'sed') return !tokens.some(t => /^-i/.test(t))
219    if (head === 'find' || head === 'fd' || head === 'fdfind') return !tokens.some(t => WRITE_FLAGS.test(t))
220    return READERS.has(head)
221  })
222}
223
224export function readTargets(command: string, sessionCwd: string, stdout: string, home = ''): string[] {
225  let cwd = sessionCwd
226  const out = new Set<string>()
227  let listsPaths = false
228  for (const raw of segments(command)) {
229    const tokens = stripGlobals(raw).map(t => posix(t.replace(/^["']|["']$/g, '')))
230    const head = tokens[0]?.split('/').pop() ?? ''
231    if (head === 'cd' && tokens[1]) {
232      cwd = resolve(cwd, tokens[1], home)
233      continue
234    }
235    if (!READERS.has(head)) continue
236    if (head === 'sed' && tokens.some(t => /^-i/.test(t))) continue
237    if (OUTPUT_PATHS.has(head)) listsPaths = true
238    const args = tokens.slice(1)
239    let skipPattern = PATTERN_FIRST.has(head) && !args.some(t => t === '-e' || t === '-f' || t === '--files')
240    for (let i = 0; i < args.length; i++) {
241      const a = args[i] ?? ''
242      if (a.startsWith('-')) {
243        if (/^-(e|f|g|t|T|m|A|B|C|-glob|-type|-max-count|name|iname|maxdepth|mindepth)$/.test(a)) i++
244        continue
245      }
246      if (skipPattern) {
247        skipPattern = false
248        continue
249      }
250      if (/[*?<>|]/.test(a)) continue
251      out.add(resolve(cwd, a, home))
252    }
253  }
254  if (listsPaths) {
255    for (const line of stdout.split('\n').slice(0, 400)) {
256      const path = posix(line).match(drivesOn() ? /^((?:[A-Za-z]:)?[^:\0]+?)(?::\d+[:-]|$|:)/ : /^([^:\0]+?)(?::\d+[:-]|$|:)/)?.[1]?.trim()
257      if (path && !path.includes(' ')) out.add(resolve(cwd, path, home))
258    }
259  }
260  return [...out].slice(0, 60)
261}
262
hooks/ft-rows.tsx 361 lines
1import type { ClientModule, RenderElement } from 'claude-code'
2
3export type Seg = { t: string; c?: string; b?: boolean; s?: boolean; i?: boolean; sh?: string; dim?: boolean; one?: boolean; spin?: boolean; bg?: string; tab?: string }
4export type RowSpec = { id: string; left: Seg[]; right: Seg[]; kind?: 'dir' | 'file' | 'link' }
5
6// The crab digs in three of the real rows: it dives into rows[0], throws things out until it
7// finds one, jumps into rows[1] and pokes its head out of rows[2]. Each of those rows gets a
8// burrow strip under it, drawn here, in the same grid as the rows.
9export type DigSpec = {
10  rows: [number, number, number]
11  color: string
12  dark: string
13  stage: string
14  glow: string
15  // Ticks into the loop to start from (a frame of the animation, for tests and screenshots).
16  start?: number
17}
18
19export type RowsProps = {
20  rows: RowSpec[]
21  active: string
22  activeBg: string
23  hoverBg: string
24  tones: Record<string, { bright: string[]; dim: string[] }>
25  spinner?: string[]
26  pointer?: boolean
27  bar?: { pos: number; size: number; thumb: string; track: string }
28  preview?: { text: string; path: string; startLine: number; markdown: boolean }
29  // Rows two cells high, so the tree has room to breathe (and for the crab to work in).
30  tall?: boolean
31  dig?: DigSpec
32}
33type Local = { hover: number; phase: number; drag: boolean; ref: { stop?: () => void; unpoint?: () => void } }
34
35const TICK_MS = 90
36const TREE_BUDGET = 70_000
37const MAX_ROWS = 1_500
38const FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
39
40// --- the crab, drawn from half blocks: a cell is one pixel wide and two pixel rows tall ---
41const STAGE_LINES = 4 // three lines of sprite space and the ground
42const PIX_ROWS = 6
43const LOOP = 112 // ticks (about 10 s)
44const BODY = ['..cccc..', '.ceccec.', 'cccccccc', '.cccccc.']
45const LOOK_L = '.eccecc.'
46const LOOK_R = '.ccecce.'
47const LEGS: [string, string][] = [
48  ['.l.ll.l.', '.l.ll.l.'],
49  ['l.l..l.l', 'l.l..l.l'],
50]
51const CRAB_W = 8
52const ITEMS = ['#EEEDE8', '#8E929A', '#D9A23A', '#7FC4F2', '#C0705A', '#4FA37A', '#A992E0', '#F5C542']
53const GEM = '#F5C542'
54const FLOOR = '#3a3a38'
55
56type Canvas = (string | undefined)[][]
57
58const blank = (w: number): Canvas => Array.from({ length: PIX_ROWS }, () => Array<string | undefined>(w).fill(undefined))
59
60function put(c: Canvas, x: number, y: number, color: string): void {
61  const row = c[y]
62  if (row && x >= 0 && x < row.length) row[x] = color
63}
64
65function crab(c: Canvas, x: number, y: number, frame: number, color: string, dark: string, look: -1 | 0 | 1): void {
66  const rows = [...BODY]
67  if (look < 0) rows[1] = LOOK_L
68  if (look > 0) rows[1] = LOOK_R
69  const [legA, legB] = LEGS[frame % 2] ?? LEGS[0] ?? ['', '']
70  const all = [...rows, legA, legB]
71  all.forEach((line, r) => {
72    for (let k = 0; k < CRAB_W; k++) {
73      const ch = line[k]
74      if (ch === 'c') put(c, x + k, y + r, color)
75      else if (ch === 'e') put(c, x + k, y + r, dark)
76      else if (ch === 'l') put(c, x + k, y + r, color)
77    }
78  })
79}
80
81type StageScene = { crab?: { x: number; y: number; frame: number; look: -1 | 0 | 1; gem: boolean } }
82
83const ease = (t: number, a: number, b: number, from: number, to: number): number => from + (to - from) * Math.min(1, Math.max(0, (t - a) / (b - a)))
84
85// Where the crab is, per burrow, for a tick of the loop.
86function script(t: number, w: number): [StageScene, StageScene, StageScene] {
87  const xA = 3
88  const xB = Math.max(xA + CRAB_W + 2, Math.floor(w / 2) - 4)
89  const xC = Math.max(xB + CRAB_W + 2, w - CRAB_W - 4)
90  const a: StageScene = {}
91  const b: StageScene = {}
92  const c: StageScene = {}
93  const frame = Math.floor(t / 2)
94  if (t < 11) a.crab = { x: Math.round(ease(t, 0, 11, -CRAB_W, xA)), y: 0, frame, look: 1, gem: false }
95  else if (t < 16) a.crab = { x: xA, y: Math.round(ease(t, 11, 16, 0, PIX_ROWS)), frame: 0, look: 0, gem: false }
96  else if (t < 66) a.crab = undefined
97  else if (t < 73) a.crab = { x: xA, y: Math.round(ease(t, 66, 73, PIX_ROWS, 0)), frame: 0, look: 0, gem: t > 70 }
98  else if (t < 80) a.crab = { x: xA, y: 0, frame: 0, look: 0, gem: true }
99  else if (t < 85) a.crab = { x: xA, y: Math.round(ease(t, 80, 85, 0, -PIX_ROWS - 2)), frame: 0, look: 0, gem: true }
100  if (t >= 84 && t < 90) b.crab = { x: xB, y: Math.round(ease(t, 84, 90, -PIX_ROWS - 2, 0)), frame: 0, look: 0, gem: true }
101  else if (t >= 90 && t < 95) b.crab = { x: xB, y: Math.round(ease(t, 90, 95, 0, PIX_ROWS)), frame: 0, look: 0, gem: false }
102  if (t >= 100 && t < 103) c.crab = { x: xC, y: Math.round(ease(t, 100, 103, PIX_ROWS, 4)), frame: 0, look: 0, gem: false }
103  else if (t >= 103 && t < 109) c.crab = { x: xC, y: 4, frame: 0, look: t % 12 < 6 ? -1 : 1, gem: false }
104  else if (t >= 109) c.crab = { x: xC, y: Math.round(ease(t, 109, 112, 4, PIX_ROWS)), frame: 0, look: 0, gem: false }
105  return [a, b, c]
106}
107
108const inside = (t: number, k: number): boolean => (k === 0 ? t >= 11 && t < 80 : k === 1 ? t >= 84 && t < 100 : t >= 100)
109
110type Cell = { ch: string; fg?: string; bg?: string }
111
112function composeStage(scene: StageScene, k: number, t: number, w: number, spec: DigSpec): RenderElement[] {
113  const canvas = blank(w)
114  const xs = [3, 0, 0]
115  const sc = scene.crab
116  const holeX = (sc?.x ?? xs[k] ?? 3) + CRAB_W / 2
117  if (k === 0) {
118    ITEMS.forEach((color, i) => {
119      const dt = t - (18 + i * 6)
120      if (dt < 0 || dt >= 15) return
121      const vy = 1.1 + (i % 3) * 0.15
122      const vx = (((i * 37) % 7) - 3) * 0.45
123      put(canvas, Math.round(3 + CRAB_W / 2 + vx * dt), Math.round(PIX_ROWS - 1 - (vy * dt - 0.08 * dt * dt)), color)
124    })
125  }
126  if (sc) {
127    crab(canvas, sc.x, sc.y, sc.frame, spec.color, spec.dark, sc.look)
128    if (sc.gem) put(canvas, sc.x + CRAB_W, Math.max(0, sc.y - 1), GEM)
129  }
130  const lines: RenderElement[] = []
131  for (let l = 0; l < 3; l++) {
132    const cells: Cell[] = []
133    for (let x = 0; x < w; x++) {
134      const top = canvas[2 * l]?.[x]
135      const bot = canvas[2 * l + 1]?.[x]
136      if (top && bot) cells.push(top === bot ? { ch: '█', fg: top } : { ch: '▀', fg: top, bg: bot })
137      else if (top) cells.push({ ch: '▀', fg: top, bg: spec.stage })
138      else if (bot) cells.push({ ch: '▄', fg: bot, bg: spec.stage })
139      else cells.push({ ch: ' ', bg: spec.stage })
140    }
141    lines.push(runs(cells))
142  }
143  // The ground, with the hole he goes into.
144  const floor: Cell[] = []
145  const hx = Math.round(holeX)
146  for (let x = 0; x < w; x++) {
147    const d = Math.abs(x - hx)
148    floor.push(d <= 4 ? { ch: d >= 4 ? '▂' : '▄', fg: FLOOR, bg: spec.stage } : { ch: ' ', bg: spec.stage })
149  }
150  lines.push(runs(floor))
151  return lines
152}
153
154function runs(cells: Cell[]): RenderElement {
155  const out: RenderElement[] = []
156  let cur: { ch: string; fg?: string; bg?: string } | null = null
157  let text = ''
158  const flush = () => {
159    if (cur) out.push(<Text color={cur.fg} backgroundColor={cur.bg}>{text}</Text>)
160  }
161  for (const c of cells) {
162    if (cur && c.fg === cur.fg && c.bg === cur.bg) {
163      text += c.ch
164    } else {
165      flush()
166      cur = c
167      text = c.ch
168    }
169  }
170  flush()
171  return <Box flexDirection="row" height={1}>{out}</Box>
172}
173
174// `Box`/`Text` come from the surface; set once per render so the helpers above can use them.
175// eslint-disable-next-line @typescript-eslint/no-explicit-any
176let Box: any
177// eslint-disable-next-line @typescript-eslint/no-explicit-any
178let Text: any
179
180function shimmer(i: number, phase: number, len: number, palette: string[]): string {
181  const band = ((phase * 1.6) % (len + 8)) - 4
182  const d = Math.abs(i - band)
183  return palette[d < 0.8 ? 3 : d < 1.8 ? 2 : d < 2.8 ? 1 : 0] ?? palette[0] ?? '#f97316'
184}
185
186const Rows: ClientModule<RowsProps, Local> = (props, surface) => {
187  const els = surface.elements
188  Box = els.Box
189  Text = els.Text
190  let state = surface.state
191  if (state === undefined) {
192    state = { hover: -1, phase: 0, drag: false, ref: {} }
193    surface.setState(state)
194  }
195  const H = props.tall ? 2 : 1
196  // Where every row (and burrow) sits, in cells, so pointer positions find their row.
197  const stageAfter = new Map<number, number>()
198  props.dig?.rows.forEach((r, k) => {
199    if (r >= 0 && !stageAfter.has(r)) stageAfter.set(r, k)
200  })
201  const layout: { top: number; h: number; row: number }[] = []
202  let y = 0
203  for (let i = 0; i < props.rows.length; i++) {
204    layout.push({ top: y, h: H, row: i })
205    y += H
206    if (stageAfter.has(i)) {
207      layout.push({ top: y, h: STAGE_LINES, row: -1 })
208      y += STAGE_LINES
209    }
210  }
211  const total = y
212  const rowAtY = (yy: number): number => layout.find(l => yy >= l.top && yy < l.top + l.h)?.row ?? -1
213
214  let visible = props.rows.length
215  const lit = props.rows.some(r => r.left.some(s => s.sh || s.spin) || r.right.some(s => s.sh || s.spin))
216  const animated = lit || Boolean(props.dig)
217  if (animated && !state.ref.stop) {
218    state.ref.stop = surface.every(TICK_MS, () => {
219      const cur = surface.state
220      if (cur) surface.setState({ ...cur, phase: cur.phase + 1 })
221    })
222  } else if (!animated && state.ref.stop) {
223    state.ref.stop()
224    state.ref.stop = undefined
225  }
226  if (props.pointer === false) {
227    state.ref.unpoint?.()
228    state.ref.unpoint = undefined
229  } else state.ref.unpoint = surface.onPointer(e => {
230    const cur = surface.state ?? state
231    const span = Math.max(1, total - 1)
232    const onBar = Boolean(props.bar) && e.x >= surface.columns - 1
233    if (props.bar && (cur.drag || (onBar && e.type === 'down' && (e.button ?? 'left') === 'left'))) {
234      if (e.type === 'up' || e.type === 'leave') {
235        surface.setState({ ...cur, drag: false })
236        return
237      }
238      if (e.type === 'down' || e.type === 'move') {
239        if (!cur.drag) surface.setState({ ...cur, drag: true, hover: -1 })
240        surface.post({ scrollTo: Math.max(0, Math.min(1, e.y / span)) })
241        return
242      }
243    }
244    const at = e.type === 'leave' || e.y < 0 ? -1 : rowAtY(e.y)
245    if (at < 0 || at >= visible) {
246      if (cur.hover !== -1) surface.setState({ ...cur, hover: -1 })
247      return
248    }
249    if (e.type === 'move' && !e.button && cur.hover !== at) surface.setState({ ...cur, hover: at })
250    const row = props.rows[at]
251    if (e.type === 'down' && e.button === 'right' && row?.id) {
252      surface.post({ copy: row.id, shift: Boolean(e.shift) })
253      return
254    }
255    if (e.type !== 'down' || (e.button ?? 'left') !== 'left' || !row) return
256    let x = 0
257    for (const seg of row.left) {
258      const w = [...seg.t].length
259      if (seg.tab && e.x >= x && e.x < x + w) {
260        surface.post({ tab: seg.tab })
261        return
262      }
263      x += w
264    }
265    if (row.id) surface.post({ press: row.id, ctrl: Boolean(e.ctrl), shift: Boolean(e.shift) })
266  })
267  surface.onKey(e => surface.post({ key: e.key, ctrl: Boolean(e.ctrl), shift: Boolean(e.shift) }))
268  // The preview draws in this same element, so the focus a click gave the tree stays put.
269  if (props.preview) {
270    const { Markdown, Code } = surface.elements
271    const p = props.preview
272    return p.markdown ? <Markdown text={p.text} /> : <Code source={p.text} path={p.path} startLine={p.startLine} wrap="truncate-end" />
273  }
274  const frames = props.spinner?.length ? props.spinner : FRAMES
275  const draw = (s: Seg) => {
276    if (s.spin) {
277      return (
278        <Text color={s.c} bold={s.b}>
279          {s.t + (frames[state.phase % frames.length] ?? '')}
280        </Text>
281      )
282    }
283    const palette = s.sh ? (s.dim ? props.tones[s.sh]?.dim : props.tones[s.sh]?.bright) : undefined
284    if (!palette) {
285      return (
286        <Text color={s.c} backgroundColor={s.bg} bold={s.b} strikethrough={s.s} italic={s.i}>
287          {s.t}
288        </Text>
289      )
290    }
291    if (s.one) {
292      return (
293        <Text color={shimmer(-1, state.phase, s.t.length, palette)}>
294          {s.t}
295        </Text>
296      )
297    }
298    const chars = [...s.t]
299    return (
300      <Text bold={s.b}>
301        {chars.map((ch, i) => (
302          <Text color={shimmer(i, state.phase, chars.length, palette)}>{ch}</Text>
303        ))}
304      </Text>
305    )
306  }
307  const t = ((props.dig?.start ?? 0) + state.phase) % LOOP
308  const dig = props.dig
309  const inRow = (i: number): boolean => Boolean(dig) && dig!.rows.some((r, k) => r === i && inside(t, k))
310  const rowEl = (r: RowSpec, i: number) => {
311    const onThumb = props.bar && i >= props.bar.pos && i < props.bar.pos + props.bar.size
312    return (
313      <Box
314        flexDirection="row"
315        height={H}
316        alignItems={props.tall ? 'center' : undefined}
317        overflow="hidden"
318        backgroundColor={r.id && r.id === props.active ? props.activeBg : inRow(i) ? dig?.glow : r.id && i === state.hover ? props.hoverBg : undefined}
319      >
320        <Box flexShrink={1} overflow="hidden">
321          {r.left.map(draw)}
322        </Box>
323        <Box flexGrow={1} />
324        {r.right.map(draw)}
325        {props.bar && (
326          <Box flexDirection="column">
327            {Array.from({ length: H }, () => (
328              <Text color={onThumb ? props.bar?.thumb : props.bar?.track}>{onThumb ? '┃' : '│'}</Text>
329            ))}
330          </Box>
331        )}
332      </Box>
333    )
334  }
335  const drawn: RenderElement[] = []
336  const w = Math.max(24, surface.columns || 40)
337  const scenes = dig ? script(t, w) : undefined
338  let size = 0
339  for (let i = 0; i < props.rows.length; i++) {
340    const el = rowEl(props.rows[i] as RowSpec, i)
341    let cost = 1_000
342    try {
343      cost = JSON.stringify(el).length
344    } catch {}
345    if (size + cost > TREE_BUDGET || drawn.length >= MAX_ROWS) {
346      visible = i
347      drawn.push(<Text dimColor>{`  … ${props.rows.length - i} more rows, collapse folders or use search`}</Text>)
348      break
349    }
350    size += cost
351    drawn.push(el)
352    const k = stageAfter.get(i)
353    if (dig && scenes && k !== undefined) {
354      drawn.push(<Box flexDirection="column" height={STAGE_LINES} overflow="hidden">{composeStage(scenes[k] ?? {}, k, t, w, dig)}</Box>)
355    }
356  }
357  return <Box flexDirection="column">{drawn}</Box>
358}
359
360export default Rows
361
hooks/ft-icons.ts 132 lines
1import type { FileNode } from '../types'
2
3const cp = (n: number) => String.fromCodePoint(n)
4
5export const CHEVRON_OPEN = cp(0xf47c)
6export const CHEVRON_CLOSED = cp(0xf460)
7
8const NAMES: Record<string, number> = {
9  'agents.md': 0xf0354,
10  'changelog.md': 0xf0354,
11  'contributing.md': 0xf0354,
12  'todo.md': 0xf0354,
13  'readme.md': 0xf0354,
14  license: 0xe60a,
15  dockerfile: 0xf0868,
16  makefile: 0xf1064,
17  'cmakelists.txt': 0xf1064,
18  justfile: 0xf05b7,
19  gemfile: 0xf0d2d,
20  rakefile: 0xf0d2d,
21  'cargo.toml': 0xe6b2,
22  'cargo.lock': 0xe6b2,
23  'pyproject.toml': 0xe6b2,
24  'requirements.txt': 0xf160e,
25  'go.mod': 0xf0afa,
26  'go.sum': 0xf07d3,
27  'package.json': 0xf0626,
28  'package-lock.json': 0xf0626,
29  'tsconfig.json': 0xf0626,
30  '.prettierrc': 0xf0626,
31  '.eslintrc': 0xf0626,
32  'pnpm-lock.yaml': 0xe6a8,
33  'yarn.lock': 0xe6a8,
34  '.gitignore': 0xf02a2,
35  '.gitmodules': 0xf0493,
36  '.editorconfig': 0xe652,
37}
38
39const EXTS: Record<string, number> = {
40  lua: 0xe620,
41  py: 0xe606,
42  pyi: 0xe606,
43  js: 0xe60c,
44  mjs: 0xe60c,
45  cjs: 0xe60c,
46  ts: 0xe628,
47  jsx: 0xe625,
48  tsx: 0xe7ba,
49  json: 0xe60b,
50  jsonc: 0xe60b,
51  yaml: 0xe6a8,
52  yml: 0xe6a8,
53  sh: 0xe795,
54  bash: 0xe795,
55  zsh: 0xe795,
56  fish: 0xe795,
57  md: 0xf0354,
58  markdown: 0xf0354,
59  css: 0xe6b8,
60  scss: 0xf031c,
61  html: 0xe736,
62  htm: 0xe736,
63  go: 0xe627,
64  rs: 0xe68b,
65  rb: 0xe791,
66  php: 0xe608,
67  java: 0xe738,
68  cs: 0xf031b,
69  sql: 0xe706,
70  graphql: 0xf20e,
71  gql: 0xf20e,
72  xml: 0xf05c0,
73  toml: 0xe6b2,
74  ini: 0xf0bc2,
75  conf: 0xf0493,
76  pdf: 0xeaeb,
77  svg: 0xf0721,
78  jpg: 0xf0225,
79  jpeg: 0xf0225,
80  png: 0xe60d,
81  gif: 0xf0d78,
82  webp: 0xf021f,
83  mp4: 0xf022b,
84  mkv: 0xf022b,
85  mov: 0xf022b,
86  mp3: 0xf0223,
87  wav: 0xf0223,
88  flac: 0xf0223,
89  zip: 0xf05c4,
90  gz: 0xf05c4,
91  xz: 0xf05c4,
92  tar: 0xf05c4,
93  qml: 0xf375,
94  cpp: 0xe61d,
95  cc: 0xe61d,
96  cxx: 0xe61d,
97  c: 0xe61e,
98  h: 0xf0af5,
99  hpp: 0xf0af5,
100  vue: 0xe6a0,
101  svelte: 0xe697,
102  csv: 0xe64a,
103  txt: 0xf09aa,
104}
105
106export function fileIcon(n: FileNode, open: boolean, isRepo: boolean): string {
107  if (n.kind === 'link') return cp(0xf481)
108  if (n.kind === 'dir') return cp(isRepo ? 0xf02a2 : open ? 0xe5fe : 0xe5ff)
109  const lower = n.name.toLowerCase()
110  const named = NAMES[lower]
111  if (named) return cp(named)
112  const dot = lower.lastIndexOf('.')
113  const ext = dot >= 0 ? lower.slice(dot + 1) : ''
114  return cp(EXTS[ext] ?? 0xf0214)
115}
116
117export const GIT_COLOR: Record<string, string> = {
118  A: '#98c379',
119  '?': '#98c379',
120  R: '#61afef',
121  C: '#61afef',
122  M: '#e5c07b',
123  T: '#e5c07b',
124}
125
126const PRIORITY = ['U', 'D', 'M', 'T', 'R', 'C', 'A', '?']
127
128export function stronger(a: string | undefined, b: string): string {
129  if (!a) return b
130  return PRIORITY.indexOf(b) < PRIORITY.indexOf(a) ? b : a
131}
132