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
Make the /md-preview status short; add /md-preview details
razorback16444c3c1c5594
diff --git a/README.md b/README.mdindex 0e10480..c9a6f57 100644--- a/README.md+++ b/README.md@@ -71,7 +71,8 @@ To try it from a clone without installing: `claude --plugin-dir /path/to/claude-| Command | What it does ||---|---|-| `/md-preview` | Shows the state, the text size, the width, the image height and the terminal font that the mod found |+| `/md-preview` | Shows the state, the theme, the text size, the width and the image height, and a problem that keeps pictures away |+| `/md-preview details` | Shows all that the mod found: where the mode comes from, the terminal font, the Typst path and why the terminal can or cannot show pictures || `/md-preview on` / `off` | Turns the pictures on or off (kept for later sessions) || `/md-preview setup` / `setup remove` | iTerm2: adds or removes the shell line that turns pictures on (then close the tab and open a new one) || `/md-preview size <80–200>` | Text size in percent. 100 is the same visual size as your terminal text. |diff --git a/hooks/lib/command.ts b/hooks/lib/command.tsindex 988f2ce..a2596ce 100644--- a/hooks/lib/command.ts+++ b/hooks/lib/command.ts@@ -9,10 +9,10 @@ import {import { isThemeName, type ThemeInfo } from './themes'// `/md-preview` arguments: on, off, size <80-200>, width [40-400|full],-// height [6-200|half|full], or nothing (show state). A `width` or `height`+// height [6-200|half|full], details (all md-preview found), or nothing (show state). A `width` or `height`// with no value shows it.export type Command =- | { kind: 'on' | 'off' | 'show' | 'probe' }+ | { kind: 'on' | 'off' | 'show' | 'details' | 'probe' }| { kind: 'size'; percent: number }| { kind: 'width'; width?: Width }| { kind: 'height'; height?: Height }@@ -28,7 +28,7 @@ export function parseCommand(args: string): Command {if (typed.length === 2 && isThemeName(typed[1]!)) return { kind: 'theme', name: typed[1]! }return { kind: 'bad' }}- if (words.length === 1 && (words[0] === 'on' || words[0] === 'off' || words[0] === 'probe')) return { kind: words[0] }+ if (words.length === 1 && (words[0] === 'on' || words[0] === 'off' || words[0] === 'details' || words[0] === 'probe')) return { kind: words[0] }if (words[0] === 'setup' && words.length === 1) return { kind: 'setup', remove: false }if (words[0] === 'setup' && words.length === 2 && words[1] === 'remove') return { kind: 'setup', remove: true }if (words[0] === 'size' && words.length === 2 && /^\d+%?$/.test(words[1]!)) {@@ -59,6 +59,7 @@ const SUBCOMMANDS = [{ name: 'height', description: `the most rows an image or a Typst drawing takes, ${MIN_HEIGHT}-${MAX_HEIGHT}, half or full` },{ name: 'theme', description: 'list the themes, or switch to one' },{ name: 'setup', description: 'iTerm2: add the shell line that turns pictures on' },+ { name: 'details', description: 'the mode source, the terminal font, Typst and the terminal' },] as constconst SIZES = [80, 90, 100, 110, 120, 125, 150, 175, 200]const WIDTHS: readonly Width[] = [80, 100, 120, 160, 200, 'full']diff --git a/hooks/register.tsx b/hooks/register.tsxindex b4550ea..629aa59 100644--- a/hooks/register.tsx+++ b/hooks/register.tsx@@ -271,15 +271,24 @@ export const register: Register = on => {}const state = (await read($, isOn)) ? 'on' : 'off'const ready = await setUp($)- const font = ready.termFont ? `terminal font ${ready.termFont}` : 'terminal font not known (Menlo assumed)'- const typst = ready.typst ? `Typst: ${ready.typst}` : 'Typst: not found (brew install typst)'- const terminal = ready.canDraw- ? `this terminal draws pictures${ready.inline ? ' (over ssh, sent as bytes)' : ''}`- : 'Claude Code draws no pictures here (it does in kitty and Ghostty, in iTerm2 3.6+ and Rio 0.5.27+ with CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1, over ssh when TERM names kitty or Ghostty or with that variable; never in tmux, screen or a background session; not in WezTerm, Warp, Konsole or others), so answers stay text'- const tried = S.setupNeeded ? ' In iTerm2, run /md-preview setup once to show pictures.' : ''- const usage = `Use /md-preview on, /md-preview off, /md-preview size <${MIN_SIZE}-${MAX_SIZE}>, /md-preview width <${MIN_WIDTH}-${MAX_WIDTH}|full>, /md-preview height <${MIN_HEIGHT}-${MAX_HEIGHT}|half|full>, or /md-preview theme [name].`- const bad = asked.kind === 'bad' ? `Not understood: "${e.args.trim()}". ` : ''- return { text: `${bad}md-preview is ${state}, theme ${S.look?.name ?? DEFAULT_THEME}, ${modeLine(S.theme, S.setting, S.settingRaw, S.host)}, text size ${S.size}%, width ${widthText(S.width)}, image and drawing height ${heightText(S.height)}, ${font}. ${typst}; ${terminal}.${tried} ${usage}` }+ const bad = asked.kind === 'bad' ? `Not understood: "${e.args.trim()}".\n` : ''+ const theme = S.look?.name ?? DEFAULT_THEME+ if (asked.kind === 'details') {+ const font = ready.termFont ? `terminal font ${ready.termFont}` : 'terminal font not known (Menlo assumed)'+ const typst = ready.typst ? `Typst: ${ready.typst}` : 'Typst: not found (brew install typst)'+ const terminal = ready.canDraw+ ? `this terminal draws pictures${ready.inline ? ' (over ssh, sent as bytes)' : ''}`+ : 'Claude Code draws no pictures here (it does in kitty and Ghostty, in iTerm2 3.6+ and Rio 0.5.27+ with CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1, over ssh when TERM names kitty or Ghostty or with that variable; never in tmux, screen or a background session; not in WezTerm, Warp, Konsole or others), so answers stay text'+ return { text: `md-preview is ${state}, theme ${theme}, ${modeLine(S.theme, S.setting, S.settingRaw, S.host)}, text size ${S.size}%, width ${widthText(S.width)}, image and drawing height ${heightText(S.height)}, ${font}. ${typst}; ${terminal}.` }+ }+ // The short state: what is set, and only the problems that keep pictures away.+ const sized = (v: number | 'full' | 'half', unit: string) => (typeof v === 'number' ? `${v} ${unit}` : v)+ const lines = [`md-preview is ${state}: theme ${theme}, ${S.theme}, text ${S.size}%, width ${sized(S.width, 'columns')}, height ${sized(S.height, 'rows')}.`]+ if (S.setupNeeded) lines.push('In iTerm2, run /md-preview setup once to show pictures.')+ else if (!ready.canDraw) lines.push('This terminal shows no pictures, so answers stay text. Pictures show in kitty, Ghostty and iTerm2 3.6+.')+ if (!ready.typst) lines.push('Typst is not installed, so answers stay text. Install it: brew install typst')+ lines.push('Change: /md-preview on | off | size | width | height | theme. More: /md-preview details')+ return { text: bad + lines.join('\n') }})on('config.set', { key: 'theme' }, async ($, e, next) => {diff --git a/tests/draw.test.ts b/tests/draw.test.tsindex 473966e..51a8205 100644--- a/tests/draw.test.ts+++ b/tests/draw.test.ts@@ -224,7 +224,7 @@ const images = (tree: any): any[] =>test('over ssh in kitty, a page goes as PNG bytes, read once', async ($, on) => {const { clock, reads } = world(on, { SSH_CONNECTION: '10.0.0.2 50000 10.0.0.1 22', TERM: 'xterm-kitty' })- const ran = await $.command.run({ command: 'md-preview', args: '' } as any)+ const ran = await $.command.run({ command: 'md-preview', args: 'details' } as any)expect(ran.text).toContain('over ssh, sent as bytes')const ui = await $.ui.mount(message('ssh-1', 'Some **bold** text.'))await clock.settle()@@ -262,7 +262,7 @@ test('locally, a page goes as a file name', async ($, on) => {test('in tmux with the engine forced, answers stay text', async ($, on) => {const { clock } = world(on, { TMUX: '/tmp/tmux-501/default,1,0', TERM: 'tmux-256color', CLAUDE_CODE_FORCE_TERMINAL_IMAGES: '1' })const ran = await $.command.run({ command: 'md-preview', args: '' } as any)- expect(ran.text).toContain('Claude Code draws no pictures here')+ expect(ran.text).toContain('This terminal shows no pictures')const ui = await $.ui.mount(message('tmux-1', 'Some **bold** text.'))await clock.settle()await ui.redraw(props('Some **bold** text.'))diff --git a/tests/images.test.ts b/tests/images.test.tsindex b57a549..29efb53 100644--- a/tests/images.test.ts+++ b/tests/images.test.ts@@ -742,7 +742,7 @@ test('with md-preview off, rows stay as they are', async ($, on) => {test('in a terminal that draws no pictures, rows stay as they are', async ($, on) => {world(on, { vars: { TERM: 'xterm-256color' }, files: { '/t/icon.png': HEAD, [`${PASTED}/5.png`]: HEAD }, dirs: [PASTED] })const shown = await ready($)- expect(shown.text).toContain('Claude Code draws no pictures here')+ expect(shown.text).toContain('This terminal shows no pictures')expect(await (await $.ui.mount(sent(attach('/t/icon.png')))).drawn()).toMatchObject(ENGINE)expect(await (await $.ui.mount(prompt('[Image #5] hi'))).drawn()).toMatchObject(ENGINE)})@@ -752,7 +752,7 @@ test('in a terminal that draws no pictures, rows stay as they are', async ($, ontest('a kept height caps sent and pasted images; height full draws them taller at the commit', async ($, on) => {const { clock, kept } = world(on, { stored: { height: 12 }, files: { '/t/tall.png': TALL, [`${PASTED}/5.png`]: TALL }, dirs: [PASTED] })const ran = await ready($)- expect(ran.text).toContain('image and drawing height 12 rows')+ expect(ran.text).toContain('height 12 rows')const ui = await $.ui.mount(sent(attach('/t/tall.png')))expect(images(await ui.drawn())[0].props.rows).toBe(12)const pasted = await $.ui.mount(prompt('[Image #5] tall'))diff --git a/tests/mode.test.ts b/tests/mode.test.tsindex 68594b6..32000f7 100644--- a/tests/mode.test.ts+++ b/tests/mode.test.ts@@ -20,14 +20,14 @@ test('with the theme on auto, the mode comes from the OS appearance, read every}return next(e)})- const ran = await $.command.run({ command: 'md-preview', args: '' } as any)+ const ran = await $.command.run({ command: 'md-preview', args: 'details' } as any)expect(ran.text).toContain('mode light (auto, from macOS)')expect(reads.length).toBe(1)// The OS goes dark: the next poll, 3 s on, sees it; one read per period.osDark = trueawait clock.advance(3000)expect(reads.length).toBe(2)- const later = await $.command.run({ command: 'md-preview', args: '' } as any)+ const later = await $.command.run({ command: 'md-preview', args: 'details' } as any)expect(later.text).toContain('mode dark (auto, from macOS)')await clock.advance(6000)expect(reads.length).toBe(4)@@ -35,14 +35,14 @@ test('with the theme on auto, the mode comes from the OS appearance, read everyawait $.config.set({ key: 'theme', value: 'light-daltonized' } as any)await clock.advance(9000)expect(reads.length).toBe(4)- const fixed = await $.command.run({ command: 'md-preview', args: '' } as any)+ const fixed = await $.command.run({ command: 'md-preview', args: 'details' } as any)expect(fixed.text).toContain('mode light (Claude Code theme light-daltonized)')await $.config.set({ key: 'theme', value: 'auto' } as any)await clock.settle()expect(reads.length).toBe(5)await clock.advance(3000)expect(reads.length).toBe(6)- const again = await $.command.run({ command: 'md-preview', args: '' } as any)+ const again = await $.command.run({ command: 'md-preview', args: 'details' } as any)expect(again.text).toContain('mode dark (auto, from macOS)')})@@ -88,12 +88,12 @@ test('with the theme on auto, a dark iTerm2 profile on a light Mac draws dark',if (argv[0] === 'defaults' && argv.includes('AppleInterfaceStyle')) return ok('', 1)return next(e)})- const ran = await $.command.run({ command: 'md-preview', args: '' } as any)+ const ran = await $.command.run({ command: 'md-preview', args: 'details' } as any)expect(ran.text).toContain('mode dark (auto, from iTerm2: profile Default background)')// iTerm2 on Automatic follows the light Mac: the profile's light colours.style = '4\n'await clock.advance(3000)- const later = await $.command.run({ command: 'md-preview', args: '' } as any)+ const later = await $.command.run({ command: 'md-preview', args: 'details' } as any)expect(later.text).toContain('mode light (auto, from iTerm2: profile Default background)')})@@ -117,14 +117,14 @@ test('with the theme on auto, Ghostty draws as its background is', async ($, on)})const files: Record<string, string> = { '/R/themes/Day': 'background = #f7f7f7\n', '/h/.config/ghostty/themes/Night': 'background = #191724\n' }on('fs.read', async ($: any, e: any, next: any) => (e.path in files ? { value: files[e.path] } : e.path.includes('ghostty') || e.path.startsWith('/R/') ? { deny: 'no such file' } as any : next(e)))- const ran = await $.command.run({ command: 'md-preview', args: '' } as any)+ const ran = await $.command.run({ command: 'md-preview', args: 'details' } as any)expect(ran.text).toContain('mode dark (auto, from Ghostty: default background)')config = 'theme = light:Day,dark:Night\nbackground = #f7f7f7\n'await clock.advance(3000)- expect((await $.command.run({ command: 'md-preview', args: '' } as any)).text).toContain('mode light (auto, from Ghostty: theme Day background)')+ expect((await $.command.run({ command: 'md-preview', args: 'details' } as any)).text).toContain('mode light (auto, from Ghostty: theme Day background)')osDark = trueawait clock.advance(3000)- expect((await $.command.run({ command: 'md-preview', args: '' } as any)).text).toContain('mode dark (auto, from Ghostty: theme Night background)')+ expect((await $.command.run({ command: 'md-preview', args: 'details' } as any)).text).toContain('mode dark (auto, from Ghostty: theme Night background)')})// kitty on a light Mac: kitty.conf's included theme is dark; once auto theme@@ -149,16 +149,16 @@ test('with the theme on auto, kitty draws as its config or auto theme is', asyncconst inKitty = (path: string) => path.includes('kitty')on('fs.exists', async ($: any, e: any, next: any) => (inKitty(e.path) ? { value: e.path in files } : next(e)))on('fs.read', async ($: any, e: any, next: any) => (e.path in files ? { value: files[e.path] } : inKitty(e.path) ? ({ deny: 'no such file' } as any) : next(e)))- const ran = await $.command.run({ command: 'md-preview', args: '' } as any)+ const ran = await $.command.run({ command: 'md-preview', args: 'details' } as any)expect(ran.text).toContain('mode dark (auto, from kitty: config background)')expect(ran.text).toContain('terminal font JetBrains Mono')files['/h/.config/kitty/dark-theme.auto.conf'] = 'background #191724\n'files['/h/.config/kitty/light-theme.auto.conf'] = 'background #fafafa\n'await clock.advance(3000)- expect((await $.command.run({ command: 'md-preview', args: '' } as any)).text).toContain('mode light (auto, from kitty: light-theme.auto.conf background)')+ expect((await $.command.run({ command: 'md-preview', args: 'details' } as any)).text).toContain('mode light (auto, from kitty: light-theme.auto.conf background)')osDark = trueawait clock.advance(3000)- expect((await $.command.run({ command: 'md-preview', args: '' } as any)).text).toContain('mode dark (auto, from kitty: dark-theme.auto.conf background)')+ expect((await $.command.run({ command: 'md-preview', args: 'details' } as any)).text).toContain('mode dark (auto, from kitty: dark-theme.auto.conf background)')})// Rio on a dark Mac with an [adaptive-theme]: the OS picks the theme file;@@ -181,12 +181,12 @@ test('with the theme on auto, Rio draws as its adaptive theme is', async ($, on)'/h/.config/rio/themes/night.toml': '[colors]\nbackground = "#191724"\n',}on('fs.read', async ($: any, e: any, next: any) => (e.path in files ? { value: files[e.path] } : e.path.includes('/rio/') ? ({ deny: 'no such file' } as any) : next(e)))- const ran = await $.command.run({ command: 'md-preview', args: '' } as any)+ const ran = await $.command.run({ command: 'md-preview', args: 'details' } as any)expect(ran.text).toContain('mode dark (auto, from Rio: theme night background)')expect(ran.text).toContain('terminal font Iosevka')files['/h/.config/rio/config.toml'] = 'force-theme = "light"\n' + files['/h/.config/rio/config.toml']await clock.advance(3000)- expect((await $.command.run({ command: 'md-preview', args: '' } as any)).text).toContain('mode light (auto, from Rio: theme day background)')+ expect((await $.command.run({ command: 'md-preview', args: 'details' } as any)).text).toContain('mode light (auto, from Rio: theme day background)')})// Over ssh the configs and the OS here are the remote computer's: auto uses@@ -210,7 +210,7 @@ for (const colorFgBg of [undefined, '0;15']) {})on('fs.exists', async ($: any, e: any, next: any) => (e.path.includes('kitty') ? (asked.push(e.path), { value: false }) : next(e)))on('fs.read', async ($: any, e: any, next: any) => (e.path.includes('kitty') ? (asked.push(e.path), { deny: 'no such file' } as any) : next(e)))- const ran = await $.command.run({ command: 'md-preview', args: '' } as any)+ const ran = await $.command.run({ command: 'md-preview', args: 'details' } as any)expect(ran.text).toContain(colorFgBg ? 'mode light (auto, from COLORFGBG)' : 'mode dark (auto, OS appearance not known, dark assumed)')expect(ran.text).toContain('terminal font not known (Menlo assumed)')expect(asked).toEqual([])@@ -237,15 +237,15 @@ test('with the theme on auto, cmux draws as its appearance is', async ($, on) =>})let cmuxJson: string | null = nullon('fs.read', async ($: any, e: any, next: any) => (e.path === '/h/.config/cmux/cmux.json' ? (cmuxJson === null ? ({ deny: 'no such file' } as any) : { value: cmuxJson }) : next(e)))- const ran = await $.command.run({ command: 'md-preview', args: '' } as any)+ const ran = await $.command.run({ command: 'md-preview', args: 'details' } as any)expect(ran.text).toContain('mode light (auto, from cmux: adaptive default theme, appearance system)')saved = 'dark\n'await clock.advance(3000)- expect((await $.command.run({ command: 'md-preview', args: '' } as any)).text).toContain('mode dark (auto, from cmux: adaptive default theme, appearance dark)')+ expect((await $.command.run({ command: 'md-preview', args: 'details' } as any)).text).toContain('mode dark (auto, from cmux: adaptive default theme, appearance dark)')saved = 'system\n'cmuxJson = '{\n // off\n "terminal": { "adaptiveDefaultTheme": false, },\n}'await clock.advance(3000)- expect((await $.command.run({ command: 'md-preview', args: '' } as any)).text).toContain('mode dark (auto, from Ghostty: default background)')+ expect((await $.command.run({ command: 'md-preview', args: 'details' } as any)).text).toContain('mode dark (auto, from Ghostty: default background)')})// iTerm2 whose TERM_PROGRAM did not come through: ITERM_PROFILE says iTerm2.@@ -265,6 +265,6 @@ test('with the theme on auto, iTerm2 with no TERM_PROGRAM is found by ITERM_PROFif (argv[0] === 'defaults' && argv.includes('AppleInterfaceStyle')) return ok('', 1)return next(e)})- const ran = await $.command.run({ command: 'md-preview', args: '' } as any)+ const ran = await $.command.run({ command: 'md-preview', args: 'details' } as any)expect(ran.text).toContain('mode dark (auto, from iTerm2: profile Default background)')})diff --git a/tests/pure.test.ts b/tests/pure.test.tsindex 2c6849f..0b9c687 100644--- a/tests/pure.test.ts+++ b/tests/pure.test.ts@@ -178,6 +178,7 @@ test('the size command takes 80 to 200 percent', () => {expect(parseCommand('size big')).toEqual({ kind: 'bad' })expect(parseCommand('bigger')).toEqual({ kind: 'bad' })expect(parseCommand('probe')).toEqual({ kind: 'probe' })+ expect(parseCommand('details')).toEqual({ kind: 'details' })expect(parseCommand('probe 3')).toEqual({ kind: 'bad' })})