Skip to content

Claude's answers, rendered: Markdown, math, charts and diagrams drawn in the Claude Code terminal. Independent project, not affiliated with Anthropic.

TypeScript claude-code-mod

Catalog scan and setup data safety

razorback169250839b8905
diff --git a/hooks/lib/draw.tsx b/hooks/lib/draw.tsx
index 85bc655..613035b 100644
--- a/hooks/lib/draw.tsx
+++ b/hooks/lib/draw.tsx
@@ -17,7 +17,7 @@ import { fsExists, fsRead, uiCopy, uiResolve, uiToast } from './api'
// FNV-1a over the parts, twice with different offsets: 16 hex digits.
export function hashOf(parts: readonly (string | number)[]): string {
- const s = parts.join('\u0000')
+ const s = parts.join('\u241f')
let a = 0x811c9dc5
let b = 0x050c5d1f
for (let i = 0; i < s.length; i++) {
diff --git a/hooks/lib/mode.ts b/hooks/lib/mode.ts
index 135ea10..393abd2 100644
--- a/hooks/lib/mode.ts
+++ b/hooks/lib/mode.ts
@@ -92,4 +92,4 @@ export function parseOsc11(text: string): Rgb | null {
// Control characters shown as text (\e, \a, \xNN), for /iui probe.
export const escapeControls = (text: string) =>
- text.replace(/[\x00-\x1f\x7f]/g, c => (c === '\x1b' ? '\\e' : c === '\x07' ? '\\a' : `\\x${c.charCodeAt(0).toString(16).padStart(2, '0')}`))
+ text.replace(/[\x00-\x1f\x7f]/g, c => (c.charCodeAt(0) === 27 ? '\\e' : c.charCodeAt(0) === 7 ? '\\a' : `\\x${c.charCodeAt(0).toString(16).padStart(2, '0')}`))
diff --git a/hooks/lib/select.ts b/hooks/lib/select.ts
index 30fb6e6..9c761a3 100644
--- a/hooks/lib/select.ts
+++ b/hooks/lib/select.ts
@@ -110,7 +110,8 @@ export const sameLit = (a: Lit | null, b: Lit | null) => a === b || (!!a && !!b
// answer stays the last one made, so only a new one counts (one there
// before ours stays as it is).
export const NATIVE_POLL_MS = 250
-const UNREAD = '\0'
+// Not read yet; no nativeOf value is this (those hold a '|').
+const UNREAD = 'unread'
const native = { timer: null as { cancel: () => void } | null, last: UNREAD as string | undefined }
const nativeOf = (s: { text: string; requestId?: string } | undefined) => (s ? `${s.requestId ?? ''}|${s.text}` : undefined)
diff --git a/hooks/lib/setup.ts b/hooks/lib/setup.ts
index b15e1d1..a1aee81 100644
--- a/hooks/lib/setup.ts
+++ b/hooks/lib/setup.ts
@@ -4,7 +4,7 @@ import type { EngineInterface } from 'claude-code'
import { S } from './state'
import { readText } from './host'
-import { envGet, fsWrite, processRun, storeGet, storeSet, uiAsk, uiLog, uiToast } from './api'
+import { envGet, fsExists, fsWrite, processRun, storeGet, storeSet, uiAsk, uiLog, uiToast } from './api'
// Claude Code turns pictures on by itself in kitty and Ghostty only; iTerm2
// needs CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1 when Claude Code starts (a mod's
@@ -67,6 +67,14 @@ async function shellRc($: EngineInterface): Promise<ShellRc | null> {
})
}
+// The start file's text: '' when it is confirmed missing, null when it
+// exists (or may exist) but cannot be read. A write over null would lose it.
+async function readRc($: EngineInterface, path: string): Promise<string | null> {
+ const there = await fsExists($, path).catch(() => true)
+ if (!there) return ''
+ return readText($, path)
+}
+
// What /iui setup says where iTerm2 does not run (not macOS): it changes no file.
export const SETUP_NOT_MAC = '/iui setup is for iTerm2, which runs on macOS only, so Intelligent UI changed no file. ' +
'On Linux, kitty and Ghostty show pictures with no setup. ' +
@@ -78,7 +86,8 @@ export async function runSetup($: EngineInterface, remove: boolean): Promise<str
const rc = await shellRc($)
const manual = `export CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1 (only in iTerm2, not in tmux or screen)`
if (!rc) return `Intelligent UI does not know your shell, so it changed no file. Add this to your shell's start file: ${manual}`
- const text = (await readText($, rc.path)) ?? ''
+ const text = await readRc($, rc.path)
+ if (text === null) return `Intelligent UI could not read ${rc.path}, so it changed no file. Add this to it yourself: ${manual}`
const restart = 'Close this tab, open a new iTerm2 tab, and start Claude Code there to see pictures.'
if (remove) {
if (!hasSetup(text)) return `${rc.path} has no Intelligent UI block.`
@@ -97,8 +106,10 @@ export async function offerSetup($: EngineInterface) {
S.setupAsked = true
if ((await storeGet($, 'setup').catch(() => undefined)) === 'never') return
const rc = await shellRc($)
- if (rc && hasSetup((await readText($, rc.path)) ?? '')) return
- const file = rc ? rc.path.replace((await envGet($, 'HOME')) ?? '\u0000', '~') : "your shell's start file"
+ const text = rc ? await readRc($, rc.path) : ''
+ if (text === null || (rc && hasSetup(text))) return
+ const home = (await envGet($, 'HOME')) ?? ''
+ const file = rc ? (home ? rc.path.replace(home, '~') : rc.path) : "your shell's start file"
const answer = await uiAsk($,
`Intelligent UI draws answers as pictures, but in iTerm2 Claude Code needs CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1 when it starts. Add one line to ${file} that sets it in iTerm2 only (not in tmux or screen)?`,
{ header: 'iui', options: ['Add the line', 'Not now', 'Never ask'] },
diff --git a/hooks/register.tsx b/hooks/register.tsx
index 919f18a..7c66380 100644
--- a/hooks/register.tsx
+++ b/hooks/register.tsx
@@ -851,7 +851,7 @@ export const register: Register = on => {
if (notice) lines.push(notice)
lines.push('Change: /iui on | off | size | width | height | grid | select | updates | theme. More: /iui details')
return { text: bad + lines.join('\n') }
- })
+ }).catch(($, e, next) => next(e))
on('config.set', { key: 'theme' }, async ($, e, next) => {
const set = await next(e)
diff --git a/tests/images.test.ts b/tests/images.test.ts
index ac7b35b..0e0ee4e 100644
--- a/tests/images.test.ts
+++ b/tests/images.test.ts
@@ -309,14 +309,14 @@ test('an [Image #0] mark and a uid that is not a number give nothing', () => {
})
test('the pasted-image path follows Claude Code folder names', () => {
- expect(projectSlug('/Users/subhagato/Development/apps/claude-intelligent-ui')).toBe('-Users-subhagato-Development-apps-claude-intelligent-ui')
+ expect(projectSlug('/Users/me/Development/apps/claude-intelligent-ui')).toBe('-Users-me-Development-apps-claude-intelligent-ui')
expect(projectSlug('/home/me/my_app.v2')).toBe('-home-me-my-app-v2')
expect(claudeTmpOf({ uid: '501\n', isMac: true })).toBe('/private/tmp/claude-501')
expect(claudeTmpOf({ uid: '1000', isMac: false })).toBe('/tmp/claude-1000')
expect(claudeTmpOf({ tmpdir: '/scratch/', uid: '1000', isMac: false })).toBe('/scratch/claude-1000')
expect(claudeTmpOf({ uid: '', isMac: true })).toBe('')
- const dir = pasteDirOf('/private/tmp/claude-501', '/Users/subhagato/Development/apps/claude-intelligent-ui', 'f8bb4b84-0e51-456a-9e49-8e5db49500ae')
- expect(pastedImagePath(dir, 5)).toBe('/private/tmp/claude-501/-Users-subhagato-Development-apps-claude-intelligent-ui/f8bb4b84-0e51-456a-9e49-8e5db49500ae/images/5.png')
+ const dir = pasteDirOf('/private/tmp/claude-501', '/Users/me/Development/apps/claude-intelligent-ui', 'f8bb4b84-0e51-456a-9e49-8e5db49500ae')
+ expect(pastedImagePath(dir, 5)).toBe('/private/tmp/claude-501/-Users-me-Development-apps-claude-intelligent-ui/f8bb4b84-0e51-456a-9e49-8e5db49500ae/images/5.png')
})
// ---- The render hooks -------------------------------------------------------
diff --git a/tests/setup.test.ts b/tests/setup.test.ts
index e918669..16a4653 100644
--- a/tests/setup.test.ts
+++ b/tests/setup.test.ts
@@ -58,7 +58,7 @@ test('a block of the other kind is replaced, not added next to the first', () =>
})
// An iTerm2 session on a Mac with the engine's pictures off and a zsh user.
-const iterm = (on: any, files: Record<string, string>, answer: string | null, o: { env?: Record<string, string>; stored?: Record<string, unknown> } = {}) => {
+const iterm = (on: any, files: Record<string, string>, answer: string | null, o: { env?: Record<string, string>; stored?: Record<string, unknown>; unreadable?: string } = {}) => {
const store = new Map(Object.entries(o.stored ?? {}))
on('store.get', async ($: any, e: any) => ({ value: store.get(e.key) }))
on('store.set', async ($: any, e: any) => (store.set(e.key, e.value), { value: undefined }))
@@ -83,7 +83,7 @@ const iterm = (on: any, files: Record<string, string>, answer: string | null, o:
? { value: { exitCode: 0, stdout: '/tmp/mdp\nDarwin\n', stderr: '', isStdoutTruncated: false, isStderrTruncated: false } }
: next(e)
})
- on('fs.read', async ($: any, e: any, next: any) => (e.path in files ? { value: files[e.path] } : e.path.startsWith('/h/') ? { deny: 'no such file' } : next(e)))
+ on('fs.read', async ($: any, e: any, next: any) => (e.path === o.unreadable ? { deny: 'EACCES: permission denied' } : e.path in files ? { value: files[e.path] } : e.path.startsWith('/h/') ? { deny: 'no such file' } : next(e)))
on('fs.exists', async ($: any, e: any, next: any) => (e.path.startsWith('/h/') ? { value: e.path in files } : next(e)))
on('fs.write', async ($: any, e: any, next: any) => {
if (!e.path.startsWith('/h/')) return next(e)
@@ -224,3 +224,35 @@ test('a yes with a shell it does not know changes no file; the toast points to t
expect(files['/h/.zshrc']).toBe('')
expect(toasts.filter(t => /transcript|tab/.test(t))).toEqual(['See the transcript for the setup.'])
})
+
+// A start file that exists but whose read fails (over 4 MiB, EACCES, an I/O error).
+const unreadable = (on: any, files: Record<string, string>, answer: string | null, o: Parameters<typeof iterm>[3] = {}) =>
+ iterm(on, files, answer, { ...o, unreadable: '/h/.zshrc' })
+
+test('/iui setup does not replace a start file whose read fails', async ($, on) => {
+ const files: Record<string, string> = { '/h/.zshrc': 'export PATH=/important:$PATH\n' }
+ unreadable(on, files, 'Not now', { stored: { setup: 'never' } })
+ const ran = await $.command.run({ command: 'iui', args: 'setup' } as any)
+ expect(ran.text).toContain('could not read /h/.zshrc')
+ expect(ran.text).toContain('changed no file')
+ expect(ran.text).toContain('export CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1')
+ expect(files['/h/.zshrc']).toBe('export PATH=/important:$PATH\n')
+})
+
+test('/iui setup remove does not write a start file whose read fails', async ($, on) => {
+ const files: Record<string, string> = { '/h/.zshrc': `${setupBlock('sh')}export A=1\n` }
+ unreadable(on, files, 'Not now', { stored: { setup: 'never' } })
+ const ran = await $.command.run({ command: 'iui', args: 'setup remove' } as any)
+ expect(ran.text).toContain('could not read /h/.zshrc')
+ expect(files['/h/.zshrc']).toBe(`${setupBlock('sh')}export A=1\n`)
+})
+
+test('a yes to the question does not write a start file whose read fails', async ($, on) => {
+ const files: Record<string, string> = { '/h/.zshrc': 'export A=1\n' }
+ const { asked, clock } = unreadable(on, files, 'Add the line')
+ await $.command.run({ command: 'iui', args: '' } as any)
+ await clock.settle()
+ // The file cannot be checked, so the question is not asked at all.
+ expect(asked.length).toBe(0)
+ expect(files['/h/.zshrc']).toBe('export A=1\n')
+})